-
Notifications
You must be signed in to change notification settings - Fork 12
/
st-to-bpm.c
69 lines (52 loc) · 1.55 KB
/
st-to-bpm.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
67
68
69
/* Semitone to BPM converter, for ANSI C, by Zoe Blade, 2020-01-16 */
#include <stdio.h>
#include <stdlib.h>
#define SEMITONE 1.059463 /* 2^(1/12), as there are 12 equally
logarithmically spaced semitones
in an octave */
float semitoneToBeatsPerMinute(int beatsPerMinute, int semitones) {
float shiftedBeatsPerMinute;
shiftedBeatsPerMinute = beatsPerMinute;
if (semitones > 0) {
/* Going up */
while (semitones > 0) {
shiftedBeatsPerMinute *= SEMITONE;
semitones--;
}
return shiftedBeatsPerMinute;
} else if (semitones == 0) {
/* Staying still */
return shiftedBeatsPerMinute;
} else {
/* Going down */
semitones *= -1; /* Make semitones a positive equivalent number */
while (semitones > 0) {
shiftedBeatsPerMinute /= SEMITONE;
semitones--;
}
return shiftedBeatsPerMinute;
}
return 0.0;
}
void semitonesToBeatsPerMinute(int beatsPerMinute) {
int semitones;
float shiftedBeatsPerMinute;
for (semitones = -5; semitones < 6; semitones++) {
shiftedBeatsPerMinute = semitoneToBeatsPerMinute(beatsPerMinute, semitones);
printf("%3d\t %+1d\t%7.3f\n", beatsPerMinute, semitones, shiftedBeatsPerMinute);
}
return;
}
int main(int argc, char *argv[]) {
int beatsPerMinute;
printf("BPM\tSemitones\t BPM\n");
printf("===\t=========\t=======\n\n");
if (argc == 1) {
for (beatsPerMinute = 80; beatsPerMinute < 145; beatsPerMinute += 5) {
semitonesToBeatsPerMinute(beatsPerMinute);
}
} else {
semitonesToBeatsPerMinute(atoi(argv[1]));
}
return 0;
}