下面这个图比较看的比较明确,结构体就是按照这种要求来处理的:
---------------------------------------------------------------------
| 单声道 | 取样1 | 取样2 | 取样3 | 取样4 |
| |--------------------------------------------------------
| 8bit量化 | 声道0 | 声道0 | 声道0 | 声道0 |
---------------------------------------------------------------------
| 双声道 | 取样1 | 取样2 |
| |--------------------------------------------------------
| 8bit量化 | 声道0(左) | 声道1(右) | 声道0(左) | 声道1(右) |
---------------------------------------------------------------------
| | 取样1 | 取样2 |
| 单声道 |--------------------------------------------------------
| 16bit量化 | 声道0 | 声道0 | 声道0 | 声道0 |
| | (低位字节) | (高位字节) | (低位字节) | (高位字节) |
---------------------------------------------------------------------
| | 取样1 |
| 双声道 |--------------------------------------------------------
| 16bit量化 | 声道0(左) | 声道0(左) | 声道1(右) | 声道1(右) |
| | (低位字节) | (高位字节) | (低位字节) | (高位字节) |
---------------------------------------------------------------------
定义结构体:
typedef struct _snd_pcm_channel_area {
/** base address of channel samples */
void *addr;
/** offset to first sample in bits */
unsigned int first;
/** samples distance in bits */
unsigned int step;
} snd_pcm_channel_area_t;
申请空间:
samples = malloc( period_size * channels * format / 8);
areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
初始化结构体:
for (chn = 0; chn < channels; chn++) {
areas[chn].addr = samples;
areas[chn].first = chn * format;
areas[chn].step = channels * format;
}
根据具体的格式准备好每次存放采样点的临时变量:
for (chn = 0; chn < channels; chn++) {
samples[chn] = (((unsigned char *)areas[chn].addr) + (areas[chn].first / 8));
steps[chn] = areas[chn].step / 8;
samples[chn] += offset * steps[chn];
}
注意步长是一个字节(8位)。
阅读(1136) | 评论(0) | 转发(0) |