New sound driver framework. Made the SDL and GTK+ frontends use it. It will help cleaning up the sound related global variables hell. It should be easy enough to port the sound drivers of the MFC frontend to it. Now if someone could do it, it would allow using the sound drivers directly from the core, removing the C wrappers and a lot of global vars.

This commit is contained in:
bgk 2008-12-26 12:19:33 +00:00
parent 753b72123d
commit 2b3a6f8c9b
6 changed files with 196 additions and 31 deletions

View File

@ -178,7 +178,7 @@ SET(SRC_SDL
src/sdl/filters.cpp
src/sdl/text.cpp
src/sdl/inputSDL.cpp
src/sdl/sndSDL.cpp
src/common/SoundSDL.cpp
src/sdl/expr.cpp
src/sdl/exprNode.cpp
src/sdl/expr-lex.cpp
@ -217,7 +217,7 @@ SET(SRC_GTK
src/gtk/tools.cpp
src/gtk/window.cpp
src/sdl/inputSDL.cpp
src/sdl/sndSDL.cpp
src/common/SoundSDL.cpp
)
IF( NOT NO_DEBUGGER )

34
src/common/SoundDriver.h Normal file
View File

@ -0,0 +1,34 @@
// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
// Copyright (C) 2008 VBA-M development team
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or(at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __VBA_SOUND_DRIVER_H__
#define __VBA_SOUND_DRIVER_H__
class SoundDriver
{
public:
virtual ~SoundDriver() {};
virtual bool init(int quality) = 0;
virtual void pause() = 0;
virtual void reset() = 0;
virtual void resume() = 0;
virtual void write(const u16 * finalWave, int length) = 0;
virtual int getBufferLength() = 0;
};
#endif // __VBA_SOUND_DRIVER_H__

View File

@ -1,21 +1,18 @@
#include <SDL.h>
#include "../System.h"
#include "../Sound.h"
#include "../Globals.h"
#include "SoundSDL.h"
extern int emulating;
const int sdlSoundSamples = 4096;
const int sdlSoundAlign = 4;
const int sdlSoundCapacity = sdlSoundSamples * 2;
const int sdlSoundTotalLen = sdlSoundCapacity + sdlSoundAlign;
static u8 sdlSoundBuffer[sdlSoundTotalLen];
static int sdlSoundRPos;
static int sdlSoundWPos;
static SDL_cond *sdlSoundCond;
static SDL_mutex *sdlSoundMutex;
u8 SoundSDL::sdlSoundBuffer[sdlSoundTotalLen];
int SoundSDL::sdlSoundRPos;
int SoundSDL::sdlSoundWPos;
SDL_cond * SoundSDL::sdlSoundCond;
SDL_mutex * SoundSDL::sdlSoundMutex;
static inline int soundBufferFree()
inline int SoundSDL::soundBufferFree()
{
int ret = sdlSoundRPos - sdlSoundWPos - sdlSoundAlign;
if (ret < 0)
@ -23,7 +20,7 @@ static inline int soundBufferFree()
return ret;
}
static inline int soundBufferUsed()
inline int SoundSDL::soundBufferUsed()
{
int ret = sdlSoundWPos - sdlSoundRPos;
if (ret < 0)
@ -31,7 +28,7 @@ static inline int soundBufferUsed()
return ret;
}
static void soundCallback(void *,u8 *stream,int len)
void SoundSDL::soundCallback(void *,u8 *stream,int len)
{
if (len <= 0 || !emulating)
return;
@ -56,15 +53,15 @@ static void soundCallback(void *,u8 *stream,int len)
SDL_mutexV(sdlSoundMutex);
}
void systemWriteDataToSoundBuffer()
void SoundSDL::write(const u16 * finalWave, int length)
{
if (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)
{
SDL_PauseAudio(0);
}
int remain = soundBufferLen;
const u8 *wave = reinterpret_cast<const u8 *>(soundFinalWave);
int remain = length;
const u8 *wave = reinterpret_cast<const u8 *>(finalWave);
SDL_mutexP(sdlSoundMutex);
@ -97,25 +94,26 @@ void systemWriteDataToSoundBuffer()
SDL_mutexV(sdlSoundMutex);
}
bool systemSoundInit()
bool SoundSDL::init(int quality)
{
SDL_AudioSpec audio;
switch(soundQuality) {
switch(quality) {
case 1:
audio.freq = 44100;
soundBufferLen = 1470*2;
_bufferLen = 1470*2;
break;
case 2:
audio.freq = 22050;
soundBufferLen = 736*2;
_bufferLen = 736*2;
break;
case 4:
audio.freq = 11025;
soundBufferLen = 368*2;
_bufferLen = 368*2;
break;
}
audio.format=AUDIO_S16SYS;
audio.format = AUDIO_S16SYS;
audio.channels = 2;
audio.samples = 1024;
audio.callback = soundCallback;
@ -133,7 +131,7 @@ bool systemSoundInit()
}
void systemSoundShutdown()
SoundSDL::~SoundSDL()
{
SDL_mutexP(sdlSoundMutex);
int iSave = emulating;
@ -152,17 +150,21 @@ void systemSoundShutdown()
emulating = iSave;
}
void systemSoundPause()
void SoundSDL::pause()
{
SDL_PauseAudio(1);
}
void systemSoundResume()
void SoundSDL::resume()
{
SDL_PauseAudio(0);
}
void systemSoundReset()
void SoundSDL::reset()
{
}
int SoundSDL::getBufferLength()
{
return _bufferLen;
}

53
src/common/SoundSDL.h Normal file
View File

@ -0,0 +1,53 @@
// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
// Copyright (C) 2008 VBA-M development team
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or(at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __VBA_SOUND_SDL_H__
#define __VBA_SOUND_SDL_H__
#include "SoundDriver.h"
class SoundSDL: public SoundDriver
{
public:
virtual ~SoundSDL();
virtual bool init(int quality);
virtual void pause();
virtual void reset();
virtual void resume();
virtual void write(const u16 * finalWave, int length);
virtual int getBufferLength();
private:
static const int sdlSoundSamples = 4096;
static const int sdlSoundAlign = 4;
static const int sdlSoundCapacity = sdlSoundSamples * 2;
static const int sdlSoundTotalLen = sdlSoundCapacity + sdlSoundAlign;
static u8 sdlSoundBuffer[sdlSoundTotalLen];
static int sdlSoundRPos;
static int sdlSoundWPos;
static SDL_cond * sdlSoundCond;
static SDL_mutex * sdlSoundMutex;
int _bufferLen;
static int soundBufferFree();
static int soundBufferUsed();
static void soundCallback(void *, u8 *stream, int len);
};
#endif // __VBA_SOUND_SDL_H__

View File

@ -20,8 +20,9 @@
#include "../dmg/gb.h"
#include "../dmg/gbGlobals.h"
#include "../Util.h"
#include "../Sound.h"
#include "../sdl/inputSDL.h"
#include "../Sound.h"
#include "../common/SoundSDL.h"
#include "window.h"
#include "intl.h"
@ -48,6 +49,8 @@ int RGB_LOW_BITS_MASK;
int systemRenderedFrames;
int systemFPS;
static SoundDriver * systemSoundDriver = 0;
inline VBA::Window * GUI()
{
return VBA::Window::poGetInstance();
@ -153,6 +156,41 @@ void systemGbBorderOn()
{
}
void systemWriteDataToSoundBuffer()
{
systemSoundDriver->write(soundFinalWave, soundBufferLen);
}
bool systemSoundInit()
{
systemSoundShutdown();
systemSoundDriver = new SoundSDL();
bool ret = systemSoundDriver->init(soundQuality);
soundBufferLen = systemSoundDriver->getBufferLength();
return ret;
}
void systemSoundShutdown()
{
delete systemSoundDriver;
}
void systemSoundPause()
{
systemSoundDriver->pause();
}
void systemSoundResume()
{
systemSoundDriver->resume();
}
void systemSoundReset()
{
systemSoundDriver->reset();
}
void debuggerMain()
{
}

View File

@ -54,6 +54,7 @@
#include "filters.h"
#include "text.h"
#include "inputSDL.h"
#include "../common/SoundSDL.h"
#ifndef _WIN32
# include <unistd.h>
@ -223,6 +224,8 @@ int sdlMirroringEnable = 0;
static int ignore_first_resize_event = 0;
static SoundDriver * systemSoundDriver = 0;
/* forward */
void systemConsoleMessage(const char*);
@ -2143,6 +2146,8 @@ int main(int argc, char **argv)
sdl_patch_num++;
}
soundInit();
bool failed = false;
IMAGE_TYPE type = utilFindType(szFile);
@ -2299,8 +2304,6 @@ int main(int argc, char **argv)
emulating = 1;
renderedFrames = 0;
soundInit();
autoFrameSkipLastTime = throttleLastTime = systemGetClock();
SDL_WM_SetCaption("VBA-M", NULL);
@ -2708,3 +2711,38 @@ int systemGetSensorY()
{
return inputGetSensorY();
}
void systemWriteDataToSoundBuffer()
{
systemSoundDriver->write(soundFinalWave, soundBufferLen);
}
bool systemSoundInit()
{
systemSoundShutdown();
systemSoundDriver = new SoundSDL();
bool ret = systemSoundDriver->init(soundQuality);
soundBufferLen = systemSoundDriver->getBufferLength();
return ret;
}
void systemSoundShutdown()
{
delete systemSoundDriver;
}
void systemSoundPause()
{
systemSoundDriver->pause();
}
void systemSoundResume()
{
systemSoundDriver->resume();
}
void systemSoundReset()
{
systemSoundDriver->reset();
}