Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1307087
  • 博文数量: 860
  • 博客积分: 425
  • 博客等级: 下士
  • 技术积分: 1464
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-20 19:57
个人简介

对技术执着

文章分类

全部博文(860)

文章存档

2019年(16)

2018年(12)

2015年(732)

2013年(85)

2012年(15)

我的朋友

分类: LINUX

2015-03-14 17:12:57

// 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;
}
阅读(1470) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~