c - Trouble creating and storing data for wave file using 2-D arrays -
recently i've been writing songs wave files. however, decided try new: use 2-d arrays. when run program, however, wave file isn't playing anything. intention create array consists of notes , store array data portion of wave file time goes by, different notes playing (i.e. notes i've assigned).
#include <stdio.h> #include <stdlib.h> #include <math.h> #define 440.00 #define 466.16 #define b 493.88 #define c 523.25 #define cs 554.37 #define d 587.33 #define ds 622.25 #define e 659.25 #define f 698.46 #define fs 739.99 #define g 783.99 #define gs 830.61 int main() { file* fp; fp = fopen("song.wav", "wb"); if (fp == null) { printf("file not exist.\n"); return exit_failure; } char chunkid[4] = "riff", format[4] = "wave", subchunk1id[4] = "fmt ", subchunk2id[4] = "data"; unsigned int chunksize, subchunk1size, subchunk2size; unsigned short int audioformat, numchannels, blockalign, bitspersample; int samplerate, byterate; chunksize = 12 + 24 + 8 - 8 + 5 * 44100 * 2; subchunk1size = 16; audioformat = 1; numchannels = 1; samplerate = 44100; byterate = 2 * samplerate; bitspersample = 16; blockalign = numchannels * bitspersample / 8; subchunk2size = 9 * byterate; int i, j; short int audio[9][9] = {a, b, cs, d, e, d, cs, b, a}; float freq, amplitude = 32700; (i = 0; < 9 * samplerate; i++){ (j = 0; j == i; j++) freq = audio[i][j] * 2.0 * m_pi; *audio[i] = amplitude * sin(freq * / samplerate); } fwrite(chunkid, 4, 1, fp); fwrite(&chunksize, 4, 1, fp); fwrite(format, 4, 1, fp); fwrite(subchunk1id, 4, 1, fp); fwrite(&subchunk1size, 4, 1, fp); fwrite(&audioformat, 2, 1, fp); fwrite(&numchannels, 2, 1, fp); fwrite(&samplerate, 4, 1, fp); fwrite(&byterate, 4, 1, fp); fwrite(&blockalign, 2, 1, fp); fwrite(&bitspersample, 2, 1, fp); fwrite(subchunk2id, 4, 1, fp); fwrite(&subchunk2size, 4, 1, fp); fwrite(audio, 2, 1, fp); fclose(fp); return exit_success; }
not quite sure tune trying write wav. file, few initial errors:
short int audio[9][9] = {a, b, cs, d, e, d, cs, b, a}; this creates 9x9 array a, b, etc. stored in audio[0], , other values initialized 0. later, trying calculate frequencies, of values audio 0.
(i = 0; < 9 * samplerate; i++){ since samplerate = 44100, going step out of bounds of audio array quickly.
*audio[i] = amplitude * sin(freq * / samplerate); what line store computed value int in audio[i][0] after said , done. i'm not sure why have 2 dimensional array when you're not trying it.
happy debugging!
Comments
Post a Comment