From ad0362690f82085645b4b6c7830eef8a97d98601 Mon Sep 17 00:00:00 2001 From: bgk Date: Fri, 26 Dec 2008 17:55:22 +0000 Subject: [PATCH] Made the MFC port use the new sound framework. Win32 devs, please check. git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@823 a31d4220-a93d-0410-bf67-fe4944624d44 --- src/common/SoundDriver.h | 2 ++ src/win32/DirectSound.cpp | 24 +++++++++++++++--------- src/win32/OpenAL.cpp | 26 ++++++++++++++++---------- src/win32/Sound.h | 25 ------------------------- src/win32/VBA.cpp | 11 ++++++----- src/win32/VBA.h | 14 ++++++++++++-- src/win32/XAudio2.cpp | 25 +++++++++++++++---------- 7 files changed, 66 insertions(+), 61 deletions(-) delete mode 100644 src/win32/Sound.h diff --git a/src/common/SoundDriver.h b/src/common/SoundDriver.h index 552e87f5..671e39af 100644 --- a/src/common/SoundDriver.h +++ b/src/common/SoundDriver.h @@ -61,6 +61,8 @@ public: * Return the size in bytes of the core sound buffer. */ virtual int getBufferLength() = 0; + + virtual void setThrottle(unsigned short throttle) { }; }; #endif // __VBA_SOUND_DRIVER_H__ diff --git a/src/win32/DirectSound.cpp b/src/win32/DirectSound.cpp index 460fb574..1e5872d6 100644 --- a/src/win32/DirectSound.cpp +++ b/src/win32/DirectSound.cpp @@ -16,7 +16,7 @@ extern bool soundBufferLow; -class DirectSound : public ISound +class DirectSound : public SoundDriver { private: LPDIRECTSOUND8 pDirectSound; // DirectSound interface @@ -25,6 +25,7 @@ private: LPDIRECTSOUNDNOTIFY8 dsbNotify; HANDLE dsbEvent; WAVEFORMATEX wfx; // Primary buffer wave format + int soundBufferLen; int soundBufferTotalLen; unsigned int soundNextPosition; @@ -32,11 +33,12 @@ public: DirectSound(); virtual ~DirectSound(); - bool init(); // initialize the primary and secondary sound buffer + bool init(int quality); // initialize the primary and secondary sound buffer void pause(); // pause the secondary sound buffer void reset(); // stop and reset the secondary sound buffer void resume(); // resume the secondary sound buffer - void write(); // write the emulated sound to the secondary sound buffer + void write(const u16 * finalWave, int length); // write the emulated sound to the secondary sound buffer + virtual int getBufferLength(); }; @@ -82,7 +84,7 @@ DirectSound::~DirectSound() } -bool DirectSound::init() +bool DirectSound::init(int quality) { HRESULT hr; DWORD freq; @@ -120,7 +122,7 @@ bool DirectSound::init() return false; } - freq = 44100 / soundQuality; + freq = 44100 / quality; // calculate the number of samples per frame first // then multiply it with the size of a sample frame (16 bit * stereo) soundBufferLen = ( freq / 60 ) * 4; @@ -221,7 +223,7 @@ void DirectSound::resume() } -void DirectSound::write() +void DirectSound::write(const u16 * finalWave, int length) { if(!pDirectSound) return; @@ -291,9 +293,9 @@ void DirectSound::write() if( SUCCEEDED( hr ) ) { // Write to pointers. - CopyMemory( lpvPtr1, soundFinalWave, dwBytes1 ); + CopyMemory( lpvPtr1, finalWave, dwBytes1 ); if ( lpvPtr2 ) { - CopyMemory( lpvPtr2, soundFinalWave + dwBytes1, dwBytes2 ); + CopyMemory( lpvPtr2, finalWave + dwBytes1, dwBytes2 ); } // Release the data back to DirectSound. @@ -304,8 +306,12 @@ void DirectSound::write() } } +int DirectSound::getBufferLength() +{ + return soundBufferLen; +} -ISound *newDirectSound() +SoundDriver *newDirectSound() { return new DirectSound(); } diff --git a/src/win32/OpenAL.cpp b/src/win32/OpenAL.cpp index 4f01653c..b76669a6 100644 --- a/src/win32/OpenAL.cpp +++ b/src/win32/OpenAL.cpp @@ -7,7 +7,7 @@ #ifndef NO_OAL // Interface -#include "Sound.h" +#include "../common/SoundDriver.h" // OpenAL #include @@ -31,17 +31,18 @@ #define debugState() // #endif -class OpenAL : public ISound +class OpenAL : public SoundDriver { public: OpenAL(); virtual ~OpenAL(); - bool init(); // initialize the sound buffer queue + bool init(int quality); // initialize the sound buffer queue void pause(); // pause the secondary sound buffer void reset(); // stop and reset the secondary sound buffer void resume(); // play/resume the secondary sound buffer - void write(); // write the emulated sound to a sound buffer + void write(const u16 * finalWave, int length); // write the emulated sound to a sound buffer + virtual int getBufferLength(); private: OPENALFNTABLE ALFunction; @@ -53,6 +54,7 @@ private: ALuint tempBuffer; ALuint source; int freq; + int soundBufferLen; #ifdef LOGALL void debugState(); @@ -143,7 +145,7 @@ void OpenAL::debugState() #endif -bool OpenAL::init() +bool OpenAL::init(int quality) { winlog( "OpenAL::init\n" ); assert( initialized == false ); @@ -172,7 +174,7 @@ bool OpenAL::init() ALFunction.alGenSources( 1, &source ); ASSERT_SUCCESS; - freq = 44100 / soundQuality; + freq = 44100 / quality; // calculate the number of samples per frame first // then multiply it with the size of a sample frame (16 bit * stereo) @@ -240,7 +242,7 @@ void OpenAL::reset() } -void OpenAL::write() +void OpenAL::write(const u16 * finalWave, int length) { if( !initialized ) return; winlog( "OpenAL::write\n" ); @@ -256,7 +258,7 @@ void OpenAL::write() for( int i = 0 ; i < theApp.oalBufferCount ; i++ ) { // Filling the buffers explicitly with silence would be cleaner, // but the very first sample is usually silence anyway. - ALFunction.alBufferData( buffer[i], AL_FORMAT_STEREO16, soundFinalWave, soundBufferLen, freq ); + ALFunction.alBufferData( buffer[i], AL_FORMAT_STEREO16, finalWave, soundBufferLen, freq ); ASSERT_SUCCESS; } @@ -302,7 +304,7 @@ void OpenAL::write() ASSERT_SUCCESS; // refill buffer - ALFunction.alBufferData( tempBuffer, AL_FORMAT_STEREO16, soundFinalWave, soundBufferLen, freq ); + ALFunction.alBufferData( tempBuffer, AL_FORMAT_STEREO16, finalWave, soundBufferLen, freq ); ASSERT_SUCCESS; // requeue buffer @@ -319,8 +321,12 @@ void OpenAL::write() } } +int OpenAL::getBufferLength() +{ + return soundBufferLen; +} -ISound *newOpenAL() +SoundDriver *newOpenAL() { winlog( "newOpenAL\n" ); return new OpenAL(); diff --git a/src/win32/Sound.h b/src/win32/Sound.h deleted file mode 100644 index a0f117e1..00000000 --- a/src/win32/Sound.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -enum AUDIO_API { - DIRECTSOUND = 0 -#ifndef NO_OAL - , OPENAL_SOUND = 1 -#endif -#ifndef NO_XAUDIO2 - , XAUDIO2 = 2 -#endif -}; - -class ISound -{ - public: - virtual ~ISound() {}; - - virtual bool init() = 0; - virtual void pause() = 0; - virtual void reset() = 0; - virtual void resume() = 0; - virtual void write() = 0; - - virtual void setThrottle( unsigned short throttle ) {}; -}; diff --git a/src/win32/VBA.cpp b/src/win32/VBA.cpp index f6ca57db..abea8d73 100644 --- a/src/win32/VBA.cpp +++ b/src/win32/VBA.cpp @@ -85,12 +85,12 @@ extern IDisplay *newDirect3DDisplay(); extern Input *newDirectInput(); -extern ISound *newDirectSound(); +extern SoundDriver *newDirectSound(); #ifndef NO_OAL -extern ISound *newOpenAL(); +extern SoundDriver *newOpenAL(); #endif #ifndef NO_XAUDIO2 -extern ISound *newXAudio2_Output(); +extern SoundDriver *newXAudio2_Output(); #endif extern void remoteStubSignal(int, int); @@ -1256,7 +1256,8 @@ bool systemSoundInit() #endif } - bool retVal = theApp.sound->init(); + bool retVal = theApp.sound->init(soundQuality); + soundBufferLen = theApp.sound->getBufferLength(); if( retVal ) { theApp.sound->setThrottle( theApp.throttle ); @@ -1342,7 +1343,7 @@ void systemWriteDataToSoundBuffer() } if( theApp.sound ) { - theApp.sound->write(); + theApp.sound->write(soundFinalWave, soundBufferLen); } } diff --git a/src/win32/VBA.h b/src/win32/VBA.h index 1b6e9fdd..eaa976d8 100644 --- a/src/win32/VBA.h +++ b/src/win32/VBA.h @@ -11,7 +11,7 @@ #include "Display.h" #include "Input.h" #include "IUpdate.h" -#include "Sound.h" +#include "../common/SoundDriver.h" #include "../System.h" #include "../Util.h" @@ -40,6 +40,16 @@ enum pixelFilterType FILTER_SIMPLE4X, FILTER_HQ4X }; +enum AUDIO_API { + DIRECTSOUND = 0 +#ifndef NO_OAL + , OPENAL_SOUND = 1 +#endif +#ifndef NO_XAUDIO2 + , XAUDIO2 = 2 +#endif +}; + #define REWIND_SIZE 400000 #define DEFAULT_BATTERY_DIR ".\\battery" @@ -160,7 +170,7 @@ class VBA : public CWinApp WavWriter *soundRecorder; CString soundRecordName; bool dsoundDisableHardwareAcceleration; - ISound *sound; + SoundDriver *sound; bool aviRecording; AVIWrite *aviRecorder; CString aviRecordName; diff --git a/src/win32/XAudio2.cpp b/src/win32/XAudio2.cpp index fd95a50a..8a0d48c9 100644 --- a/src/win32/XAudio2.cpp +++ b/src/win32/XAudio2.cpp @@ -4,13 +4,12 @@ #include "stdafx.h" // Interface -#include "Sound.h" +#include "../common/SoundDriver.h" // XAudio2 #include // Internals -#include "../Sound.h" // for soundBufferLen, soundFinalWave and soundQuality #include "../System.h" // for systemMessage() #include "../Globals.h" @@ -50,17 +49,17 @@ public: // Class Declaration class XAudio2_Output - : public ISound + : public SoundDriver { public: XAudio2_Output(); ~XAudio2_Output(); // Initialization - bool init(); + bool init(int quality); // Sound Data Feed - void write(); + void write(const u16 * finalWave, int length); // Play Control void pause(); @@ -70,6 +69,7 @@ public: // Configuration Changes void setThrottle( unsigned short throttle ); + virtual int getBufferLength(); private: bool failed; @@ -79,6 +79,7 @@ private: UINT32 bufferCount; BYTE *buffers; int currentBuffer; + int soundBufferLen; IXAudio2 *xaud; IXAudio2MasteringVoice *mVoice; // listener @@ -136,7 +137,7 @@ XAudio2_Output::~XAudio2_Output() } -bool XAudio2_Output::init() +bool XAudio2_Output::init(int quality) { if( failed || initialized ) return false; @@ -156,7 +157,7 @@ bool XAudio2_Output::init() } - freq = 44100 / (UINT32)soundQuality; + freq = 44100 / (UINT32)quality; // calculate the number of samples per frame first // then multiply it with the size of a sample frame (16 bit * stereo) @@ -279,7 +280,7 @@ bool XAudio2_Output::init() } -void XAudio2_Output::write() +void XAudio2_Output::write(const u16 * finalWave, int length) { if( !initialized || failed ) return; @@ -311,7 +312,7 @@ void XAudio2_Output::write() } // copy & protect the audio data in own memory area while playing it - CopyMemory( &buffers[ currentBuffer * soundBufferLen ], soundFinalWave, soundBufferLen ); + CopyMemory( &buffers[ currentBuffer * soundBufferLen ], finalWave, soundBufferLen ); buf.AudioBytes = soundBufferLen; buf.pAudioData = &buffers[ currentBuffer * soundBufferLen ]; @@ -372,8 +373,12 @@ void XAudio2_Output::setThrottle( unsigned short throttle ) ASSERT( hr == S_OK ); } +int XAudio2_Output::getBufferLength() +{ + return soundBufferLen; +} -ISound *newXAudio2_Output() +SoundDriver *newXAudio2_Output() { return new XAudio2_Output(); }