void audiosource_portaudio::close() { err = Pa_StopStream(stream_in); if (err != paNoError) { std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl; throw audiosourceexception("Pa_StopStream"); } err = Pa_CloseStream(stream_in); if (err != paNoError) { std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl; throw audiosourceexception("Pa_CloseStream"); } err = Pa_StopStream(stream_out); if (err != paNoError) { std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl; throw audiosourceexception("Pa_StopStream"); } err = Pa_CloseStream(stream_out); if (err != paNoError) { std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl; throw audiosourceexception("Pa_CloseStream"); } err = Pa_Terminate(); if (err != paNoError) { std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl; throw audiosourceexception("Pa_Terminate"); } }
void audiosource_portaudio::init() { err = Pa_Initialize(); if( err != paNoError ) { std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl; throw audiosourceexception("Pa_Initialize"); } PaHostApiIndex api_idx; const PaHostApiInfo* info = Pa_GetHostApiInfo(Pa_GetDefaultHostApi()); std::cerr << "Default device: " << info->name << std::endl; for (api_idx = 0; api_idx < Pa_GetHostApiCount(); ++api_idx) { info = Pa_GetHostApiInfo(api_idx); std::cerr << "device " << api_idx << ": " << info->name << std::endl; } //int frames_per_buffer = get_sample_rate(); int frames_per_buffer = paFramesPerBufferUnspecified; /* * We have two separate streams for input and output to work-around a Debian specific * bug on PortAudio. */ /* Open an audio I/O stream. */ err = Pa_OpenDefaultStream( &stream_in, get_in_channel_count(), /* input channels */ 0, /* output */ paFloat32, /* 32 bit floating point output */ get_sample_rate(), frames_per_buffer, /* frames per buffer, i.e. the number of sample frames that PortAudio will request from the callback. Many apps may want to use paFramesPerBufferUnspecified, which tells PortAudio to pick the best, possibly changing, buffer size.*/ portaudio_in_callback, /* this is your callback function */ static_cast<void*>(this) ); /*This is a pointer that will be passed to your callback*/ if( err != paNoError ) { std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl; throw audiosourceexception("Pa_OpenDefaultStream"); } err = Pa_StartStream( stream_in ); if( err != paNoError ) { std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl; throw audiosourceexception("Pa_StartStream"); } /* Open an audio I/O stream. */ err = Pa_OpenDefaultStream( &stream_out, 0, /* input channels */ get_out_channel_count(), /* output */ paFloat32, /* 32 bit floating point output */ get_sample_rate(), frames_per_buffer, /* frames per buffer, i.e. the number of sample frames that PortAudio will request from the callback. Many apps may want to use paFramesPerBufferUnspecified, which tells PortAudio to pick the best, possibly changing, buffer size.*/ portaudio_out_callback, /* this is your callback function */ static_cast<void*>(this) ); /*This is a pointer that will be passed to your callback*/ if( err != paNoError ) { std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl; throw audiosourceexception("Pa_OpenDefaultStream"); } err = Pa_StartStream( stream_out ); if( err != paNoError ) { std::cerr << "PortAudio error: " << Pa_GetErrorText( err ) << std::endl; throw audiosourceexception("Pa_StartStream"); } }
void audiosource_alsa::init() { int err; std::string in_device = config::Instance()->alsa_in_device(); std::string out_device = config::Instance()->alsa_out_device(); if ((err = snd_pcm_open(&p_handle_, out_device.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0) { std::cerr << "snd_pcm_open playback error: " << snd_strerror(err) << std::endl; close(); throw audiosourceexception("snd_pcm_open"); } if ((err = snd_pcm_set_params(p_handle_, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, get_out_channel_count(), get_sample_rate(), 1, /* FIXME: resample? */ 500000)) < 0) { /* 0.5sec */ std::cerr << "snd_pcm_set_params playback error: " << snd_strerror(err) << " ch:" << get_out_channel_count() << " rate: " << get_sample_rate() << std::endl; close(); throw audiosourceexception("snd_pcm_set_params"); } snd_pcm_hw_params_t *hw_params; if ((err = snd_pcm_open(&c_handle_, in_device.c_str(), SND_PCM_STREAM_CAPTURE, 0)) < 0) { std::cerr << "snd_pcm_open capture error: " << snd_strerror(err) << std::endl; close(); throw audiosourceexception("snd_pcm_open"); } #if 0 if ((err = snd_pcm_set_params(c_handle_, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, get_in_channel_count(), get_sample_rate(), 1, /* FIXME: resample? */ 500000)) < 0) { /* 0.5sec */ std::cerr << "snd_pcm_set_params capture error: " << snd_strerror(err) << " ch:" << get_in_channel_count() << " rate: " << get_sample_rate() << std::endl; close(); throw audiosourceexception("snd_pcm_set_params"); } #endif if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { std::cerr << "cannot allocate hardware parameter structure " << snd_strerror (err) << std::endl; throw audiosourceexception("snd_pcm_hw_params_malloc"); } if ((err = snd_pcm_hw_params_any (c_handle_, hw_params)) < 0) { std::cerr << "cannot initialize hardware parameter structure " << snd_strerror (err) << std::endl; throw audiosourceexception("snd_pcm_hw_params_any"); } if ((err = snd_pcm_hw_params_set_access (c_handle_, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { std::cerr << "alsa error " << snd_strerror (err) << std::endl; throw audiosourceexception("snd_pcm_hw_params_set_access"); } if ((err = snd_pcm_hw_params_set_format (c_handle_, hw_params, SND_PCM_FORMAT_S16)) < 0) { std::cerr << "alsa error " << snd_strerror (err) << std::endl; throw audiosourceexception("snd_pcm_hw_params_set_format"); } unsigned int rate = get_sample_rate(); if ((err = snd_pcm_hw_params_set_rate_near (c_handle_, hw_params, &rate, 0)) < 0) { std::cerr << "alsa error " << snd_strerror (err) << std::endl; throw audiosourceexception("snd_pcm_hw_params_set_rate_near"); } if ((int)rate != get_sample_rate()) { std::cerr << "requested rate " << get_sample_rate() << " not available got " << rate << std::endl; throw audiosourceexception("error"); } if ((err = snd_pcm_hw_params_set_channels (c_handle_, hw_params, get_in_channel_count())) < 0) { std::cerr << "alsa error " << snd_strerror (err) << std::endl; throw audiosourceexception("snd_pcm_hw_params_set_channels"); } if ((err = snd_pcm_hw_params (c_handle_, hw_params)) < 0) { std::cerr << "alsa error " << snd_strerror (err) << std::endl; throw audiosourceexception("snd_pcm_hw_params"); } snd_pcm_hw_params_free (hw_params); }