Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
Would it be possible to attach a sample soundfile? Something simple like a sine wave? –  Ray Dey Nov 21 '11 at 4:57

1 Answer 1

Your code makes sense to me.

You should break this down into stages:

  1. Confirm the dynamic range of samples from getWaveData()
    Write each sample to a file as a string, then using somethign like Python or Octave (any tool that can read floats from a file, put them in some kind of vector, and then send that to the soundcard automatically) read your samples in and aurally confirm that FMOD itself is correct.

  2. Confirm your quantization step
    Having confirmed that sounds good, take the same data and read it into a C++ program that does the 16 bit quantization and again writes each sample out to a file as readable text.

    Now you can read that file in with Python, convert each int you read to a short in raw string format, and write that out to a sound file using the wave module. Play it back in your media player to confirm that it sounds good.

Also maybe consider plotting the data so you can visually spot anomalies (like going outside of the -1 to 1.0 pre-quantization). If it sounds good at each of these stages, then maybe it's time to revisit how you wrote the WAVE header.

Also agreed with Ray Dey, a compare/contrast soundfile would be helpful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.