-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.c
49 lines (41 loc) · 1.14 KB
/
sim.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "main.h"
int square(int a) {
a &= 0xFF;
if (a > 0x7F)
return 1;
else
return -1;
}
int gen_sim(const unsigned int frame_size, const unsigned long frame_count, const char channels,
unsigned int const * frequencies, unsigned int const * volumes) {
unsigned long size;
unsigned long f, s;
unsigned int c;
unsigned char * wave_out;
unsigned long data_idx = 0;
unsigned long idx;
int mix;
float adjust;
size = (frame_count * frame_size) + 1;
wave_out = calloc(size, sizeof(unsigned char));
if (wave_out == NULL)
return 0;
// For each frame
for (f = 0; f < frame_count - 1; f++) {
// Generate samples with dirty square wave algorithm
for (s = 0; s < frame_size; s++) {
adjust = s / frame_size / 2;
mix = 0;
for (c = 0; c < channels; c++) {
idx = (f * channels) + c;
mix += 64 * (square(0xFF * frequencies[idx] * adjust) * attenuation_lut[(int)volumes[idx]]);
}
wave_out[data_idx++] = (unsigned char)mix;
}
}
FILE * fsim = fopen("psgtalk.raw", "wb");
fwrite(wave_out, size, 1, fsim);
fclose(fsim);
free(wave_out);
return 1;
}