kfrlib / kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
https://www.kfrlib.com
GNU General Public License v2.0
1.64k stars 252 forks source link

Audio writer output is garbled and much smaller than it should be #126

Open nsdrozario opened 3 years ago

nsdrozario commented 3 years ago

Platform: MSYS2 / Clang 11.0.0 on KFR 4.2.0 One of my projects seemed to have odd behavior with audio IO, so I made a test of audio input and output.

#define KFR_ENABLE_WAV 1
#include <kfr/all.hpp>
#include <iostream>
using std::cout;
using namespace kfr;

int main() {

    univector<float> audio;
    univector2d<float> audio_channels;
    audio_reader_wav<float> reader (open_file_for_reading("input.wav"));

    audio_channels = reader.read_channels();

    audio_writer_wav<float> writer(open_file_for_writing("output.wav"), reader.format());
    univector<float> interleaved_audio = interleave(audio_channels);

    if (interleaved_audio.size() == reader.format().length * reader.format().channels) {
        cout << "yes\n";
    }

    writer.write(interleaved_audio.data(), reader.format().length * reader.format().channels);

    return 0;

}

This should just create a file identical output.wav that is identical to input.wav. I included a check to see if the audio that audio_reader_wav read in was correct, and it was since it outputs yes in the if statement, so I know that audio_reader_wav read in the correct amount of data.

My input.wav file was in stereo 44.1khz containing 443161 samples and it is a bit below 4 mb, at about while the size of the audio output I get is only 4 kb, also at stereo 44.1khz but only 506 samples . I don't get why this is happening because I specified the correct size to write in my call to write(), while the output I get is several orders of magnitude below the intended output. What should I do to solve this?

Edit: On closer inspection it seems like the 506 samples of output.wav are the first 506 samples of input.wav.

nsdrozario commented 3 years ago

I also had similar results with directly putting the univector2d output of read_channels into write_channels as well.