gpt4 book ai didi

c - 在 C 中,如何将 1 channel PCM 转换为 2 channel PCM?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:38 27 4
gpt4 key购买 nike

我正在使用 libao 和 libsndfile 来读取和播放音频。我想通过将一个 channel 复制为两个来将单声道流转换为立体声流。此测试代码将正确播放立体声剪辑,但会非常快速和高音播放单声道剪辑。此外,我在 free(output); 调用中收到“双重释放或损坏”。我做错了什么?

/* Converting a 1-channel stream to 2-channels
* compile with "gcc -o stereoize stereoize.c -lao -lsndfile"
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ao/ao.h>
#include <sndfile.h>

#define BUFFSIZE 512

void stereoize(short *, short *, size_t);
void playfile(FILE *);

int main(int argc, char *argv[])
{
FILE *fp;

if (argc != 2) {
printf("usage: %s <filename>\n", argv[0]);
exit(1);
}

fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Cannot open %s.\n", argv[1]);
exit(2);
}

playfile(fp);
fclose(fp);
printf("Finished.\n");

return 0;
}

void playfile(FILE *fp)
{
int default_driver;
int frames_read;
int count;
int toread;
short *buffer;
short *output;

ao_device *device;
ao_sample_format format;

SNDFILE *sndfile;
SF_INFO sf_info;


ao_initialize();
default_driver = ao_default_driver_id();

sf_info.format = 0;

sndfile = sf_open_fd(fileno(fp), SFM_READ, &sf_info, 0);

memset(&format, 0, sizeof(ao_sample_format));

format.byte_format = AO_FMT_NATIVE;
format.bits = 16;
format.rate = sf_info.samplerate;
// format.channels = sf_info.channels;
format.channels = 2;

printf("Channels: %d\n", sf_info.channels);
printf("Samplerate: %d\n", sf_info.samplerate);

if (sf_info.channels > 2 || sf_info.channels < 1) {
printf("Sorry. Only 1 or 2 channels, please.\n");
exit(1);
}
device = ao_open_live(default_driver, &format, NULL);
if (device == NULL) {
printf("Error opening sound device.\n");
exit(1);
}

buffer = malloc(BUFFSIZE * sf_info.channels * sizeof(short));
output = malloc(BUFFSIZE * 2 * sizeof(short));

frames_read = 0;
toread = sf_info.frames * sf_info.channels;

while (toread > 0) {
if (toread < BUFFSIZE * sf_info.channels)
count = toread;
else
count = BUFFSIZE * sf_info.channels;

frames_read = sf_read_short(sndfile, buffer, count);

if (sf_info.channels == 1)
stereoize(output, buffer, count);
else
memcpy(output, buffer, count * sizeof(short));

if (sf_info.channels == 1)
ao_play(device, (char *)output, 2 * frames_read * sizeof(short));
else
ao_play(device, (char *)output, frames_read * sizeof(short));

toread = toread - frames_read;
}

free(buffer);
free(output);
ao_close(device);
sf_close(sndfile);
ao_shutdown();

return;
}

void stereoize(short *outbuf, short *inbuf, size_t length)
{
int count;
int outcount;

outcount = 0;
for (count = 0; count < length; count++) {
outbuf[outcount] = outbuf[outcount+1] = inbuf[count];
outcount += 2;
}
}

最佳答案

语句 stereoize(output, buffer, count * sizeof(short)); 很可能是导致问题的原因。 stereoize 函数需要样本数作为 length 参数,但您传递的是总 block 大小(以字节为单位)。改成stereoize(output, buffer, count);,看看能不能解决问题。另请查看@Joachim Pileborg 的评论。

关于c - 在 C 中,如何将 1 channel PCM 转换为 2 channel PCM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728655/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com