Jump to content
  • Advertisement
Sign in to follow this  

creating wav file

This topic is 2322 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using visual studio.
I was creating a new from scratch wav file using the format described here https://ccrma.stanfo...cts/WaveFormat/
i ended it and my program creates the file but when trying to play it isnt, showing the common message:
Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.
Here is the code its very simple, a the file will play for 1 sec whatever sound it plays from those numbers i putted there, with 48000 hz, 2 channels, 24 bit.
int main()
FILE *fp;
fp = fopen("soundd.wav","wb");
if (fp)
////////////////// FIRST CHUNK ///////////////////
BYTE id[4];
fwrite(&id,sizeof(BYTE),4,fp);//write 'RIFF'
unsigned __int32 n=288036;
fwrite(&n,sizeof(unsigned __int32),1,fp);//write file size

fwrite(&id,sizeof(BYTE),4,fp);//write 'WAVE'
////////////////// SECOND CHUNK ///////////////////
id[0]=(BYTE)'f';id[1]=(BYTE)'m';id[2]=(BYTE)'t';id[3]=(BYTE)' ';
fwrite(&id,sizeof(BYTE),4,fp);//write 'fmt '
n=16; //16 for PCM
fwrite(&id,sizeof(unsigned __int32),1,fp);//write subchunk size

unsigned __int16 l[2];
fwrite(&l,sizeof(__int16),2,fp);//write audio format and num channels
fwrite(&n,sizeof(__int32),1,fp);//write audio sample rate
fwrite(&n,sizeof(__int32),1,fp);//write audio byte rate
fwrite(&l,sizeof(__int16),2,fp);//write audio block align and bit per sample
////////////////// THIRD CHUNK ///////////////////
fwrite(&id,sizeof(BYTE),4,fp);//write 'data'
fwrite(&n,sizeof(__int32),1,fp);//write audio data size

BYTE d[6];
for(int i = 0;i<48000;i++)
d[0]=(BYTE)0x76;d[1]=(BYTE)0x65;d[2]=(BYTE)0x04;//left sound
d[3]=(BYTE)0x76;d[4]=(BYTE)0x65;d[5]=(BYTE)0x04;//right sound
fwrite(&d,sizeof(BYTE),6,fp);//write audio data
fclose (fp);

return 0;

Share this post

Link to post
Share on other sites
right now i spotted a very rare error the I from RIFF is "i" not "I" so now i fixed that but now when i try playing it it says:
Windows Media Player encountered a problem while playing the file.
Dont getsucked by that idiotic error i have there, i wana know if my variables like the integer are corrects, because they haveto be in little endian... or any thing i wana know what is wrong.

Share this post

Link to post
Share on other sites
When you write out the format block what you're essentially writing is a WAVEFORMATEX structure (if the cbSize member is missing it's assumed to be zero). The documentation for that says:

Bits per sample for the wFormatTag format type. If wFormatTag is WAVE_FORMAT_PCM, then wBitsPerSample should be equal to 8 or 16.[/quote]
If wFormatTag is WAVE_FORMAT_EXTENSIBLE, this value can be any integer multiple of 8 and represents the container size, not necessarily the sample size; for example, a 20-bit sample size is in a 24-bit container.[/quote]

Share this post

Link to post
Share on other sites
I dont understand how to use that structure in my program,after reading throug all it.
So after reading that if i use WAVE_FORMAT_PCM i should only use 8 or 16 bit(it doesnt says must use),so i still can use 24 bit, i really dont know.
But still dont know what to do with that, what do i need to change in my program?
I really really wanna use 24 bit audio.

Share this post

Link to post
Share on other sites
any help on whats wrong with my program code? I want someone at least to say me if the format or structure of my way of writing that little program is correct so i can at least know that my mistake is in any wrong letter or number I wrote.

Share this post

Link to post
Share on other sites
i think the best thing is that you load header from valid wav file and then you will be able to use it

#pragma pack( push,1)
struct TWaveHeader {
char RiffID[4];// : array [1..4] of char;
LongWord RiffLen;
char WaveID[4];// : array [1..4] of char;
char FmtID[4];// : array [1..4] of char;
LongWord FmtLen;// : ;
Word FormatTag;// : ;
Word Stereo;// : ;
LongWord Freq;// : ;
LongWord BytesPSec;// : LongWord;
Word BlockAlign;// : ;
Word BitsPer;// : Word;
char DataID[4];// : array [1..[4];// of char;
LongWord DataLen ;// : ;
#pragma pack(pop)

void __fastcall TWave::SaveFile(AnsiString filename)
FILE* f = fopen(filename.c_str(),"wb+");

void __fastcall TWave::OpenFile(AnsiString filename)
int j;
FILE* f = fopen(filename.c_str(),"rb");
// Target
if (shoot_dx == false) {
Data.set_length(HEADER.DataLen / 2);
sData = new unsigned long[HEADER.DataLen / 2];


typedef DynamicArray<short> TDataArray;

struct TWave {
float maxdistance;
float actw;
TWaveHeader HEADER;
TDataArray Data;
unsigned long * sData;
bool shoot_dx;
//TSoundCaptureStream CapturedAudio;// : ;
//TWaveStream WaveStream;// : ;
// __fastcall TWave();
// __fastcall ~TWave();

peace cool.png happy.png rolleyes.gif

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!