Menu

How to Get FFT of the Wav File?

Help
kazathul
2014-07-18
2014-07-23
  • kazathul

    kazathul - 2014-07-18

    Hi there,
    I'm trying to get the FFT of a pure tone wav file. I've actually added (overloaded) a function in the clunk::WavFile class like follows:

    Sample * WavFile::load(clunk::Buffer &buffer, Context &context, const std::string &fname) {
        FILE *f = fopen(fname.c_str(), "rb");
        if (!f)
            throw std::runtime_error("cannot open file: " + fname);
        WavFile wav(f);
        wav.read();
        std::auto_ptr<Sample> sample(context.create_sample());
        sample->init(wav._data, wav._spec);
        sample->name = fname;
        // Copy the data
        buffer = wav._data;
        return sample.release();
    }
    

    And in my main I'm doing something like this:

    clunk::Buffer zeBuffer;
    clunk::Sample * pureTone = clunk::WavFile::load(zeBuffer, context, "pureTone.wav");
    

    I just want the FFT of the sound file. How can I do it?
    I appreciate your help,
    Thanks in advance.

     

    Last edit: kazathul 2014-07-18
  • Vladimir Menshakov

    it's as simple as the following code.

    fft_context<512> fft;
    for(unsigned i = 0; i < 512; ++i)
        //assuming signed 16-bit samples
        //you could always convert raw data via Resample::resample(dst_format, dst_buffer, src_format, src_buffer);
        fft.data[i] = std::complex(samples[i] / 32768.0, 0);
    
    fft.fft();
    

    I don't think you need to override something in wave file. I will add the api for getting raw format/sample data from sample right away :)

    Sorry for delay in responses, I was on business trip in Taiwan.

     

    Last edit: Vladimir Menshakov 2014-07-18
  • Vladimir Menshakov

    also you could cast raw sample data via AudioFormat<T> template as follows:

    //assuming your data in s16:
    assert(sample->get_spec().format == AudioSpec::S16);
    
    typedef AudioFormat<AudioSpec::S16>::Type src_type;
    const src_type *src = static_cast< const src_type* >(sample->get_data().get_ptr());
    

    this is the most convenient and fastest way to access raw data.

    PS I just added the needed API and removed friend declaration

     

    Last edit: Vladimir Menshakov 2014-07-18
  • kazathul

    kazathul - 2014-07-23

    I'm also away for a short vacation at the moment.
    I'll definitely look into this once I get back this weekend.
    Appreciated.

     

Log in to post a comment.