0
0

Hello,
I need to write to a wav file a loaded sound. I used the recordtodisk example to create a function that takes a sound and writes it to a wav file. The resulting wav files got distortion.

Here is my code

[code:3q1k7lpi]
sys->createSound("drumloop.wav", FMOD_SOFTWARE | FMOD_ACCURATETIME , 0, &modified_sound);

int channels, bits;
unsigned int length;
float rate;
modified_sound->getFormat(NULL,NULL,&channels,&bits);
modified_sound->getDefaults(&rate,NULL,NULL,NULL);
modified_sound->getLength(&length, FMOD_TIMEUNIT_PCMBYTES);

string filename = "test.wav";

FILE *fp;
fp = fopen(filename.c_str(), "wb");
if (!fp)
    return -2;
fseek(fp, 0, 0);

// We create the WAV header
typedef struct
{
    signed char id[4];
    int         size;
} RiffChunk;

struct
{
    RiffChunk       chunk           ;
    unsigned short  wFormatTag      ;    /* format type  */
    unsigned short  nChannels       ;    /* number of channels (i.e. mono, stereo...)  */
    unsigned int    nSamplesPerSec  ;    /* sample rate  */
    unsigned int    nAvgBytesPerSec ;    /* for buffer estimation  */
    unsigned short  nBlockAlign     ;    /* block size of data  */
    unsigned short  wBitsPerSample  ;    /* number of bits per sample of mono data */
} FmtChunk  = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)(rate) * (channels) * bits / 8, 1 * (channels) * bits / 8, bits } ;

struct
{
    RiffChunk   chunk;
} DataChunk = { {{'d','a','t','a'}, length } };

struct
{
    RiffChunk   chunk;
    signed char rifftype[4];
} WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + length }, {'W','A','V','E'} };


// Write out the WAV header.

fwrite(&WavHeader, sizeof(WavHeader), 1, fp);
fwrite(&FmtChunk, sizeof(FmtChunk), 1, fp);
fwrite(&DataChunk, sizeof(DataChunk), 1, fp);

// We write the WAV data
void *ptr1, *ptr2;
unsigned int len1, len2;
modified_sound->lock(0,length,&ptr1,&ptr2,&len1,&len2);

int written = fwrite(ptr1,1,len1,fp);
modified_sound->unlock(ptr1,ptr2,len1,len2);
fclose(fp);

[/code:3q1k7lpi]

Is there a way to avoid this distortion or should I find another way to write the wav file ?

Thanks!

  • You must to post comments
0
0

That’s probably just the .wav format, fmod stores data signed, wav stores unsigned – for 8 bit data only. This is done by xoring the data with 128.

  • You must to post comments
0
0

oh! okay.

Thank you !

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.