-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathgenwave.c
35 lines (29 loc) · 814 Bytes
/
genwave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <math.h>
#include <stdio.h>
#include "wave.h"
#ifndef M_PI
# define M_PI 3.1415926535897932384
#endif
/* Write a sine wave to outFile. */
static void genSineWave(waveFile outFile, int sampleRate, int period, int amplitude, int numPeriods) {
int i, j;
short value;
double x;
for (i = 0; i < numPeriods; i++) {
for (j = 0; j < period; j++) {
x = (double)j * (2.0 * M_PI) / period;
value = (short)(amplitude * sin(x));
writeToWaveFile(outFile, &value, 1);
}
}
}
int main(int argc, char** argv) {
int sampleRate = 96000;
int freq = 200;
int period = sampleRate / freq;
int amplitude = 6000;
waveFile outFile = openOutputWaveFile("out.wav", sampleRate, 1);
genSineWave(outFile, sampleRate, period, amplitude, 500);
closeWaveFile(outFile);
return 0;
}