gpt4 book ai didi

使用 libsamplerate 的 C 变调器

转载 作者:行者123 更新时间:2023-11-30 17:52:24 32 4
gpt4 key购买 nike

第一篇文章,我正在尝试使用 libsamplerate 和 libsndfile 制作一个简单的变调器。我通过制作一个简单的采样率调节器然后对其进行修改,以最基本的形式实现了这一点,我通过改变比率浮点值来改变音高。变调器移动 - 在正弦音调上听起来非常好 - 如果您将其用于音频,您可以听到声音 block 之间出现的间隙 - 特别是如果您将文件调高。我想知道是否有一种方法可以使代码更有效地应对这种情况或某种插值函数或库,我可以毫不费力地实现它们。我对 C 非常陌生 - 以前只通过 PD 处理声音,这是我的第一个项目 - 据我了解,libsamplerate 并不是真正为实现音高转换而设计的,所以我知道要实现这一点需要一些技巧。

谢谢

这是我的代码

    #include <stdio.h>
#include </usr/local/include/sndfile.h>
#include </usr/local/include/samplerate.h>

#define BUFFER_LEN 44100 //defines buffer length

#define MAX_CHANNELS 2 //defines max channels

int main ()
{
static float datain [BUFFER_LEN]; //static defines as a global variable

static float dataout [BUFFER_LEN]; //static defines as a global variable

SNDFILE *infile, *outfile; //determines file open function + pointers

/*descriptor*/SF_INFO /*sf_open*/ sfinfo, sfinfo2;

int readcount;//used to store data in while ((readcount = sf...

const char *infilename/*pointer*/ = "/tmp/input.wav"; //const means that it is a value that cannot change
//after initialisation

const char *outfilename/*pointer*/ = "/tmp/soundchanged.wav"; //const means that it is a value that cannot change
//after initialisation

SRC_DATA src_data; //struct from libsamplerate library
//http://www.mega-nerd.com/SRC/api_misc.html#SRC_DATA

infile = sf_open (infilename/*pointer*/, SFM_READ, &sfinfo); //infile declares a file variable, SFM_READ-reads file
//sfinfo -function of sfopen

outfile = sf_open (outfilename/*pointer*/, SFM_WRITE, &sfinfo); //outfile declares a file variable, SFM_WRITE-writes file
//sfinfo -function of sfopen

src_data.data_in = datain; //used to pass audio data into the converter

src_data.input_frames = BUFFER_LEN; //supply the converter with the lengths of the arrays
//(in frames) pointed to by the data_in

src_data.data_out = dataout; //supplies the converter with an array to hold the converter's output

src_data.output_frames = BUFFER_LEN; //supply the converter with the lengths of the arrays
//(in frames) pointed to by the data_out

/*------->*/src_data.src_ratio = 0.2 /*changing this number changes the pitch of the output file*/;
//specifies the conversion ratio defined as the input sample rate
//divided by the output sample rate

src_simple (&src_data/*reference address of src_data*/, SRC_SINC_BEST_QUALITY, 1);//

while ((readcount = sf_read_float (infile, datain, BUFFER_LEN)))//while readcount is equal to
//sf_read_float - function call: infile,datain and BUFFER_LEN
//this data is then fed into the converter argument below


{
src_simple (&src_data, SRC_SINC_BEST_QUALITY, 1); //selects converter
//http://www.mega-nerd.com/SRC/api_misc.html#SRC_DATA

sf_write_float (outfile, dataout, readcount);
//write the data in the array pointed to by ptr to the file
};


sf_close (infile);
sf_close (outfile); // closes infile,outfile
//The close function closes the file, deallocates
//its internal buffers and returns 0 on success or an error value otherwise.

sf_open ("/tmp/soundchanged.wav", SFM_READ, &sfinfo2/*reference address of sfinfo2*/);

printf("%d", sfinfo2.samplerate);//outputs samplerate

return 0;
}

最佳答案

过去最便宜的时间音调转换器(在老式磁带播放器中)在调高时的工作方式是简单地复制重新采样的声音 block 或其部分,以填补空白。稍微不那么糟糕的则在零交叉处将 block 拼接在一起。这种方法确实有效,但听起来质量还差得很远。

关于使用 libsamplerate 的 C 变调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16216509/

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