基,
目标:我正在尝试使用sox-resample库,从输入速率(44.1Khz,2通道,16位)进行下采样, 输出速率(16Khz,1通道,16位)。是否可以在此过程中使用sox-resample库。
Sox Resample库源位于:http://sourceforge.net/p/soxr/code/ci/master/tree/
我试过soxr_oneshot这个api,输出产生的,没有输入缓冲区的原始声音, 它有一个奇怪的声音输出,远离原始的声音输入。
还尝试了soxr_create和soxr_process apis,但没有得到正确的输出。
我不确定,如果这些apis(soxr_oneshot,soxr_create和soxr_process)有一个选项, 降级通道数,以及我们是否可以指定16位或8位。
也不确定,如果我应该使用LibSox apis,例如sox_create_effect / sox_add_effect,以及这是否是更好的选择。
以下是使用示例代码,soxr_oneshot,请告诉我,可能出现的问题,或者您有任何建议。
#define INPUT_RATE (44100)
#define OUTPUT_RATE (16000)
#define INPUT_NUM_CHANNELS (2)
#define OUTPUT_NUM_CHANNELS (1)
#define OUTPUT_BLOCK_ALIGN (2)
/*
ibuf - pointer to input buffer.
length - length of input buffer.
block_align - sample size of input rate.
*/
static int sox_test_function(void *ibuf, UINT32 length, UINT32 block_align)
{
size_t olen = (size_t)((length * OUTPUT_RATE * OUTPUT_NUM_CHANNELS)/ (INPUT_RATE * OUTPUT_NUM_CHANNELS));
float * obuf = (float *)malloc(OUTPUT_BLOCK_ALIGN * olen);
memset(obuf, 0, (OUTPUT_BLOCK_ALIGN * olen));
size_t odone;
FILE *ofile;
UINT32 written = 0;
//SKR: Need to find out, if we can downgrade # of channels..can we do that..??
//also not sure..how to specify 16 bit/8 bit..etc..
soxr_error_t err = soxr_oneshot(
INPUT_RATE,
OUTPUT_RATE,
1,
ibuf , length/block_align, NULL,
obuf, olen/OUTPUT_BLOCK_ALIGN, &odone,
NULL,
NULL,
NULL);
ofile = fopen("nwave_file_16k_pcm.raw", "wb");
if (ofile == NULL) {
perror("Invalid file specified.");
exit(-1);
}
written = fwrite(obuf, OUTPUT_BLOCK_ALIGN, odone, ofile); // Consume output.
return 0;
}
请告诉我,可能出现的问题,或者您有任何建议。
答案 0 :(得分:0)
请参阅sox源文件中示例程序中的example3.c.您已使用sox效果“rate”进行下采样。我试过了&能够成功地对wav文件进行下采样,但是我遇到了波形被修剪的问题。您可以参考我的帖子Downsampling a wav file using libsox