-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.c
More file actions
100 lines (83 loc) · 2.38 KB
/
Copy pathaudio.c
File metadata and controls
100 lines (83 loc) · 2.38 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// audio.c — see audio.h for the module overview.
#include "audio.h"
#include "system.h"
static const Note *s_sequence = 0;
static uint8_t s_sequenceLen = 0;
static uint8_t s_index = 0;
static uint32_t s_noteStartMs = 0;
static bool s_playing = false;
static Note s_singleNoteBuf[1]; // backing storage for audioPlayTone()
static void StartNote(uint8_t index, uint32_t nowMs) {
s_index = index;
s_noteStartMs = nowMs;
systemAudioSetFrequency(s_sequence[index].freqHz);
}
void audioInit(void) {
systemAudioInit();
}
void audioSetVolume(uint8_t vol) {
systemAudioSetVolume(vol);
}
void audioPlayTone(uint16_t freqHz, uint16_t durationMs) {
s_singleNoteBuf[0].freqHz = freqHz;
s_singleNoteBuf[0].durationMs = durationMs;
audioPlaySequence(s_singleNoteBuf, 1);
}
void audioPlaySequence(const Note *notes, uint8_t count) {
if (count == 0) {
audioStop();
return;
}
s_sequence = notes;
s_sequenceLen = count;
s_playing = true;
StartNote(0, systemGetTimeMs());
}
void audioUpdate(uint32_t nowMs) {
if (!s_playing) return;
if (nowMs < s_noteStartMs) return;
if ((nowMs - s_noteStartMs) >= s_sequence[s_index].durationMs) {
uint8_t next = (uint8_t)(s_index + 1);
if (next >= s_sequenceLen) {
audioStop();
} else {
StartNote(next, s_noteStartMs + s_sequence[s_index].durationMs);
}
}
}
void audioStop(void) {
s_playing = false;
s_sequence = 0;
s_sequenceLen = 0;
systemAudioStop();
}
bool audioIsPlaying(void) {
return s_playing;
}
void audioPlaySfxMenuMove(void) {
static const Note sfx[] = { {880, 40} };
audioPlaySequence(sfx, 1);
}
void audioPlaySfxMenuSelect(void) {
static const Note sfx[] = { {659, 50}, {988, 80} };
audioPlaySequence(sfx, 2);
}
void audioPlaySfxGameOver(void) {
static const Note sfx[] = {
{440, 150},
{415, 150},
{392, 150},
{370, 400}
};
audioPlaySequence(sfx, 4);
}
void audioPlaySfxHighScore(void) {
static const Note sfx[] = {
{523, 100}, // C5
{659, 100}, // E5
{784, 100}, // G5
{1046, 200}, // C6
{1046, 200} // C6 (repeat)
};
audioPlaySequence(sfx, 5);
}