How can I make Siren sound in C?

367 views Asked by At

I want to make Siren sound which's frequency changing 960Hz and 770Hz every 0.65sec. (in 8sec Wav file) But I have no idea how to build function as I write above. I tried to use 'for(...=0; ... < 0.65; ...++)' every period. But y[0] and y[1] are function, so I'm confused. My final goal is to make siren wav sound, which come from right side to left side.

To say to the point, I want to know how to make frequency changeable 960Hz and 770Hz every 0.65 sec. I'll be thankful to you if you give me advice to achieve my final goal.

As I'm not good at English, if you're hard to understand my Question, plz comment me.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream> 

#pragma once 
#define WAVE_FORMAT_UNKNOWN 0X0000; 
#define WAVE_FORMAT_PCM 0X0001;
#define WAVE_FORMAT_MS_ADPCM 0X0002; 
#define WAVE_FORMAT_IEEE_FLOAT 0X0003; 
#define WAVE_FORMAT_ALAW 0X0006; 
#define WAVE_FORMAT_MULAW 0X0007; 
#define WAVE_FORMAT_IMA_ADPCM 0X0011; 
#define WAVE_FORMAT_YAMAHA_ADPCM 0X0016;
#define WAVE_FORMAT_GSM 0X0031; 
#define WAVE_FORMAT_ITU_ADPCM 0X0040; 
#define WAVE_FORMAT_MPEG 0X0050;
#define WAVE_FORMAT_EXTENSIBLE 0XFFFE;


#define DURATION 8 
#define SAMPLE_RATE 48000
#define CHANNEL 2
#define BIT_RATE 16

typedef struct {
    unsigned char ChunkID[4]; // Contains the letters "RIFF" in ASCII form 
    unsigned int ChunkSize; // This is the size of the rest of the chunk following this number 
    unsigned char Format[4]; // Contains the letters "WAVE" in ASCII form 
} RIFF;

typedef struct {
    unsigned char ChunkID[4]; // Contains the letters "fmt " in ASCII form 
    unsigned int ChunkSize; // 16 for PCM. This is the size of the rest of the Subchunk which follows this number. 
    unsigned short AudioFormat; // PCM = 1 
    unsigned short NumChannels; // Mono = 1, Stereo = 2, etc. 
    unsigned int SampleRate; // 8000, 44100, etc. 
    unsigned int AvgByteRate; // SampleRate * NumChannels * BitsPerSample/8 
    unsigned short BlockAlign; // NumChannels * BitsPerSample/8 
    unsigned short BitPerSample; // 8 bits = 8, 16 bits = 16, etc 
} FMT;

typedef struct {
    char ChunkID[4]; // Contains the letters "data" in ASCII form 
    unsigned int ChunkSize; // NumSamples * NumChannels * BitsPerSample/8 
} DATA;

typedef struct {
    RIFF Riff;
    FMT Fmt;
    DATA Data;
} WAVE_HEADER;



int main() {
    FILE* f_out;
    f_out = fopen("D:\\test.wav", "wb");

    WAVE_HEADER header;
    memcpy(header.Riff.ChunkID, "RIFF", 4);
    header.Riff.ChunkSize = DURATION * SAMPLE_RATE * CHANNEL * BIT_RATE / 8 + 36;
    memcpy(header.Riff.Format, "WAVE", 4);

    memcpy(header.Fmt.ChunkID, "fmt ", 4);
    header.Fmt.ChunkSize = 0x10;
    header.Fmt.AudioFormat = WAVE_FORMAT_PCM;
    header.Fmt.NumChannels = CHANNEL;
    header.Fmt.SampleRate = SAMPLE_RATE;
    header.Fmt.AvgByteRate = SAMPLE_RATE * CHANNEL * BIT_RATE / 8;
    header.Fmt.BlockAlign = CHANNEL * BIT_RATE / 8;
    header.Fmt.BitPerSample = BIT_RATE;

    memcpy(header.Data.ChunkID, "data", 4);
    header.Data.ChunkSize = DURATION * SAMPLE_RATE * CHANNEL * BIT_RATE / 8;

    fwrite(&header, sizeof(header), 1, f_out);

    short y[2]; 
    double high_freq = 960;
    double low_freq = 770;
    for (int i = 0; i < SAMPLE_RATE * DURATION; i++) {
        double level_l = (double)i / (SAMPLE_RATE * DURATION);
        double level_r = 1.0 - level_l;

        y[0] = (short)(30000 * sin(2 * 3.141592 * i * high_freq / SAMPLE_RATE) * level_l);
        y[1] = (short)(30000 * sin(2 * 3.141592 * i * low_freq / SAMPLE_RATE) * level_r);
        fwrite(&y[0], sizeof(short), 1, f_out);
        fwrite(&y[1], sizeof(short), 1, f_out);
    }


    fclose(f_out);
    return 0;
}
1

There are 1 answers

1
Craig Estey On BEST ANSWER

You are outputting each of the two frequencies on alternate samples. That is, a steady tone of one frequency in the left channel and a steady tone of the other frequency in the right channel.

What we need to do is maintain the same frequency for a given sub-duration and flip between them. And, the same frequency is fed into both channels [albeit with different volume levels].

Here's a slight refactor that does that. It is annotated.

I'm not sure about the level you're using (e.g. level_l and level_r). I think it sounds better with them being the same (i.e. the siren gets closer), so I made level_r just be level_l as an option. But, I left the original L/R scaling intact.

Edit: After listening to the above, the siren sounded more like a true [European] siren when I shortened the sub-duration. I'm not sure it's still 0.65 seconds, but it sounded better [to me]

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//#include <iostream>
#include <string.h>
#include <math.h>

//#pragma once
#define WAVE_FORMAT_UNKNOWN 0X0000;
#define WAVE_FORMAT_PCM 0X0001;
#define WAVE_FORMAT_MS_ADPCM 0X0002;
#define WAVE_FORMAT_IEEE_FLOAT 0X0003;
#define WAVE_FORMAT_ALAW 0X0006;
#define WAVE_FORMAT_MULAW 0X0007;
#define WAVE_FORMAT_IMA_ADPCM 0X0011;
#define WAVE_FORMAT_YAMAHA_ADPCM 0X0016;
#define WAVE_FORMAT_GSM 0X0031;
#define WAVE_FORMAT_ITU_ADPCM 0X0040;
#define WAVE_FORMAT_MPEG 0X0050;
#define WAVE_FORMAT_EXTENSIBLE 0XFFFE;

#define DURATION 8
#define SAMPLE_RATE 48000
#define CHANNEL 2
#define BIT_RATE 16

typedef struct {
    unsigned char ChunkID[4];           // Contains the letters "RIFF" in ASCII form
    unsigned int ChunkSize;             // This is the size of the rest of the chunk following this number
    unsigned char Format[4];            // Contains the letters "WAVE" in ASCII form
} RIFF;

typedef struct {
    unsigned char ChunkID[4];           // Contains the letters "fmt " in ASCII form
    unsigned int ChunkSize;             // 16 for PCM. This is the size of the rest of the Subchunk which follows this number.
    unsigned short AudioFormat;         // PCM = 1
    unsigned short NumChannels;         // Mono = 1, Stereo = 2, etc.
    unsigned int SampleRate;            // 8000, 44100, etc.
    unsigned int AvgByteRate;           // SampleRate * NumChannels * BitsPerSample/8
    unsigned short BlockAlign;          // NumChannels * BitsPerSample/8
    unsigned short BitPerSample;        // 8 bits = 8, 16 bits = 16, etc
} FMT;

typedef struct {
    char ChunkID[4];                    // Contains the letters "data" in ASCII form
    unsigned int ChunkSize;             // NumSamples * NumChannels * BitsPerSample/8
} DATA;

typedef struct {
    RIFF Riff;
    FMT Fmt;
    DATA Data;
} WAVE_HEADER;

int
main()
{
    FILE *f_out;

#if 0
    f_out = fopen("D:\\test.wav", "wb");
#else
    f_out = fopen("test.wav", "wb");
#endif

    WAVE_HEADER header;

    memcpy(header.Riff.ChunkID, "RIFF", 4);
    header.Riff.ChunkSize = DURATION * SAMPLE_RATE * CHANNEL * BIT_RATE / 8 + 36;
    memcpy(header.Riff.Format, "WAVE", 4);

    memcpy(header.Fmt.ChunkID, "fmt ", 4);
    header.Fmt.ChunkSize = 0x10;
    header.Fmt.AudioFormat = WAVE_FORMAT_PCM;
    header.Fmt.NumChannels = CHANNEL;
    header.Fmt.SampleRate = SAMPLE_RATE;
    header.Fmt.AvgByteRate = SAMPLE_RATE * CHANNEL * BIT_RATE / 8;
    header.Fmt.BlockAlign = CHANNEL * BIT_RATE / 8;
    header.Fmt.BitPerSample = BIT_RATE;

    memcpy(header.Data.ChunkID, "data", 4);
    header.Data.ChunkSize = DURATION * SAMPLE_RATE * CHANNEL * BIT_RATE / 8;

    fwrite(&header, sizeof(header), 1, f_out);

    short y[2];
    double high_freq = 960;
    double low_freq = 770;

#if 0
    for (int i = 0; i < SAMPLE_RATE * DURATION; i++) {
        double level_l = (double) i / (SAMPLE_RATE * DURATION);
        double level_r = 1.0 - level_l;

        y[0] = (short) (30000 * sin(2 * 3.141592 * i * high_freq / SAMPLE_RATE) * level_l);
        y[1] = (short) (30000 * sin(2 * 3.141592 * i * low_freq / SAMPLE_RATE) * level_r);
        fwrite(&y[0], sizeof(short), 1, f_out);
        fwrite(&y[1], sizeof(short), 1, f_out);
    }
#endif

#if 1
    // number of samples to flip on
#if 0
    int flipfreq = (SAMPLE_RATE * 65) / 100;
#else
    int flipfreq = (SAMPLE_RATE * 65) / 200;
#endif

    // current frequency to use
    int curtyp = 0;

    for (int i = 0; i < SAMPLE_RATE * DURATION; i++) {
        // after 0.65 seconds, change the frequency
        if ((i % flipfreq) == 0)
            curtyp = ! curtyp;

        // use the frequency for this period
        double cur_freq = curtyp ? high_freq : low_freq;

        double level_l = (double) i / (SAMPLE_RATE * DURATION);
#if 1
        double level_r = 1.0 - level_l;
#else
        double level_r = level_l;
#endif

        y[0] = (short) (30000 * sin(2 * 3.141592 * i * cur_freq / SAMPLE_RATE) * level_l);
        y[1] = (short) (30000 * sin(2 * 3.141592 * i * cur_freq / SAMPLE_RATE) * level_r);
        fwrite(&y[0], sizeof(short), 1, f_out);
        fwrite(&y[1], sizeof(short), 1, f_out);
    }
#endif

    fclose(f_out);
    return 0;
}