Exemple #1
0
void SDLAudioSink::play()
{
    if (_started)
    {
        if (_paused)
        {
            SDL_PauseAudio(0);
            _paused = false;
        }
        return;
    }

    _started = true;
    _paused = false;

    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);

}
Exemple #2
0
void SDLAudioSink::play()
{
    if (_started)
    {
        if (_paused)
        {
            SDL_PauseAudio(0);
            _paused = false;
        }
        return;
    }

    _started = true;
    _paused = false;

    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.channels = _audioStream->audioNbChannels();
    wanted_specs.silence = 0;
    wanted_specs.samples = 1024;
    wanted_specs.callback = soundReadCallback;
    wanted_specs.userdata = this;
    wanted_specs.format = 0;

#if SDL_MAJOR_VERSION>=2
    switch(_audioStream->audioSampleFormat())
    {
        case(osg::AudioStream::SAMPLE_FORMAT_U8):  { wanted_specs.format = AUDIO_U8; OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_U8"<<std::endl;  break;}
        case(osg::AudioStream::SAMPLE_FORMAT_S16): { wanted_specs.format = AUDIO_S16SYS; OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_S16"<<std::endl; break; }
        case(osg::AudioStream::SAMPLE_FORMAT_S24): { OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_S24 NOT Supported by SDL."<<std::endl; break; }
        case(osg::AudioStream::SAMPLE_FORMAT_S32): { wanted_specs.format = AUDIO_S32SYS; OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_S32"<<std::endl; break; }
        case(osg::AudioStream::SAMPLE_FORMAT_F32): { wanted_specs.format = AUDIO_F32SYS; OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_F32"<<std::endl; break; }
    }
#else
    switch(_audioStream->audioSampleFormat())
    {
        case(osg::AudioStream::SAMPLE_FORMAT_U8):  { wanted_specs.format = AUDIO_U8; OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_U8"<<std::endl;  break;}
        case(osg::AudioStream::SAMPLE_FORMAT_S16): { wanted_specs.format = AUDIO_S16SYS; OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_S16"<<std::endl; break; }
        case(osg::AudioStream::SAMPLE_FORMAT_S24): { OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_S24 NOT Supported by SDL1.x."<<std::endl; break; }
        case(osg::AudioStream::SAMPLE_FORMAT_S32): { OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_S32 NOT Supported by SDL1.x"<<std::endl; break; }
        case(osg::AudioStream::SAMPLE_FORMAT_F32): { OSG_NOTICE<<"    SampleFormat  = SAMPLE_FORMAT_F32 NOT Supported by SDL1.x"<<std::endl; break; }
    }
#endif

    if (wanted_specs.format!=0)
    {

        if (SDL_OpenAudio(&wanted_specs, &specs) < 0)
            throw "SDL_OpenAudio() failed (" + std::string(SDL_GetError()) + ")";

        SDL_PauseAudio(0);
    }
    else
    {
        throw "SDL_OpenAudio() does not support audio format";
    }
}