Thread: Sound

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx


    I'd like to create a .wav file containing a sound wave that starts at a certain frequency and fades to another frequency.

    I believe that y = sin(2 * pi * x * f) will graph a sine wave of frequency f where x is time in seconds. So, I planned to just vary f.
    Given freq_start & freq_end, then the change in frequency over time wave_length is:
    y = (freq_end - freq_start) / wave_length * x + freq_start
    Right? (Basic y = mx+b line)
    So, I replaced f in the first equation with that. The resulting wave faded down in freqency, hit bottom, and when back up. I just want it to fade downwards. The code that does all this stuff is:
    void write_short(FILE *fp, unsigned short i)
    	int x;
    	for(x = 0; x < 2; x++) fputc((i >> (x * 8)) & 0xFF, fp);
    int main()
    	// Begin writing data:
    	freq_diff = freq_end - freq_start;
    	printf("Frequency difference is %d Hz.\n", freq_diff);
    	for(x = 0; x < SAMPLE_RATE * samp_time; x++)
    		ctime = (double) x / (double) SAMPLE_RATE;
    		hz = (double) freq_diff / (double) samp_time * ctime + (double) freq_start;
    		amp = sin(2.0 * M_PI * ctime * hz);
    		//printf("ctime = %g, hz = %g, amp = %g\n", ctime, hz, amp);
    		//scanf("%d", &y);
    		iamp = amp * 32767.0;
    		write_short(fp, (unsigned short) iamp);
    	return 0;
    Compiles with no errors (Using gcc -Wall -o wavegen.exe wavegen.c) but gives a bad wave, as described above. ctime is gradually going up to the length, in seconds, of the wave file, as it should. Hz is gradually going down from freq_start to freq_end, again, as it should. SAMPLE_RATE is 44100. Yet the wave goes down in pitch, then back up. It should only go down. Full source is attached.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Registered User
    Join Date
    Jul 2002
    Cleaned up a bit:
    Works fine for me Nice program btw :O

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    I don't know what the actual frequencies produced are, nor have I fully investigated this "fix", but this seems to produce steadily increasing or decreasing tones.
          if ( freq_diff < 0 )
             amp = sin(2.0 * M_PI * (samp_time - ctime) * hz);
             amp = sin(2.0 * M_PI * ctime * hz);
    What started me on this (bad habit of throwing code at an issue without understanding what I am doing) was this:

    Perhaps you can do better investigation than my cursory attempt.
    Last edited by Dave_Sinkula; 11-16-2005 at 10:21 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sound lags in multi-thread version
    By VirtualAce in forum Game Programming
    Replies: 23
    Last Post: 08-27-2008, 11:54 AM
  2. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Help me modify my Sound Server
    By zdude in forum C Programming
    Replies: 1
    Last Post: 05-14-2003, 05:15 PM
  5. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM