Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15358638
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类: LINUX

2009-11-15 17:48:34

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