第一篇文章, 我正在尝试使用libsamplerate和libsndfile创建一个简单的pitchshifter。 我通过制作一个简单的采样率covnerter然后黑客攻击,以最基本的形式实现了这一点,我通过改变比率浮动值来改变音高。 变速杆移位 - 在正弦音上听起来非常精细 - 如果你将它用于音频,你可以听到声音块之间的间隙 - 特别是如果你把声音调高。 我想知道是否有一种方法可以使代码更有效地对付这种或某种插值函数或库,我可以毫不费力地实现它。 我是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;
}
答案 0 :(得分:0)
最便宜的时间间距转换器在投球时用于工作(在旧磁带播放器中)的方式是简单地复制重采样声音块或其部分,以填补间隙。稍差一点的那些在零交叉处将块拼接在一起。这种方式有效,但远没有高质量的声音。