btrme 0 Newbie Poster

I tried to save my record to wave file with using portaudio library.The samples are saved in recordedSamples array and I tried to add headers of wave file.I am doing something wrong.So,could you please rectify this ?? Thankss...Here is my code :

int main(void)
{
    PaStreamParameters  inputParameters,
                        outputParameters;
    PaStream*           stream;
    PaError             err = paNoError;
    paTestData          data;
    int                 i;
    int                 totalFrames;
    int                 numSamples;
    int                 numBytes;
    SAMPLE              max, val;
    char* myData[ numSamples ];
    double        average;
    int j;


       int myDataSize = numSamples;
       int myChunkSize = 36 + myDataSize;    
    printf("patest_record.c\n"); fflush(stdout);

    data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
    data.frameIndex = 0;
    numSamples = totalFrames * NUM_CHANNELS;
    numBytes = numSamples * sizeof(SAMPLE);
    data.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */
    if( data.recordedSamples == NULL )
    {
        printf("Could not allocate record array.\n");
        goto done;
    }
    for( i=0; i<numSamples; i++ ) data.recordedSamples[i] = 0;

    err = Pa_Initialize();
    if( err != paNoError ) goto done;

    inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
    if (inputParameters.device == paNoDevice) {
        fprintf(stderr,"Error: No default input device.\n");
        goto done;
    }
    inputParameters.channelCount = 2;                    /* stereo input */
    inputParameters.sampleFormat = PA_SAMPLE_TYPE;
    inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
    inputParameters.hostApiSpecificStreamInfo = NULL;

    /* Record some audio. -------------------------------------------- */
    err = Pa_OpenStream(
              &stream,
              &inputParameters,
              NULL,                  /* &outputParameters, */
              SAMPLE_RATE,
              FRAMES_PER_BUFFER,
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
              recordCallback,
              &data );
    if( err != paNoError ) goto done;

    err = Pa_StartStream( stream );
    if( err != paNoError ) goto done;
    printf("\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout);

    while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
    {
        Pa_Sleep(1000);
        printf("index = %d\n", data.frameIndex ); fflush(stdout);
    }
    if( err < 0 ) goto done;

    err = Pa_CloseStream( stream );
      if( err != paNoError ) goto done;


 /*Save samples in myData array*/

     for(j=0;j<numSamples; j++){
myData[i] =(char*)( data.recordedSamples[i] );
}


/* Write recorded data to a file. */
#if WRITE_TO_FILE
    {
        FILE  *fid;

        fid = fopen("/home/acer/Masaüstü/recordedson.raw", "wb");
        if( fid == NULL )
        {
            printf("Could not open file.");
        }
        else
        {
            fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid );
            fclose( fid );
            printf("Wrote data to 'recorded.raw'\n");
        }

/*    }
#endif

#if WRITE_TO_WAV_FILE
    {
*/
int sampleRate = 44100; 

int bitsPerSample = 16;

int subchunk1size = 16;

int numChannels = 1;

//int subchunk2size = numSamples * numChannels * bitsPerSample/8;
int subchunk2size = myDataSize * numChannels ;

int chunksize = 36+subchunk2size;

int audioFormat = 1;

int byteRate = sampleRate *numChannels*bitsPerSample/8;

int blockAlign = numChannels*bitsPerSample/8;

     fstream myFile ( "/home/acer/Masaüstü/kaydett.wav", ios::out |ios::binary);

                        myFile.seekp( 0, ios::beg ); 
            myFile.write( "RIFF", 4 );
            myFile.write( (char*) &chunksize, 4 );
            myFile.write( "WAVE", 4 );
            myFile.write( "fmt ", 4 );
            myFile.write( (char*) &subchunk1size, 4 );
            myFile.write( (char*) &audioFormat, 2 );
            myFile.write( (char*) &numChannels, 2 );
            myFile.write( (char*) &sampleRate, 4 );
            myFile.write( (char*) &byteRate, 4 );
            myFile.write( (char*) &blockAlign, 2 );
            myFile.write( (char*) &bitsPerSample, 2 );
            myFile.write( "data", 4 );
            //myFile.write( (char*) &myDataSize, 4 );
            myFile.write( (char*) &subchunk2size,4);
            myFile.write( myData, myDataSize);

}

#endif