| 334 | | |
| 335 | | ~SDLAudioSink() |
| 336 | | { |
| 337 | | if (_playing) |
| 338 | | { |
| 339 | | |
| 340 | | SDL_PauseAudio(1); |
| 341 | | SDL_CloseAudio(); |
| 342 | | |
| 343 | | osg::notify(osg::NOTICE)<<"~SDLAudioSink() destructor, but still playing"<<std::endl; |
| 344 | | } |
| 345 | | } |
| 346 | | |
| 347 | | virtual void startPlaying() |
| 348 | | { |
| 349 | | _playing = true; |
| 350 | | osg::notify(osg::NOTICE)<<"SDLAudioSink()::startPlaying()"<<std::endl; |
| 351 | | |
| 352 | | osg::notify(osg::NOTICE)<<" audioFrequency()="<<_audioStream->audioFrequency()<<std::endl; |
| 353 | | osg::notify(osg::NOTICE)<<" audioNbChannels()="<<_audioStream->audioNbChannels()<<std::endl; |
| 354 | | osg::notify(osg::NOTICE)<<" audioSampleFormat()="<<_audioStream->audioSampleFormat()<<std::endl; |
| 355 | | |
| 356 | | SDL_AudioSpec specs = { 0 }; |
| 357 | | SDL_AudioSpec wanted_specs = { 0 }; |
| 358 | | |
| 359 | | wanted_specs.freq = _audioStream->audioFrequency(); |
| 360 | | wanted_specs.format = AUDIO_S16SYS; |
| 361 | | wanted_specs.channels = _audioStream->audioNbChannels(); |
| 362 | | wanted_specs.silence = 0; |
| 363 | | wanted_specs.samples = 1024; |
| 364 | | wanted_specs.callback = soundReadCallback; |
| 365 | | wanted_specs.userdata = this; |
| 366 | | |
| 367 | | if (SDL_OpenAudio(&wanted_specs, &specs) < 0) |
| 368 | | throw "SDL_OpenAudio() failed (" + std::string(SDL_GetError()) + ")"; |
| 369 | | |
| 370 | | SDL_PauseAudio(0); |
| 371 | | |
| 372 | | } |
| | 334 | |
| | 335 | ~SDLAudioSink(); |
| | 336 | |
| | 337 | virtual void startPlaying(); |
| 381 | | |
| 382 | | void SDLAudioSink::soundReadCallback(void * const user_data, Uint8 * const data, const int datalen) |
| 383 | | { |
| 384 | | SDLAudioSink * sink = reinterpret_cast<SDLAudioSink*>(user_data); |
| 385 | | osg::ref_ptr<osg::AudioStream> as = sink->_audioStream.get(); |
| 386 | | if (as.valid()) |
| 387 | | { |
| 388 | | as->consumeAudioBuffer(data, datalen); |
| 389 | | } |
| 390 | | } |
| 391 | | |
| 392 | | #endif |
| 393 | | |
| 394 | | #if defined(USE_SDL) && defined(__APPLE__) |
| 395 | | // SDL under OSX causes a link error with a unresolved _main symbol |
| 396 | | // so we have to add this dummy implementation to get round it. |
| 397 | | main() {} |
| | 604 | #if USE_SDL |
| | 605 | |
| | 606 | #include "SDL.h" |
| | 607 | |
| | 608 | SDLAudioSink::~SDLAudioSink() |
| | 609 | { |
| | 610 | if (_playing) |
| | 611 | { |
| | 612 | |
| | 613 | SDL_PauseAudio(1); |
| | 614 | SDL_CloseAudio(); |
| | 615 | |
| | 616 | osg::notify(osg::NOTICE)<<"~SDLAudioSink() destructor, but still playing"<<std::endl; |
| | 617 | } |
| | 618 | } |
| | 619 | |
| | 620 | void SDLAudioSink::startPlaying() |
| | 621 | { |
| | 622 | _playing = true; |
| | 623 | osg::notify(osg::NOTICE)<<"SDLAudioSink()::startPlaying()"<<std::endl; |
| | 624 | |
| | 625 | osg::notify(osg::NOTICE)<<" audioFrequency()="<<_audioStream->audioFrequency()<<std::endl; |
| | 626 | osg::notify(osg::NOTICE)<<" audioNbChannels()="<<_audioStream->audioNbChannels()<<std::endl; |
| | 627 | osg::notify(osg::NOTICE)<<" audioSampleFormat()="<<_audioStream->audioSampleFormat()<<std::endl; |
| | 628 | |
| | 629 | SDL_AudioSpec specs = { 0 }; |
| | 630 | SDL_AudioSpec wanted_specs = { 0 }; |
| | 631 | |
| | 632 | wanted_specs.freq = _audioStream->audioFrequency(); |
| | 633 | wanted_specs.format = AUDIO_S16SYS; |
| | 634 | wanted_specs.channels = _audioStream->audioNbChannels(); |
| | 635 | wanted_specs.silence = 0; |
| | 636 | wanted_specs.samples = 1024; |
| | 637 | wanted_specs.callback = soundReadCallback; |
| | 638 | wanted_specs.userdata = this; |
| | 639 | |
| | 640 | if (SDL_OpenAudio(&wanted_specs, &specs) < 0) |
| | 641 | throw "SDL_OpenAudio() failed (" + std::string(SDL_GetError()) + ")"; |
| | 642 | |
| | 643 | SDL_PauseAudio(0); |
| | 644 | |
| | 645 | } |
| | 646 | |
| | 647 | void SDLAudioSink::soundReadCallback(void * const user_data, Uint8 * const data, const int datalen) |
| | 648 | { |
| | 649 | SDLAudioSink * sink = reinterpret_cast<SDLAudioSink*>(user_data); |
| | 650 | osg::ref_ptr<osg::AudioStream> as = sink->_audioStream.get(); |
| | 651 | if (as.valid()) |
| | 652 | { |
| | 653 | as->consumeAudioBuffer(data, datalen); |
| | 654 | } |
| | 655 | } |
| | 656 | |
| | 657 | #endif |
| | 658 | |