-
Notifications
You must be signed in to change notification settings - Fork 8
/
tone.c
88 lines (63 loc) · 2.19 KB
/
tone.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
/*
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 "tone.h"
#include "sound.h"
unsigned char wave[WAVEFORM_SIZE];
static unsigned int ptr;
static unsigned int skip_d;
static unsigned int counter;
static unsigned int skip;
unsigned long __udiv3216(unsigned long, unsigned int);
void tone_init(void) {
int i;
for(i = 0; i < WAVEFORM_SIZE/2; i++)
wave[i] = (255*i)/(WAVEFORM_SIZE/2);
for(i = WAVEFORM_SIZE/2; i < WAVEFORM_SIZE; i++)
wave[i] = 255 - (255*(i-WAVEFORM_SIZE/2))/(WAVEFORM_SIZE/2);
}
void tone_setup(unsigned int dHz) {
if(dHz < 550)
dHz = 550;
if(dHz > 78120 / 2)
dHz = 78120 / 2;
// Compute the skip value
skip_d = __builtin_divud(__builtin_muluu(dHz, 1420) + 7812/2,7812);
skip = skip_d / 100;
skip_d -= skip*100;
if(skip_d)
skip_d = 142/skip_d;
counter = 0;
// Don't reset ptr as we will have a smoother transistion ...
}
void tone_fill_buffer(unsigned char *b, unsigned int c) {
unsigned int i;
for(i = 0; i < c; i++) {
*b++ = wave[ptr];
ptr += skip;
if(skip_d && (++counter == skip_d)) {
counter = 0;
ptr++;
}
if(ptr > WAVEFORM_SIZE - 1)
ptr -= WAVEFORM_SIZE;
}
}
void tone_set_waveform(int * buf) {
int i;
for(i = 0; i < WAVEFORM_SIZE; i++)
wave[i] = buf[i] + 128;
}