本文整理汇总了C++中snd_pcm_close函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_close函数的具体用法?C++ snd_pcm_close怎么用?C++ snd_pcm_close使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_close函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: alsa_init
//.........这里部分代码省略.........
ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Unsupported sample format"));
goto Error;
}
break;
default:
ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Unsupported sample format"));
goto Error;
}
alsa_sample_size = (alsa_bits / 8) * (alsa_stereo ? 2 : 1);
if (fragsize == 0) {
unsigned int size = alsa_rate * ALSA_DEFAULT_BUFFER_MS / 1000 / numfrags;
fragsize = 1;
while (fragsize < size)
fragsize <<= 1;
}
snd_pcm_hw_params_malloc(&hwparams);
snd_pcm_sw_params_malloc(&swparams);
ALSA9_CHECK(snd_pcm_hw_params_any(pcm_handle, hwparams));
ALSA9_CHECK(snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED));
ALSA9_CHECK(snd_pcm_hw_params_set_format(pcm_handle, hwparams, format));
ALSA9_CHECK(snd_pcm_hw_params_set_channels(pcm_handle, hwparams, alsa_stereo + 1));
ALSA9_CHECK(snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &alsa_rate, NULL));
ALSA9_CHECK(snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &fragsize, NULL));
ALSA9_CHECK(snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &numfrags, NULL));
ALSA9_CHECK(snd_pcm_hw_params(pcm_handle, hwparams));
ALSA9_CHECK(snd_pcm_hw_params_get_period_size(hwparams, &alsa_bufsize, NULL));
ALSA9_CHECK(snd_pcm_hw_params_get_periods(hwparams, &alsa_fragments, NULL));
TRACE (PREFIX_I "alsa_bufsize = %ld, alsa_fragments = %d\n", alsa_bufsize, alsa_fragments);
ALSA9_CHECK(snd_pcm_sw_params_current(pcm_handle, swparams));
ALSA9_CHECK(snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, alsa_bufsize));
ALSA9_CHECK(snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, fragsize));
ALSA9_CHECK(snd_pcm_sw_params_set_xfer_align(pcm_handle, swparams, 1));
ALSA9_CHECK(snd_pcm_sw_params(pcm_handle, swparams));
/* Allocate mixing buffer. */
alsa_bufdata = _AL_MALLOC_ATOMIC(alsa_bufsize * alsa_sample_size);
if (!alsa_bufdata) {
ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Can not allocate audio buffer"));
goto Error;
}
/* Initialise mixer. */
digi_alsa.voices = voices;
if (_mixer_init(alsa_bufsize * (alsa_stereo ? 2 : 1), alsa_rate,
alsa_stereo, ((alsa_bits == 16) ? 1 : 0),
&digi_alsa.voices) != 0) {
ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Can not init software mixer"));
goto Error;
}
snd_pcm_prepare(pcm_handle);
pdc = snd_pcm_poll_descriptors_count (pcm_handle);
if (pdc <= 0) {
ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Invalid poll descriptors count"));
goto Error;
}
ufds = _AL_MALLOC(sizeof(struct pollfd) * pdc);
if (ufds == NULL) {
ustrzcpy(allegro_error, ALLEGRO_ERROR_SIZE, get_config_text("Not enough memory for poll descriptors"));
goto Error;
}
ALSA9_CHECK(snd_pcm_poll_descriptors(pcm_handle, ufds, pdc));
poll_next = 0;
_mix_some_samples((uintptr_t) alsa_bufdata, 0, alsa_signed);
/* Add audio interrupt. */
_unix_bg_man->register_func(alsa_update);
uszprintf(alsa_desc, sizeof(alsa_desc),
get_config_text
("Alsa 0.9, Device '%s': %d bits, %s, %d bps, %s"),
alsa_device, alsa_bits,
uconvert_ascii((alsa_signed ? "signed" : "unsigned"), tmp1),
alsa_rate, uconvert_ascii((alsa_stereo ? "stereo" : "mono"), tmp2));
digi_driver->desc = alsa_desc;
return 0;
Error:
if (pcm_handle) {
snd_pcm_close(pcm_handle);
pcm_handle = NULL;
}
return -1;
}
示例2: main
int main() {
long loops;
int rc,j = 0;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val,val2;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
FILE *fp ;
if( (fp = fopen("sound.wav","r")) < 0)//南拳妈妈 - 你不像她.wav
printf("open sound.wav fial\n");
if(fseek(fp,0,SEEK_SET) < 0)
printf("put fp start to first error\n ");
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default",
SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr, "unable to open pcm device: %s/n",
snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params,
SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 1);
/* 44100 bits/second sampling rate (CD quality) */
val = 8000;
snd_pcm_hw_params_set_rate_near(handle, params,
&val, &dir);
/* Set period size to 32 frames. */
frames = 1024; //设置的值没有反应
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); //
printf("frames is %d\n",(int)frames);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr, "unable to set hw parameters: %s/n", snd_strerror(rc));
exit(1);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames,
&dir);
size = frames * 2; /* 2 bytes/sample, 2 channels */
buffer = (char *) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
/* 5 seconds in microseconds divided by * period time */
loops = 10000000 / val;
while (loops > 0) {
loops--;
rc = fread(buffer,1, size,fp);
//rc = read(0,buffer,size);
//printf("%d\n",j++);
if (rc == 0) {
fprintf(stderr, "end of file on input\n");
break;
} else if (rc != size) {
fprintf(stderr, "short read: read %d bytes\n", rc);
}
//else printf("fread to buffer success\n");
rc = snd_pcm_writei(handle, buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr, "error from writei: %s\n",
snd_strerror(rc));
} else if (rc != (int)frames) {
fprintf(stderr, "short write, write %d frames\n", rc);
}
}
/*******************************************************************/
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
//.........这里部分代码省略.........
示例3: alsa_init
static int alsa_init(const char *param, int *speed,
int *fragsize, int *fragnr, int *channels)
{
int err, dir;
unsigned int rate, periods;
snd_pcm_uframes_t period_size;
snd_pcm_hw_params_t *hwparams;
if (!param) {
param = "default";
}
snd_pcm_hw_params_alloca(&hwparams);
if ((err = snd_pcm_open(&handle, param, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
log_message(LOG_DEFAULT, "Playback open error for '%s': %s", param,
snd_strerror(err));
return 1;
}
if ((err = snd_pcm_hw_params_any(handle, hwparams)) < 0) {
log_message(LOG_DEFAULT, "Broken configuration for playback: no configurations available: %s", snd_strerror(err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
log_message(LOG_DEFAULT, "Access type not available for playback: %s", snd_strerror(err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16)) < 0) {
log_message(LOG_DEFAULT, "Sample format not available for playback: %s", snd_strerror(err));
goto fail;
}
if ((err = snd_pcm_hw_params_set_channels(handle, hwparams, *channels)) < 0) {
log_message(LOG_DEFAULT, "Channels count (%i) not available for playbacks: %s", *channels, snd_strerror(err));
goto fail;
}
rate = (unsigned int)*speed;
if ((err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, 0)) < 0) {
log_message(LOG_DEFAULT, "Rate %iHz not available for playback: %s", *speed, snd_strerror(err));
goto fail;
}
if (rate != (unsigned int)*speed) {
printf("Rate doesn't match (requested %iHz, got %iHz)", *speed, rate);
*speed = rate;
}
period_size = *fragsize;
dir = 0;
if ((err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &dir)) < 0) {
log_message(LOG_DEFAULT, "Unable to set period size %li for playback: %s", period_size, snd_strerror(err));
goto fail;
}
*fragsize = period_size;
periods = *fragnr;
dir = 0;
if ((err = snd_pcm_hw_params_set_periods_near(handle, hwparams, &periods, &dir)) < 0) {
log_message(LOG_DEFAULT, "Unable to set periods %i for playback: %s", periods, snd_strerror(err));
goto fail;
}
*fragnr = periods;
alsa_can_pause = snd_pcm_hw_params_can_pause(hwparams);
if ((err = snd_pcm_hw_params(handle, hwparams)) < 0) {
log_message(LOG_DEFAULT, "Unable to set hw params for playback: %s", snd_strerror(err));
goto fail;
}
alsa_bufsize = (*fragsize)*(*fragnr);
alsa_fragsize = *fragsize;
alsa_channels = *channels;
return 0;
fail:
snd_pcm_close(handle);
handle = NULL;
return 1;
}
示例4: wodOpen
//.........这里部分代码省略.........
err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
if (err < 0) {
WARN("Rate %d Hz not available for playback: %s\n", wwo->format.Format.nSamplesPerSec, snd_strerror(rate));
retcode = WAVERR_BADFORMAT;
goto errexit;
}
if (!ALSA_NearMatch(rate, wwo->format.Format.nSamplesPerSec)) {
WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwo->format.Format.nSamplesPerSec, rate);
retcode = WAVERR_BADFORMAT;
goto errexit;
}
TRACE("Got this format: %dx%dx%d %s\n",
wwo->format.Format.nSamplesPerSec,
wwo->format.Format.wBitsPerSample,
wwo->format.Format.nChannels,
ALSA_getFormat(wwo->format.Format.wFormatTag));
dir=0;
EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
dir=0;
EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
snd_pcm_sw_params_current(pcm, sw_params);
EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set start threshold");
EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
#undef EXIT_ON_ERROR
snd_pcm_prepare(pcm);
if (TRACE_ON(wave))
ALSA_TraceParameters(hw_params, sw_params, FALSE);
/* now, we can save all required data for later use... */
wwo->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
wwo->dwPartialOffset = 0;
ALSA_InitRingMessage(&wwo->msgRing);
wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwo->dwThreadID));
if (wwo->hThread)
SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
else
{
ERR("Thread creation for the wodPlayer failed!\n");
CloseHandle(wwo->hStartUpEvent);
retcode = MMSYSERR_NOMEM;
goto errexit;
}
WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
CloseHandle(wwo->hStartUpEvent);
wwo->hStartUpEvent = INVALID_HANDLE_VALUE;
TRACE("handle=%p\n", pcm);
TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
wwo->format.Format.wBitsPerSample, wwo->format.Format.nAvgBytesPerSec,
wwo->format.Format.nSamplesPerSec, wwo->format.Format.nChannels,
wwo->format.Format.nBlockAlign);
HeapFree( GetProcessHeap(), 0, sw_params );
wwo->pcm = pcm;
wwo->hctl = hctl;
if ( wwo->hw_params )
snd_pcm_hw_params_free(wwo->hw_params);
wwo->hw_params = hw_params;
return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
errexit:
if (pcm)
snd_pcm_close(pcm);
if (hctl)
{
snd_hctl_free(hctl);
snd_hctl_close(hctl);
}
if ( hw_params )
snd_pcm_hw_params_free(hw_params);
HeapFree( GetProcessHeap(), 0, sw_params );
if (wwo->msgRing.ring_buffer_size > 0)
ALSA_DestroyRingMessage(&wwo->msgRing);
return retcode;
}
示例5: playback
int playback(char* inputFileName) {
long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
char *buffer;
int f = 0;
if (strlen(inputFileName)) {
f = open(inputFileName, O_RDONLY);
}
/* Open PCM device for playback. */
rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
if (rc < 0) {
fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(rc));
exit(1);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 bits/second sampling rate (CD quality) */
val = 44100;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
/* Set period size to 32 frames. */
frames = 32;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));
pthread_exit(NULL);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
size = frames * 4; /* 2 bytes/sample, 2 channels */
buffer = (char *) malloc(size);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
/* 5 seconds in microseconds divided by
* period time */
loops = 10 * 30000000 / val;
while (loops > 0) {
loops--;
rc = read(f, buffer, size);
if (rc == 0) {
fprintf(stderr, "end of file on input\n");
break;
} else if (rc != size) {
fprintf(stderr, "short read: read %d bytes\n", rc);
}
rc = snd_pcm_writei(handle, buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
fprintf(stderr,
"error from writei: %s\n",
snd_strerror(rc));
} else if (rc != (int) frames) {
fprintf(stderr, "short write, write %d frames\n", rc);
}
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
if (f)close(f);
return 0;
}
示例6: free_pcm
void free_pcm(void* args) {
free_pcm_args* pargs = (free_pcm_args*) args;
snd_pcm_drain(pargs->handle);
snd_pcm_close(pargs->handle);
free(pargs->buffer);
}
示例7: snd_pcm_close
void QAudioDeviceInfoInternal::close()
{
if(handle)
snd_pcm_close(handle);
handle = 0;
}
示例8: QLatin1String
//.........这里部分代码省略.........
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S16_BE);
} else if(format.sampleType() == QAudioFormat::UnSignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U16_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U16_BE);
}
break;
case 32:
if(format.sampleType() == QAudioFormat::SignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S32_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S32_BE);
} else if(format.sampleType() == QAudioFormat::UnSignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U32_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U32_BE);
}
}
// For now, just accept only audio/pcm codec
if(!format.codec().startsWith(QLatin1String("audio/pcm"))) {
err=-1;
} else
testCodec = true;
if(err>=0 && format.channels() != -1) {
err = snd_pcm_hw_params_test_channels(handle,params,format.channels());
if(err>=0)
err = snd_pcm_hw_params_set_channels(handle,params,format.channels());
if(err>=0)
testChannel = true;
}
if(err>=0 && format.frequency() != -1) {
err = snd_pcm_hw_params_test_rate(handle,params,format.frequency(),0);
if(err>=0)
err = snd_pcm_hw_params_set_rate(handle,params,format.frequency(),dir);
if(err>=0)
testFreq = true;
}
if((err>=0 && format.sampleSize() != -1) &&
(format.sampleType() != QAudioFormat::Unknown)) {
switch(format.sampleSize()) {
case 8:
if(format.sampleType() == QAudioFormat::SignedInt)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S8);
else if(format.sampleType() == QAudioFormat::UnSignedInt)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U8);
break;
case 16:
if(format.sampleType() == QAudioFormat::SignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S16_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S16_BE);
} else if(format.sampleType() == QAudioFormat::UnSignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U16_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U16_BE);
}
break;
case 32:
if(format.sampleType() == QAudioFormat::SignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S32_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_S32_BE);
} else if(format.sampleType() == QAudioFormat::UnSignedInt) {
if(format.byteOrder() == QAudioFormat::LittleEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U32_LE);
else if(format.byteOrder() == QAudioFormat::BigEndian)
err = snd_pcm_hw_params_set_format(handle,params,SND_PCM_FORMAT_U32_BE);
}
}
if(err>=0) {
testSize = true;
testType = true;
}
}
if(err>=0)
err = snd_pcm_hw_params(handle, params);
if(err == 0) {
// settings work
// close()
if(handle)
snd_pcm_close(handle);
return true;
}
if(handle)
snd_pcm_close(handle);
return false;
}
示例9: sa_stream_open
int
sa_stream_open(sa_stream_t *s) {
snd_output_t* out;
char* buf;
size_t bufsz;
snd_pcm_hw_params_t* hwparams;
snd_pcm_sw_params_t* swparams;
int dir;
snd_pcm_uframes_t period;
if (s == NULL) {
return SA_ERROR_NO_INIT;
}
if (s->output_unit != NULL) {
return SA_ERROR_INVALID;
}
pthread_mutex_lock(&sa_alsa_mutex);
/* Turn off debug output to stderr */
snd_lib_error_set_handler(quiet_error_handler);
if (snd_pcm_open(&s->output_unit,
"default",
SND_PCM_STREAM_PLAYBACK,
0) < 0) {
pthread_mutex_unlock(&sa_alsa_mutex);
return SA_ERROR_NO_DEVICE;
}
if (snd_pcm_set_params(s->output_unit,
#ifdef SA_LITTLE_ENDIAN
SND_PCM_FORMAT_S16_LE,
#else
SND_PCM_FORMAT_S16_BE,
#endif
SND_PCM_ACCESS_RW_INTERLEAVED,
s->n_channels,
s->rate,
1,
500000) < 0) {
snd_pcm_close(s->output_unit);
s->output_unit = NULL;
pthread_mutex_unlock(&sa_alsa_mutex);
return SA_ERROR_NOT_SUPPORTED;
}
/* ugly alsa-pulse plugin detection */
snd_output_buffer_open(&out);
snd_pcm_dump(s->output_unit, out);
bufsz = snd_output_buffer_string(out, &buf);
s->pulseaudio = bufsz >= strlen(ALSA_PA_PLUGIN) &&
strncmp(buf, ALSA_PA_PLUGIN, strlen(ALSA_PA_PLUGIN)) == 0;
snd_output_close(out);
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_hw_params_current(s->output_unit, hwparams);
snd_pcm_hw_params_get_period_size(hwparams, &period, &dir);
pthread_mutex_unlock(&sa_alsa_mutex);
return SA_SUCCESS;
}
示例10: snd_pcm_close
~ALSADevice()
{
if (handle != 0)
snd_pcm_close (handle);
}
示例11: alsaspdifsink_find_pcm_device
//.........这里部分代码省略.........
goto beach;
count = snd_ctl_elem_list_get_used (clist);
for (idx = 0; idx < count; idx++) {
if (strstr (snd_ctl_elem_list_get_name (clist, idx), spdif_name) != NULL)
break;
}
if (idx == count) {
/* No SPDIF mixer availble */
err = 0;
goto beach;
}
snd_ctl_elem_id_malloc (&cid);
snd_ctl_elem_list_get_id (clist, idx, cid);
/* Now find a PCM device for IEC 958 */
if ((err = snd_ctl_card_info (ctl, info)) < 0)
goto beach;
dev = -1;
do {
if (snd_ctl_pcm_next_device (ctl, &dev) < 0)
goto beach;
if (dev < 0)
break; /* No more devices */
/* Filter for playback devices */
snd_pcm_info_set_device (pinfo, dev);
snd_pcm_info_set_subdevice (pinfo, 0);
snd_pcm_info_set_stream (pinfo, SND_PCM_STREAM_PLAYBACK);
if ((err = snd_ctl_pcm_info (ctl, pinfo)) < 0) {
if (err != -ENOENT)
goto beach; /* Genuine error */
/* Device has no playback streams */
continue;
}
if (strstr (snd_pcm_info_get_name (pinfo), "IEC958") == NULL)
continue; /* Not the device we are looking for */
count = snd_pcm_info_get_subdevices_count (pinfo);
GST_LOG_OBJECT (sink, "Device %d has %d subdevices\n", dev,
snd_pcm_info_get_subdevices_count (pinfo));
for (idx = 0; idx < count; idx++) {
snd_pcm_info_set_subdevice (pinfo, idx);
if ((err = snd_ctl_pcm_info (ctl, pinfo)) < 0)
goto beach;
g_assert (snd_pcm_info_get_stream (pinfo) == SND_PCM_STREAM_PLAYBACK);
GST_LOG_OBJECT (sink, "Found playback stream on dev %d sub-d %d\n", dev,
idx);
/* Found a suitable PCM device, let's open it */
g_snprintf (pcm_name, 24, "hw:%d,%d", card, dev);
if ((err =
snd_pcm_open (&(pcm), pcm_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
goto beach;
break;
}
} while (pcm == NULL);
if (pcm != NULL) {
snd_ctl_elem_value_t *cval;
snd_aes_iec958_t iec958;
/* Have a PCM device and a mixer, set things up */
snd_ctl_elem_value_malloc (&cval);
snd_ctl_elem_value_set_id (cval, cid);
snd_ctl_elem_value_get_iec958 (cval, &iec958);
iec958.status[0] = IEC958_AES0_NONAUDIO;
iec958.status[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
iec958.status[2] = 0;
iec958.status[3] = IEC958_AES3_CON_FS_48000;
snd_ctl_elem_value_set_iec958 (cval, &iec958);
snd_ctl_elem_value_free (cval);
sink->pcm = pcm;
pcm = NULL;
err = 0;
}
beach:
if (pcm)
snd_pcm_close (pcm);
if (clist)
snd_ctl_elem_list_clear (clist);
if (ctl)
snd_ctl_close (ctl);
if (clist)
snd_ctl_elem_list_free (clist);
if (cid)
snd_ctl_elem_id_free (cid);
if (info)
snd_ctl_card_info_free (info);
if (pinfo)
snd_pcm_info_free (pinfo);
return err;
}
示例12: alsa_open
//.........这里部分代码省略.........
} ;
if ((err = snd_pcm_open (&alsa_dev, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{ fprintf (stderr, "cannot open audio device \"%s\" (%s)\n", device, snd_strerror (err)) ;
goto catch_error ;
} ;
snd_pcm_nonblock (alsa_dev, 0) ;
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
{ fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_any (alsa_dev, hw_params)) < 0)
{ fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_access (alsa_dev, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
{ fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_format (alsa_dev, hw_params, SND_PCM_FORMAT_FLOAT)) < 0)
{ fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_rate_near (alsa_dev, hw_params, &samplerate, 0)) < 0)
{ fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_channels (alsa_dev, hw_params, channels)) < 0)
{ fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_buffer_size_near (alsa_dev, hw_params, &alsa_buffer_frames)) < 0)
{ fprintf (stderr, "cannot set buffer size (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params_set_period_size_near (alsa_dev, hw_params, &alsa_period_size, 0)) < 0)
{ fprintf (stderr, "cannot set period size (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_hw_params (alsa_dev, hw_params)) < 0)
{ fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
/* extra check: if we have only one period, this code won't work */
snd_pcm_hw_params_get_period_size (hw_params, &alsa_period_size, 0) ;
snd_pcm_hw_params_get_buffer_size (hw_params, &buffer_size) ;
if (alsa_period_size == buffer_size)
{ fprintf (stderr, "Can't use period equal to buffer size (%lu == %lu)", alsa_period_size, buffer_size) ;
goto catch_error ;
} ;
snd_pcm_hw_params_free (hw_params) ;
if ((err = snd_pcm_sw_params_malloc (&sw_params)) != 0)
{ fprintf (stderr, "%s: snd_pcm_sw_params_malloc: %s", __func__, snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_sw_params_current (alsa_dev, sw_params)) != 0)
{ fprintf (stderr, "%s: snd_pcm_sw_params_current: %s", __func__, snd_strerror (err)) ;
goto catch_error ;
} ;
/* note: set start threshold to delay start until the ring buffer is full */
snd_pcm_sw_params_current (alsa_dev, sw_params) ;
if ((err = snd_pcm_sw_params_set_start_threshold (alsa_dev, sw_params, buffer_size)) < 0)
{ fprintf (stderr, "cannot set start threshold (%s)\n", snd_strerror (err)) ;
goto catch_error ;
} ;
if ((err = snd_pcm_sw_params (alsa_dev, sw_params)) != 0)
{ fprintf (stderr, "%s: snd_pcm_sw_params: %s", __func__, snd_strerror (err)) ;
goto catch_error ;
} ;
snd_pcm_sw_params_free (sw_params) ;
snd_pcm_reset (alsa_dev) ;
catch_error :
if (err < 0 && alsa_dev != NULL)
{ snd_pcm_close (alsa_dev) ;
return NULL ;
} ;
return alsa_dev ;
} /* alsa_open */
示例13: snd_record
void * snd_record(void* voidargs) {
struct snd_record_args* args = (snd_record_args*) voidargs;
*args->returnObj.state = 0;
long loops;
int rc;
int size;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
uint16_t *buffer;
int* signalNewState = args->signalNewState;
bool continuous_capture = args->duration <= 0 ? true : false;
/* Open PCM device for recording (capture). */
rc = snd_pcm_open(&handle, (const char*) args->dev_name.c_str(), SND_PCM_STREAM_CAPTURE, 0);
pthread_cleanup_push(deallocate_srarg, voidargs);
if (rc < 0) {
fp_err(FPOL_PCM, "unable to open pcm device: %s", snd_strerror(rc));
fp_debug(FPOL_PCM, "dev_name: %s", (char*) args->dev_name.c_str());
args->returnObj.errorcode = 1;
//pthread_cleanup_pop(1);
//return NULL;
pthread_exit(NULL);
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* Set the desired hardware parameters. */
/* Interleaved mode */
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
/* Signed 16-bit little-endian format */
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 samples/second sampling rate (CD quality) */
val = args->samplingFrequency;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
/* Set period size to 32 frames. */
frames = 1152; //32; //
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));
std::cout << "\n" << getTime() << " snd_record: unable to set hw parameters: " << snd_strerror(rc) << "\n";
pthread_exit(NULL);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
size = frames * 4; /* 2 bytes/sample, 2 channels */
buffer = (uint16_t*) malloc(size);
free_pcm_args fpa;
fpa.handle = handle;
fpa.buffer = buffer;
pthread_cleanup_push(&free_pcm, (void*) &fpa);
/* We want to loop for 5 seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
loops = args->duration * 1000000 / val;
*args->returnObj.state = 1;
while (*signalNewState >= 0 && (loops > 0 || continuous_capture)) {
loops--;
rc = snd_pcm_readi(handle, (void**) buffer, frames);
if (rc == -EPIPE) {
/* EPIPE means overrun */
fprintf(stderr, "overrun occurred\n");
snd_pcm_prepare(handle);
} else if (rc < 0) {
args->returnObj.error = std::string("error from read: ") + std::string(snd_strerror(rc));
std::cout << "\n" << getTime() << " snd_record(): error from read: " << snd_strerror(rc) << "\n";
break;
} else if (rc != (int) frames) {
fprintf(stderr, "short read, read %d frames\n", rc);
}
(*(args->periodbuffer))[*args->periodbufferfloat].initferryperiod(size, 2);
memcpy((*args->periodbuffer)[*args->periodbufferfloat].period, buffer, (*args->periodbuffer)[*args->periodbufferfloat].length);
(*args->periodbufferfloat)++;
(*args->periodbufferfloat) %= *args->periodbufferlength;
// if (rc != size) {
// fprintf(stderr, "short write: wrote %d bytes\n", rc);
// }
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
pthread_cleanup_pop(0);
*args->returnObj.state = -1;
pthread_cleanup_pop(0);
return NULL;
}
示例14: printf
//.........这里部分代码省略.........
if (snd_pcm_sw_params_set_stop_threshold(pcm_handle, swparams, 0xffffffff) != 0) {
fprintf(stderr, "Error in snd_pcm_sw_params_set_stop_threshold. : %s\n", snd_strerror(err));
return -1;
}
if (snd_pcm_sw_params(pcm_handle, swparams) != 0) {
fprintf(stderr, "Error in snd_pcm_sw_params. : %s\n", snd_strerror(err));
return -1;
}
if ((err = snd_pcm_prepare(pcm_handle)) < 0) {
fprintf(stderr, "Error snd_pcm_prepare : %s\n", snd_strerror(err));
return -1;
}
// allocate the audio output buffer
pOutputBuffer = new int16_t[Channels * FragmentSize];
this->bInitialized = true;
return 0;
}
/**
* Checks if sound card supports the chosen parameters.
*
* @returns true if hardware supports it
*/
bool AlsaIO::HardwareParametersSupported(uint channels, int samplerate, uint numfragments, uint fragmentsize) {
pcm_name = "hw:0,0";
if (snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0) < 0) return false;
snd_pcm_hw_params_alloca(&hwparams);
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
snd_pcm_close(pcm_handle);
return false;
}
if (snd_pcm_hw_params_test_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
snd_pcm_close(pcm_handle);
return false;
}
#if WORDS_BIGENDIAN
if (snd_pcm_hw_params_test_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_BE) < 0) {
#else // little endian
if (snd_pcm_hw_params_test_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE) < 0) {
#endif
snd_pcm_close(pcm_handle);
return false;
}
int dir = 0;
if (snd_pcm_hw_params_test_rate(pcm_handle, hwparams, samplerate, dir) < 0) {
snd_pcm_close(pcm_handle);
return false;
}
if (snd_pcm_hw_params_test_channels(pcm_handle, hwparams, channels) < 0) {
snd_pcm_close(pcm_handle);
return false;
}
if (snd_pcm_hw_params_test_periods(pcm_handle, hwparams, numfragments, dir) < 0) {
snd_pcm_close(pcm_handle);
return false;
}
if (snd_pcm_hw_params_test_buffer_size(pcm_handle, hwparams, (fragmentsize * numfragments)) < 0) {
snd_pcm_close(pcm_handle);
return false;
}
示例15: pcm_open_set_device
//.........这里部分代码省略.........
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
/* Two channels (stereo) */
snd_pcm_hw_params_set_channels(handle, params, 2);
/* 44100 bits/second sampling rate (CD quality) */
val = 44100;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
/* Write the parameters to the driver */
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
fprintf(stderr, "unable to set hw parameters: %s\n", snd_strerror(rc));
exit(1);
}
/* Display information about the PCM interface */
printf("PCM handle name = '%s'\n", snd_pcm_name(handle));
printf("PCM state = %s\n", snd_pcm_state_name(snd_pcm_state(handle)));
snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *) & val);
printf("access type = %s\n", snd_pcm_access_name((snd_pcm_access_t) val));
snd_pcm_hw_params_get_format(params, (snd_pcm_format_t*) & val);
printf("format = '%s' (%s)\n", snd_pcm_format_name((snd_pcm_format_t) val), snd_pcm_format_description((snd_pcm_format_t) val));
snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *) & val);
printf("subformat = '%s' (%s)\n", snd_pcm_subformat_name((snd_pcm_subformat_t) val), snd_pcm_subformat_description((snd_pcm_subformat_t) val));
snd_pcm_hw_params_get_channels(params, &val);
printf("channels = %d\n", val);
snd_pcm_hw_params_get_rate(params, &val, &dir);
printf("rate = %d sps\n", val);
snd_pcm_hw_params_get_period_time(params,
&val, &dir);
printf("period time = %d us\n", val);
snd_pcm_hw_params_get_period_size(params,
&frames, &dir);
printf("period size = %d frames\n", (int) frames);
snd_pcm_hw_params_get_buffer_time(params,
&val, &dir);
printf("buffer time = %d us\n", val);
snd_pcm_hw_params_get_buffer_size(params,
(snd_pcm_uframes_t *) & val);
printf("buffer size = %d frames\n", val);
snd_pcm_hw_params_get_periods(params, &val, &dir);
printf("periods per buffer = %d frames\n", val);
snd_pcm_hw_params_get_rate_numden(params,
&val, &val2);
printf("exact rate = %d/%d bps\n", val, val2);
val = snd_pcm_hw_params_get_sbits(params);
printf("significant bits = %d\n", val);
snd_pcm_hw_params_get_tick_time(params, &val, &dir);
printf("tick time = %d us\n", val);
val = snd_pcm_hw_params_is_batch(params);
printf("is batch = %d\n", val);
val = snd_pcm_hw_params_is_block_transfer(params);
printf("is block transfer = %d\n", val);
val = snd_pcm_hw_params_is_double(params);
printf("is double = %d\n", val);
val = snd_pcm_hw_params_is_half_duplex(params);
printf("is half duplex = %d\n", val);
val = snd_pcm_hw_params_is_joint_duplex(params);
printf("is joint duplex = %d\n", val);
val = snd_pcm_hw_params_can_overrange(params);
printf("can overrange = %d\n", val);
val = snd_pcm_hw_params_can_mmap_sample_resolution(params);
printf("can mmap = %d\n", val);
val = snd_pcm_hw_params_can_pause(params);
printf("can pause = %d\n", val);
val = snd_pcm_hw_params_can_resume(params);
printf("can resume = %d\n", val);
val = snd_pcm_hw_params_can_sync_start(params);
printf("can sync start = %d\n", val);
snd_pcm_close(handle);
return 0;
}