buffer up all the audio upfront

This commit is contained in:
Chris Forbes
2010-08-11 22:56:32 +12:00
parent 8dd9848636
commit ca6debde66
4 changed files with 69 additions and 38 deletions

View File

@@ -6,15 +6,12 @@
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
* see LICENSE. * see LICENSE.
*/ */
#endregion #endregion
using System.Collections; using System;
using System.Collections.Generic; using System.Drawing;
using System.Drawing; using System.IO;
using System.IO;
using System;
using System.Drawing.Imaging;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
public class VqaReader public class VqaReader
@@ -48,7 +45,8 @@ namespace OpenRA.FileFormats
int[,] frameData; int[,] frameData;
byte[] audioData; // audio for this frame: 22050Hz 16bit mono pcm, uncompressed. byte[] audioData; // audio for this frame: 22050Hz 16bit mono pcm, uncompressed.
public byte[] AudioData { get { return audioData; } } public byte[] AudioData { get { return audioData; } }
public int CurrentFrame { get { return currentFrame; } }
public VqaReader( Stream stream ) public VqaReader( Stream stream )
{ {
@@ -107,13 +105,49 @@ namespace OpenRA.FileFormats
offsets[i] = reader.ReadUInt32(); offsets[i] = reader.ReadUInt32();
if (offsets[i] > 0x40000000) offsets[i] -= 0x40000000; if (offsets[i] > 0x40000000) offsets[i] -= 0x40000000;
offsets[i] <<= 1; offsets[i] <<= 1;
} }
CollectAudioData();
// Load the first frame // Load the first frame
currentFrame = 0; currentFrame = 0;
AdvanceFrame(); AdvanceFrame();
} }
void CollectAudioData()
{
var ms = new MemoryStream();
var adpcmIndex = 0;
for (var i = 0; i < Frames; i++)
{
stream.Seek(offsets[i], SeekOrigin.Begin);
BinaryReader reader = new BinaryReader(stream);
var end = (i < Frames - 1) ? offsets[i + 1] : stream.Length;
while (reader.BaseStream.Position < end)
{
var type = new String(reader.ReadChars(4));
var length = Swap(reader.ReadUInt32());
switch (type)
{
case "SND2":
var rawAudio = reader.ReadBytes((int)length);
ms.Write(rawAudio);
break;
default:
reader.ReadBytes((int)length);
break;
}
if (reader.PeekChar() == 0) reader.ReadByte();
}
}
audioData = AudLoader.LoadSound(ms.ToArray(), ref adpcmIndex);
}
public void AdvanceFrame() public void AdvanceFrame()
{ {
// Seek to the start of the frame // Seek to the start of the frame
@@ -129,11 +163,8 @@ namespace OpenRA.FileFormats
switch(type) switch(type)
{ {
case "SND2": case "SND2":
// Don't parse sound (yet); skip data // Don't parse sound here.
{ reader.ReadBytes((int)length);
var rawAudio = reader.ReadBytes((int)length);
audioData = AudLoader.LoadSound(rawAudio, ref adpcmIndex);
}
break; break;
case "VQFR": case "VQFR":
DecodeVQFR(reader); DecodeVQFR(reader);
@@ -147,9 +178,7 @@ namespace OpenRA.FileFormats
} }
if (++currentFrame == Frames) if (++currentFrame == Frames)
currentFrame = cbOffset = cbChunk = 0; currentFrame = cbOffset = cbChunk = 0;
} }
int adpcmIndex = 0;
// VQA Frame // VQA Frame
public void DecodeVQFR(BinaryReader reader) public void DecodeVQFR(BinaryReader reader)

View File

@@ -48,10 +48,11 @@ namespace OpenRA
public static void SetListenerPosition(float2 position) { soundEngine.SetListenerPosition(position); } public static void SetListenerPosition(float2 position) { soundEngine.SetListenerPosition(position); }
static ISoundSource rawSource;
public static void PlayRaw(byte[] raw) public static void PlayRaw(byte[] raw)
{ {
var sound = LoadSoundRaw(raw); rawSource = LoadSoundRaw(raw);
soundEngine.Play2D(sound, false, true, float2.Zero, SoundVolume); soundEngine.Play2D(rawSource, false, true, float2.Zero, SoundVolume);
} }
public static void Play(string name) public static void Play(string name)

View File

@@ -6,14 +6,12 @@
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
* see LICENSE. * see LICENSE.
*/ */
#endregion #endregion
using System;
using System.Drawing; using System;
using System.Linq; using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Traits;
using OpenRA.Widgets;
using OpenRA.FileFormats;
namespace OpenRA.Widgets namespace OpenRA.Widgets
{ {
@@ -37,19 +35,22 @@ namespace OpenRA.Widgets
int lastTime; int lastTime;
bool advanceNext = false; bool advanceNext = false;
public override void DrawInner(World world) public override void DrawInner(World world)
{ {
if (video == null) if (video == null)
LoadVideo(Video); {
LoadVideo(Video);
Sound.PlayRaw(video.AudioData);
}
int t = Environment.TickCount; int t = Environment.TickCount;
int dt = t - lastTime; int dt = t - lastTime;
if (advanceNext) if (advanceNext)
{ {
if (video.CurrentFrame == 0)
Sound.PlayRaw(video.AudioData);
advanceNext = false; advanceNext = false;
video.AdvanceFrame(); video.AdvanceFrame();
Sound.PlayRaw(video.AudioData);
} }
if (dt > timestep) if (dt > timestep)

View File

@@ -3,7 +3,7 @@ VqaPlayer:
Y:WINDOW_BOTTOM - 200 Y:WINDOW_BOTTOM - 200
Width:200 Width:200
Height:200 Height:200
Video:aagun.vqa Video:ally10.vqa
Background@MAINMENU_BG: Background@MAINMENU_BG:
Id:MAINMENU_BG Id:MAINMENU_BG
X:(WINDOW_RIGHT - WIDTH)/2 X:(WINDOW_RIGHT - WIDTH)/2