Re-ordered SDL class to try and avoid OSX issues with _main.

This commit is contained in:
Robert Osfield 2009-05-12 13:24:08 +00:00
parent e179ecc69d
commit 9c5498376c

View File

@ -332,44 +332,9 @@ class SDLAudioSink : public osg::AudioSink
_playing(false), _playing(false),
_audioStream(audioStream) {} _audioStream(audioStream) {}
~SDLAudioSink() ~SDLAudioSink();
{
if (_playing)
{
SDL_PauseAudio(1); virtual void startPlaying();
SDL_CloseAudio();
osg::notify(osg::NOTICE)<<"~SDLAudioSink() destructor, but still playing"<<std::endl;
}
}
virtual void startPlaying()
{
_playing = true;
osg::notify(osg::NOTICE)<<"SDLAudioSink()::startPlaying()"<<std::endl;
osg::notify(osg::NOTICE)<<" audioFrequency()="<<_audioStream->audioFrequency()<<std::endl;
osg::notify(osg::NOTICE)<<" audioNbChannels()="<<_audioStream->audioNbChannels()<<std::endl;
osg::notify(osg::NOTICE)<<" audioSampleFormat()="<<_audioStream->audioSampleFormat()<<std::endl;
SDL_AudioSpec specs = { 0 };
SDL_AudioSpec wanted_specs = { 0 };
wanted_specs.freq = _audioStream->audioFrequency();
wanted_specs.format = AUDIO_S16SYS;
wanted_specs.channels = _audioStream->audioNbChannels();
wanted_specs.silence = 0;
wanted_specs.samples = 1024;
wanted_specs.callback = soundReadCallback;
wanted_specs.userdata = this;
if (SDL_OpenAudio(&wanted_specs, &specs) < 0)
throw "SDL_OpenAudio() failed (" + std::string(SDL_GetError()) + ")";
SDL_PauseAudio(0);
}
virtual bool playing() const { return _playing; } virtual bool playing() const { return _playing; }
static void soundReadCallback(void * user_data, uint8_t * data, int datalen); static void soundReadCallback(void * user_data, uint8_t * data, int datalen);
@ -378,23 +343,6 @@ class SDLAudioSink : public osg::AudioSink
osg::observer_ptr<osg::AudioStream> _audioStream; osg::observer_ptr<osg::AudioStream> _audioStream;
}; };
void SDLAudioSink::soundReadCallback(void * const user_data, Uint8 * const data, const int datalen)
{
SDLAudioSink * sink = reinterpret_cast<SDLAudioSink*>(user_data);
osg::ref_ptr<osg::AudioStream> as = sink->_audioStream.get();
if (as.valid())
{
as->consumeAudioBuffer(data, datalen);
}
}
#endif
#if defined(USE_SDL) && defined(__APPLE__)
// SDL under OSX causes a link error with a unresolved _main symbol
// so we have to add this dummy implementation to get round it.
main() {}
#endif #endif
int main(int argc, char** argv) int main(int argc, char** argv)
@ -653,3 +601,58 @@ int main(int argc, char** argv)
} }
} }
#if USE_SDL
#include "SDL.h"
SDLAudioSink::~SDLAudioSink()
{
if (_playing)
{
SDL_PauseAudio(1);
SDL_CloseAudio();
osg::notify(osg::NOTICE)<<"~SDLAudioSink() destructor, but still playing"<<std::endl;
}
}
void SDLAudioSink::startPlaying()
{
_playing = true;
osg::notify(osg::NOTICE)<<"SDLAudioSink()::startPlaying()"<<std::endl;
osg::notify(osg::NOTICE)<<" audioFrequency()="<<_audioStream->audioFrequency()<<std::endl;
osg::notify(osg::NOTICE)<<" audioNbChannels()="<<_audioStream->audioNbChannels()<<std::endl;
osg::notify(osg::NOTICE)<<" audioSampleFormat()="<<_audioStream->audioSampleFormat()<<std::endl;
SDL_AudioSpec specs = { 0 };
SDL_AudioSpec wanted_specs = { 0 };
wanted_specs.freq = _audioStream->audioFrequency();
wanted_specs.format = AUDIO_S16SYS;
wanted_specs.channels = _audioStream->audioNbChannels();
wanted_specs.silence = 0;
wanted_specs.samples = 1024;
wanted_specs.callback = soundReadCallback;
wanted_specs.userdata = this;
if (SDL_OpenAudio(&wanted_specs, &specs) < 0)
throw "SDL_OpenAudio() failed (" + std::string(SDL_GetError()) + ")";
SDL_PauseAudio(0);
}
void SDLAudioSink::soundReadCallback(void * const user_data, Uint8 * const data, const int datalen)
{
SDLAudioSink * sink = reinterpret_cast<SDLAudioSink*>(user_data);
osg::ref_ptr<osg::AudioStream> as = sink->_audioStream.get();
if (as.valid())
{
as->consumeAudioBuffer(data, datalen);
}
}
#endif