Browse Source

handle bg music in isr and add second pulse channel

Thomas B 1 month ago
parent
commit
a3f5c04881
9 changed files with 154 additions and 37 deletions
  1. 8
    2
      src/game.c
  2. 6
    6
      src/main.c
  3. 4
    0
      src/obj.c
  4. 77
    23
      src/sound.c
  5. 2
    0
      src/sound.h
  6. 1
    0
      src/sound_game.c
  7. 1
    6
      src/sound_menu.c
  8. 53
    0
      src/sound_over.c
  9. 2
    0
      src/timer.c

+ 8
- 2
src/game.c View File

@@ -60,6 +60,7 @@ enum ACCELERATION {
60 60
 
61 61
 static uint8_t pause_screen(void) NONBANKED {
62 62
     snd_music_off();
63
+    snd_note_off();
63 64
 
64 65
     uint8_t n = 0;
65 66
 
@@ -122,6 +123,7 @@ static void status(uint8_t health, uint8_t power, uint8_t *hiwater) NONBANKED {
122 123
 
123 124
 static void show_explosion(uint16_t power) NONBANKED {
124 125
     snd_music_off();
126
+    snd_note_off();
125 127
     sample_play_explosion_ship();
126 128
 
127 129
     for (uint8_t n = 0; n < (4 * 4 * 4); n++) {
@@ -136,6 +138,9 @@ static void show_explosion(uint16_t power) NONBANKED {
136 138
 }
137 139
 
138 140
 int32_t game(void) NONBANKED {
141
+    snd_music_off();
142
+    snd_note_off();
143
+
139 144
     disable_interrupts();
140 145
     DISPLAY_OFF;
141 146
     map_game();
@@ -166,11 +171,9 @@ int32_t game(void) NONBANKED {
166 171
     DISPLAY_ON;
167 172
     enable_interrupts();
168 173
 
169
-    snd_music_off();
170 174
     snd_game_music();
171 175
 
172 176
     while(1) {
173
-        snd_play();
174 177
         key_read();
175 178
 
176 179
         enum ACCELERATION acc = 0;
@@ -333,6 +336,9 @@ int32_t game(void) NONBANKED {
333 336
                 break;
334 337
             }
335 338
 
339
+            // restart bg music
340
+            snd_game_music();
341
+
336 342
             // re-draw ship sprite
337 343
             redraw = 1;
338 344
         }

+ 6
- 6
src/main.c View File

@@ -69,7 +69,6 @@ static void highscore(uint8_t is_black) NONBANKED {
69 69
     }
70 70
 
71 71
     while (1) {
72
-        snd_play();
73 72
         key_read();
74 73
 
75 74
         if (key_pressed(J_A) || key_pressed(J_B)) {
@@ -90,7 +89,6 @@ static void about_screen(void) NONBANKED {
90 89
     SHOW_WIN;
91 90
 
92 91
     while (1) {
93
-        snd_play();
94 92
         key_read();
95 93
 
96 94
         if (key_pressed(J_A) || key_pressed(J_B) || key_pressed(J_SELECT)) {
@@ -190,6 +188,9 @@ static void splash_anim(uint8_t *hiwater) NONBANKED {
190 188
 }
191 189
 
192 190
 static void splash(void) NONBANKED {
191
+    snd_music_off();
192
+    snd_note_off();
193
+
193 194
     disable_interrupts();
194 195
     DISPLAY_OFF;
195 196
     map_title();
@@ -209,11 +210,9 @@ static void splash(void) NONBANKED {
209 210
     DISPLAY_ON;
210 211
     enable_interrupts();
211 212
 
212
-    snd_music_off();
213 213
     snd_menu_music();
214 214
 
215 215
     while (1) {
216
-        snd_play();
217 216
         key_read();
218 217
 
219 218
         if (key_pressed(J_LEFT)) {
@@ -295,6 +294,9 @@ static void splash(void) NONBANKED {
295 294
 }
296 295
 
297 296
 static uint16_t ask_name(int32_t score) NONBANKED {
297
+    snd_music_off();
298
+    snd_note_off();
299
+
298 300
     disable_interrupts();
299 301
     DISPLAY_OFF;
300 302
     map_title();
@@ -315,7 +317,6 @@ static uint16_t ask_name(int32_t score) NONBANKED {
315 317
     DISPLAY_ON;
316 318
     enable_interrupts();
317 319
 
318
-    snd_music_off();
319 320
     snd_gameover_music();
320 321
 
321 322
     char name[3] = { 'a', 'a', 'a' };
@@ -323,7 +324,6 @@ static uint16_t ask_name(int32_t score) NONBANKED {
323 324
     win_name_draw(convert_name(name[0], name[1], name[2]), score < 0, pos);
324 325
 
325 326
     while (1) {
326
-        snd_play();
327 327
         key_read();
328 328
 
329 329
         if (key_pressed(J_LEFT)) {

+ 4
- 0
src/obj.c View File

@@ -361,6 +361,10 @@ int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *
361 361
                 break;
362 362
         }
363 363
 
364
+        if (!objs[i].active) {
365
+            continue;
366
+        }
367
+
364 368
         spr_draw(objs[i].sprite, FLIP_NONE, objs[i].off_x >> POS_SCALE_OBJS, objs[i].off_y >> POS_SCALE_OBJS, 0, hiwater);
365 369
     }
366 370
 

+ 77
- 23
src/sound.c View File

@@ -65,6 +65,24 @@ static void play_note(enum notes note) NONBANKED {
65 65
     }
66 66
 }
67 67
 
68
+static void play_note2(enum notes note) NONBANKED {
69
+    if (note < SILENCE) {
70
+        START_ROM_BANK(BANK(sound));
71
+        uint16_t freq = frequencies[note];
72
+        END_ROM_BANK();
73
+
74
+        NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
75
+        NR22_REG = 0x70; // half volume, no change
76
+        NR23_REG = freq & 0xFF; // given frequency
77
+        NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
78
+    } else {
79
+        NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
80
+        NR22_REG = 0x10; // 'lowest' volume without pop, no change
81
+        NR23_REG = 0x00; // lowest frequency
82
+        NR24_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
83
+    }
84
+}
85
+
68 86
 static void play_drum(enum drums drum) NONBANKED {
69 87
     switch (drum) {
70 88
         case dKick:
@@ -98,7 +116,14 @@ void snd_init(void) BANKED {
98 116
 }
99 117
 
100 118
 void snd_music_off(void) BANKED {
119
+    CRITICAL {
120
+        music = NULL;
121
+    }
122
+}
123
+
124
+void snd_note_off(void) BANKED {
101 125
     play_note(SILENCE);
126
+    play_note2(SILENCE);
102 127
 }
103 128
 
104 129
 static void play_current_note(void) NONBANKED {
@@ -107,41 +132,60 @@ static void play_current_note(void) NONBANKED {
107 132
     }
108 133
 
109 134
     START_ROM_BANK(bank);
110
-    if (music->notes) {
111
-        if (music->notes[off] != END) {
112
-            play_note(music->notes[off]);
135
+        if (music->notes) {
136
+            if (music->notes[off] != END) {
137
+                play_note(music->notes[off]);
138
+            }
113 139
         }
114
-    }
115
-    if (music->drums) {
116
-        if (music->drums[off] != dEND) {
117
-            play_drum(music->drums[off]);
140
+        if (music->notes2) {
141
+            if (music->notes2[off] != END) {
142
+                play_note(music->notes2[off]);
143
+            }
144
+        }
145
+        if (music->drums) {
146
+            if (music->drums[off] != dEND) {
147
+                play_drum(music->drums[off]);
148
+            }
118 149
         }
119
-    }
120 150
     END_ROM_BANK();
121 151
 }
122 152
 
123 153
 void snd_menu_music(void) BANKED {
124
-    music = &music_menu;
125
-    bank = BANK(sound_menu);
126
-    off = 0;
127
-    last_t = timer_get();
128
-    play_current_note();
154
+    // TODO
155
+    snd_gameover_music();
156
+    return;
157
+
158
+    CRITICAL {
159
+        music = &music_menu;
160
+        bank = BANK(sound_menu);
161
+        off = 0;
162
+        last_t = timer_get();
163
+        play_current_note();
164
+    }
129 165
 }
130 166
 
131 167
 void snd_game_music(void) BANKED {
132
-    music = &music_game;
133
-    bank = BANK(sound_game);
134
-    off = 0;
135
-    last_t = timer_get();
136
-    play_current_note();
168
+    // TODO
169
+    snd_music_off();
170
+    return;
171
+
172
+    CRITICAL {
173
+        music = &music_game;
174
+        bank = BANK(sound_game);
175
+        off = 0;
176
+        last_t = timer_get();
177
+        play_current_note();
178
+    }
137 179
 }
138 180
 
139 181
 void snd_gameover_music(void) BANKED {
140
-    music = &music_over;
141
-    bank = BANK(sound_over);
142
-    off = 0;
143
-    last_t = timer_get();
144
-    play_current_note();
182
+    CRITICAL {
183
+        music = &music_over;
184
+        bank = BANK(sound_over);
185
+        off = 0;
186
+        last_t = timer_get();
187
+        play_current_note();
188
+    }
145 189
 }
146 190
 
147 191
 void snd_play(void) NONBANKED {
@@ -150,6 +194,7 @@ void snd_play(void) NONBANKED {
150 194
     }
151 195
 
152 196
     START_ROM_BANK(bank);
197
+    CRITICAL {
153 198
         uint16_t diff = timer_get() - last_t;
154 199
         if (diff >= music->duration) {
155 200
             off++;
@@ -163,6 +208,14 @@ void snd_play(void) NONBANKED {
163 208
                 }
164 209
             }
165 210
 
211
+            if (music->notes2) {
212
+                if (music->notes2[off] != END) {
213
+                    play_note2(music->notes2[off]);
214
+                } else {
215
+                    off = 0xFFFF;
216
+                }
217
+            }
218
+
166 219
             if (music->drums) {
167 220
                 if (music->drums[off] != dEND) {
168 221
                     play_drum(music->drums[off]);
@@ -171,5 +224,6 @@ void snd_play(void) NONBANKED {
171 224
                 }
172 225
             }
173 226
         }
227
+    }
174 228
     END_ROM_BANK();
175 229
 }

+ 2
- 0
src/sound.h View File

@@ -61,6 +61,7 @@ enum drums {
61 61
 
62 62
 struct music {
63 63
     enum notes * const notes;
64
+    enum notes * const notes2;
64 65
     enum drums * const drums;
65 66
     uint16_t duration;
66 67
 };
@@ -68,6 +69,7 @@ struct music {
68 69
 void snd_init(void) BANKED;
69 70
 
70 71
 void snd_music_off(void) BANKED;
72
+void snd_note_off(void) BANKED;
71 73
 void snd_menu_music(void) BANKED;
72 74
 void snd_game_music(void) BANKED;
73 75
 void snd_gameover_music(void) BANKED;

+ 1
- 0
src/sound_game.c View File

@@ -40,6 +40,7 @@ const enum notes game_music[] = {
40 40
 
41 41
 const struct music music_game = {
42 42
     .notes = game_music,
43
+    .notes2 = NULL,
43 44
     .drums = NULL,
44 45
     .duration = 200,
45 46
 };

+ 1
- 6
src/sound_menu.c View File

@@ -29,17 +29,12 @@
29 29
 BANKREF(sound_menu)
30 30
 
31 31
 const enum notes menu_music[] = {
32
-    C3, C3, G3, G3, A3, A3, G3, SILENCE,
33
-    F3, F3, E3, E3, D3, D3, C3, SILENCE,
34
-    G3, G3, F3, F3, E3, E3, D3, D3,
35
-    G3, G3, F3, F3, E3, E3, D3, D3,
36
-    C3, C3, G3, G3, A3, A3, G3, SILENCE,
37
-    F3, F3, E3, E3, D3, D3, C3, SILENCE,
38 32
     SILENCE, SILENCE, END
39 33
 };
40 34
 
41 35
 const struct music music_menu = {
42 36
     .notes = menu_music,
37
+    .notes2 = NULL,
43 38
     .drums = NULL,
44 39
     .duration = 200,
45 40
 };

+ 53
- 0
src/sound_over.c View File

@@ -108,6 +108,58 @@ const enum notes over_notes[] = {
108 108
     END
109 109
 };
110 110
 
111
+#define ADD_LOOP \
112
+    Ab2, SIL, Ab2, SIL, Ab2, SIL, Ab2, SIL, \
113
+    Ab2, SIL, Ab2, SIL, Ab2, SIL, Ab2, SIL
114
+
115
+#define ADD_LOOP_2 \
116
+     A2, SIL,  A2, SIL,  A2, SIL,  A2, SIL, \
117
+     A2, SIL,  A2, SIL,  A2, SIL,  A2, SIL
118
+
119
+#define ADD_LOOP_3 \
120
+     E2, SIL,  E2, SIL,  E2, SIL,  E2, SIL, \
121
+     E2, SIL,  E2, SIL,  E2, SIL,  E2, SIL
122
+
123
+const enum notes over_notes2[] = {
124
+    ADD_LOOP, ADD_LOOP_2,
125
+    ADD_LOOP, ADD_LOOP_3,
126
+    ADD_LOOP, ADD_LOOP_2,
127
+
128
+    Db3, Db3, Db3, Db3, Db3, Db3, Db3, Db3,
129
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
130
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
131
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
132
+
133
+     D3,  D3,  D3,  D3,  D3,  D3,  D3,  D3,
134
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
135
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
136
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
137
+
138
+    // repeat
139
+
140
+    ADD_LOOP, ADD_LOOP_2,
141
+    ADD_LOOP, ADD_LOOP_3,
142
+    ADD_LOOP, ADD_LOOP_2,
143
+
144
+    Db3, Db3, Db3, Db3, Db3, Db3, Db3, Db3,
145
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
146
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
147
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
148
+
149
+    D3,  D3,  D3,  D3,  D3,  D3,  D3,  D3,
150
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
151
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
152
+    SIL, SIL, SIL, SIL, SIL, SIL, SIL, SIL,
153
+
154
+    // end
155
+
156
+    SILENCE, SILENCE, SILENCE, SILENCE,
157
+    SILENCE, SILENCE, SILENCE, SILENCE,
158
+    SILENCE, SILENCE, SILENCE, SILENCE,
159
+    SILENCE, SILENCE, SILENCE, SILENCE,
160
+    END
161
+};
162
+
111 163
 #define DRUM_LOOP \
112 164
     dKi, dSI, dSI, dSI, dSI, dSI, dKi, dSI, \
113 165
     dSn, dSI, dSI, dSI, dKi, dSI, dSI, dSI \
@@ -142,6 +194,7 @@ const enum drums over_drums[] = {
142 194
 
143 195
 const struct music music_over = {
144 196
     .notes = over_notes,
197
+    .notes2 = over_notes2,
145 198
     .drums = over_drums,
146 199
     .duration = 120,
147 200
 };

+ 2
- 0
src/timer.c View File

@@ -18,6 +18,7 @@
18 18
  */
19 19
 
20 20
 #include "sample.h"
21
+#include "sound.h"
21 22
 #include "timer.h"
22 23
 
23 24
 static volatile uint16_t count = 0;
@@ -26,6 +27,7 @@ static void timer_isr(void) NONBANKED {
26 27
     if ((count & 0x03) == 0) {
27 28
         sample_isr();
28 29
     }
30
+    snd_play();
29 31
     count++;
30 32
 }
31 33
 

Loading…
Cancel
Save