fmod getWaveData() export to WAVE file help (C++)

Posted by eddietree on Game Development See other posts from Game Development or by eddietree
Published on 2011-11-19T08:31:04Z Indexed on 2011/11/19 10:16 UTC
Read the original article Hit count: 514

Filed under:
|

I am trying to export the current sound that is being played by the FMOD::System into a WAVE file by calling getWaveData(). I have the header of the wave file correct, and currently trying to write to the wave file each frame like so:

const unsigned int samplesPerSec = 48000;
const unsigned int fps = 60;
const int numSamples = samplesPerSec / fps;
float data[2][numSamples];
short conversion[numSamples*2];

m_fmodsys->getWaveData( &data[0][0], numSamples, 0 ); // left channel
m_fmodsys->getWaveData( &data[1][0], numSamples, 1 ); // right channel

int littleEndian = IsLittleEndian();

for ( int i = 0; i < numSamples; ++i )
{
    // left channel
    float coeff_left = data[0][i];
    short val_left =  (short)(coeff_left * 0x7FFF);

    // right channel
    float coeff_right = data[1][i];
    short val_right =  (short)(coeff_right * 0x7FFF);

    // handle endianness
    if ( !littleEndian )
    {
        val_left = ((val_left & 0xff) << 8) | (val_left >> 8);
        val_right = ((val_right & 0xff) << 8) | (val_right >> 8);
    }

    conversion[i*2+0] = val_left;
    conversion[i*2+1] = val_right;
}

fwrite((void*)&conversion[0], sizeof(conversion[0]), numSamples*2, m_fh);
m_dataLength += sizeof(conversion);

Currently, the timing of the sound is correct, but the sample seems clipped way harshly. More specifically, I am outputting four beats in time. When I playback the wave-file, the beats timing is correct but it just sounds way fuzzy and clipped. Am I doing something wrong with my calculation?

I am exporting in 16-bits, two channels.

Thanks in advance! :)

Reference (WAVE file format): http://www.sonicspot.com/guide/wavefiles.html

© Game Development or respective owner

Related posts about c++

Related posts about fmod