Skip to content

Instantly share code, notes, and snippets.

@svenoaks
Created May 28, 2023 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenoaks/34992b89aa9d05b340fb5bca907d9089 to your computer and use it in GitHub Desktop.
Save svenoaks/34992b89aa9d05b340fb5bca907d9089 to your computer and use it in GitHub Desktop.
testFormantShifter
void testFormantShifter() {
AudioFile<float> audioFile, audioFileReference, audioFileResult;
string filename = "../" + std::string("test1") + ".wav";
string filenameResult = "../" + std::string("formantResult") + ".wav";
audioFile.load(filename);
auto& audioFrames = audioFile.samples;
const int channels = audioFrames.size(); // the number of channels is determined by the size of audioFrames
const int samplerate = 44100; // assuming CD quality sound
SMP::FormantShifter shifter(channels, samplerate);
// Set the frequency ratio (as needed)
double freqRatio = 1.0; // set to desired value
shifter.setFrequencyRatio(freqRatio);
// The chunk size
const int chunkSize = 2048 + 512;
// Creating pointers to the channels
std::vector<const float*> channelPointers(channels);
// Process audio frames
for (size_t i = 0; i < audioFrames[0].size(); i += chunkSize) {
// Ensuring that the frame size is a multiple of chunkSize
if (audioFrames[0].size() - i < chunkSize) {
break;
}
// Setting up the input pointers for this chunk
for (int ch = 0; ch < channels; ++ch) {
channelPointers[ch] = &audioFrames[ch][i];
}
// Creating output for this chunk
std::vector<std::vector<float>> outputChunk(channels, std::vector<float>(chunkSize, 0.0f));
// Processing this chunk
shifter.process(channelPointers.data(), chunkSize, outputChunk);
// Replacing original data with processed data
for (int ch = 0; ch < channels; ++ch) {
std::copy(outputChunk[ch].begin(), outputChunk[ch].end(), &audioFrames[ch][i]);
}
}
audioFile.save(filenameResult);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment