I am trying to implement a program with pitch corrects inputted audio signals.
I have taken a .wav file, used autocorrelation to find the period of the signal every 0.04s. I now have an array of amplitude points which take the period length of the signal every 0.04s and repeat those values up until the next 0.04s. (I know that the signal has not been stretched yet, this is the final step).
How can I use libsnd file to write these values back to the original .wav file so that it can be played?
I tried something like:
SF_INFO sfinfo;
sfinfo.frames;
const char* path = "Vocal3.wav";
SNDFILE * outfile = sf_open(path, SFM_WRITE, &sfinfo);
for (k = 0; k < num; ++k) {
sf_count_t count = sf_write_float(outfile, &array[k], num);
sf_write_sync(outfile);
}
sf_close(outfile);
but it creates a .wav file of 0 bytes that won't open. Do I have to create wav file from scratch? Does this mean creating the header too? Some pointers on how to do this would be much appreciated.
EDIT:
I now think I understand the syntax of libsndfile a bit better, and I tried something like:
SF_INFO sfinfo;
SNDFILE* outfile;
outfile = sf_open(filename, SFM_RDWR, &sfinfo);
sf_count_t count = sf_writef_float(outfile, array, sfinfo.frames);
const char* error = sf_strerror(outfile);
std::cout << error << "\n";
sf_close(outfile);
Which doesn't overwrite the file, it instead appends the new amplitude values to the original file. Is there a way the original values can be removed?
EDIT 2:
The original files can be removed using the seek functions - http://www.mega-nerd.com/libsndfile/api.html#seek - thanks to Matthias for the help