Some fixes to the latest SDL sound code. Sounds seem to keep better

sync with the video updates in this version.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@116 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2002-10-12 15:24:49 +00:00
parent 1810444904
commit 88b0994efb
3 changed files with 44 additions and 44 deletions

View File

@ -13,15 +13,15 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: SoundSDL.cxx,v 1.1 2002-10-11 13:07:00 stephena Exp $
// $Id: SoundSDL.cxx,v 1.2 2002-10-12 15:24:49 stephena Exp $
//============================================================================
#include <SDL.h>
#include "SoundSDL.hxx"
static uInt8 currentVolume;
static MediaSource* _mediaSource;
static uInt8 _myCurrentVolume;
static MediaSource* _myMediaSource;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SoundSDL::SoundSDL(bool activate)
@ -64,8 +64,8 @@ SoundSDL::SoundSDL(bool activate)
mySampleRate = obtained.freq;
// Take care of the static stuff ...
currentVolume = 0;
_mediaSource = (MediaSource*) NULL;
_myCurrentVolume = 0;
_myMediaSource = (MediaSource*) NULL;
SDL_PauseAudio(0);
}
@ -73,6 +73,7 @@ SoundSDL::SoundSDL(bool activate)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SoundSDL::~SoundSDL()
{
close();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -105,8 +106,6 @@ void SoundSDL::mute(bool state)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SoundSDL::close()
{
SDL_PauseAudio(0);
if(myIsInitializedFlag)
SDL_CloseAudio();
@ -120,28 +119,26 @@ void SoundSDL::setSoundVolume(uInt32 percent)
return;
if((percent >= 0) && (percent <= 100))
currentVolume = (int) (((float) percent / 100.0) * (float) SDL_MIX_MAXVOLUME);
_myCurrentVolume = (int) (((float) percent / 100.0) * (float) SDL_MIX_MAXVOLUME);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SoundSDL::updateSound(MediaSource& mediaSource)
void SoundSDL::setMediaSource(MediaSource& mediaSource)
{
// this is a HUGE HACK and will disappear soon
_mediaSource = &mediaSource;
_myMediaSource = &mediaSource;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void SoundSDL::fillAudio(void* udata, uInt8* stream, Int32 len)
{
if(!_mediaSource)
if(!_myMediaSource)
return;
uInt32 samples = _mediaSource->numberOfAudioSamples();
if(samples == 0)
return;
uInt8 buffer[len];
_mediaSource->dequeueAudioSamples(buffer, len);
SDL_MixAudio(stream, buffer, len, currentVolume);
// Dequeue samples as long as full fragments are available
if(_myMediaSource->numberOfAudioSamples() >= (uInt32) len)
{
uInt8 buffer[len];
_myMediaSource->dequeueAudioSamples(buffer, (uInt32)len);
SDL_MixAudio(stream, buffer, len, _myCurrentVolume);
}
}

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: SoundSDL.hxx,v 1.1 2002-10-11 13:07:01 stephena Exp $
// $Id: SoundSDL.hxx,v 1.2 2002-10-12 15:24:49 stephena Exp $
//============================================================================
#ifndef SOUNDSDL_HXX
@ -28,7 +28,7 @@
This class implements the sound API for SDL.
@author Stephen Anthony
@version $Id: SoundSDL.hxx,v 1.1 2002-10-11 13:07:01 stephena Exp $
@version $Id: SoundSDL.hxx,v 1.2 2002-10-12 15:24:49 stephena Exp $
*/
class SoundSDL
{
@ -52,9 +52,9 @@ class SoundSDL
uInt32 getSampleRate() const;
/**
Return true iff the sound device was successfully initlaized.
Return true iff the sound device was successfully initialized.
@return true iff the sound device was successfully initlaized.
@return true iff the sound device was successfully initialized
*/
bool isSuccessfullyInitialized() const;
@ -67,25 +67,27 @@ class SoundSDL
void setSoundVolume(uInt32 volume);
/**
Update the sound device using the audio sample from the specified
media source.
Notifies this class of the MediaSource object where sample data
may be obtained. The SDL sound api is thread-based, so the SDL
audio callback directly queries the MediaSource when it requires
more audio samples.
@param mediaSource The media source to get audio samples from.
@param mediaSource The MediaSource where sample data is obtained
*/
void updateSound(MediaSource& mediaSource);
void setMediaSource(MediaSource& mediaSource);
/**
Closes the soudn device
*/
void close();
Set the mute state of the sound object.
/**
Set the mute state of the sound object
@param state Mutes sound iff true
@param state Mutes sound if true, unmute if false
*/
void mute(bool state);
/**
Closes the sound device
*/
void close();
private:
// Indicates if the sound device was successfully initialized
bool myIsInitializedFlag;
@ -99,12 +101,11 @@ class SoundSDL
// Indicates if the sound is currently muted
bool myIsMuted;
/**
Some stuff which is required to be static since the SDL audio
callback is a C-style function.
*/
private:
/**
The callback used by the SDL sound API. It obtains samples
from the MediaSource as it needs them.
*/
static void fillAudio(void* udata, uInt8* stream, Int32 len);
};

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id: mainSDL.cxx,v 1.30 2002-10-11 13:07:01 stephena Exp $
// $Id: mainSDL.cxx,v 1.31 2002-10-12 15:24:49 stephena Exp $
//============================================================================
#include <fstream>
@ -587,7 +587,7 @@ void togglePause()
thePauseIndicator = true;
}
// Pause the console
// Pause the console and the audio
theConsole->mediaSource().pause(thePauseIndicator);
// Show a different palette depending on pause state
@ -1573,6 +1573,10 @@ int main(int argc, char* argv[])
theConsole = new Console(image, size, filename,
theEvent, propertiesSet, sound.getSampleRate());
// Let the sound object know about the MediaSource.
// The sound object takes care of getting samples from the MediaSource.
sound.setMediaSource(theConsole->mediaSource());
// Free the image since we don't need it any longer
delete[] image;
@ -1625,7 +1629,6 @@ int main(int argc, char* argv[])
startTime = getTicks();
theConsole->mediaSource().update();
sound.updateSound(theConsole->mediaSource());
updateDisplay(theConsole->mediaSource());
handleEvents();
@ -1665,7 +1668,6 @@ int main(int argc, char* argv[])
if(!thePauseIndicator)
{
theConsole->mediaSource().update();
sound.updateSound(theConsole->mediaSource());
}
updateDisplay(theConsole->mediaSource());
handleEvents();