// set the silence data on the buffer
int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
{
int width;
unsigned char *dst, *pat;
if (format < 0 || format > SNDRV_PCM_FORMAT_LAST)
return -EINVAL;
if (samples == 0)
return 0;
width = pcm_formats[format].phys; /* physical width */
pat = pcm_formats[format].silence;
if (! width)
return -EINVAL;
/* signed or 1 byte data */
if (pcm_formats[format].signd == 1 || width <= 8) {
// 格式为有符号或者量化位数不大于8bits的[luther.gliethttp]
unsigned int bytes = samples * width / 8; // 填充samples次采样总数据[luther.gliethttp]
memset(data, *pat, bytes);
return 0;
}
/* non-zero samples, fill using a loop */
width /= 8;
dst = data;
#if 0
while (samples--) {
memcpy(dst, pat, width);
dst += width;
}
#else
/* a bit optimization for constant width */
switch (width) {
case 2: // 16bits
while (samples--) {
memcpy(dst, pat, 2); // 每个样点2个字节,一共samples个采样点[luther.gliethttp]
dst += 2;
}
break;
case 3: // 24bits
while (samples--) {
memcpy(dst, pat, 3); // 每个样点3个字节,一共samples个采样点[luther.gliethttp]
dst += 3;
}
break;
case 4: // 32bits
while (samples--) {
memcpy(dst, pat, 4); // 每个样点4个字节,一共samples个采样点[luther.gliethttp]
dst += 4;
}
break;
case 8: // 64bits
while (samples--) {
memcpy(dst, pat, 8); // 每个样点8个字节,一共samples个采样点[luther.gliethttp]
dst += 8;
}
break;
}
#endif
return 0;
}
阅读(2723) | 评论(0) | 转发(3) |