void SoundPlayer::main() { BH_TRACE_INIT("SoundPlayer"); unsigned i; for(i = 0; i < retries; ++i) { if(snd_pcm_open(&handle, "hw:0", SND_PCM_STREAM_PLAYBACK, 0) >= 0) break; Thread::sleep(retryDelay); } ASSERT(i < retries); snd_pcm_hw_params_t* params; VERIFY(!snd_pcm_hw_params_malloc(¶ms)); VERIFY(!snd_pcm_hw_params_any(handle, params)); VERIFY(!snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)); VERIFY(!snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)); VERIFY(!snd_pcm_hw_params_set_rate_near(handle, params, &sampleRate, 0));; VERIFY(!snd_pcm_hw_params_set_channels(handle, params, 2)); VERIFY(!snd_pcm_hw_params(handle, params)); VERIFY(!snd_pcm_hw_params_get_period_size(params, &periodSize, 0)); snd_pcm_hw_params_free(params); while(isRunning() && !closing) { flush(); VERIFY(sem.wait()); } VERIFY(!snd_pcm_close(handle)); }
AudioProvider::AudioProvider() { allChannels ? channels = 4 : channels = 2; int brokenFirst = (theDamageConfigurationHead.audioChannelsDefect[0] ? 1 : 0) + (theDamageConfigurationHead.audioChannelsDefect[1] ? 1 : 0); int brokenSecond = (theDamageConfigurationHead.audioChannelsDefect[2] ? 1 : 0) + (theDamageConfigurationHead.audioChannelsDefect[3] ? 1 : 0); unsigned i; for(i = 0; i < retries; ++i) { if(snd_pcm_open(&handle, allChannels ? "4channelsDeinterleaved" : brokenFirst > brokenSecond ? "hw:0,0,1" : "hw:0", snd_pcm_stream_t(SND_PCM_STREAM_CAPTURE | SND_PCM_NONBLOCK), 0) >= 0) break; Thread::sleep(retryDelay); } ASSERT(i < retries); snd_pcm_hw_params_t* params; VERIFY(!snd_pcm_hw_params_malloc(¶ms)); VERIFY(!snd_pcm_hw_params_any(handle, params)); VERIFY(!snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)); VERIFY(!snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)); VERIFY(!snd_pcm_hw_params_set_rate_near(handle, params, &sampleRate, 0)); VERIFY(!snd_pcm_hw_params_set_channels(handle, params, channels)); VERIFY(!snd_pcm_hw_params(handle, params)); snd_pcm_hw_params_free(params); VERIFY(!snd_pcm_prepare(handle)); ASSERT(channels <= 4); short buf[4]; VERIFY(snd_pcm_readi(handle, buf, 1) >= 0); }
static void audio_renderer_init() { int rc; decoder = opus_decoder_create(SAMPLE_RATE, CHANNEL_COUNT, &rc); snd_pcm_hw_params_t *hw_params; snd_pcm_sw_params_t *sw_params; snd_pcm_uframes_t period_size = FRAME_SIZE * CHANNEL_COUNT * 2; snd_pcm_uframes_t buffer_size = 12 * period_size; unsigned int sampleRate = SAMPLE_RATE; /* Open PCM device for playback. */ CHECK_RETURN(snd_pcm_open(&handle, audio_device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) /* Set hardware parameters */ CHECK_RETURN(snd_pcm_hw_params_malloc(&hw_params)); CHECK_RETURN(snd_pcm_hw_params_any(handle, hw_params)); CHECK_RETURN(snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)); CHECK_RETURN(snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE)); CHECK_RETURN(snd_pcm_hw_params_set_rate_near(handle, hw_params, &sampleRate, NULL)); CHECK_RETURN(snd_pcm_hw_params_set_channels(handle, hw_params, CHANNEL_COUNT)); CHECK_RETURN(snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffer_size)); CHECK_RETURN(snd_pcm_hw_params_set_period_size_near(handle, hw_params, &period_size, NULL)); CHECK_RETURN(snd_pcm_hw_params(handle, hw_params)); snd_pcm_hw_params_free(hw_params); /* Set software parameters */ CHECK_RETURN(snd_pcm_sw_params_malloc(&sw_params)); CHECK_RETURN(snd_pcm_sw_params_current(handle, sw_params)); CHECK_RETURN(snd_pcm_sw_params_set_start_threshold(handle, sw_params, buffer_size - period_size)); CHECK_RETURN(snd_pcm_sw_params_set_avail_min(handle, sw_params, period_size)); CHECK_RETURN(snd_pcm_sw_params(handle, sw_params)); snd_pcm_sw_params_free(sw_params); CHECK_RETURN(snd_pcm_prepare(handle)); }
AudioProvider::AudioProvider() { unsigned i; for(i = 0; i < retries; ++i) { if(snd_pcm_open(&handle, "hw:0", snd_pcm_stream_t(SND_PCM_STREAM_CAPTURE | SND_PCM_NONBLOCK), 0) >= 0) break; SystemCall::sleep(retryDelay); } ASSERT(i < retries); snd_pcm_hw_params_t* params; VERIFY(!snd_pcm_hw_params_malloc(¶ms)); VERIFY(!snd_pcm_hw_params_any(handle, params)); VERIFY(!snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)); VERIFY(!snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)); VERIFY(!snd_pcm_hw_params_set_rate_near(handle, params, &sampleRate, 0)); VERIFY(!snd_pcm_hw_params_set_channels(handle, params, channels)); VERIFY(!snd_pcm_hw_params(handle, params)); snd_pcm_hw_params_free(params); VERIFY(!snd_pcm_prepare(handle)); ASSERT(channels <= 4); short buf[4]; VERIFY(snd_pcm_readi(handle, buf, 1) >= 0); }
RD_BOOL alsa_format_supported(RD_WAVEFORMATEX * pwfx) { #if 0 int err; snd_pcm_hw_params_t *hwparams = NULL; if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0) { error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); return False; } if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) { error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err)); return False; } snd_pcm_hw_params_free(hwparams); #endif if (pwfx->wFormatTag != WAVE_FORMAT_PCM) return False; if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) return False; if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) return False; if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050)) return False; return True; }
static boolean audin_alsa_set_params(AudinALSADevice* alsa, snd_pcm_t* capture_handle) { int error; snd_pcm_hw_params_t* hw_params; if ((error = snd_pcm_hw_params_malloc(&hw_params)) < 0) { DEBUG_WARN("snd_pcm_hw_params_malloc (%s)", snd_strerror(error)); return False; } snd_pcm_hw_params_any(capture_handle, hw_params); snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(capture_handle, hw_params, alsa->format); snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &alsa->actual_rate, NULL); snd_pcm_hw_params_set_channels_near(capture_handle, hw_params, &alsa->actual_channels); snd_pcm_hw_params(capture_handle, hw_params); snd_pcm_hw_params_free(hw_params); snd_pcm_prepare(capture_handle); if ((alsa->actual_rate != alsa->target_rate) || (alsa->actual_channels != alsa->target_channels)) { DEBUG_DVC("actual rate %d / channel %d is " "different from target rate %d / channel %d, resampling required.", alsa->actual_rate, alsa->actual_channels, alsa->target_rate, alsa->target_channels); } return True; }
ALSA(unsigned channels, unsigned samplerate, const std::string& device = "default") : runnable(true), pcm(nullptr), params(nullptr), fps(samplerate) { int rc = snd_pcm_open(&pcm, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0); if (rc < 0) { runnable = false; throw DeviceException(General::join("Unable to open PCM device ", snd_strerror(rc))); } snd_pcm_format_t fmt = type_to_format(T()); if (snd_pcm_hw_params_malloc(¶ms) < 0) { runnable = false; throw DeviceException("Failed to allocate memory."); } runnable = false; if ( (snd_pcm_hw_params_any(pcm, params) < 0) || (snd_pcm_hw_params_set_access(pcm, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) || (snd_pcm_hw_params_set_channels(pcm, params, channels) < 0) || (snd_pcm_hw_params_set_format(pcm, params, fmt) < 0) || (snd_pcm_hw_params_set_rate(pcm, params, samplerate, 0) < 0) || ((rc = snd_pcm_hw_params(pcm, params)) < 0) ) throw DeviceException(General::join("Unable to install HW params: ", snd_strerror(rc))); runnable = true; }
/* * Set some stuff up. */ static int configureInitialState(const char* pathName, AudioState* audioState) { #if BUILD_SIM_WITHOUT_AUDIO return 0; #else audioState->handle = NULL; snd_pcm_open(&audioState->handle, "default", SND_PCM_STREAM_PLAYBACK, 0); if (audioState->handle) { snd_pcm_hw_params_t *params; snd_pcm_hw_params_malloc(¶ms); snd_pcm_hw_params_any(audioState->handle, params); snd_pcm_hw_params_set_access(audioState->handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(audioState->handle, params, SND_PCM_FORMAT_S16_LE); unsigned int rate = 44100; snd_pcm_hw_params_set_rate_near(audioState->handle, params, &rate, NULL); snd_pcm_hw_params_set_channels(audioState->handle, params, 2); snd_pcm_hw_params(audioState->handle, params); snd_pcm_hw_params_free(params); } else { wsLog("Couldn't open audio hardware, faking it\n"); } return 0; #endif }
ALSAAudioCaptureDevice::ALSAAudioCaptureDevice(const char* pcmDeviceName) :pcmDevice(0),pcmHwParams(0) { /* Open the PCM device: */ int error=snd_pcm_open(&pcmDevice,pcmDeviceName,SND_PCM_STREAM_CAPTURE,0); if(error<0) { pcmDevice=0; Misc::throwStdErr("Sound::ALSAAudioCaptureDevice::ALSAAudioCaptureDevice: Error %s while opening PCM device %s",snd_strerror(error),pcmDeviceName); } /* Allocate and initialize a hardware parameter context: */ if((error=snd_pcm_hw_params_malloc(&pcmHwParams))<0) { snd_pcm_close(pcmDevice); Misc::throwStdErr("Sound::ALSAAudioCaptureDevice::ALSAAudioCaptureDevice: Error %s while allocating hardware parameter context",snd_strerror(error)); } if((error=snd_pcm_hw_params_any(pcmDevice,pcmHwParams))<0) { snd_pcm_hw_params_free(pcmHwParams); snd_pcm_close(pcmDevice); Misc::throwStdErr("Sound::ALSAAudioCaptureDevice::ALSAAudioCaptureDevice: Error %s while initializing hardware parameter context",snd_strerror(error)); } /* Set the PCM device's access method: */ if((error=snd_pcm_hw_params_set_access(pcmDevice,pcmHwParams,SND_PCM_ACCESS_RW_INTERLEAVED))<0) { snd_pcm_hw_params_free(pcmHwParams); snd_pcm_close(pcmDevice); Misc::throwStdErr("Sound::ALSAAudioCaptureDevice::ALSAAudioCaptureDevice: Error %s while setting PCM device's access method",snd_strerror(error)); } }
int main(int argc,char *argv[]){ int i = 0; int err; char buf[128]; snd_pcm_t *playback_handle; int rate = 22025; int channels = 2; snd_pcm_hw_params_t *hw_params; if((err = snd_pcm_open(&playback_handle,"default",SND_PCM_STREAM_PLAYBACK,0)) < 0){ fprintf(stderr,"can't open!%s(%s)\n","default",snd_strerror(err)); exit(1); } if((err = snd_pcm_hw_params_malloc(&hw_params) < 0)){ fprintf(stderr,"can't open!(%s)\n",snd_strerror(err)); exit(1); } if((err = snd_pcm_hw_params_any(playback_handle,hw_params)) < 0){ fprintf(stderr,"can't open(%s)\n",snd_strerror(err)); exit(1); } if((err = snd_pcm_hw_params_set_access(playback_handle,hw_params,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0){ fprintf(stderr,"can't open(%s)\n",snd_strerror(err)); exit(1); } if((err = snd_pcm_hw_params_set_format(playback_handle,hw_params,SND_PCM_FORMAT_S16_LE)) < 0){ fprintf(stderr,"can't set(%s)\n",snd_strerror(err)); exit(1); } if((err = snd_pcm_hw_params_set_rate_near(playback_handle,hw_params,&rate,0)) < 0){ fprintf(stderr,"can't set(%s)\n",snd_strerror(err)); exit(1); } if((err = snd_pcm_hw_params_set_channels(playback_handle,hw_params,channels)) < 0){ fprintf(stderr,"can't set(%s)\n",snd_strerror(err)); exit(1); } if((err = snd_pcm_hw_params(playback_handle,hw_params)) < 0){ fprintf(stderr,"can't open(%s)\n",snd_strerror(err)); exit(1); } snd_pcm_hw_params_free(hw_params); if((err = snd_pcm_prepare(playback_handle)) < 0){ fprintf(stderr,"can't prepare(%s)\n",snd_strerror(err)); exit(1); } i = 0; while(i < 256){ memset(buf,i,128); err = snd_pcm_writei(playback_handle,buf,32); //fprintf(stderr,"write %d\n",err); if(err < 0){ snd_pcm_prepare(playback_handle); printf("a"); } i++; } snd_pcm_close(playback_handle); exit(0); }
static Int32 InitAudioCaptureDevice (Int32 channels, UInt32 sample_rate, Int32 driver_buf_size) { snd_pcm_hw_params_t *hw_params; Int32 err; if ((err = snd_pcm_open (&capture_handle, ALSA_CAPTURE_DEVICE, SND_PCM_STREAM_CAPTURE, 0)) < 0) { fprintf (stderr, "AUDIO >> cannot open audio device plughw:1,0 (%s)\n", snd_strerror (err)); return -1; } // printf ("AUDIO >> opened %s device\n", ALSA_CAPTURE_DEVICE); if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot allocate hardware parameter structure (%s)\n", err, capture_handle); } if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot initialize hardware parameter structure (%s)\n", err, capture_handle); } if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot set access type (%s)\n", err, capture_handle); } if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot set sample format (%s)\n", err, capture_handle); } if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &sample_rate, 0)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot set sample rate (%s)\n", err, capture_handle); } if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, channels)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot set channel count (%s)\n", err, capture_handle); } if ((err = snd_pcm_hw_params_set_buffer_size (capture_handle, hw_params, driver_buf_size)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot set buffer size (%s)\n", err, capture_handle); } if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot set parameters (%s)\n", err, capture_handle); } snd_pcm_hw_params_free (hw_params); if ((err = snd_pcm_prepare (capture_handle)) < 0) { AUD_DEVICE_PRINT_ERROR_AND_RETURN("cannot prepare audio interface for use (%s)\n", err, capture_handle); } return 0; }
static BOOL tsmf_alsa_set_format(ITSMFAudioDevice *audio, UINT32 sample_rate, UINT32 channels, UINT32 bits_per_sample) { int error; snd_pcm_uframes_t frames; snd_pcm_hw_params_t *hw_params; snd_pcm_sw_params_t *sw_params; TSMFAlsaAudioDevice *alsa = (TSMFAlsaAudioDevice *) audio; if(!alsa->out_handle) return FALSE; snd_pcm_drop(alsa->out_handle); alsa->actual_rate = alsa->source_rate = sample_rate; alsa->actual_channels = alsa->source_channels = channels; alsa->bytes_per_sample = bits_per_sample / 8; error = snd_pcm_hw_params_malloc(&hw_params); if(error < 0) { WLog_ERR(TAG, "snd_pcm_hw_params_malloc failed"); return FALSE; } snd_pcm_hw_params_any(alsa->out_handle, hw_params); snd_pcm_hw_params_set_access(alsa->out_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(alsa->out_handle, hw_params, SND_PCM_FORMAT_S16_LE); snd_pcm_hw_params_set_rate_near(alsa->out_handle, hw_params, &alsa->actual_rate, NULL); snd_pcm_hw_params_set_channels_near(alsa->out_handle, hw_params, &alsa->actual_channels); frames = sample_rate; snd_pcm_hw_params_set_buffer_size_near(alsa->out_handle, hw_params, &frames); snd_pcm_hw_params(alsa->out_handle, hw_params); snd_pcm_hw_params_free(hw_params); error = snd_pcm_sw_params_malloc(&sw_params); if(error < 0) { WLog_ERR(TAG, "snd_pcm_sw_params_malloc"); return FALSE; } snd_pcm_sw_params_current(alsa->out_handle, sw_params); snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params, frames / 2); snd_pcm_sw_params(alsa->out_handle, sw_params); snd_pcm_sw_params_free(sw_params); snd_pcm_prepare(alsa->out_handle); DEBUG_TSMF("sample_rate %d channels %d bits_per_sample %d", sample_rate, channels, bits_per_sample); DEBUG_TSMF("hardware buffer %d frames", (int)frames); if((alsa->actual_rate != alsa->source_rate) || (alsa->actual_channels != alsa->source_channels)) { DEBUG_TSMF("actual rate %d / channel %d is different " "from source rate %d / channel %d, resampling required.", alsa->actual_rate, alsa->actual_channels, alsa->source_rate, alsa->source_channels); } return TRUE; }
int AudioALSA::init_audio() { int err = 0, dir = 1; unsigned int tmp_sampfreq = sampfreq; std::cout << qPrintable(tr("initializing audio at ")) << qPrintable(dsp_devicename) << std::endl; if ((err = snd_pcm_open(&capture_handle, dsp_devicename.toStdString().c_str(), SND_PCM_STREAM_CAPTURE, 0)) < 0) { std::cerr << "cannot open audio device " << qPrintable(dsp_devicename) << " (" << snd_strerror(err) << ")." << std::endl; exit (1); } if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) { std::cerr << "cannot allocate hardware parameter structure (" << snd_strerror(err) << ")." << std::endl; exit (1); } if ((err = snd_pcm_hw_params_any(capture_handle, hw_params)) < 0) { std::cerr << "cannot initialize hardware parameter structure (" << snd_strerror(err) << ")." << std::endl; exit (1); } if ((err = snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { std::cerr << "cannot set access type (" << snd_strerror(err) << ")." << std::endl; exit (1); } if ((err = snd_pcm_hw_params_set_format(capture_handle, hw_params, SND_PCM_FORMAT_U8)) < 0) { std::cerr << "cannot set sample format (" << snd_strerror(err) << ")." << std::endl; exit (1); } if ((err = snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &tmp_sampfreq, &dir)) < 0) { std::cerr << "cannot set sample rate (" << snd_strerror(err) << ")." << std::endl; exit (1); } sampfreq = tmp_sampfreq; if ((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params, 1)) < 0) { std::cerr << "cannot set channel count (" << snd_strerror(err) << ")." << std::endl; exit (1); } if ((err = snd_pcm_hw_params(capture_handle, hw_params)) < 0) { std::cerr << "cannot set parameters (" << snd_strerror(err) << ")." << std::endl; exit (1); } snd_pcm_hw_params_free(hw_params); if ((err = snd_pcm_prepare(capture_handle)) < 0) { std::cerr << "cannot prepare audio interface for use (" << snd_strerror(err) << std::endl; exit (1); } blksize = 256; return 1; }
AlsaDevice *alsa_device_sample( const char *device_name, unsigned int rate ) { int err; snd_pcm_hw_params_t *hw_params; static snd_output_t *jcd_out; AlsaDevice *dev = malloc( sizeof( *dev ) ); if ( !dev ) return NULL; dev->device_name = malloc( 1 + strlen( device_name ) ); if ( !dev->device_name ) { free(dev); return NULL; } strcpy(dev->device_name, device_name); err = snd_output_stdio_attach( &jcd_out, stdout, 0 ); if ( ( err = snd_pcm_open ( &dev->capture_handle, dev->device_name, SND_PCM_STREAM_CAPTURE, 0 ) ) < 0 ) { rc = 0; fprintf (stderr, "\033[0;31m[vokoscreen] alsa_device_sample() in alsadevice.c: cannot open audio device %s (%s)\033[0;0m\n", dev->device_name, snd_strerror (err) ); return NULL; } else { rc = 1; // fprintf (stderr, "[vokoscreen] alsa_device_sample() in alsadevice.c: open audio device %s (%s)\n", dev->device_name, snd_strerror (err) ); } if ( ( err = snd_pcm_hw_params_malloc ( &hw_params ) ) < 0 ) { fprintf (stderr, "[vokoscreen] alsa_device_sample() in alsadevice.c: cannot allocate hardware parameter structure (%s)\n", snd_strerror( err ) ); } if ( ( err = snd_pcm_hw_params_any( dev->capture_handle, hw_params ) ) < 0 ) { fprintf (stderr, "[vokoscreen] alsa_device_sample() in alsadevice.c: cannot initialize hardware parameter structure (%s)\n", snd_strerror( err ) ); } if ( ( err = snd_pcm_hw_params_set_rate_near (dev->capture_handle, hw_params, &rate, 0 ) ) < 0 ) { fprintf( stderr, "[vokoscreen] alsa_device_sample() in alsadevice.c: cannot set sample rate (%s)\n", snd_strerror( err ) ); rc = 0; } else { rc = 1; rcSampleRate = rate; } //fprintf ( stderr, "[vokoscreen] alsa_device_sample() in alsadevice.c: Samplerate = %d\n", rate ); snd_pcm_close( dev->capture_handle ); free( dev->device_name ); free( dev ); return dev; }
AudioAlsa::AudioAlsa( bool & _success_ful, Mixer* _mixer ) : AudioDevice( tLimit<ch_cnt_t>( ConfigManager::inst()->value( "audioalsa", "channels" ).toInt(), DEFAULT_CHANNELS, SURROUND_CHANNELS ), _mixer ), m_handle( NULL ), m_hwParams( NULL ), m_swParams( NULL ), m_convertEndian( false ) { _success_ful = false; int err; if( ( err = snd_pcm_open( &m_handle, probeDevice().toLatin1().constData(), SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 ) { printf( "Playback open error: %s\n", snd_strerror( err ) ); return; } snd_pcm_hw_params_malloc( &m_hwParams ); snd_pcm_sw_params_malloc( &m_swParams ); if( ( err = setHWParams( channels(), SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 ) { printf( "Setting of hwparams failed: %s\n", snd_strerror( err ) ); return; } if( ( err = setSWParams() ) < 0 ) { printf( "Setting of swparams failed: %s\n", snd_strerror( err ) ); return; } // set FD_CLOEXEC flag for all file descriptors so forked processes // do not inherit them struct pollfd * ufds; int count = snd_pcm_poll_descriptors_count( m_handle ); ufds = new pollfd[count]; snd_pcm_poll_descriptors( m_handle, ufds, count ); for( int i = 0; i < qMax( 3, count ); ++i ) { const int fd = ( i >= count ) ? ufds[0].fd+i : ufds[i].fd; int oldflags = fcntl( fd, F_GETFD, 0 ); if( oldflags < 0 ) continue; oldflags |= FD_CLOEXEC; fcntl( fd, F_SETFD, oldflags ); } delete[] ufds; _success_ful = true; }
static void alsa_set_hw_params(struct alsa_dev *dev, snd_pcm_t *handle, unsigned int rate, int channels, int period) { int dir, ret; snd_pcm_uframes_t period_size; snd_pcm_uframes_t buffer_size; snd_pcm_hw_params_t *hw_params; ret = snd_pcm_hw_params_malloc(&hw_params); if (ret < 0) syslog_panic("Cannot allocate hardware parameters: %s\n", snd_strerror(ret)); ret = snd_pcm_hw_params_any(handle, hw_params); if (ret < 0) syslog_panic("Cannot initialize hardware parameters: %s\n", snd_strerror(ret)); ret = snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); if (ret < 0) syslog_panic("Cannot set access type: %s\n", snd_strerror(ret)); ret = snd_pcm_hw_params_set_format(handle, hw_params, SND_PCM_FORMAT_S16_LE); if (ret < 0) syslog_panic("Cannot set sample format: %s\n", snd_strerror(ret)); ret = snd_pcm_hw_params_set_rate_near(handle, hw_params, &rate, 0); if (ret < 0) syslog_panic("Cannot set sample rate: %s\n", snd_strerror(ret)); ret = snd_pcm_hw_params_set_channels(handle, hw_params, channels); if (ret < 0) syslog_panic("Cannot set channel number: %s\n", snd_strerror(ret)); period_size = period; dir = 0; ret = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &period_size, &dir); if (ret < 0) syslog_panic("Cannot set period size: %s\n", snd_strerror(ret)); ret = snd_pcm_hw_params_set_periods(handle, hw_params, PERIODS, 0); if (ret < 0) syslog_panic("Cannot set period number: %s\n", snd_strerror(ret)); buffer_size = period_size * PERIODS; dir = 0; ret = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &buffer_size); if (ret < 0) syslog_panic("Cannot set buffer size: %s\n", snd_strerror(ret)); ret = snd_pcm_hw_params(handle, hw_params); if (ret < 0) syslog_panic("Cannot set capture parameters: %s\n", snd_strerror(ret)); snd_pcm_hw_params_free(hw_params); }
AlsaDevice *alsa_device_open( const char *device_name, int channels ) { int err; snd_pcm_hw_params_t *hw_params; static snd_output_t *jcd_out; AlsaDevice *dev = malloc( sizeof( *dev ) ); if ( !dev ) return NULL; dev->device_name = malloc( 1 + strlen( device_name ) ); if ( !dev->device_name ) { free(dev); return NULL; } strcpy(dev->device_name, device_name); dev->channels = channels; err = snd_output_stdio_attach( &jcd_out, stdout, 0 ); int okOpen = 0; while ( okOpen == 0 ) { if ( ( err = snd_pcm_open ( &dev->capture_handle, dev->device_name, SND_PCM_STREAM_CAPTURE, 0 ) ) < 0 ) { rc = 0; } else { rc = 1; okOpen = 1; } } if ( ( err = snd_pcm_hw_params_malloc ( &hw_params ) ) < 0 ) { fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror( err ) ); } if ((err = snd_pcm_hw_params_any (dev->capture_handle, hw_params)) < 0) { fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror( err ) ); } if ( ( err = snd_pcm_hw_params_set_channels( dev->capture_handle, hw_params, channels ) ) < 0 ) { rc = 0; } else { rc = 1; } snd_pcm_close( dev->capture_handle ); free( dev->device_name ); free( dev ); return dev; }
static int set_device(Instance *pi, const char *value) { ALSAio_private *priv = (ALSAio_private *)pi; int rc = 0; int i; String_free(&priv->c.device); priv->c.device = String_new(value); if (priv->c.handle) { snd_pcm_close(priv->c.handle); } /* Try matching by description first... */ Range available_alsa_devices = {}; get_device_range(pi, &available_alsa_devices); for (i=0; i < available_alsa_devices.descriptions.count; i++) { if (strstr(available_alsa_devices.descriptions.items[i]->bytes, value)) { puts("found it!"); priv->c.device = String_new(available_alsa_devices.strings.items[i]->bytes); break; } } Range_clear(&available_alsa_devices); if (String_is_none(priv->c.device)) { /* Not found, try value as supplied. */ priv->c.device = String_new(value); } rc = snd_pcm_open(&priv->c.handle, s(priv->c.device), priv->c.mode, 0); if (rc < 0) { fprintf(stderr, "*** snd_pcm_open %s: %s\n", s(priv->c.device), snd_strerror(rc)); goto out; } fprintf(stderr, "ALSA device %s opened, handle=%p\n", s(priv->c.device), priv->c.handle); /* Allocate hardware parameter structure, and call "any", and use the resulting hwparams in subsequent calls. I had tried calling _any() in each get/set function, but recording failed, so it seems ALSA doesn't work that way. */ snd_pcm_hw_params_malloc(&priv->c.hwparams); rc = snd_pcm_hw_params_any(priv->c.handle, priv->c.hwparams); /* Might as well set interleaved here, too. */ rc = snd_pcm_hw_params_set_access(priv->c.handle, priv->c.hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); if (rc != 0) { fprintf(stderr, "*** snd_pcm_hw_params_set_access %s: %s\n", s(priv->c.device), snd_strerror(rc)); } out: return rc; }
static int set_params(struct alsa_device_data * alsa_data) { snd_pcm_hw_params_t * hw_params; snd_pcm_sw_params_t * sw_params; int error; snd_pcm_uframes_t frames; snd_pcm_drop(alsa_data->out_handle); error = snd_pcm_hw_params_malloc(&hw_params); if (error < 0) { LLOGLN(0, ("set_params: snd_pcm_hw_params_malloc failed")); return 1; } snd_pcm_hw_params_any(alsa_data->out_handle, hw_params); snd_pcm_hw_params_set_access(alsa_data->out_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); snd_pcm_hw_params_set_format(alsa_data->out_handle, hw_params, alsa_data->format); snd_pcm_hw_params_set_rate_near(alsa_data->out_handle, hw_params, &alsa_data->actual_rate, NULL); snd_pcm_hw_params_set_channels_near(alsa_data->out_handle, hw_params, &alsa_data->actual_channels); frames = alsa_data->actual_rate * 4; snd_pcm_hw_params_set_buffer_size_near(alsa_data->out_handle, hw_params, &frames); snd_pcm_hw_params(alsa_data->out_handle, hw_params); snd_pcm_hw_params_free(hw_params); error = snd_pcm_sw_params_malloc(&sw_params); if (error < 0) { LLOGLN(0, ("set_params: snd_pcm_sw_params_malloc")); return 1; } snd_pcm_sw_params_current(alsa_data->out_handle, sw_params); snd_pcm_sw_params_set_start_threshold(alsa_data->out_handle, sw_params, frames / 2); snd_pcm_sw_params(alsa_data->out_handle, sw_params); snd_pcm_sw_params_free(sw_params); snd_pcm_prepare(alsa_data->out_handle); LLOGLN(10, ("set_params: hardware buffer %d frames, playback buffer %.2g seconds", (int)frames, (double)frames / 2.0 / (double)alsa_data->actual_rate)); if ((alsa_data->actual_rate != alsa_data->source_rate) || (alsa_data->actual_channels != alsa_data->source_channels)) { LLOGLN(0, ("set_params: actual rate %d / channel %d is different from source rate %d / channel %d, resampling required.", alsa_data->actual_rate, alsa_data->actual_channels, alsa_data->source_rate, alsa_data->source_channels)); } return 0; }
StackAudioDevice *stack_alsa_audio_device_create(const char *name, uint32_t channels, uint32_t sample_rate) { // Debug fprintf(stderr, "stack_alsa_audio_device_create(\"%s\", %u, %u) called\n", name, channels, sample_rate); // Allocate the new device StackAlsaAudioDevice *device = new StackAlsaAudioDevice(); device->stream = NULL; if (snd_pcm_open(&device->stream, name, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) != 0) { fprintf(stderr, "stack_alsa_audio_device_create: snd_pcm_open() failed\n"); return NULL; } // Get some initial hardware parameters snd_pcm_hw_params_t *hw_params = NULL; snd_pcm_hw_params_malloc(&hw_params); snd_pcm_hw_params_any(device->stream, hw_params); // Choose the correct sample rate if (snd_pcm_hw_params_set_rate(device->stream, hw_params, sample_rate, 0) < 0) { fprintf(stderr, "stack_alsa_audio_device_create: snd_pcm_hw_params_set_rate() failed\n"); } // Set the correct number of channels if (snd_pcm_hw_params_set_channels(device->stream, hw_params, channels) < 0) { fprintf(stderr, "stack_alsa_audio_device_create: snd_pcm_hw_params_set_channels() failed\n"); } // Set the format to 32-bit floating point, which is what Stack // uses internally if (snd_pcm_hw_params_set_format(device->stream, hw_params, SND_PCM_FORMAT_FLOAT_LE) < 0) { fprintf(stderr, "stack_alsa_audio_device_create: snd_pcm_hw_params_set_format() failed\n"); } // Apply the hardware parameters to the device snd_pcm_hw_params(device->stream, hw_params); // Set up superclass STACK_AUDIO_DEVICE(device)->_class_name = "StackAlsaAudioDevice"; STACK_AUDIO_DEVICE(device)->channels = channels; STACK_AUDIO_DEVICE(device)->sample_rate = sample_rate; // Start the PCM stream if (snd_pcm_start(device->stream) < 0) { fprintf(stderr, "stack_alsa_audio_device_create: snd_pcm_start() failed\n"); } // Return the newly created device return STACK_AUDIO_DEVICE(device); }
AudioSourceNix::AudioSourceNix(int c, int r) : AudioSource(c, r) { int err; unsigned int ap_rate = rate; unsigned int ap_chan = channels; err = snd_pcm_open(&device, ADDR, SND_PCM_STREAM_CAPTURE, 0); if (err < 0) { g2g = PR_FALSE; return; } err = snd_pcm_hw_params_malloc(¶ms); if (err < 0) { g2g = PR_FALSE; return; } err = snd_pcm_hw_params_any(device, params); if (err < 0) { g2g = PR_FALSE; return; } err = snd_pcm_hw_params_set_access( device, params, SND_PCM_ACCESS_RW_INTERLEAVED ); if (err < 0) { g2g = PR_FALSE; return; } err = snd_pcm_hw_params_set_format( device, params, SND_PCM_FORMAT_S16_LE ); if (err < 0) { g2g = PR_FALSE; return; } /* For rate and channels if we don't get * what we want, that's too bad */ err = snd_pcm_hw_params_set_rate_near( device, params, &ap_rate, 0 ); if (err < 0) { err = snd_pcm_hw_params_get_rate( params, &ap_rate, 0 ); if (err < 0) { g2g = PR_FALSE; return; } } err = snd_pcm_hw_params_set_channels( device, params, ap_chan ); if (err < 0) { err = snd_pcm_hw_params_get_channels( params, &ap_chan ); if (err < 0) { g2g = PR_FALSE; return; } } g2g = PR_TRUE; rec = PR_FALSE; rate = ap_rate; channels = ap_chan; snd_pcm_close(device); }
GstCaps * gst_alsa_probe_supported_formats (GstObject * obj, gchar * device, snd_pcm_t * handle, const GstCaps * template_caps) { snd_pcm_hw_params_t *hw_params; snd_pcm_stream_t stream_type; GstCaps *caps; gint err; snd_pcm_hw_params_malloc (&hw_params); if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) goto error; stream_type = snd_pcm_stream (handle); caps = gst_caps_copy (template_caps); if (!(caps = gst_alsa_detect_formats (obj, hw_params, caps))) goto subroutine_error; if (!(caps = gst_alsa_detect_rates (obj, hw_params, caps))) goto subroutine_error; if (!(caps = gst_alsa_detect_channels (obj, hw_params, caps))) goto subroutine_error; /* Try opening IEC958 device to see if we can support that format (playback * only for now but we could add SPDIF capture later) */ if (stream_type == SND_PCM_STREAM_PLAYBACK) { snd_pcm_t *pcm = gst_alsa_open_iec958_pcm (obj, device); if (G_LIKELY (pcm)) { gst_caps_append (caps, gst_caps_from_string (PASSTHROUGH_CAPS)); snd_pcm_close (pcm); } } snd_pcm_hw_params_free (hw_params); return caps; /* ERRORS */ error: { GST_ERROR_OBJECT (obj, "failed to query formats: %s", snd_strerror (err)); snd_pcm_hw_params_free (hw_params); return NULL; } subroutine_error: { GST_ERROR_OBJECT (obj, "failed to query formats"); snd_pcm_hw_params_free (hw_params); return NULL; } }
static int setup_playback(void) { int err; snd_pcm_hw_params_t *pcm_params; if ((err = snd_pcm_hw_params_malloc(&pcm_params)) < 0 || (err = snd_pcm_hw_params_any(pcm_handle, pcm_params)) < 0 || (err = snd_pcm_hw_params_set_access(pcm_handle, pcm_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0 || (err = snd_pcm_hw_params_set_format(pcm_handle, pcm_params, pcm_format)) < 0 || (err = snd_pcm_hw_params_set_rate_near(pcm_handle, pcm_params, &pcm_hz, 0)) < 0 || (err = snd_pcm_hw_params_set_channels(pcm_handle, pcm_params, 1)) < 0) { module_logger(&alsaSound, _L|LOG_USER | LOG_ERROR, _("params setup error: %s\n"), snd_strerror(err)); return 0; } #if 0 // ??? pcm_samplesize = pcm_periodsize * pcm_periods / pcm_framesize; if ((err = snd_pcm_hw_params_set_periods(pcm_handle, pcm_params, pcm_periods, 0)) < 0 || (err = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, pcm_params, &pcm_samplesize)) < 0) { module_logger(&alsaSound, _L|LOG_USER | LOG_ERROR, _("cannot set up period size: %s\n"), snd_strerror(err)); // continue } samplebuf = (s8 *) malloc(pcm_samplesize * pcm_framesize); #else pcm_samplesize = pcm_periodsize; samplebuf = (s16 *) malloc(pcm_samplesize * sizeof(samplebuf[0])); #endif if ((err = snd_pcm_hw_params(pcm_handle, pcm_params)) < 0) { module_logger(&alsaSound, _L|LOG_USER | LOG_ERROR, _("params setting error: %s\n"), snd_strerror(err)); return 0; } snd_pcm_hw_params_free(pcm_params); if ((err = snd_pcm_prepare(pcm_handle)) < 0) { module_logger(&alsaSound, _L|LOG_USER | LOG_ERROR, _("error preparing PCM handle\n"), snd_strerror(err)); return 0; } return 1; }
static ALCboolean alsa_start_playback(ALCdevice *device) { alsa_data *data = (alsa_data*)device->ExtraData; snd_pcm_hw_params_t *hp = NULL; snd_pcm_access_t access; const char *funcerr; int err; snd_pcm_hw_params_malloc(&hp); #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error CHECK(snd_pcm_hw_params_current(data->pcmHandle, hp)); /* retrieve configuration info */ CHECK(snd_pcm_hw_params_get_access(hp, &access)); #undef CHECK snd_pcm_hw_params_free(hp); hp = NULL; data->size = snd_pcm_frames_to_bytes(data->pcmHandle, device->UpdateSize); if(access == SND_PCM_ACCESS_RW_INTERLEAVED) { data->buffer = malloc(data->size); if(!data->buffer) { ERR("buffer malloc failed\n"); return ALC_FALSE; } data->thread = StartThread(ALSANoMMapProc, device); } else { err = snd_pcm_prepare(data->pcmHandle); if(err < 0) { ERR("snd_pcm_prepare(data->pcmHandle) failed: %s\n", snd_strerror(err)); return ALC_FALSE; } data->thread = StartThread(ALSAProc, device); } if(data->thread == NULL) { ERR("Could not create playback thread\n"); free(data->buffer); data->buffer = NULL; return ALC_FALSE; } return ALC_TRUE; error: ERR("%s failed: %s\n", funcerr, snd_strerror(err)); if(hp) snd_pcm_hw_params_free(hp); return ALC_FALSE; }
bool GqRecordALSA::create_record() { int err; if ((err = snd_pcm_open(&m_pcapture_handle, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) { fprintf(stderr, "cannot open audio device (%s)\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_hw_params_malloc(&m_hw_params)) < 0) { fprintf(stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_hw_params_any(m_pcapture_handle, m_hw_params)) < 0) { fprintf(stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_hw_params_set_access(m_pcapture_handle, m_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { fprintf(stderr, "cannot set access type (%s)\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_hw_params_set_format(m_pcapture_handle, m_hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { fprintf(stderr, "cannot set sample format (%s)\n", snd_strerror(err)); exit(1); } unsigned int nsp = 16000; int nrt = 0; if ((err = snd_pcm_hw_params_set_rate_near(m_pcapture_handle, m_hw_params, &nsp, &nrt)) < 0) { fprintf(stderr, "cannot set sample rate (%s)\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_hw_params_set_channels(m_pcapture_handle, m_hw_params, 1)) < 0) { fprintf(stderr, "cannot set channel count (%s)\n", snd_strerror(err)); exit(1); } if ((err = snd_pcm_hw_params(m_pcapture_handle, m_hw_params)) < 0) { fprintf(stderr, "cannot set parameters (%s)\n", snd_strerror(err)); exit(1); } return true; }
static void * alsa_new (void) { guint i; gint err; alsa_driver *d = g_new(alsa_driver, 1); d->device = g_strdup("hw:0,0"); d->bits = 8; d->stereo = 0; d->buffer_size = 14; d->playrate = 44100; d->minfreq_old = 0; d->maxfreq_old = 0; d->address_old = 0; d->bufsize_old = 0; d->num_periods = 1; d->can8 = TRUE; d->can16 = TRUE; d->canmono = TRUE; d->canstereo = TRUE; d->signedness8 = FALSE; d->signedness16 = TRUE; d->persizemin = 256; d->persizemax = 8192; for(i = 0; i < NUM_FORMATS; i++) { d->devcap[i].minfreq = 22050; d->devcap[i].maxfreq = 44100; d->devcap[i].minbufsize = 512; d->devcap[i].maxbufsize = 16384; } d->soundfd = 0; d->sndbuf = NULL; d->polltag = NULL; d->pfd = NULL; d->verbose = FALSE; d->hwtest = TRUE; if((err = snd_output_stdio_attach(&(d->output), stdout,0)) < 0) { alsa_error(N_("Error attaching sound output"), err); return NULL; } snd_pcm_hw_params_malloc(&(d->hwparams)); snd_pcm_sw_params_malloc(&(d->swparams)); alsa_make_config_widgets(d); return d; }
static snd_pcm_t* init_playback_device(const char* devname) { snd_pcm_t *playback_handle; snd_pcm_hw_params_t *hw_params; chk(snd_pcm_open(&playback_handle, devname, SND_PCM_STREAM_PLAYBACK,0), "cannot open audio device (%s)\n"); chk(snd_pcm_hw_params_malloc(&hw_params), "cannot allocate hardware parameter structure (%s)\n"); chk(snd_pcm_hw_params_any(playback_handle, hw_params), "cannot initialize hardware parameter structure (%s)\n"); chk(snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED), "cannot set access type (%s)\n"); chk(snd_pcm_hw_params_set_format(playback_handle, hw_params, FORMAT), "cannot set sample format (%s)\n"); unsigned rate = BITRATE; chk(snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &rate, 0), "cannot set sample rate (%s)\n"); chk(snd_pcm_hw_params_set_channels(playback_handle, hw_params, NUMCHANNELS),"cannot set channel count (%s)\n"); chk(snd_pcm_hw_params(playback_handle, hw_params), "cannot set parameters (%s)\n"); snd_pcm_hw_params_free(hw_params); chk(snd_pcm_prepare(playback_handle), "cannot prepare audio interface for use (%s)\n"); return playback_handle; }
void setup_input_stream( void) { int err; snd_pcm_hw_params_t *hw_params; if( (err = snd_pcm_open( &capture_handle, CF_device, SND_PCM_STREAM_CAPTURE, 0)) < 0) bailout( "cannot open audio device %s (%s)\n", CF_device, snd_strerror( err)); if( (err = snd_pcm_hw_params_malloc( &hw_params)) < 0 || (err = snd_pcm_hw_params_any( capture_handle, hw_params)) < 0) bailout( "cannot init hardware params struct (%s)\n", snd_strerror( err)); unsigned int rate_min, rate_max; snd_pcm_hw_params_get_rate_min( hw_params, &rate_min, 0); snd_pcm_hw_params_get_rate_max( hw_params, &rate_max, 0); report( 1, "rate min %d max %d", rate_min, rate_max); if( (err = snd_pcm_hw_params_set_access( capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) bailout("cannot set access type (%s)\n", snd_strerror (err)); if ((err = snd_pcm_hw_params_set_format( capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) bailout( "cannot set sample format (%s)\n", snd_strerror (err)); if ((err = snd_pcm_hw_params_set_rate_near( capture_handle, hw_params, &CF_sample_rate, 0)) < 0) bailout( "cannot set sample rate (%s)\n", snd_strerror (err)); report( 0, "sample rate set: %d", CF_sample_rate); if( (err = snd_pcm_hw_params_set_channels( capture_handle, hw_params, CF_chans)) < 0) bailout( "cannot set channel count (%s)\n", snd_strerror (err)); if( (err = snd_pcm_hw_params( capture_handle, hw_params)) < 0) bailout( "cannot set parameters (%s)\n", snd_strerror (err)); snd_pcm_hw_params_free( hw_params); if ((err = snd_pcm_prepare( capture_handle)) < 0) bailout( "cannot prepare soundcard (%s)", snd_strerror (err)); }
int audio_drv_init(void) { unsigned int rate = 44100; int err; snd_pcm_hw_params_t *hw_params; if ((err = snd_pcm_open(&playback_handle, "hw", SND_PCM_STREAM_PLAYBACK, 0)) < 0) return -1; if ((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) return -2; if ((err = snd_pcm_hw_params_any(playback_handle, hw_params)) < 0) return -3; if ((err = snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) return -4; if ((err = snd_pcm_hw_params_set_format(playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) return -5; if ((err = snd_pcm_hw_params_set_rate_near(playback_handle, hw_params, &rate, 0)) < 0) return -6; if ((err = snd_pcm_hw_params_set_channels(playback_handle, hw_params, 2)) < 0) return -7; if ((err = snd_pcm_hw_params_set_period_size(playback_handle, hw_params, 4096, 0)) < 0) return -8; if ((err = snd_pcm_hw_params_set_periods(playback_handle, hw_params, 4, 0)) < 0) return -9; if ((err = snd_pcm_hw_params(playback_handle, hw_params)) < 0) return -10; snd_pcm_hw_params_free(hw_params); if ((err = snd_pcm_prepare(playback_handle)) < 0) return -11; return 0; }
int snd_param_init(struct playerHandles *ph, int *enc, int *channels, unsigned int *rate){ snd_pcm_uframes_t bufsize=*rate/4; int x=0; *enc=SND_PCM_FORMAT_S16_LE; snd_pcm_drop(ph->sndfd); snd_pcm_hw_params_malloc(&ph->params); if(ph->params==NULL){ fprintf(stderr,"can't malloc params\n"); return 1; } if(snd_pcm_hw_params_any(ph->sndfd,ph->params)<0)fprintf(stderr,"can't init params\n"); if(snd_pcm_hw_params_set_access(ph->sndfd,ph->params,SND_PCM_ACCESS_RW_INTERLEAVED)<0)fprintf(stderr,"no access\n"); if(snd_pcm_hw_params_set_format(ph->sndfd,ph->params,*enc)<0)fprintf(stderr,"can't set fmt\n"); if(snd_pcm_hw_params_set_channels(ph->sndfd,ph->params,*channels)<0)fprintf(stderr,"can't set channels\n"); if(snd_pcm_hw_params_set_rate_near(ph->sndfd,ph->params,rate,0)<0)fprintf(stderr,"can't set rate\n"); if(snd_pcm_hw_params_set_buffer_size_near(ph->sndfd,ph->params,&bufsize)<0)fprintf(stderr,"can't set buffer size\n"); if((x=snd_pcm_hw_params(ph->sndfd,ph->params))<0)fprintf(stderr,"can't set parms: %s\n",snd_strerror(x)); snd_pcm_hw_params_free(ph->params); return 0; }