Skip to content

Commit be91761

Browse files
howard0suhexfet
authored andcommitted
Reduce ROM usage of music module (#529)
* Reduce ROM usage by music module 1. Parse the name of tone instead of a table. 2. Only store one scale of freq and caculate others.
1 parent 6183bd4 commit be91761

1 file changed

Lines changed: 57 additions & 42 deletions

File tree

src/music.c

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,6 @@ static struct {u8 note; u8 duration;} Notes[100];
2424
static u8 Volume;
2525
static u8 next_note;
2626
static u8 num_notes;
27-
struct NoteMap {
28-
const char str[4];
29-
u16 note;
30-
};
31-
static const struct NoteMap note_map[] = {
32-
{"xx", 0}, {"a", 220}, {"ax", 233}, {"b", 247},
33-
34-
{"c0", 262}, {"cx0",277}, {"d0", 294}, {"dx0",311}, {"e0", 330}, {"f0", 349},
35-
{"fx0",370}, {"g0", 392}, {"gx0",415}, {"a0", 440}, {"ax0",466}, {"b0", 494},
36-
37-
{"c1", 523}, {"cx1",554}, {"d1", 587}, {"dx1",622}, {"e1", 659}, {"f1", 698},
38-
{"fx1",740}, {"g1", 784}, {"gx1",831}, {"a1", 880}, {"ax1",932}, {"b1", 988},
39-
40-
{"c2", 1047},{"cx2",1109},{"d2", 1175},{"dx2",1245},{"e2", 1319},{"f2", 1397},
41-
{"fx2",1480},{"g2", 1568},{"gx2",1661},{"a2", 1760},{"ax2",1865},{"b2", 1976},
42-
43-
{"c3", 2093},{"cx3",2217},{"d3", 2349},{"dx3",2489},{"e3", 2637},{"f3", 2794},
44-
{"fx3",2960},{"g3", 3136},{"gx3",3322},{"a3", 3520},{"ax3",3729},{"b3", 3951},
45-
46-
{"c4", 4186},{"cx4",4435},{"d4", 4699},{"dx4",4978},{"e4", 5274},{"f4", 5588},
47-
{"fx4",5920},{"g4", 6272},{"gx4",6645},{"a4", 7040},{"ax4",7459},{"b4", 7902},
48-
};
4927

5028
#if NUM_TIMERS > 4
5129
#error "Number of timers is != 4. This will cause the Alarm music to not work properly"
@@ -85,17 +63,62 @@ static u8 playback_device;
8563
#endif
8664
static u8 vibrate;
8765

88-
#define NUM_NOTES (sizeof(note_map) / sizeof(struct NoteMap))
66+
#define NUM_FREQ_ONE_SCALE 12
67+
static const u16 freqs[] = {220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415};
8968

69+
static u16 get_freq(u8 note)
70+
{
71+
u16 freq = freqs[(note - 1) % NUM_FREQ_ONE_SCALE];
72+
for (int j = 0; j < (note - 1) / NUM_FREQ_ONE_SCALE; ++j)
73+
freq *= 2;
74+
75+
return freq;
76+
}
77+
78+
static u8 get_note(const char *value)
79+
{
80+
u8 offset;
81+
s8 level = 0;
82+
u8 upper = 0;
83+
switch(value[0])
84+
{
85+
case 'c': offset = 0; break;
86+
case 'd': offset = 2; break;
87+
case 'e': offset = 4; break;
88+
case 'f': offset = 5; break;
89+
case 'g': offset = 7; break;
90+
case 'a': offset = 9; break;
91+
case 'b': offset = 11; break;
92+
default:
93+
return 0;
94+
}
95+
96+
char b;
97+
if (value[1] == 'x'){
98+
upper = 1;
99+
b = value[2];
100+
}
101+
else if (value[1] == '\0')
102+
b = '\0';
103+
else
104+
b = value[1];
105+
106+
if (b != '\0')
107+
level = b - '0' + 1;
108+
109+
if (level > 5)
110+
return 0;
111+
112+
return NUM_FREQ_ONE_SCALE * level + offset + upper - 8;
113+
}
90114

91115
static int ini_handler(void* user, const char* section, const char* name, const char* value)
92116
{
93-
u16 i;
94117
const char *requested_sec = (const char *)user;
95118
if (strcasecmp(section, requested_sec) == 0) {
96119
#if HAS_EXTENDED_AUDIO
97120
if (strcasecmp("device", name) == 0) {
98-
for (i = 1; i < AUDDEV_LAST; i++) {
121+
for (u16 i = 1; i < AUDDEV_LAST; i++) {
99122
if (strcasecmp(audio_devices[i], value) == 0) {
100123
playback_device = i;
101124
break;
@@ -115,21 +138,18 @@ static int ini_handler(void* user, const char* section, const char* name, const
115138
// The music volume should be controlled by TX volume setting as well as sound.ini
116139
Volume = Transmitter.volume * Volume/10; // = Transmitter.volume * 10 * sound_volume/100;
117140
}
118-
for(i = 0; i < NUM_NOTES; i++) {
119-
if(strcasecmp(note_map[i].str, name) == 0) {
120-
Notes[num_notes].note = i;
121-
Notes[num_notes].duration = atoi(value) / 10; //convert from msec to centi-secs
122-
num_notes++;
123-
return 1;
124-
}
125-
}
141+
Notes[num_notes].note = get_note(name);
142+
Notes[num_notes].duration = atoi(value) / 10; //convert from msec to centi-secs
143+
num_notes++;
144+
return 1;
126145
}
127146
return 1;
128147
}
148+
129149
u16 next_note_cb() {
130150
if (next_note == num_notes)
131151
return 0;
132-
SOUND_SetFrequency(note_map[Notes[next_note].note].note, Volume);
152+
SOUND_SetFrequency(get_freq(Notes[next_note].note), Volume);
133153
return Notes[next_note++].duration * 10;
134154
}
135155

@@ -143,20 +163,15 @@ void MUSIC_Beep(char* note, u16 duration, u16 interval, u8 count)
143163
return;
144164
if(count > sizeof(Notes)/2)
145165
count = sizeof(Notes)/2;
146-
for(i = 0; i < NUM_NOTES; i++) {
147-
if(strcasecmp(note_map[i].str, note) == 0) {
148-
tone = i;
149-
break;
150-
}
151-
}
166+
tone = get_note(note);
152167
num_notes = count*2;
153168
for(i=0; i<count; i++) {
154169
Notes[i*2].note = tone;
155170
Notes[i*2].duration = duration / 10;
156171
Notes[(i*2)+1].note = 0;
157172
Notes[(i*2)+1].duration = interval / 10;
158173
}
159-
SOUND_SetFrequency(note_map[Notes[0].note].note, Volume);
174+
SOUND_SetFrequency(get_freq(Notes[0].note), Volume);
160175
SOUND_Start((u16)Notes[0].duration * 10, next_note_cb, vibrate);
161176
}
162177

@@ -221,7 +236,7 @@ void MUSIC_Play(u16 music)
221236
#endif
222237

223238
if(! num_notes) return;
224-
SOUND_SetFrequency(note_map[Notes[0].note].note, Volume);
239+
SOUND_SetFrequency(get_freq(Notes[next_note].note), Volume);
225240
SOUND_Start((u16)Notes[0].duration * 10, next_note_cb, vibrate);
226241
}
227242

0 commit comments

Comments
 (0)