| 11 | namespace audio |
| 12 | { |
| 13 | std::vector<float> AudioCapture::LoadAudioFile(std::string filePath) |
| 14 | { |
| 15 | SF_INFO inputSoundFileInfo; |
| 16 | SNDFILE* infile = nullptr; |
| 17 | infile = sf_open(filePath.c_str(), SFM_READ, &inputSoundFileInfo); |
| 18 | |
| 19 | float audioIn[inputSoundFileInfo.channels * inputSoundFileInfo.frames]; |
| 20 | sf_read_float(infile, audioIn, inputSoundFileInfo.channels * inputSoundFileInfo.frames); |
| 21 | |
| 22 | float sampleRate = 16000.0f; |
| 23 | float srcRatio = sampleRate / (float)inputSoundFileInfo.samplerate; |
| 24 | int outputFrames = ceilf(inputSoundFileInfo.frames * srcRatio); |
| 25 | |
| 26 | // Convert to mono |
| 27 | std::vector<float> monoData(inputSoundFileInfo.frames); |
| 28 | for(int i = 0; i < inputSoundFileInfo.frames; i++) |
| 29 | { |
| 30 | for(int j = 0; j < inputSoundFileInfo.channels; j++) |
| 31 | monoData[i] += audioIn[i * inputSoundFileInfo.channels + j]; |
| 32 | monoData[i] /= inputSoundFileInfo.channels; |
| 33 | } |
| 34 | |
| 35 | // Resample |
| 36 | SRC_DATA srcData; |
| 37 | srcData.data_in = monoData.data(); |
| 38 | srcData.input_frames = inputSoundFileInfo.frames; |
| 39 | |
| 40 | std::vector<float> dataOut(outputFrames); |
| 41 | srcData.data_out = dataOut.data(); |
| 42 | |
| 43 | srcData.output_frames = outputFrames; |
| 44 | srcData.src_ratio = srcRatio; |
| 45 | |
| 46 | src_simple(&srcData, SRC_SINC_BEST_QUALITY, 1); |
| 47 | |
| 48 | sf_close(infile); |
| 49 | |
| 50 | return dataOut; |
| 51 | } |
| 52 | |
| 53 | void AudioCapture::InitSlidingWindow(float* data, size_t dataSize, int minSamples, size_t stride) |
| 54 | { |
no test coverage detected