added support for WAVE sound files

closes #2174
This commit is contained in:
Matthias Mailänder
2013-06-09 21:41:40 +02:00
parent d898899de7
commit 0554ef35b7
3 changed files with 88 additions and 1 deletions

View File

@@ -0,0 +1,78 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.IO;
using System.Text;
namespace OpenRA.FileFormats
{
public class WavLoader
{
public readonly int FileSize;
public readonly string Format;
public readonly int FmtChunkSize;
public readonly int AudioFormat;
public readonly int Channels;
public readonly int SampleRate;
public readonly int ByteRate;
public readonly int BlockAlign;
public readonly int BitsPerSample;
public readonly int DataSize;
public readonly byte[] RawOutput;
public readonly int ListChunkSize;
public WavLoader(Stream s)
{
while (s.Position < s.Length)
{
if ((s.Position & 1) == 1)
s.ReadByte(); // Alignment
var type = s.ReadASCII(4);
switch (type)
{
case "RIFF":
FileSize = s.ReadInt32();
Format = s.ReadASCII(4);
if (Format != "WAVE")
throw new NotSupportedException("Not a canonical WAVE file.");
break;
case "fmt ":
FmtChunkSize = s.ReadInt32();
if (FmtChunkSize != 16)
throw new NotSupportedException("{0} fmt chunk size is not a supported encoding scheme.".F(FmtChunkSize));
AudioFormat = s.ReadInt16();
if (AudioFormat != 1)
throw new NotSupportedException("Non-PCM compression is not supported.");
Channels = s.ReadInt16();
SampleRate = s.ReadInt32();
ByteRate = s.ReadInt32();
BlockAlign = s.ReadInt16();
BitsPerSample = s.ReadInt16();
break;
case "data":
DataSize = s.ReadInt32();
RawOutput = s.ReadBytes(DataSize);
break;
case "LIST":
ListChunkSize = s.ReadInt32();
s.ReadBytes(ListChunkSize); // Ignore annotation meta data.
break;
default:
throw new NotSupportedException("{0} chunks are not supported.".F(type));
}
}
}
}
}

View File

@@ -144,6 +144,7 @@
<Compile Include="Graphics\VxlReader.cs" />
<Compile Include="Graphics\HvaReader.cs" />
<Compile Include="StreamExts.cs" />
<Compile Include="FileFormats\WavLoader.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">

View File

@@ -35,9 +35,17 @@ namespace OpenRA
return null;
}
if (filename.ToLowerInvariant().EndsWith("wav"))
return LoadWave(new WavLoader(FileSystem.Open(filename)));
else
return LoadSoundRaw(AudLoader.LoadSound(FileSystem.Open(filename)));
}
static ISoundSource LoadWave(WavLoader wave)
{
return soundEngine.AddSoundSourceFromMemory(wave.RawOutput, wave.Channels, wave.BitsPerSample, wave.SampleRate);
}
static ISoundSource LoadSoundRaw(byte[] rawData)
{
return soundEngine.AddSoundSourceFromMemory(rawData, 1, 16, 22050);