wav file fade in out help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: wav file fade in out help

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    1

    wav file fade in out help

    Hi I am after some guidance. The task is to read a WAV file, request a fade in /out time for the track from the user and the do the fade by modifying the binary file then writing it as a new file I have attached my code so far but do not know how to do the fade and how to write it as a new file. Please help!

    ##edited by moderator##

    The code is below:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void main ()
    {
    FILE *oldwav,*newwav;
    char *stgfilename;
    char riff[5];
    char wave[5];
    char fmt[5];
    char data[5];
    
    
    unsigned long int length, binary, stereo, mono, bits, sample, bps, bpsam, bitsps, sound, ldata;
    int s, m, time, count;
    float fadein, track, track1, fadeout;
    
    /* */
    stgfilename=(char*)malloc(81);
    printf("Please enter the file path of your wav file\n\r");
    gets(stgfilename);
    
    oldwav=fopen(stgfilename,"rb+");
    
    if(oldwav==NULL)
    {
    	printf("Cannot find the file specified\n\r");
    	exit(1);
    }
    
    /* To start the RIFF Chunk must be determined*/
    
    fread(riff,1,4,oldwav);
    riff[4]='\0';
    
    printf("header contains %s this is a wav file!!\n\r", riff);
    if (strcmp(riff,"RIFF")!=0)
    {
    	printf("error in header not wav file\n\r");
        fclose(oldwav);
    	exit(1);
    }
    
    
    fread(&length,4,1,oldwav);
    
    printf("The length of the wav file is %lx hex\n\r", length);
    printf("The length of the wav file is %ld decimal\n\r", length);
    
    
    fread(wave,1,4,oldwav);
    wave[5]='\0';
    
    printf("the format header contains %s\n\r", wave);
    if (strcmp(wave,"WAVE")!=0)
    {
    	printf("error not in WAV format\n\r");
        fclose(oldwav);
    	exit(1);
    }
    
    /* Secondly the FORMAT Chunk must be determined*/
    
    fread(fmt,1,4,oldwav);
    fmt[3]='\0';
    fmt[4]='\0';
    
    printf("the format header contains %s\n\r", fmt);
    if (strcmp(fmt,"fmt")!=0)
    {
    	printf("error\n\r");
        fclose(oldwav);
    	exit(1);
    }
    
    fread(&binary,4,1,oldwav);
    
    if (binary!=0X10)
    {
    	printf("error\n\r");
        fclose(oldwav);
    	exit(1);
    }
    printf("header file contains %lx\n\r",binary);
    
    fread(&bits,2,1,oldwav);
    
    if (bits!=0X01)
    {
    	printf("error corrupted file this should be 01 always!\n\r");
    	fclose(oldwav);
    	exit(1);
    }
    printf("Check bytes 8-9 are 01 ---> Correct 01\n\r");
    
    
    fread (&stereo,2,1,oldwav);
    
    if (stereo!=0X02)
    {
    	printf("the file is not stereo\n\r");
    }
    	if (stereo==0x02)
    	{
    	printf("the file is stereo\n\r");
    	s=2;
    	}
    	if (stereo!=0X01)
    	{
    		printf("the file is not mono\n\r");
    	}
    	if (stereo==0x01)
    	{
    	printf("the file is mono\n\r");
    	m=1;
    	}
    
    fread(&sample,4,1,oldwav);
    
    printf("The sample rate of the wav file is %lx hex\n\r", sample);
    printf("The sample rate of the wav file is %ld decimal\n\r", sample);
    
    fread(&bps,4,1,oldwav);
    
    printf("The bytes per second of the wav file is %lx hex\n\r", bps);
    printf("The bytes per second of the wav file is %ld decimal\n\r", bps);
    
    fread(&bpsam,2,1,oldwav);
    
    printf("The bytes per sample of the wav file is %lx hex\n\r", bpsam);
    printf("The bytes per sample of the wav file is %ld decimal\n\r", bpsam);
    
    
    if (bpsam==0x01)
    {
    	printf("this is 8 bit mono!!\n\r");
    	s=1;
    }
    if (bpsam==0X02)
    {
    	printf("this is 8 bit stereo or 16 bit mono!!\n\r");
    	s=2;
    }
    	
    if (bpsam==0X04)
    {
    	printf("this is 16 bit stereo!!\n\r");
    	s=4;	
    }
    
    printf("the value of s is ---> %d this a multiplier for?\n\r",s);
    fread(&bitsps,2,1,oldwav);
    
    printf("the bits per sample of this wav file is %lx hex\n\r", bitsps);
    printf("the bits per sample of this wav file is %ld decimal\n\r", bitsps);
    
    /* Thirdly the DATA Chunk must be determined*/
    
    fread(data,1,4,oldwav);
    data[4]='\0';
    
    if (strcmp(data,"data")!=0)
    {
    	printf("error\n\r");
        fclose(oldwav);
    	exit(1);
    }
    printf("third line contains %s !!\n\r", data);
    
    fread(&sound,4,1,oldwav);
    
    printf("length of data to follow %lx hex\n\r", sound);
    printf("length of data to follow %ld decimal\n\r", sound);
    
    
    	/* Convert the length of the file into seconds */	
    
    	time=sound/(s*sample);
    	printf("time of track = %d seconds\n\r",time);
    
    
    
    	/* Providing the fadein time the user requires*/
    
    	printf("INPUT TIME FOR FADEIN FROM START...\n\r");
    	scanf("%f",&fadein);
    	printf("The fade in time is %f seconds\n\n\r",fadein);
    	track=(fadein*bps);
    	printf("the fadein in bytes = %f\n\r",track);
    
    		/* Check the fadein time to make sure it is not less than or equal to zero*/
    
    		if (fadein<=0)									
    		{												
    			printf("error time can't be less than or equal to zero\n\r");				
    			printf(" INPUT TIME FOR FADEIN FROM START...\n\r");
    			scanf("%f",&fadein);
    			printf("The fade in time is %f seconds\n\n\r",fadein);
    				track=fadein*bps;
    				printf("the fadein in bytes = %f\n\r",track);
    
    
    		}
    
    				if (fadein>=time)									
    		{												
    			printf("error time can't be more than or equal time of track\n\r");				
    			printf(" INPUT TIME FOR FADEIN FROM START...\n\r");
    			scanf("%f",&fadein);
    			printf("The fade in time is %f seconds\n\n\r",fadein);
    				track=fadein*bps;
    				printf("the fadein in bytes = %f\n\r",track);
    		}
    	/* Providing the fadeout time the user requires*/
    
    	printf("INPUT TIME FOR FADEOUT TO START...\n\r");
    	scanf("%f",&fadeout);
    	printf("The fade out time is %f seconds\n\n\r",fadeout);
    	track1=fadeout*bps;
    	printf("the fadeout in bytes = %f\n\r",track1);
    
    		/* Check the fadeout time to make sure it is not less than or equal to zero*/
    
    		if (fadeout>=time)									
    		{												
    			printf("error time can't be more than or equal to the length of the track\n\r");				
    			printf(" INPUT TIME FOR FADEOUT TO START...\n\r");
    			scanf("%f",&fadeout);
    			printf("The fade out time is %f seconds\n\n\r",fadeout);
    				track1=fadeout*bps;
    				printf("the fadeout in bytes = %f\n\r",track1);
    		}
    
    		if (fadeout<=fadein)									
    		{												
    			printf("THIS IS IMPOSSIBLE PLEASE CHECK YOUR FADE OUT TIME\n\r");				
    			printf(" INPUT TIME FOR FADEOUT TO START...\n\r");
    			scanf("%f",&fadeout);
    			printf("The fade out time is %f seconds\n\n\r",fadeout);
    				track1=fadeout*bps;
    				printf("the fadeout in bytes = %f\n\r",track1);
    		}
    		/* Fade in Calculation*/
    
    
    }
    Attached Files Attached Files
    Last edited by Danny; 04-14-2005 at 07:56 PM.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Attaching .txt files is usually OK but I'm paranoid about attachments in general and besides, many people are put off by the very idea of downloading a file just in order to reply to a thread so I extracted the code from the attached file and pasted it inline.
    Danny Kalev

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    http://www.borg.com/~jglatt/tech/wave.htm had a nice description of the file format. Somehow, you have to figure out 'volume' or signal power or whatever it's called and decrease that (hopefully, it can be just a % times a group of bytes).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center