Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: February 25, 2025, 5:36 am

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating WAV files
#7
RE: Generating WAV files
Solved it myself. Here is the correct program:
Code:
#include <stdint.h>
#include <stdio.h>
#include <math.h>

int main() {
    FILE *wav=fopen("example.wav","wb");
    fprintf(wav,"RIFF");
    const int sampleRate=8192;
    int32_t ChunkSize=36+8*sampleRate*2;
    fwrite(&ChunkSize,4,1,wav);
    fprintf(wav,"WAVEfmt "); //This line has been changed!
    int32_t Subchunk1Size=16; //PCM header is always 16 bytes
    fwrite(&Subchunk1Size,4,1,wav);
    int16_t AudioFormat=1; //PCM
    fwrite(&AudioFormat,2,1,wav);
    int16_t NumChannels=1; //MONO audio.
    fwrite(&NumChannels,2,1,wav);
    int32_t SampleRate=sampleRate;
    fwrite(&SampleRate,4,1,wav);
    int32_t ByteRate=2*sampleRate;
    fwrite(&ByteRate,4,1,wav);
    int16_t BlockAlign=2;
    fwrite(&BlockAlign,2,1,wav);
    int16_t BitsPerSample=16;
    fwrite(&BitsPerSample,2,1,wav);
    fprintf(wav,"data");
    int32_t Subchunk2Size=ChunkSize-36;
    for (int i=0; i<8*sampleRate; i++) {
        float currentFrequency;
        if (i<=sampleRate)
            currentFrequency=262; //The C tone is 262Hz.
        else if (i>sampleRate && i<=2*sampleRate)
            currentFrequency=294; //The D tone is 294Hz.
        else if (i>2*sampleRate && i<=3*sampleRate)
            currentFrequency=330; //The E tone is 330Hz.
        else if (i>3*sampleRate && i<=4*sampleRate)
            currentFrequency=349; //The F tone.
        else if (i>4*sampleRate && i<=5*sampleRate)
            currentFrequency=391; //The G tone.
        else if (i>5*sampleRate && i<=6*sampleRate)
            currentFrequency=440; //The A tone.
        else if (i>6*sampleRate && i<=7*sampleRate)
            currentFrequency=494;  //The H sound.
        else
            currentFrequency=523; //The Tenor C.
        float baseFrequency=sin(2*M_PI*currentFrequency*i/sampleRate)*16384;
        float secondHarmony=sin(2*M_PI*2*currentFrequency*i/sampleRate)*4096;
        float thirdHarmony=sin(2*M_PI*3*currentFrequency*i/sampleRate)*41024;
        float currentAmplitude=baseFrequency+secondHarmony+thirdHarmony*exp(-(float)(i%sampleRate+2000)/2000);
        int16_t numberToBeWritten=currentAmplitude;
        fwrite(&numberToBeWritten,2,1,wav);
    }
    fclose(wav);
}
Basically, I forgot to put the space character somewhere, and the program still compiled, but it didn't do what it was supposed to do.
Reply



Messages In This Thread
Generating WAV files - by FlatAssembler - May 3, 2020 at 11:52 am
RE: Generating WAV files - by Gawdzilla Sama - May 3, 2020 at 11:54 am
RE: Generating WAV files - by BrianSoddingBoru4 - May 3, 2020 at 12:25 pm
RE: Generating WAV files - by zebo-the-fat - May 3, 2020 at 12:30 pm
RE: Generating WAV files - by FlatAssembler - May 3, 2020 at 12:31 pm
RE: Generating WAV files - by zebo-the-fat - May 3, 2020 at 12:33 pm
RE: Generating WAV files - by FlatAssembler - May 4, 2020 at 5:15 am
RE: Generating WAV files - by Ranjr - May 4, 2020 at 9:13 am
RE: Generating WAV files - by FlatAssembler - May 4, 2020 at 1:34 pm

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help unzipping old zip files popeyespappy 29 4082 December 19, 2018 at 8:10 pm
Last Post: ignoramus
  Can't unzip a bunch of files: Oldandeasilyconfused 4 2276 May 23, 2012 at 11:13 pm
Last Post: Oldandeasilyconfused
  Multi Language Subtitle Files Darwinian 2 1299 April 6, 2012 at 4:49 pm
Last Post: Darwinian



Users browsing this thread: 2 Guest(s)