1
2
3
4
5
6
7#include <common.h>
8#include <sound.h>
9
10void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
11 uint freq, uint channels)
12{
13 const unsigned short amplitude = 16000;
14 const int period = freq ? sample_rate / freq : 0;
15 const int half = period / 2;
16
17 assert(freq);
18
19
20 if (size % 2)
21 size--;
22
23 while (size) {
24 int i, j;
25
26 for (i = 0; size && i < half; i++) {
27 size -= 2;
28 for (j = 0; j < channels; j++)
29 *data++ = amplitude;
30 }
31 for (i = 0; size && i < period - half; i++) {
32 size -= 2;
33 for (j = 0; j < channels; j++)
34 *data++ = -amplitude;
35 }
36 }
37}
38