mirror of
https://github.com/crskycode/GARbro.git
synced 2024-11-27 07:34:00 +08:00
implemented Ogg/Opus audio.
This commit is contained in:
parent
128e86412f
commit
eaf9cdca46
@ -35,6 +35,14 @@
|
||||
<BaseAddress>16777216</BaseAddress>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Concentus, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Concentus.1.1.3\lib\portable-net45+win+wpa81+wp80\Concentus.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Concentus.Oggfile, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Concentus.OggFile.1.0.0\lib\portable-net45+win+wpa81+wp80\Concentus.Oggfile.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="PresentationCore" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.Composition" />
|
||||
@ -52,6 +60,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CellWorks\ArcDB.cs" />
|
||||
<Compile Include="Opus\AudioOPUS.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="WebP\ImageWEBP.cs" />
|
||||
</ItemGroup>
|
||||
|
99
Experimental/Opus/AudioOPUS.cs
Normal file
99
Experimental/Opus/AudioOPUS.cs
Normal file
@ -0,0 +1,99 @@
|
||||
//! \file AudioOPUS.cs
|
||||
//! \date Thu Dec 15 11:38:40 2016
|
||||
//! \brief Handler for Ogg/Opus containers.
|
||||
//
|
||||
// Copyright (C) 2016 by morkt
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.ComponentModel.Composition;
|
||||
using System.IO;
|
||||
using Concentus.Oggfile;
|
||||
using Concentus.Structs;
|
||||
|
||||
namespace GameRes.Formats.Opus
|
||||
{
|
||||
[Export(typeof(AudioFormat))]
|
||||
public class OpusAudio : AudioFormat
|
||||
{
|
||||
public override string Tag { get { return "OPUS"; } }
|
||||
public override string Description { get { return "Ogg/Opus audio format"; } }
|
||||
public override uint Signature { get { return 0; } }
|
||||
public override bool CanWrite { get { return false; } }
|
||||
|
||||
public override SoundInput TryOpen (IBinaryStream file)
|
||||
{
|
||||
if (file.Signature != 0x5367674F) // 'OggS'
|
||||
return null;
|
||||
var header = file.ReadHeader (0x1C);
|
||||
int table_size = header[0x1A];
|
||||
if (table_size < 1)
|
||||
return null;
|
||||
int header_size = header[0x1B];
|
||||
if (header_size < 0x10)
|
||||
return null;
|
||||
int header_pos = 0x1B + table_size;
|
||||
header = file.ReadHeader (header_pos + header_size);
|
||||
if (!header.AsciiEqual (header_pos, "OpusHead"))
|
||||
return null;
|
||||
int channels = header[header_pos+9];
|
||||
// int rate = header.ToInt32 (header_pos+0xC);
|
||||
int rate = 48000;
|
||||
file.Position = 0;
|
||||
var decoder = OpusDecoder.Create (rate, channels);
|
||||
var ogg_in = new OpusOggReadStream (decoder, file.AsStream);
|
||||
var pcm = new MemoryStream();
|
||||
try
|
||||
{
|
||||
using (var output = new BinaryWriter (pcm, System.Text.Encoding.UTF8, true))
|
||||
{
|
||||
while (ogg_in.HasNextPacket)
|
||||
{
|
||||
var packet = ogg_in.DecodeNextPacket();
|
||||
if (packet != null)
|
||||
{
|
||||
for (int i = 0; i < packet.Length; ++i)
|
||||
output.Write (packet[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
var format = new WaveFormat
|
||||
{
|
||||
FormatTag = 1,
|
||||
Channels = (ushort)channels,
|
||||
SamplesPerSecond = (uint)rate,
|
||||
BitsPerSample = 16,
|
||||
};
|
||||
format.BlockAlign = (ushort)(format.Channels*format.BitsPerSample/8);
|
||||
format.AverageBytesPerSecond = format.SamplesPerSecond*format.BlockAlign;
|
||||
pcm.Position = 0;
|
||||
var sound = new RawPcmInput (pcm, format);
|
||||
file.Dispose();
|
||||
return sound;
|
||||
}
|
||||
catch
|
||||
{
|
||||
pcm.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Concentus" version="1.1.3" targetFramework="net45" />
|
||||
<package id="Concentus.OggFile" version="1.0.0" targetFramework="net45" />
|
||||
<package id="System.Data.SQLite.Core" version="1.0.103" targetFramework="net45" />
|
||||
</packages>
|
Loading…
Reference in New Issue
Block a user