本文整理汇总了C++中snd_pcm_hw_params_alloca函数的典型用法代码示例。如果您正苦于以下问题:C++ snd_pcm_hw_params_alloca函数的具体用法?C++ snd_pcm_hw_params_alloca怎么用?C++ snd_pcm_hw_params_alloca使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了snd_pcm_hw_params_alloca函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sizeof
// Initialize the BlockSound class
BlockSound::BlockSound() {
sample_size = 0;
#ifdef __APPLE__
remaining = 0;
UInt32 size = sizeof(device);
if (AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
&size, (void *)&device) != noErr) return;
size = sizeof(format);
if (AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyStreamFormat,
&size, &format) != noErr) return;
// Set up a format we like...
format.mSampleRate = 44100.0; // 44.1kHz
format.mChannelsPerFrame = 2; // stereo
if (AudioDeviceSetProperty(device, NULL, 0, false,
kAudioDevicePropertyStreamFormat,
sizeof(format), &format) != noErr) return;
// Check we got linear pcm - what to do if we did not ???
if (format.mFormatID != kAudioFormatLinearPCM) return;
// Attach the callback and start the device
# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
if (AudioDeviceCreateIOProcID(device, audio_cb, (void *)this, &audio_proc_id) != noErr) return;
AudioDeviceStart(device, audio_proc_id);
# else
if (AudioDeviceAddIOProc(device, audio_cb, (void *)this) != noErr) return;
AudioDeviceStart(device, audio_cb);
# endif
sample_size = (int)format.mSampleRate;
#elif defined(WIN32)
WAVEFORMATEX format;
memset(&format, 0, sizeof(format));
format.cbSize = sizeof(format);
format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = 2;
format.nSamplesPerSec = 44100;
format.nAvgBytesPerSec = 44100 * 4;
format.nBlockAlign = 4;
format.wBitsPerSample = 16;
data_handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, format.nSamplesPerSec * 4);
if (!data_handle) return;
data_ptr = (LPSTR)GlobalLock(data_handle);
header_handle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof(WAVEHDR));
if (!header_handle) return;
header_ptr = (WAVEHDR *)GlobalLock(header_handle);
header_ptr->lpData = data_ptr;
header_ptr->dwFlags = 0;
header_ptr->dwLoops = 0;
if (waveOutOpen(&device, WAVE_MAPPER, &format, 0, 0, WAVE_ALLOWSYNC)
!= MMSYSERR_NOERROR) return;
sample_size = format.nSamplesPerSec;
#else
# ifdef HAVE_ALSA_ASOUNDLIB_H
handle = NULL;
if (snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0) >= 0) {
// Initialize PCM sound stuff...
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16);
snd_pcm_hw_params_set_channels(handle, params, 2);
unsigned rate = 44100;
int dir;
snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir);
snd_pcm_uframes_t period = (int)rate;
snd_pcm_hw_params_set_period_size_near(handle, params, &period, &dir);
sample_size = rate;
if (snd_pcm_hw_params(handle, params) < 0) {
sample_size = 0;
snd_pcm_close(handle);
handle = NULL;
}
}
# endif // HAVE_ALSA_ASOUNDLIB_H
#endif // __APPLE__
if (sample_size) {
//.........这里部分代码省略.........
示例2: dbug_out
int Aisound::Open_Pcm()
{
BEGINFUNC_USING_BY_VOICE
int err;
int dir;
int size;
char *buffer;
unsigned int val;
snd_pcm_uframes_t frames;
snd_pcm_hw_params_t* params;
int channels = 0;
if ( (err = snd_pcm_open(&handle, "plug:cmd", SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
dbug_out("cannot open audio device (%s)\n", snd_strerror (err));
goto OpenPcmFail;
}
dbug_out("After snd_pcm_open for SND_PCM_STREAM_PLAYBACK");
snd_pcm_hw_params_alloca(¶ms);//为å�‚æ•°å�˜é‡�分é…�空间
if( (err = snd_pcm_hw_params_any(handle, params)) < 0)//�数�始化
{
dbug_out("snd_pcm_hw_params_any failed! err = %d\n", err);
goto OpenPcmFail;
}
if( (err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)//设置为交错模�
{
dbug_out("snd_pcm_hw_params_set_access failed! err = %d\n", err);
goto OpenPcmFail;
}
if( (err = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE)) < 0)//使用16ä½�æ ·æœ¬
{
dbug_out("snd_pcm_hw_params_set_format failed! err = %d\n", err);
goto OpenPcmFail;
}
channels = val = 1;
if( (err = snd_pcm_hw_params_set_channels(handle, params, val)) < 0)//设置为立体声
{
dbug_out("snd_pcm_hw_params_set_channels failed! err = %d\n", err);
goto OpenPcmFail;
}
val = 16000;
if( (err = snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir)) < 0)//è®¾ç½®é‡‡æ ·çŽ‡ä¸º44.1KHz
{
dbug_out("snd_pcm_hw_params_set_rate_near failed! err = %d\n", err);
goto OpenPcmFail;
}
#if 1
frames = 32;
if( (err = snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir)) < 0)
{
dbug_out("snd_pcm_hw_params_set_period_size_near failed! err = %d\n", err);
goto OpenPcmFail;
}
if ( (err = snd_pcm_hw_params(handle, params)) < 0)
{
dbug_out("snd_pcm_hw_params-failed! err = %d err: %s\n", err, snd_strerror(err));
goto OpenPcmFail;
}
if( (err = snd_pcm_hw_params_get_period_size(params, &frames, &dir)) < 0)
{
dbug_out("snd_pcm_hw_params_get_period_size failed! err = %d\n", err);
goto OpenPcmFail;
}
size = frames * channels * 2;
buffer = (char *)malloc(sizeof(char) * size);
if( (err = snd_pcm_hw_params_get_period_time(params, &val, &dir)) < 0)
{
dbug_out("snd_pcm_hw_params_get_period_time failed! err = %d\n", err);
goto OpenPcmFail;
}
return 0;
OpenPcmFail:
return -1;
#endif
ENDFUNC_USING_BY_VOICE
}
示例3: snd_pcm_hw_params_alloca
bool CAESinkALSA::InitializeHW(AEAudioFormat &format)
{
snd_pcm_hw_params_t *hw_params;
snd_pcm_hw_params_alloca(&hw_params);
memset(hw_params, 0, snd_pcm_hw_params_sizeof());
snd_pcm_hw_params_any(m_pcm, hw_params);
snd_pcm_hw_params_set_access(m_pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
unsigned int sampleRate = format.m_sampleRate;
#if defined(HAS_AMLPLAYER) || defined(HAS_LIBAMCODEC)
// alsa/kernel lies, so map everything to 44100 or 48000
switch(sampleRate)
{
case 11025:
case 22050:
case 88200:
case 176400:
sampleRate = 44100;
break;
case 8000:
case 16000:
case 24000:
case 32000:
case 96000:
case 192000:
case 384000:
sampleRate = 48000;
break;
}
#endif
unsigned int channelCount = format.m_channelLayout.Count();
snd_pcm_hw_params_set_rate_near (m_pcm, hw_params, &sampleRate, NULL);
snd_pcm_hw_params_set_channels_near(m_pcm, hw_params, &channelCount);
/* ensure we opened X channels or more */
if (format.m_channelLayout.Count() > channelCount)
{
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Unable to open the required number of channels");
}
/* update the channelLayout to what we managed to open */
format.m_channelLayout.Reset();
for (unsigned int i = 0; i < channelCount; ++i)
format.m_channelLayout += ALSAChannelMap[i];
snd_pcm_format_t fmt = AEFormatToALSAFormat(format.m_dataFormat);
if (fmt == SND_PCM_FORMAT_UNKNOWN)
{
/* if we dont support the requested format, fallback to float */
format.m_dataFormat = AE_FMT_FLOAT;
fmt = SND_PCM_FORMAT_FLOAT;
}
/* try the data format */
if (snd_pcm_hw_params_set_format(m_pcm, hw_params, fmt) < 0)
{
/* if the chosen format is not supported, try each one in decending order */
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Your hardware does not support %s, trying other formats", CAEUtil::DataFormatToStr(format.m_dataFormat));
for (enum AEDataFormat i = AE_FMT_MAX; i > AE_FMT_INVALID; i = (enum AEDataFormat)((int)i - 1))
{
if (AE_IS_RAW(i) || i == AE_FMT_MAX)
continue;
if (m_passthrough && i != AE_FMT_S16BE && i != AE_FMT_S16LE)
continue;
fmt = AEFormatToALSAFormat(i);
if (fmt == SND_PCM_FORMAT_UNKNOWN || snd_pcm_hw_params_set_format(m_pcm, hw_params, fmt) < 0)
{
fmt = SND_PCM_FORMAT_UNKNOWN;
continue;
}
int fmtBits = CAEUtil::DataFormatToBits(i);
int bits = snd_pcm_hw_params_get_sbits(hw_params);
if (bits != fmtBits)
{
/* if we opened in 32bit and only have 24bits, pack into 24 */
if (fmtBits == 32 && bits == 24)
i = AE_FMT_S24NE4;
else
continue;
}
/* record that the format fell back to X */
format.m_dataFormat = i;
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Using data format %s", CAEUtil::DataFormatToStr(format.m_dataFormat));
break;
}
/* if we failed to find a valid output format */
if (fmt == SND_PCM_FORMAT_UNKNOWN)
{
CLog::Log(LOGERROR, "CAESinkALSA::InitializeHW - Unable to find a suitable output format");
return false;
}
}
//.........这里部分代码省略.........
示例4: main
int main()
{
int rc;
snd_pcm_t* handle;
snd_pcm_hw_params_t* params;
unsigned int val;
unsigned int val2;
int dir;
snd_pcm_uframes_t frames;
if ( (rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0)
{
std::cerr << "unable to open pcm devices: " << snd_strerror(rc) << std::endl;
exit(1);
}
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(handle, params);
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(handle, params, 2);
val = 44100;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir);
if ( (rc = snd_pcm_hw_params(handle, params)) < 0)
{
std::cerr << "unable to set hw parameters: " << snd_strerror(rc) << std::endl;
exit(1);
}
std::cout << "PCM handle name = " << snd_pcm_name(handle) << std::endl;
std::cout << "PCM state = " << snd_pcm_state_name(snd_pcm_state(handle)) << std::endl;
snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *)&val);
std::cout << "access type = " << snd_pcm_access_name((snd_pcm_access_t)val) << std::endl;
snd_pcm_hw_params_get_format(params, (snd_pcm_format_t*)(&val));
std::cout << "format = '" << snd_pcm_format_name((snd_pcm_format_t)val) << "' (" << snd_pcm_format_description((snd_pcm_format_t)val) << ")" << std::endl;
snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val);
std::cout << "subformat = '" <<
snd_pcm_subformat_name((snd_pcm_subformat_t)val) << "' (" << snd_pcm_subformat_description((snd_pcm_subformat_t)val) << ")" << std::endl;
snd_pcm_hw_params_get_channels(params, &val);
std::cout << "channels = " << val << std::endl;
snd_pcm_hw_params_get_rate(params, &val, &dir);
std::cout << "rate = " << val << " bps" << std::endl;
snd_pcm_hw_params_get_period_time(params, &val, &dir);
std::cout << "period time = " << val << " us" << std::endl;
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
std::cout << "period size = " << static_cast<int>(frames) << " frames" << std::endl;
snd_pcm_hw_params_get_buffer_time(params, &val, &dir);
std::cout << "buffer time = " << val << " us" << std::endl;
snd_pcm_hw_params_get_buffer_size(params, (snd_pcm_uframes_t *) &val);
std::cout << "buffer size = " << val << " frames" << std::endl;
snd_pcm_hw_params_get_periods(params, &val, &dir);
std::cout << "periods per buffer = " << val << " frames" << std::endl;
snd_pcm_hw_params_get_rate_numden(params, &val, &val2);
std::cout << "exact rate = " << val/val2 << " bps" << std::endl;
val = snd_pcm_hw_params_get_sbits(params);
std::cout << "significant bits = " << val << std::endl;
snd_pcm_hw_params_get_tick_time(params, &val, &dir);
std::cout << "tick time = " << val << " us" << std::endl;
val = snd_pcm_hw_params_is_batch(params);
std::cout << "is batch = " << val << std::endl;
val = snd_pcm_hw_params_is_block_transfer(params);
std::cout << "is block transfer = " << val << std::endl;
val = snd_pcm_hw_params_is_double(params);
std::cout << "is double = " << val << std::endl;
val = snd_pcm_hw_params_is_half_duplex(params);
std::cout << "is half duplex = " << val << std::endl;
val = snd_pcm_hw_params_is_joint_duplex(params);
std::cout << "is joint duplex = " << val << std::endl;
val = snd_pcm_hw_params_can_overrange(params);
std::cout << "can overrange = " << val << std::endl;
//.........这里部分代码省略.........
示例5: getParam
bool ALSAWriter::processParams(bool *paramsCorrected)
{
const unsigned chn = getParam("chn").toUInt();
const unsigned rate = getParam("rate").toUInt();
const bool resetAudio = channels != chn || sample_rate != rate;
channels = chn;
sample_rate = rate;
if (resetAudio || err)
{
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(¶ms);
close();
QString chosenDevName = devName;
if (autoFindMultichannelDevice && channels > 2)
{
bool mustAutoFind = true, forceStereo = false;
if (!snd_pcm_open(&snd, chosenDevName.toLocal8Bit(), SND_PCM_STREAM_PLAYBACK, 0))
{
if (snd_pcm_type(snd) == SND_PCM_TYPE_HW)
{
unsigned max_chn = 0;
snd_pcm_hw_params_any(snd, params);
mustAutoFind = snd_pcm_hw_params_get_channels_max(params, &max_chn) || max_chn < channels;
}
#ifdef HAVE_CHMAP
else if (paramsCorrected)
{
snd_pcm_chmap_query_t **chmaps = snd_pcm_query_chmaps(snd);
if (chmaps)
snd_pcm_free_chmaps(chmaps);
else
forceStereo = true;
}
#endif
snd_pcm_close(snd);
snd = nullptr;
}
if (mustAutoFind)
{
QString newDevName;
if (channels <= 4)
newDevName = "surround40";
else if (channels <= 6)
newDevName = "surround51";
else
newDevName = "surround71";
if (!newDevName.isEmpty() && newDevName != chosenDevName)
{
if (ALSACommon::getDevices().first.contains(newDevName))
chosenDevName = newDevName;
else if (forceStereo)
{
channels = 2;
*paramsCorrected = true;
}
}
}
}
if (!chosenDevName.isEmpty())
{
bool sndOpen = !snd_pcm_open(&snd, chosenDevName.toLocal8Bit(), SND_PCM_STREAM_PLAYBACK, 0);
if (devName != chosenDevName)
{
if (sndOpen)
QMPlay2Core.logInfo("ALSA :: " + devName + "\" -> \"" + chosenDevName + "\"");
else
{
sndOpen = !snd_pcm_open(&snd, devName.toLocal8Bit(), SND_PCM_STREAM_PLAYBACK, 0);
QMPlay2Core.logInfo("ALSA :: " + tr("Cannot open") + " \"" + chosenDevName + "\", " + tr("back to") + " \"" + devName + "\"");
}
}
if (sndOpen)
{
snd_pcm_hw_params_any(snd, params);
snd_pcm_format_t fmt = SND_PCM_FORMAT_UNKNOWN;
if (!snd_pcm_hw_params_test_format(snd, params, SND_PCM_FORMAT_S32))
{
fmt = SND_PCM_FORMAT_S32;
sample_size = 4;
}
else if (!snd_pcm_hw_params_test_format(snd, params, SND_PCM_FORMAT_S16))
{
fmt = SND_PCM_FORMAT_S16;
sample_size = 2;
}
else if (!snd_pcm_hw_params_test_format(snd, params, SND_PCM_FORMAT_S8))
{
fmt = SND_PCM_FORMAT_S8;
sample_size = 1;
}
unsigned delay_us = round(delay * 1000000.0);
if (fmt != SND_PCM_FORMAT_UNKNOWN && set_snd_pcm_hw_params(snd, params, fmt, channels, sample_rate, delay_us))
{
bool err2 = false;
if (channels != chn || sample_rate != rate)
{
//.........这里部分代码省略.........
示例6: play
void play(int sec, int freq, char *file)
{
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 fd;
/* Check ranges */
if (sec < 1 || sec > 4000) {
printf("WARNING: Incorrect time to play range [1,4000] s\n");
printf("\tSetting time to play to 5s...\n");
sec = 5;
}
if (freq < 1000 || freq > 100000) {
printf("ERROR: Incorrect frequency range [1000,100000] Hz\n");
printf("\tSetting frequency to 44.1 kHz...\n");
freq = 44100;
}
/* Open file */
fd = open(file, O_RDONLY);
if (fd < 0) {
/* There was an error opening the file */
printf("ERROR: Couldn't open file to play\n");
printf("\tPlease make sure file exists\n");
} else {
/* Print that the file is playing with its parameters */
printf("Playing file %s for %d seconds", file, sec);
printf(" and frequency %d...\n", freq);
/* 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 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);
/* freq bits/second sampling rate (CD quality) */
val = freq;
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));
exit(1);
}
/* 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 sec seconds */
snd_pcm_hw_params_get_period_time(params, &val, &dir);
/* sec seconds in microseconds divided by period time */
loops = sec*1000000 / val;
while (loops > 0) {
loops--;
rc = read(fd, 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) {
//.........这里部分代码省略.........
示例7: GLOBAL_DEF
Error AudioDriverALSA::init() {
active=false;
thread_exited=false;
exit_thread=false;
pcm_open = false;
samples_in = NULL;
samples_out = NULL;
mix_rate = GLOBAL_DEF("audio/mix_rate",44100);
output_format = OUTPUT_STEREO;
channels = 2;
int status;
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
#define CHECK_FAIL(m_cond)\
if (m_cond) {\
fprintf(stderr,"ALSA ERR: %s\n",snd_strerror(status));\
snd_pcm_close(pcm_handle);\
ERR_FAIL_COND_V(m_cond,ERR_CANT_OPEN);\
}
//todo, add
//6 chans - "plug:surround51"
//4 chans - "plug:surround40";
status = snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
ERR_FAIL_COND_V( status<0, ERR_CANT_OPEN );
snd_pcm_hw_params_alloca(&hwparams);
status = snd_pcm_hw_params_any(pcm_handle, hwparams);
CHECK_FAIL( status<0 );
status = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
CHECK_FAIL( status<0 );
//not interested in anything else
status = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE);
CHECK_FAIL( status<0 );
//todo: support 4 and 6
status = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, 2);
CHECK_FAIL( status<0 );
status = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &mix_rate, NULL);
CHECK_FAIL( status<0 );
int latency = GLOBAL_DEF("audio/output_latency",25);
buffer_size = nearest_power_of_2( latency * mix_rate / 1000 );
// set buffer size from project settings
status = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size);
CHECK_FAIL( status<0 );
// make period size 1/8
period_size = buffer_size >> 3;
status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, NULL);
CHECK_FAIL( status<0 );
unsigned int periods=2;
status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, NULL);
CHECK_FAIL( status<0 );
status = snd_pcm_hw_params(pcm_handle,hwparams);
CHECK_FAIL( status<0 );
//snd_pcm_hw_params_free(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
status = snd_pcm_sw_params_current(pcm_handle, swparams);
CHECK_FAIL( status<0 );
status = snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, period_size);
CHECK_FAIL( status<0 );
status = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
CHECK_FAIL( status<0 );
status = snd_pcm_sw_params(pcm_handle, swparams);
CHECK_FAIL( status<0 );
samples_in = memnew_arr(int32_t, period_size*channels);
samples_out = memnew_arr(int16_t, period_size*channels);
snd_pcm_nonblock(pcm_handle, 0);
mutex=Mutex::create();
thread = Thread::create(AudioDriverALSA::thread_func, this);
return OK;
};
示例8: init_audio_device
static int init_audio_device(common_data_t *p_common_data)
{
int ret;
int dir = 0;
unsigned int val;
snd_pcm_hw_params_t *p_params;
snd_pcm_uframes_t buffer_size;
unsigned int buffer_time;
unsigned int period_time;
snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
size_t bits_per_sample;
size_t bits_per_frame;
ret = snd_pcm_open(&p_common_data->handle, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0);
if (ret < 0) {
dbg_alsa("unable to open pcm device: %s\n", snd_strerror(ret));
return -1;
}
snd_pcm_hw_params_alloca(&p_params);
snd_pcm_hw_params_any(p_common_data->handle, p_params);
snd_pcm_hw_params_set_access(p_common_data->handle, p_params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(p_common_data->handle, p_params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(p_common_data->handle, p_params, 2);
val = 44100;
snd_pcm_hw_params_set_rate_near(p_common_data->handle, p_params, &val, &dir);
snd_pcm_hw_params_get_buffer_time_max(p_params, &buffer_time, 0);
// if (buffer_time > 500000)
// buffer_time = 500000;
//
// period_time = buffer_time / 4;
//
// snd_pcm_hw_params_set_period_time_near(p_common_data->handle, p_params, &period_time, 0);
// snd_pcm_hw_params_set_buffer_time_near(p_common_data->handle, p_params, &buffer_time, 0);
p_common_data->period_size = 2048;
snd_pcm_hw_params_set_period_size_near(p_common_data->handle, p_params, &p_common_data->period_size, &dir);
ret = snd_pcm_hw_params(p_common_data->handle, p_params);
if (ret < 0){
dbg_alsa("unable to set hw parameters: %s\n", snd_strerror(ret));
exit(1);
}
ret = snd_pcm_state(p_common_data->handle);
dbg("state is %d\n", ret);
snd_pcm_hw_params_get_period_size(p_params, &p_common_data->period_size, &dir);
snd_pcm_hw_params_get_buffer_size(p_params, &buffer_size);
if (p_common_data->period_size == buffer_size) {
dbg_alsa("Can't use period size equal to buffer size (%lu == %lu)\n", p_common_data->period_size, buffer_size);
}
dbg("period size is : %lu frames\n", p_common_data->period_size);
dbg("buffer size is : %lu frames\n", buffer_size);
bits_per_sample = snd_pcm_format_physical_width(format);
bits_per_frame = bits_per_sample * 2;
p_common_data->chunk_bytes = p_common_data->period_size * bits_per_frame / 8;
//g_buf = (msg_t *)malloc(sizeof(msg_t)+(char)p_common_data->chunk_bytes);
dbg("sample rate is %d\n", val);
dbg("bits_per_sample %d\n", bits_per_sample);
dbg("bits_per_frame %d\n", bits_per_frame);
dbg("chunk_bytes %d\n", p_common_data->chunk_bytes);
dbg("PCM handle name = '%s'\n",snd_pcm_name(p_common_data->handle));
return 0;
}
示例9: snd_pcm_hw_params_alloca
bool CAESinkALSA::InitializeHW(AEAudioFormat &format)
{
snd_pcm_hw_params_t *hw_params;
snd_pcm_hw_params_alloca(&hw_params);
memset(hw_params, 0, snd_pcm_hw_params_sizeof());
snd_pcm_hw_params_any(m_pcm, hw_params);
snd_pcm_hw_params_set_access(m_pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
unsigned int sampleRate = format.m_sampleRate;
unsigned int channelCount = format.m_channelLayout.Count();
snd_pcm_hw_params_set_rate_near (m_pcm, hw_params, &sampleRate, NULL);
snd_pcm_hw_params_set_channels_near(m_pcm, hw_params, &channelCount);
/* ensure we opened X channels or more */
if (format.m_channelLayout.Count() > channelCount)
{
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Unable to open the required number of channels");
}
/* update the channelLayout to what we managed to open */
format.m_channelLayout.Reset();
for (unsigned int i = 0; i < channelCount; ++i)
format.m_channelLayout += ALSAChannelMap[i];
snd_pcm_format_t fmt = AEFormatToALSAFormat(format.m_dataFormat);
if (fmt == SND_PCM_FORMAT_UNKNOWN)
{
/* if we dont support the requested format, fallback to float */
format.m_dataFormat = AE_FMT_FLOAT;
fmt = SND_PCM_FORMAT_FLOAT;
}
/* try the data format */
if (snd_pcm_hw_params_set_format(m_pcm, hw_params, fmt) < 0)
{
/* if the chosen format is not supported, try each one in decending order */
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Your hardware does not support %s, trying other formats", CAEUtil::DataFormatToStr(format.m_dataFormat));
for (enum AEDataFormat i = AE_FMT_MAX; i > AE_FMT_INVALID; i = (enum AEDataFormat)((int)i - 1))
{
if (AE_IS_RAW(i) || i == AE_FMT_MAX)
continue;
if (m_passthrough && i != AE_FMT_S16BE && i != AE_FMT_S16LE)
continue;
fmt = AEFormatToALSAFormat(i);
if (fmt == SND_PCM_FORMAT_UNKNOWN || snd_pcm_hw_params_set_format(m_pcm, hw_params, fmt) < 0)
{
fmt = SND_PCM_FORMAT_UNKNOWN;
continue;
}
int fmtBits = CAEUtil::DataFormatToBits(i);
int bits = snd_pcm_hw_params_get_sbits(hw_params);
if (bits != fmtBits)
{
/* if we opened in 32bit and only have 24bits, pack into 24 */
if (fmtBits == 32 && bits == 24)
i = AE_FMT_S24NE4;
else
continue;
}
/* record that the format fell back to X */
format.m_dataFormat = i;
CLog::Log(LOGINFO, "CAESinkALSA::InitializeHW - Using data format %s", CAEUtil::DataFormatToStr(format.m_dataFormat));
break;
}
/* if we failed to find a valid output format */
if (fmt == SND_PCM_FORMAT_UNKNOWN)
{
CLog::Log(LOGERROR, "CAESinkALSA::InitializeHW - Unable to find a suitable output format");
return false;
}
}
unsigned int periods;
snd_pcm_uframes_t periodSize, bufferSize;
snd_pcm_hw_params_get_buffer_size_max(hw_params, &bufferSize);
bufferSize = std::min(bufferSize, (snd_pcm_uframes_t)8192);
periodSize = bufferSize / ALSA_PERIODS;
periods = ALSA_PERIODS;
CLog::Log(LOGDEBUG, "CAESinkALSA::InitializeHW - Request: periodSize %lu, periods %u, bufferSize %lu", periodSize, periods, bufferSize);
/* work on a copy of the hw params */
snd_pcm_hw_params_t *hw_params_copy;
snd_pcm_hw_params_alloca(&hw_params_copy);
/* try to set the buffer size then the period size */
snd_pcm_hw_params_copy(hw_params_copy, hw_params);
snd_pcm_hw_params_set_buffer_size_near(m_pcm, hw_params_copy, &bufferSize);
snd_pcm_hw_params_set_period_size_near(m_pcm, hw_params_copy, &periodSize, NULL);
snd_pcm_hw_params_set_periods_near (m_pcm, hw_params_copy, &periods , NULL);
//.........这里部分代码省略.........
示例10: digi_init
/* Initialise audio devices. */
int digi_init()
{
int err, tmp;
char *device = "plughw:0,0";
snd_pcm_hw_params_t *params;
pthread_attr_t attr;
pthread_mutexattr_t mutexattr;
//added on 980905 by adb to init sound kill system
memset(SampleHandles, 255, sizeof(SampleHandles));
//end edit by adb
/* Open the ALSA sound device */
if ((err = snd_pcm_open(&snd_devhandle,device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
con_printf(CON_CRITICAL, "open failed: %s\n", snd_strerror( err ));
return -1;
}
snd_pcm_hw_params_alloca(¶ms);
err = snd_pcm_hw_params_any(snd_devhandle, params);
if (err < 0) {
con_printf(CON_CRITICAL,"ALSA: Error %s\n", snd_strerror(err));
return -1;
}
err = snd_pcm_hw_params_set_access(snd_devhandle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
con_printf(CON_CRITICAL,"ALSA: Error %s\n", snd_strerror(err));
return -1;
}
err = snd_pcm_hw_params_set_format(snd_devhandle, params, SND_PCM_FORMAT_U8);
if (err < 0) {
con_printf(CON_CRITICAL,"ALSA: Error %s\n", snd_strerror(err));
return -1;
}
err = snd_pcm_hw_params_set_channels(snd_devhandle, params, 2);
if (err < 0) {
con_printf(CON_CRITICAL,"ALSA: Error %s\n", snd_strerror(err));
return -1;
}
tmp = 11025;
err = snd_pcm_hw_params_set_rate_near(snd_devhandle, params, &tmp, NULL);
if (err < 0) {
con_printf(CON_CRITICAL,"ALSA: Error %s\n", snd_strerror(err));
return -1;
}
snd_pcm_hw_params_set_periods(snd_devhandle, params, 3, 0);
snd_pcm_hw_params_set_buffer_size(snd_devhandle,params, (SOUND_BUFFER_SIZE*3)/2);
err = snd_pcm_hw_params(snd_devhandle, params);
if (err < 0) {
con_printf(CON_CRITICAL,"ALSA: Error %s\n", snd_strerror(err));
return -1;
}
/* Start the mixer thread */
/* We really should check the results of these */
pthread_mutexattr_init(&mutexattr);
pthread_mutex_init(&mutex,&mutexattr);
pthread_mutexattr_destroy(&mutexattr);
if (pthread_attr_init(&attr) != 0) {
con_printf(CON_CRITICAL, "failed to init attr\n");
snd_pcm_close( snd_devhandle );
return -1;
}
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread_id,&attr,mixer_thread,NULL);
pthread_attr_destroy(&attr);
digi_initialised = 1;
return 0;
}
示例11: ags_devout_alsa_init
void
ags_devout_alsa_init(AgsDevout *devout,
GError **error)
{
static unsigned int period_time = 100000;
static snd_pcm_format_t format = SND_PCM_FORMAT_S16;
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *hwparams;
unsigned int val;
snd_pcm_uframes_t frames;
unsigned int rate;
unsigned int rrate;
unsigned int channels;
snd_pcm_uframes_t size;
snd_pcm_sframes_t buffer_size;
snd_pcm_sframes_t period_size;
snd_pcm_sw_params_t *swparams;
int period_event = 0;
int err, dir;
/* Open PCM device for playback. */
if ((err = snd_pcm_open(&handle, devout->out.alsa.device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
return;
}
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_sw_params_alloca(&swparams);
/* choose all parameters */
err = snd_pcm_hw_params_any(handle, hwparams);
if (err < 0) {
printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
return;
}
/* set hardware resampling */
err = snd_pcm_hw_params_set_rate_resample(handle, hwparams, 1);
if (err < 0) {
printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
return;
}
/* set the interleaved read/write format */
err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
printf("Access type not available for playback: %s\n", snd_strerror(err));
return;
}
/* set the sample format */
err = snd_pcm_hw_params_set_format(handle, hwparams, format);
if (err < 0) {
printf("Sample format not available for playback: %s\n", snd_strerror(err));
return;
}
/* set the count of channels */
channels = devout->dsp_channels;
err = snd_pcm_hw_params_set_channels(handle, hwparams, channels);
if (err < 0) {
printf("Channels count (%i) not available for playbacks: %s\n", channels, snd_strerror(err));
return;
}
/* set the stream rate */
rate = devout->frequency;
rrate = rate;
err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rrate, 0);
if (err < 0) {
printf("Rate %iHz not available for playback: %s\n", rate, snd_strerror(err));
return;
}
if (rrate != rate) {
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
exit(-EINVAL);
}
/* set the buffer size */
size = devout->buffer_size;
err = snd_pcm_hw_params_set_buffer_size(handle, hwparams, size);
if (err < 0) {
printf("Unable to set buffer size %i for playback: %s\n", size, snd_strerror(err));
return;
}
buffer_size = size;
/* set the period time */
err = snd_pcm_hw_params_set_period_time_near(handle, hwparams, &period_time, &dir);
if (err < 0) {
printf("Unable to set period time %i for playback: %s\n", period_time, snd_strerror(err));
return;
}
err = snd_pcm_hw_params_get_period_size(hwparams, &size, &dir);
if (err < 0) {
//.........这里部分代码省略.........
示例12: ags_devout_pcm_info
void
ags_devout_pcm_info(char *card_id,
guint *channels_min, guint *channels_max,
guint *rate_min, guint *rate_max,
guint *buffer_size_min, guint *buffer_size_max,
GError **error)
{
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
unsigned int val;
int dir;
snd_pcm_uframes_t frames;
int err;
/* Open PCM device for playback. */
handle = NULL;
rc = snd_pcm_open(&handle, card_id, SND_PCM_STREAM_PLAYBACK, 0);
if(rc < 0) {
g_message("unable to open pcm device: %s\n\0", snd_strerror(rc));
g_set_error(error,
AGS_DEVOUT_ERROR,
AGS_DEVOUT_ERROR_LOCKED_SOUNDCARD,
"unable to open pcm device: %s\n\0",
snd_strerror(rc));
return;
}
/* Allocate a hardware parameters object. */
snd_pcm_hw_params_alloca(¶ms);
/* Fill it in with default values. */
snd_pcm_hw_params_any(handle, params);
/* channels */
snd_pcm_hw_params_get_channels_min(params, &val);
*channels_min = val;
snd_pcm_hw_params_get_channels_max(params, &val);
*channels_max = val;
/* samplerate */
dir = 0;
snd_pcm_hw_params_get_rate_min(params, &val, &dir);
*rate_min = val;
dir = 0;
snd_pcm_hw_params_get_rate_max(params, &val, &dir);
*rate_max = val;
/* buffer size */
dir = 0;
snd_pcm_hw_params_get_buffer_size_min(params, &frames);
*buffer_size_min = frames;
dir = 0;
snd_pcm_hw_params_get_buffer_size_max(params, &frames);
*buffer_size_max = frames;
snd_pcm_close(handle);
}
示例13: AudioOutputDevice
/**
* Create and initialize Alsa audio output device with given parameters.
*
* @param Parameters - optional parameters
* @throws AudioOutputException if output device cannot be opened
*/
AudioOutputDeviceAlsa::AudioOutputDeviceAlsa(std::map<String,DeviceCreationParameter*> Parameters) : AudioOutputDevice(Parameters), Thread(true, true, 1, 0) {
pcm_handle = NULL;
stream = SND_PCM_STREAM_PLAYBACK;
this->uiAlsaChannels = ((DeviceCreationParameterInt*)Parameters["CHANNELS"])->ValueAsInt();
this->uiSamplerate = ((DeviceCreationParameterInt*)Parameters["SAMPLERATE"])->ValueAsInt();
this->FragmentSize = ((DeviceCreationParameterInt*)Parameters["FRAGMENTSIZE"])->ValueAsInt();
uint Fragments = ((DeviceCreationParameterInt*)Parameters["FRAGMENTS"])->ValueAsInt();
String Card = ((DeviceCreationParameterString*)Parameters["CARD"])->ValueAsString();
dmsg(2,("Checking if hw parameters supported...\n"));
if (HardwareParametersSupported(Card, uiAlsaChannels, uiSamplerate, Fragments, FragmentSize)) {
pcm_name = "hw:" + Card;
}
else {
fprintf(stderr, "Warning: your soundcard doesn't support chosen hardware parameters; ");
fprintf(stderr, "trying to compensate support lack with plughw...");
fflush(stdout);
pcm_name = "plughw:" + Card;
}
dmsg(2,("HW check completed.\n"));
int err;
snd_pcm_hw_params_alloca(&hwparams); // Allocate the snd_pcm_hw_params_t structure on the stack.
/* Open PCM. The last parameter of this function is the mode. */
/* If this is set to 0, the standard mode is used. Possible */
/* other values are SND_PCM_NONBLOCK and SND_PCM_ASYNC. */
/* If SND_PCM_NONBLOCK is used, read / write access to the */
/* PCM device will return immediately. If SND_PCM_ASYNC is */
/* specified, SIGIO will be emitted whenever a period has */
/* been completely processed by the soundcard. */
if ((err = snd_pcm_open(&pcm_handle, pcm_name.c_str(), stream, 0)) < 0) {
throw AudioOutputException(String("Error opening PCM device ") + pcm_name + ": " + snd_strerror(err));
}
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) {
throw AudioOutputException(String("Error, cannot initialize hardware parameter structure: ") + snd_strerror(err));
}
/* Set access type. This can be either */
/* SND_PCM_ACCESS_RW_INTERLEAVED or */
/* SND_PCM_ACCESS_RW_NONINTERLEAVED. */
if ((err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
throw AudioOutputException(String("Error snd_pcm_hw_params_set_access: ") + snd_strerror(err));
}
/* Set sample format */
#if WORDS_BIGENDIAN
if ((err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_BE)) < 0)
#else // little endian
if ((err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE)) < 0)
#endif
{
throw AudioOutputException(String("Error setting sample format: ") + snd_strerror(err));
}
int dir = 0;
/* Set sample rate. If the exact rate is not supported */
/* by the hardware, use nearest possible rate. */
#if ALSA_MAJOR > 0
if((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &uiSamplerate, &dir)) < 0)
#else
if((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, uiSamplerate, &dir)) < 0)
#endif
{
throw AudioOutputException(String("Error setting sample rate: ") + snd_strerror(err));
}
if ((err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, uiAlsaChannels)) < 0) {
throw AudioOutputException(String("Error setting number of channels: ") + snd_strerror(err));
}
/* Set number of periods. Periods used to be called fragments. */
if ((err = snd_pcm_hw_params_set_periods(pcm_handle, hwparams, Fragments, dir)) < 0) {
throw AudioOutputException(String("Error setting number of periods: ") + snd_strerror(err));
}
/* Set buffer size (in frames). The resulting latency is given by */
/* latency = periodsize * periods / (rate * bytes_per_frame) */
if ((err = snd_pcm_hw_params_set_buffer_size(pcm_handle, hwparams, (FragmentSize * Fragments))) < 0) {
throw AudioOutputException(String("Error setting buffersize: ") + snd_strerror(err));
}
/* Apply HW parameter settings to */
/* PCM device and prepare device */
if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) {
throw AudioOutputException(String("Error setting HW params: ") + snd_strerror(err));
}
if (snd_pcm_sw_params_malloc(&swparams) != 0) {
throw AudioOutputException(String("Error in snd_pcm_sw_params_malloc: ") + snd_strerror(err));
}
//.........这里部分代码省略.........
示例14: availableDevices
bool QAlsaAudioDeviceInfo::testSettings(const QAudioFormat& format) const
{
// Set nearest to closest settings that do work.
// See if what is in settings will work (return value).
int err = -1;
snd_pcm_t* pcmHandle;
snd_pcm_hw_params_t *params;
QString dev;
#if(SND_LIB_MAJOR == 1 && SND_LIB_MINOR == 0 && SND_LIB_SUBMINOR >= 14)
dev = device;
if (dev.compare(QLatin1String("default")) == 0) {
QList<QByteArray> devices = availableDevices(QAudio::AudioOutput);
if (!devices.isEmpty())
dev = QLatin1String(devices.first().constData());
}
#else
if (dev.compare(QLatin1String("default")) == 0) {
dev = QLatin1String("hw:0,0");
} else {
int idx = 0;
char *name;
QString shortName = device.mid(device.indexOf(QLatin1String("="),0)+1);
while(snd_card_get_name(idx,&name) == 0) {
if(shortName.compare(QLatin1String(name)) == 0)
break;
idx++;
}
dev = QString(QLatin1String("hw:%1,0")).arg(idx);
}
#endif
snd_pcm_stream_t stream = mode == QAudio::AudioOutput
? SND_PCM_STREAM_PLAYBACK : SND_PCM_STREAM_CAPTURE;
if (snd_pcm_open(&pcmHandle, dev.toLocal8Bit().constData(), stream, 0) < 0)
return false;
snd_pcm_nonblock(pcmHandle, 0);
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcmHandle, params);
// set the values!
snd_pcm_hw_params_set_channels(pcmHandle, params, format.channelCount());
snd_pcm_hw_params_set_rate(pcmHandle, params, format.sampleRate(), 0);
snd_pcm_format_t pcmFormat = SND_PCM_FORMAT_UNKNOWN;
switch (format.sampleSize()) {
case 8:
if (format.sampleType() == QAudioFormat::SignedInt)
pcmFormat = SND_PCM_FORMAT_S8;
else if (format.sampleType() == QAudioFormat::UnSignedInt)
pcmFormat = SND_PCM_FORMAT_U8;
break;
case 16:
if (format.sampleType() == QAudioFormat::SignedInt) {
pcmFormat = format.byteOrder() == QAudioFormat::LittleEndian
? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_S16_BE;
} else if (format.sampleType() == QAudioFormat::UnSignedInt) {
pcmFormat = format.byteOrder() == QAudioFormat::LittleEndian
? SND_PCM_FORMAT_U16_LE : SND_PCM_FORMAT_U16_BE;
}
break;
case 32:
if (format.sampleType() == QAudioFormat::SignedInt) {
pcmFormat = format.byteOrder() == QAudioFormat::LittleEndian
? SND_PCM_FORMAT_S32_LE : SND_PCM_FORMAT_S32_BE;
} else if (format.sampleType() == QAudioFormat::UnSignedInt) {
pcmFormat = format.byteOrder() == QAudioFormat::LittleEndian
? SND_PCM_FORMAT_U32_LE : SND_PCM_FORMAT_U32_BE;
} else if (format.sampleType() == QAudioFormat::Float) {
pcmFormat = format.byteOrder() == QAudioFormat::LittleEndian
? SND_PCM_FORMAT_FLOAT_LE : SND_PCM_FORMAT_FLOAT_BE;
}
}
if (pcmFormat != SND_PCM_FORMAT_UNKNOWN)
err = snd_pcm_hw_params_set_format(pcmHandle, params, pcmFormat);
// For now, just accept only audio/pcm codec
if (!format.codec().startsWith(QLatin1String("audio/pcm")))
err = -1;
if (err >= 0 && format.channelCount() != -1) {
err = snd_pcm_hw_params_test_channels(pcmHandle, params, format.channelCount());
if (err >= 0)
err = snd_pcm_hw_params_set_channels(pcmHandle, params, format.channelCount());
}
if (err >= 0 && format.sampleRate() != -1) {
err = snd_pcm_hw_params_test_rate(pcmHandle, params, format.sampleRate(), 0);
if (err >= 0)
err = snd_pcm_hw_params_set_rate(pcmHandle, params, format.sampleRate(), 0);
}
if (err >= 0 && pcmFormat != SND_PCM_FORMAT_UNKNOWN)
err = snd_pcm_hw_params_set_format(pcmHandle, params, pcmFormat);
//.........这里部分代码省略.........
示例15: alsa_init
int alsa_init(alsa_dev_t *dev, void *ptr, void (*callback)(snd_async_handler_t *))
{
snd_pcm_t *phandle;
snd_pcm_t *chandle;
int err;
snd_pcm_hw_params_t *hwparams_capture, *hwparams_playback;
snd_pcm_sw_params_t *swparams_capture, *swparams_playback;
struct sched_param param;
err = sched_getparam(0, ¶m);
if (err < 0) {
perror("sched_getparam():");
exit(-1);
}
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
err = sched_setscheduler(0, SCHED_FIFO, ¶m);
if (err < 0) {
perror("sched_setscheduler():");
exit(-1);
}
snd_pcm_hw_params_alloca(&hwparams_capture);
snd_pcm_sw_params_alloca(&swparams_capture);
snd_pcm_hw_params_alloca(&hwparams_playback);
snd_pcm_sw_params_alloca(&swparams_playback);
err = snd_output_stdio_attach(&dev->output, stdout, 0);
if (err < 0) {
printf("Output failed: %s\n", snd_strerror(err));
exit(-1);
}
printf("Playback device is %s\n", dev->pdevice);
printf("Stream parameters are %dHz, %s, %d channels\n", dev->rate, snd_pcm_format_name(dev->format), dev->channels);
if ((err = snd_pcm_open(&phandle, dev->pdevice, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
printf("Playback open error: %s\n", snd_strerror(err));
exit(-1);
}
if ((err = alsa_set_hwparams(dev, phandle, hwparams_playback, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
printf("Setting of hwparams_playback failed: %s\n", snd_strerror(err));
exit(-1);
}
if ((err = alsa_set_swparams(dev, phandle, swparams_playback)) < 0) {
printf("Setting of swparams_playback failed: %s\n", snd_strerror(err));
exit(-1);
}
snd_pcm_dump(phandle, dev->output);
printf("Capture device is %s\n", dev->cdevice);
if ((err = snd_pcm_open(&chandle, dev->cdevice, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
printf("Capture open error: %s\n", snd_strerror(err));
exit(-1);
}
if ((err = alsa_set_hwparams(dev, chandle, hwparams_capture, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
printf("Setting of hwparams_capture failed: %s\n", snd_strerror(err));
exit(-1);
}
if ((err = alsa_set_swparams(dev, chandle, swparams_capture)) < 0) {
printf("Setting of swparams_capture failed: %s\n", snd_strerror(err));
exit(-1);
}
snd_pcm_dump(chandle, dev->output);
if ((err = snd_pcm_link(phandle, chandle)) < 0)
{
printf("snd_pcm_link() failed: %s\n", snd_strerror(err));
exit(-1);
}
dev->chandle = chandle;
dev->phandle = phandle;
err = alsa_async_direct_loop(dev, ptr, callback);
if (err < 0)
printf("Transfer failed: %s\n", snd_strerror(err));
return 0;
}