|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Loading a Wave File |
|
![]() trondb Member since: 10/2/2002 From: San Francisco, USA |
||||
|
|
||||
| Hi all, I am trying to make a program that sends a wave file from a client to a server. I want to use streaming... So I am trying to use dPawns tutorial to read wavs... Using Visual C++, I get error messages shown below... This is the original from the tutorial... ---------------------------------------------------------- void Load_Wave_File(char *fname) { FILE *fp; fp = fopen(fname,"rb"); if (fp) { BYTE id[4], *sound_buffer; //four bytes to hold 'RIFF' DWORD size; //32 bit value to hold file size short format_tag, channels, block_align, bits_per_sample; //our 16 values DWORD format_length, sample_rate, avg_bytes_sec, data_size, i; //our 32 bit values fread(id, sizeof(BYTE), 4, fp); //read in first four bytes if (!strcmp(id, "RIFF")) { //we had 'RIFF' let's continue fread(size, sizeof(DWORD), 1, fp); //read in 32bit size value fread(id, sizeof(BYTE), 4, fp); //read in 4 byte string now if (!strcmp(id,"WAVE")) { //this is probably a wave file since it contained "WAVE" fread(id, sizeof(BYTE), 4, fp); //read in 4 bytes "fmt "; fread(&format_length, sizeof(DWORD),1,fp); fread(&format_tag, sizeof(short), 1, fp); //check mmreg.h (i think?) for other // possible format tags like ADPCM fread(&channels, sizeof(short),1,fp); //1 mono, 2 stereo fread(&sample_rate, sizeof(DWORD), 1, fp); //like 44100, 22050, etc... fread(&avg_bytes_sec, sizeof(short), 1, fp); //probably won't need this fread(&block_align, sizeof(short), 1, fp); //probably won't need this fread(&bits_per_sample, sizeof(short), 1, fp); //8 bit or 16 bit file? fread(id, sizeof(BYTE), 4, fp); //read in 'data' fread(&data_size, sizeof(DWORD), 1, fp); //how many bytes of sound data we have sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size); //set aside sound buffer space fread(sound_buffer, sizeof(BYTE), data_size, fp); //read in our whole sound data chunk } else printf("Error: RIFF file but not a wave file\n"); } else printf("Error: not a RIFF file\n"); } } --------------------------------------------------------------- REGARDING if (!strcmp(id, "RIFF")) error C2664: 'strcmp' : cannot convert parameter 1 from 'unsigned char [4]' to 'const char *' REGARDING fread(size, sizeof(DWORD), 1, fp); //read in 32bit size value error C2664: 'fread' : cannot convert parameter 1 from 'unsigned long' to 'void *' REGARDING if (!strcmp(id,"WAVE")) same as for error #1.... ------------------------- Later on in my program, I also need to send the WAVE-data over a socket using I want to split the soundbuffer into chunks so i can send it as a stream, so that several clients can connect in the middle of the 'broadcast' and start listening in the middle of the audio-file... So I'm not sure how to store the buffers, and what formats I can use for this... If i have the header-info stored at the server, i can send that to all connecting clients first, and then they can start receiving the audiodata from the position in the wavefile that the server is currently sending.. PLEASE HELP, this is a very important part of my school-project due next Monday, and I am stuck!!! Basically, what I need to learn, is _what datatypes_ I can use for this to read the wave data at the server - and send it over the socket - in order to store a wave file there identical to the one from the server. Thanks alot guys, Tron "That's Tron. He fights for the users." |
||||
|
||||
![]() _Danneman_ Member since: 12/6/2002 From: Porno-Land Sweden |
||||
|
|
||||
| Wow, this article rules!! Just gotta put it to more practice now, to carve the knowledge into my brain. |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| My solution, i AM NOT EXPERT: #include <stdio.h> #include <windows.h> void Load_Wave_File(char); void Load_Wave_File(char *fname) { FILE *fp; fp = fopen(fname,"rb" if (fp) { BYTE id[4], *sound_buffer; //four bytes to hold 'RIFF' DWORD size[32]; //32 bit value to hold file size fread(id,sizeof(BYTE),4,fp); //read in first four bytes char d[]= ""; d[0] = id[0]; d[1] = id[1]; d[2] = id[2]; d[3] = id[3]; //printf("dehors id :%s et d: %s\n",id, d); if (!strcmp(d,"RIFF" { //we had 'RIFF' let's continue fread(size,sizeof(DWORD),1,fp); //read in 32bit size value //read in 32bit size value fread(id,sizeof(BYTE),4,fp); //read in 4 byte string now char d[]= ""; d[0] = id[0]; d[1] = id[1]; d[2] = id[2]; d[3] = id[3]; printf(" id :%s et d: %s\n",id, d); //pour vérifier qu'on entre dans la boucle if (!strcmp(d,"WAVE" { //this is probably a wave file since it contained "WAVE" short format_tag[16], channels[16], block_align[16], bits_per_sample[16]; DWORD format_length[32], sample_rate[32], avg_bytes_sec[32], data_size[32], i[32]; //our 32 bit format info values void *malloc(size_t n); fread(id, sizeof(BYTE), 4, fp); //read in 4 bytes "fmt "; fread(&format_length, sizeof(DWORD),1,fp); fread(&format_tag, sizeof(short), 1, fp); //check mmreg.h (i think?) for other // possible format tags like ADPCM fread(&channels, sizeof(short),1,fp); //1 mono, 2 stereo fread(&sample_rate, sizeof(DWORD), 1, fp); //like 44100, 22050, etc... fread(&avg_bytes_sec, sizeof(short), 1, fp); //probably won't need this fread(&block_align, sizeof(short), 1, fp); //probably won't need this fread(&bits_per_sample, sizeof(short), 1, fp); //8 bit or 16 bit file? fread(id, sizeof(BYTE), 4, fp); //read in 'data' fread(&data_size, sizeof(DWORD), 1, fp); //how many bytes of sound data we have //sound_buffer = (BYTE *) malloc (sizeof(BYTE) * data_size[32]); //set aside sound buffer space //fread(sound_buffer, sizeof(BYTE), data_size[32], fp); //read in our whole sound data chunk printf("c'est un fichier wave id :%s d: %s format: %s compression: %s channels: %s sample rate: %s avg_bytes_sec: %s block_align: %s bits_per_sample: %s", id, d, format_length, format_tag, channels, sample_rate, avg_bytes_sec, block_align, bits_per_sample); } else printf("Error: RIFF file but not a wave file\n" } else printf("Error: not a RIFF file\n" } } main() { char fname[50]; printf("entres le nom du fichier\n" scanf("%s", &fname); Load_Wave_File(fname); } |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
Does sound_buffer now contain the raw data of the wave file? Also can they printed out as strings (how can I see what is in the array) |
||||
|
||||
![]() 1111111111 Member since: 1/18/2006 From: gavirate, Italy |
||||
|
|
||||
| good vay to read wave file ------>>run OK /* must link with winmm.lib. If using Visual C++, go to Build->Settings. Flip to the Link page, and add winmm.lib to the library/object modules. This app shows how to use the MMIO (RIFF) File Parsing functions. It opens, reads, and displays info about the file C:\Windows\Chord.wav. */ #include <windows.h> #include <stdio.h> #include <conio.h> #include <mmsystem.h> #include <malloc.h> /* Name of the WAVE file */ TCHAR WaveName[] = "W:\\AUDIO\\HORN.WAV"; /* WAVEFORMATEX structure for reading in the WAVE fmt chunk */ WAVEFORMATEX WaveFormat; /* Size of waveform data */ DWORD WaveDataSize; // Display strings const char PCM[] = "PCM (uncompressed)"; const char IBM[] = "Adaptive Differential"; const char MuLaw[] = "IBM mu-law"; const char ALaw[] = "IBM a-law"; const char Mono[] = "Mono"; const char Stereo[] = "Stereo"; // ************************** PrintWaveInfo() ***************************** // Prints info on the WAVE. void PrintWaveInfo() { register char * ptr; char buffer[20]; /* Display wave size */ printf("Wave size = %u\n", WaveDataSize); /* Display Format */ switch (WaveFormat.wFormatTag) { case 1: ptr = (char *)&PCM[0]; break; case 0x0101: ptr = (char *)MuLaw[0]; break; case 0x0102: ptr = (char *)ALaw[0]; break; case 0x0103: ptr = (char *)&IBM[0]; break; default: _ultoa(WaveFormat.wFormatTag, &buffer[0], 10); ptr = &buffer[0]; } printf("Format = %s\n", ptr); /* Display bit resolution */ printf("Resolution = %u bit\n", WaveFormat.wBitsPerSample); /* Display number of chans */ switch (WaveFormat.nChannels) { case 1: ptr = (char *)&Mono[0]; break; case 2: ptr = (char *)&Stereo[0]; break; default: _ultoa(WaveFormat.nChannels, &buffer[0], 10); ptr = &buffer[0]; } printf("Number of chans = %s\n", ptr); /* Display sample rate */ printf("Sample rate = %u Hz\n", WaveFormat.nSamplesPerSec); } /* ******************************** main() ******************************** */ int main(int argc, char **argv) { HMMIO hmmio; /* file handle for open file */ MMCKINFO mmckinfoParent; /* parent chunk information structure */ MMCKINFO mmckinfoSubchunk; /* subchunk information structure */ /* Open the file for reading with buffered I/O. Let windows use its default internal buffer */ if ((hmmio = mmioOpen(&WaveName[0], 0, MMIO_READ|MMIO_ALLOCBUF))) { /* Tell Windows to locate a WAVE FileType chunk header somewhere in the file. This marks the start of any embedded WAVE format within the file */ mmckinfoParent.fccType = mmioFOURCC('W', 'A', 'V', 'E'); if (mmioDescend(hmmio, (LPMMCKINFO)&mmckinfoParent, 0, MMIO_FINDRIFF)) { /* Oops! No embedded WAVE format within this file */ printf("This file doesn't contain a WAVE!\n"); /* Close the file and exit with error */ out: mmioClose(hmmio, 0); return(-2); } /* Tell Windows to locate the WAVE's "fmt " chunk, and read in its size field */ mmckinfoSubchunk.ckid = mmioFOURCC('f', 'm', 't', ' '); if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK)) { /* Oops! The required fmt chunk was not found! */ printf("Required fmt chunk was not found!\n"); goto out; } /* Tell Windows to read in the "fmt " chunk into our WAVEFORMATEX structure */ if (mmioRead(hmmio, (HPSTR)&WaveFormat, mmckinfoSubchunk.cksize) != (LRESULT)mmckinfoSubchunk.cksize) { /* Oops! */ printf("An erroring reading the fmt chunk!\n"); goto out; } /* Ascend out of the "fmt " subchunk. If you plan to parse any other chunks in the file, you need to "ascend" out of any chunk that you've mmioDescend()'ed into */ mmioAscend(hmmio, &mmckinfoSubchunk, 0); /* Tell Windows to locate the data chunk. Upon return, the file pointer will be ready to read in the actual waveform data within the data chunk */ mmckinfoSubchunk.ckid = mmioFOURCC('d', 'a', 't', 'a'); if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK)) { /* Oops! */ printf("An erroring reading the data chunk!\n"); goto out; } /* Get the size of the data chunk (ie, the size of the waveform data) */ WaveDataSize = mmckinfoSubchunk.cksize; /* Close the file */ mmioClose(hmmio, 0); /* Print info about the wave */ PrintWaveInfo(); /* Success */ getch(); return(0); } /* Oops! */ printf("Can't find the WAVE file!\n"); getch(); return(-1); } |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| Hi all, this is the first time ive posted on this forum. I am currently studying Computer Games Technology at University, and have been programming in C/C++ and OpenGL for the past year. Over the year I have been using GLUT and plenty of other extension libraries to cut coding corners. However before I go on my placement year, I am eradicating these libraries in favour of basic coding. So far I have replaced GLUT and I am now replacing FMOD. Using OpenAL however means that I have to use ALUT to load in the .wav files, this I am trying to avoid. Googling has resulted in nothing but this Tutorial (that the guy didn't even bother to compile, if it was a tutorial why not???). However like everyone else here its proving difficult, Ive fixed most of the the syntax errors, however when I run the program all I get is white screen and a huge memory leak (despite the fact I the free memory after its loaded into the buffer), please can anyone help! //Sound Loader bool sound::Load(char *Filename) { alGenBuffers(1, &Buffer); if (alGetError() != AL_NO_ERROR) return AL_FALSE; /*alutLoadWAVFile(Filename, &Format, &Data, &Size, &Freq, 0);*/ if (WAV->Load(Filename)) { alBufferData(Buffer,WAV->GetFormat(),WAV->GetData(),WAV->GetSize(), WAV->GetFreq()); } WAV->~wave; |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| This is my first post here too, but I couldn't resist. Just happened upon this thread while searching for something totally unrelated.... anyway to the Anonymous Poster working with OpenAL, this is what I usually use: (modified & commented on the fly, so beware of typos) unsigned int alSource; bool sound::Load setBGM(char* Filename) { // Create 1 source alGenSources(1, &alSource); // Try to create a buffer from the WAV file ALuint alSampleSet=alutCreateBufferFromFile(Filename); if (alSampleSet==AL_NONE) // If it failed, we bail { std::cout < |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| [sorry for the double post, but I screwed the pooch the first time around] This is my first post here too, but I couldn't resist. Just happened upon this thread while searching for something totally unrelated.... anyway to the Anonymous Poster working with OpenAL, this is what I usually use: (modified & commented on the fly, so beware of typos) unsigned int alSource; bool sound::Load setBGM(char* Filename) { // Create 1 source alGenSources(1, &alSource); // Try to create a buffer from the WAV file ALuint alSampleSet=alutCreateBufferFromFile(Filename); if (alSampleSet==AL_NONE) // If it failed, we bail { /* Print some error message */ return false; } alSourcei(alSource, AL_BUFFER, alSampleSet); // Bind the buffer to the source // No Error Checks this way, but it's shorter if you like living dangerously // alSourcei(alSource, AL_BUFFER, alutCreateBufferFromFile(Filename)); // set source properties -- modify 'em for your own purposes alSource3f(alSource,AL_POSITION, 0, 0, 0); alSource3f(alSource,AL_VELOCITY, 0, 0, 0); alSourcei(alSource,AL_LOOPING,AL_TRUE); alSourcei(alSource,AL_SOURCE_RELATIVE,AL_TRUE); alSourcef(alSource,AL_GAIN,0.1f); alSourcef(alSource,AL_ROLLOFF_FACTOR,1.0); alSourcef(alSource,AL_REFERENCE_DISTANCE,30.0); alSourcef(alSource,AL_MAX_DISTANCE,100.0); // play the sound (this code was from my BGM, so it's set to loop and start automatically) alSourcePlay(alSource); return true; } Hope this helps! --=[ AC ]=-- (By the way, AP -- what University are you attending?) |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| Incidentally, if you'd rather have OpenAL load the WAV from a resource file (so you don't have to include a bunch of WAV files with your executable... more like what I was originally looking for) you can use something like this: char* LoadWavRes(const int& ID /* in */, DWORD& size /* out */) // Returns a pointer to WAV data loaded from a resource file // ID is the resource's ID number // size is the size of the WAV data { char* WavData; size=0; HRSRC FR = FindResource(NULL, (char*)ID, "WAVE"); if (!FR) {WinErr(); return NULL;} // WinErr is just an error reporting function (GetLastError) size=SizeofResource(NULL,FR); HGLOBAL LR = LoadResource(NULL, FR); if (!LR) {WinErr(); return NULL;} WavData = (char*)LockResource(LR); FreeResource(LR); // These are obsolete in Win32 and FreeResource(FR); // there might be a better way, but... return WavData; } void setBGM() { DWORD WavSize; char* WavData=LoadWavRes(BGM3, WavSize); if (WavData) { // Create 1 source alGenSources(1, &alSource); ALuint alSampleSet=alutCreateBufferFromFileImage (WavData, WavSize); if (alSampleSet==AL_NONE) {fprintf (stderr, "ALUT error: %s\n", alutGetErrorString (alutGetError ()));} alSourcei(alSource, AL_BUFFER, alSampleSet); //set source properties (same as before) alSourcef(alSource, foo); //play the sound alSourcePlay(alSource); } } --=[ AC ]=-- |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|