C++ help, reading from wav

Discussion in 'Software' started by frsamuel, Nov 17, 2009.

  1. frsamuel

    frsamuel Private E-2

    Hallo
    I am trying to read the contents of an audio file using c++. What i'm trying to do is to get the information from it. I am thinking that i could get some numbers that should represent the amplitude of the signal(so that the sound is composed of many numbers). I am trying to get the numbers, like the example from the description of a Wave file, from this link https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
    I tryed to get the info , but the problem is that i get some FFFFFFEB values, that i don't find in that standard from the link above. Those values are even at the beginning, where they shoudn't be. Did i made a mistake in this program i wrote?
    Thank you very much

    This is the code:
    Code:
    #include <iostream>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
    	const char *g="file.txt";
    	const char *h="out.txt";
    	
    	FILE *file;
    	FILE *pFile;
    	char *buffer;
    	unsigned long fileLen;
    
    	//Open file
    	file = fopen(g, "rb");
    	if (!file){fprintf(stderr, "Unable to open file %s", "file1.WAV");}
    	
    	//Get file length
    	fseek(file, 0, SEEK_END);
    	fileLen=ftell(file);
    	fseek(file, 0, SEEK_SET);
    
    	//Allocate memory
    	buffer=(char *)malloc(sizeof(char)*(fileLen+1));
    	if (!buffer){fprintf(stderr, "Memory error!");fclose(file);}
    
    	//Read file contents into buffer
    	fread(buffer, fileLen, 1, file);
    	fclose(file);
    
    	//Do what ever with buffer
    	//46
     //   for (int c=0;c<fileLen;c++)
        for (int c=0;c<46;c++)
    {
         printf("%.2X ", (int)buffer[c]);
    
         // put an extra space between every 4 bytes
         if (c % 4 == 3)
         {
             printf(" ");
         }
    
         // Display 16 bytes per line
         if (c % 16 == 15)
         {
             printf("\n");
         }
    }
    
    	
    	
    	 pFile = fopen ( h , "wb" );
    	// fwrite (buffer , 1 , sizeof(buffer) , pFile );
    //	  char name [100];
    //for (int c=0;c<fileLen;c++)
    for (int c=0;c<46;c++)
    {
         fprintf(pFile,"%.2X ", (int)buffer[c]);
    
         // put an extra space between every 4 bytes
         if (c % 4 == 3)
         {
             fprintf(pFile," ");
         }
    
         // Display 16 bytes per line
         if (c % 16 == 15)
         {
             fprintf(pFile,"\n");
         }
    }
    
      fclose (pFile);
    	
    		free(buffer);
        return 0;
    }
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds