Fossil SCM

fossil-scm / src / alerts / mkwav.c
Blame History Raw 89 lines
1
/*
2
** This C program was used to generate the "g-minor-triad.wav" file.
3
** A small modification generated the "b-flat.wav" file.
4
**
5
** This code is saved as an historical reference. It is not part
6
** of Fossil.
7
*/
8
#include <stdio.h>
9
#include <math.h>
10
#include <stdlib.h>
11
12
/*
13
** Write a four-byte little-endian integer value to out.
14
*/
15
void write_int4(FILE *out, unsigned int i){
16
unsigned char z[4];
17
z[0] = i&0xff;
18
z[1] = (i>>8)&0xff;
19
z[2] = (i>>16)&0xff;
20
z[3] = (i>>24)&0xff;
21
fwrite(z, 4, 1, out);
22
}
23
24
/*
25
** Write out the WAV file
26
*/
27
void write_wave(
28
const char *zFilename, /* The file to write */
29
unsigned int nData, /* Bytes of data */
30
unsigned char *aData /* 8000 samples/sec, 8 bit samples */
31
){
32
const unsigned char aWavFmt[] = {
33
0x57, 0x41, 0x56, 0x45, /* "WAVE" */
34
0x66, 0x6d, 0x74, 0x20, /* "fmt " */
35
0x10, 0x00, 0x00, 0x00, /* 16 bytes in the "fmt " section */
36
0x01, 0x00, /* FormatTag: WAVE_FORMAT_PCM */
37
0x01, 0x00, /* 1 channel */
38
0x40, 0x1f, 0x00, 0x00, /* 8000 samples/second */
39
0x40, 0x1f, 0x00, 0x00, /* 8000 bytes/second */
40
0x01, 0x00, /* Block alignment */
41
0x08, 0x00, /* bits/sample */
42
0x64, 0x61, 0x74, 0x61, /* "data" */
43
};
44
FILE *out = fopen(zFilename,"wb");
45
if( out==0 ){
46
fprintf(stderr, "cannot open \"%s\" for writing\n", zFilename);
47
exit(1);
48
}
49
fwrite("RIFF", 4, 1, out);
50
write_int4(out, nData+4+20+8);
51
fwrite(aWavFmt, sizeof(aWavFmt), 1, out);
52
write_int4(out, nData);
53
fwrite(aData, nData, 1, out);
54
fclose(out);
55
}
56
57
int main(int argc, char **argv){
58
int i = 0;
59
unsigned char aBuf[800];
60
# define N sizeof(aBuf)
61
# define pitch1 195.9977*2 /* G */
62
# define pitch2 233.0819*2 /* B-flat */
63
# define pitch3 293.6648*2 /* D */
64
while( i<N/2 ){
65
double v;
66
v = 99.0*sin((2*M_PI*pitch3*i)/8000);
67
if( i<200 ){
68
v = v*i/200.0;
69
}else if( i>N-200 ){
70
v = v*(N-i)/200.0;
71
}
72
aBuf[i] = (char)(v+99.0);
73
i++;
74
}
75
while( i<N ){
76
double v;
77
v = 99.0*sin((2*M_PI*pitch1*i)/8000);
78
if( i<200 ){
79
v = v*i/200.0;
80
}else if( i>N-200 ){
81
v = v*(N-i)/200.0;
82
}
83
aBuf[i] = (char)(v+99.0);
84
i++;
85
}
86
write_wave("out.wav", N, aBuf);
87
return 0;
88
}
89

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button