-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdm.c
67 lines (57 loc) · 1.5 KB
/
sdm.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
pcspkr - listen to pcm sound over the internal pc speaker
Written in 2014 by <Ahmet Inan> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdio.h>
#include "pcspkr.h"
int sigma_delta_modulation(int x, int order)
{
static int sum;
if (!order)
sum = x;
int y = sum >= 0;
int e = (y << 24) - (1 << 23);
if (order >= 2) {
static int sum2;
sum2 += x - e;
x = sum2;
}
sum += x - e;
return y;
}
int integrator_cascade(int x)
{
static int sum0, sum1, sum2;
return sum2 += (sum1 += (sum0 += x));
}
int comb_cascade(int x)
{
static int prv2, prv1, prv0;
int tmp = x;
tmp -= prv0; prv0 = x; x = tmp;
tmp -= prv1; prv1 = x; x = tmp;
tmp -= prv2; prv2 = x; x = tmp;
return x;
}
int main(int argc, char **argv)
{
int order = 1;
if (argc == 2)
order = atoi(argv[1]);
set_io_permissions();
install_signal_handlers();
set_realtime_scheduling();
prepare_hardware();
for (short s; fread_unlocked(&s, 2, 1, stdin) == 1;) {
int comb = comb_cascade(s);
for (int i = 0; i < 16; ++i) {
int intp = integrator_cascade(comb);
comb = 0;
move_speaker(sigma_delta_modulation(intp, order));
}
}
disable_speaker_and_counter();
return 0;
}