ヲヲヲ
ヲヲヲ

Reputation: 61

C output Wav file didn't produce any sound

I tried to create a c code that produce 10 sec of C note. But it seem the output .wav file didn't produce any sound.

I'm still new in C programming and it would be helpful if you can point my mistakes.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

//music note
#define C 261.6256

#define TIME 10
#define POINT 20
#define AMP 10000
#define c 5

//wav file header
typedef struct
{
    char ChuckID[4];
    unsigned long ChuckSize;
    char format[4];
    char subChunk1ID[4];
    unsigned long SubChunk1Size;
    unsigned short AudioFormat;
    unsigned short NumChannels;
    unsigned long SampleRate;
    unsigned long ByteRate;
    unsigned short block_allign;
    unsigned short bits_per_sample;
    char data[4];
    unsigned long data_size;

    /*char  riff_tag[4];
    int     riff_length;
    char    wave_tag[4];
    char    fmt_tag[4];
    int     fmt_length;
    short   audio_format;
    short   num_channels;
    int     sample_rate;
    int     byte_rate;
    short   block_align;
    short   bits_per_sample;
    char    data_tag[4];
    int     data_length;*/
} wavheader;

int main(int argc, char **argv)
{
    wavheader wave = {"RIFF",1764036,"WAVE","fmt",16,1,1,44100,176400,4,32,"data",1764000};

    float data;
    float f = C;
    int fs = 44100;
    int k;
    float *buff;

    FILE *out_file = fopen("ongaku.wav","w");
    buff = (float*)malloc(sizeof(float)*fs*TIME);

    for (k = 0; k<(int)(TIME*fs); k++)
    {
        data=AMP*sin(2*M_PI*f*k/fs);
        //printf("%f\n",data);
    }

    fwrite(buff,sizeof(float),fs*TIME,out_file);

    return 0;
}

Upvotes: 3

Views: 203

Answers (2)

Weather Vane
Weather Vane

Reputation: 34585

I have this working with 8-bit data but unsuccessful with 12/16-bit let alone float data. One thing that's essential, is not to hard code buffer sizes in the header. Other points to watch out for are endian-ness (I happened not to need to adjust), and structure packing (ditto). My use of BPS/8 would also come unstuck when working with 12-bit data.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define FREQ    261.6256    // C
//#define FREQ    440.0       // A
#define M_PI    3.14159265358979323846
#define TIME    10
#define AMP     64.0        // don't use max volume
#define MID     128.0       // 8-bit is range 0..255
//#define MID   0.0         // 16-bit is range -32767.. 32767
#define BPS     8
#define CHANNS  1
#define RATE    44100

//wav file header
typedef struct {
    char ChuckID[4];
    unsigned long ChuckSize;
    char format[4];
    char subChunk1ID[4];
    unsigned long SubChunk1Size;
    unsigned short AudioFormat;
    unsigned short NumChannels;
    unsigned long SampleRate;
    unsigned long ByteRate;
    unsigned short block_allign;
    unsigned short bits_per_sample;
    char data[4];
    unsigned long data_size;
} wavheader;

int main(int argc, char **argv)
{
    int k, samples = RATE * TIME;
    double data;
    FILE *out_file;
    unsigned char *buff;
    wavheader wave = {
        "RIFF",
        36 + samples * CHANNS * BPS/8,
        "WAVE",
        "fmt ",         // "fmt" was error in OP
        16, 
        1,
        CHANNS,
        RATE,
        RATE * CHANNS * BPS/8,
        CHANNS * BPS/8,
        BPS,
        "data",
        samples * CHANNS * BPS/8 
        };

    buff = malloc(BPS/8 * samples);
    out_file = fopen("ongaku.wav","w");
    fwrite(&wave, sizeof(wave), 1, out_file);
    for (k=0; k<samples; k++) {
        data = MID + AMP * sin(2 * M_PI * FREQ * TIME * k / (double)samples);
        buff[k] = (unsigned char)floor(data+0.5);
    }
    fwrite(buff, BPS/8, samples, out_file);
    fclose (out_file);
    free (buff);
    return 0;
}

Upvotes: 1

Codeek
Codeek

Reputation: 1624

Put some data into buff, I guess your data variable is holding that value. and after that if everything else is working correctly, use

fflush(out_file);

or use

fclose(out_file);

Upvotes: 1

Related Questions