-
Notifications
You must be signed in to change notification settings - Fork 8
/
playback.c
274 lines (239 loc) · 5.01 KB
/
playback.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Thymio-II Firmware
Copyright (C) 2011 Philippe Retornaz <philippe dot retornaz at epfl dot ch>,
Mobots group (http://mobots.epfl.ch), Robotics system laboratory (http://lsro.epfl.ch)
EPFL Ecole polytechnique federale de Lausanne (http://www.epfl.ch)
See authors.txt for more details about other contributors.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <types/types.h>
#include <skel-usb.h>
#include "playback.h"
#include "sound.h"
#include "tone.h"
#include "sd.h"
static int time;
static const struct music * m;
static const struct music * m_start;
static unsigned char l;
static unsigned char generate_events;
// Last note is 0
struct music {
unsigned int freq;
unsigned int duration;
};
static const struct music m_poweron[] =
{
{3400,13},
{4400,7},
{5100,7},
{6400,7},
{7800,7},
{0,0}
};
static const struct music m_poweroff[] =
{
{7800,13},
{6400,7},
{5100,7},
{4400,7},
{3400,7},
{0,0}
};
static const struct music m_button[] =
{
{1100,7},
{2200,7},
{0,0},
};
static const struct music m_button_m[] =
{
{3300,7},
{4400,13},
{6600,7},
{0,0},
};
static const struct music m_freefall[] =
{
{20280/2,2},
{17750/2,1},
{14650/2,2},
{12790/2,2},
{25690/2,1},
{23140/2,1},
{20620/2,2},
{19920/2,1},
{17790/2,2},
{12790/2,2},
{26110/2,1},
{16780/2,1},
{24300/2,2},
{23640/2,1},
{22940/2,1},
{29690/2,2},
{0,0},
};
static const struct music m_tap[] =
{
{6600, 5},
{2750, 4},
{0,0},
};
static const struct music m_f_detect [] =
{
{4400,7},
{5500,7},
{0,0}
};
static const struct music m_f_ok[] =
{
{4400,7},
{5500,7},
{6600,7},
{0,0}
};
void playback_enable_event(void) {
generate_events = 1;
}
void playback_notify_eop(void) {
if(generate_events)
SET_EVENT(EVENT_SOUND_FINISHED);
}
static int playback_buffer(unsigned char * b) {
if(time > 0)
if(--time == 0) {
if(m) {
m++;
if(m->duration) {
time = m->duration;
tone_setup(m->freq);
} else if(!l){
time = 0;
m = 0;
playback_notify_eop();
return 0;
} else {
m = m_start;
time = m->duration;
tone_setup(m->freq);
}
} else {
time = 0;
playback_notify_eop();
return 0;
}
}
tone_fill_buffer(b,SOUND_OBUFSZ);
return 1;
}
void play_note(unsigned int freq, unsigned int duration) {
sound_playback_hold();
m = 0;
// Duration in ~ 0.1s.
tone_setup(freq);
if(duration == 0)
time = -1;
else
time = duration;
sound_playback_enable(playback_buffer);
}
static void play_music(const struct music * p, int loop) {
sound_playback_hold();
m_start = p;
m = p;
l = loop;
tone_setup(m->freq);
time = m->duration ;
sound_playback_enable(playback_buffer);
}
void play_user_sound(char * name) {
if(!name) {
sound_playback_disable();
return;
}
if(!sd_play_file(name, 0))
play_note(A4, 10);
}
static int _play_sound(int number, int loop) {
int ret = 0;
switch(number) {
case SOUND_DISABLE:
sound_playback_disable();
break;
case SOUND_POWERON:
if(!sd_play_file("s0.wav",loop)) {
play_music(m_poweron,loop);
}
break;
case SOUND_POWEROFF:
// Poweroff is a special case, we don't want to have it
// user configurable as the user could mess with the poweroff
// if(!sd_play_file("s1.raw",loop)) {
play_music(m_poweroff,loop);
// }
break;
case SOUND_BUTTON:
if(!sd_play_file("s2.wav",loop)) {
play_music(m_button,loop);
}
break;
case SOUND_BUTTON_M:
if(!sd_play_file("s3.wav",loop)) {
play_music(m_button_m, loop);
}
break;
case SOUND_FREEFALL:
if(!sd_play_file("s4.wav",loop)) {
play_music(m_freefall, loop);
}
break;
case SOUND_TAP:
if(!sd_play_file("s5.wav",loop)) {
play_music(m_tap,loop);
}
break;
case SOUND_F_DETECT:
if(!sd_play_file("s6.wav",loop)) {
play_music(m_f_detect, loop);
}
break;
case SOUND_F_OK:
if(!sd_play_file("s7.wav",loop)) {
play_music(m_f_ok, loop);
}
break;
default:
ret = -1;
}
return ret;
}
int play_sound(int number) {
return _play_sound(number, 0);
}
void play_sound_loop(int number) {
_play_sound(number, 1);
}
void play_sound_block(int number) {
_play_sound(number,0);
if(number == SOUND_DISABLE)
return;
while(m) barrier();
}
void play_frequency(int freq, int time) {
if(time < 0) {
sound_playback_disable();
} else {
if(time > 0 && time < 4)
time = 4;
play_note(((unsigned int) abs(freq)) * 10, time);
}
}