Click here to Skip to main content
15,889,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi! i want to record audio from microphone. but i couldnt record audio correctly. my program is below. can anyone say what is my wrong?
record is a buffer which is have audio data.

C#
void saveRecord(short *record)
{
    FILE *f;
    int bitsPerSample = 16;
    int subchunk1size = 16;
    int numChannels = 1;
    //int subchunk2size = size *numChannels;
    int subchunk2size = bufnum * numChannels;
    int sampleRate = 44100;
    int chunksize = 36+subchunk2size;
    int audioFormat = 1;
    int byteRate = sampleRate*numChannels*bitsPerSample/8;
    int blockAlign = numChannels*bitsPerSample/8;
    printf("bufnum : %d", bufnum);
    f = fopen("word.wav", "wb");
    fwrite("RIFF",1, 4, f);
    fwrite((char*) &chunksize, 1, 4, f);
    fwrite("WAVE",1, 4, f);
    fwrite("fmt ",1, 4, f);
    fwrite((char*) &subchunk1size, 1, 4, f);
    fwrite((char*) &audioFormat, 1, 2, f);
    fwrite((char*) &numChannels, 1, 2, f);
    fwrite((char*) &sampleRate, 1, 4, f);
    fwrite((char*) &byteRate, 1, 4, f);
    fwrite((char*) &blockAlign, 1, 2, f);
    fwrite((char*) &bitsPerSample, 1, 2, f);
    fwrite("data", 1, 4, f);
    fwrite((char*) &subchunk2size, 1, 4, f);
    fwrite((char *)record, 1, bufnum, f);
    fclose(f);
}
Posted

int sampleRate = 44100;
fwrite((char*) &sampleRate, 1, 4, f);
 
int byteRate = sampleRate*numChannels*bitsPerSample/8;
fwrite((char*) &byteRate, 1, 4, f);
  
int subchunk2size = bufnum * numChannels;
fwrite((char*) &subchunk2size, 1, 4, f);


I think problem it is same with answer1 , sizeof(int) = 2 or 4
 
Share this answer
 
when i changed them what you suggest it results run time error;

C#
void saveRecord(short *record)
{
    FILE *f;
    int bitsPerSample = 16;
    int subchunk1size = 16;
    int numChannels = 1;
    //int subchunk2size = size *numChannels;
    int subchunk2size = bufnum * numChannels;
    int sampleRate = 44100;
    int chunksize = 36+subchunk2size;
    int audioFormat = 1;
    int byteRate = sampleRate*numChannels*bitsPerSample/8;
    int blockAlign = numChannels*bitsPerSample/8;
    printf("bufnum : %d", bufnum);
    f = fopen("word.wav", "wb");
    fwrite("RIFF",1, 4, f);
    fwrite((char*) &chunksize, 1, 4, f);
    fwrite("WAVE",1, 4, f);
    fwrite("fmt ",1, 4, f);
    fwrite((char*) &subchunk1size, 1, 4, f);
    fwrite((char*) &audioFormat, 1, 4, f);
    fwrite((char*) &numChannels, 1, 4, f);
    fwrite((char*) &sampleRate, 1, 4, f);
    fwrite((char*) &byteRate, 1, 4, f);
    fwrite((char*) &blockAlign, 1, 4, f);
    fwrite((char*) &bitsPerSample, 1, 2, f);
    fwrite("data", 1, 4, f);
    fwrite((char*) &subchunk2size, 1, 4, f);
    fwrite((char *)record,1, bufnum, f);
    fclose(f);
}
 
Share this answer
 
Comments
tanakahitori 29-Dec-10 5:19am    
int bitsPerSample = 16;
fwrite((char*) &bitsPerSample, 1, 2, f);

//if you think sizeof(int) = 4 bytes
//why you still only wrote 2 bytes for "bitsPerSample"


and please tell me more where is your problem?
you couldnt play the buffer after the recording is ready?
or you cann't play your "word.wav" in other Audio-player after you saved "word.wav" from buffer?
int bitsPerSample = 16;
fwrite((char*) &bitsPerSample, 1, 2, f);

//if you think sizeof(int) = 4 bytes
//why you still only wrote 2 bytes for "bitsPerSample"

and please tell me more where is your problem?

you couldnt play the buffer after the recording is ready?

or you cann't play your "word.wav" in other Audio-player after you saved "word.wav" from buffer?
 
Share this answer
 
in my project winmm functions are recording audio with buffersize. and i combine them in record array. and i want to save this total audio data in a wav file. my problem is its record but not correct data. its playing differt noises.
 
Share this answer
 
Comments
tanakahitori 29-Dec-10 5:33am    
"my problem is its record" ... Is buffer incorrect? or wave-file incorrect?
int chunksize = 36+subchunk2size;
fwrite((char*) &chunksize, 1, 4, f);
 
int subchunk1size = 16;
fwrite((char*) &subchunk1size, 1, 4, f);
 
//sizeof(int) = 2 , you wrote 4 bytes to file, maybe this is problem 
 
printf("bufnum : %d", bufnum); 
//you called printf, seem your program runs under DOS 16bit-system
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900