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

+ 6
- 6
src/main.c View File

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

+ 4
- 0
src/obj.c View File

361
                 break;
361
                 break;
362
         }
362
         }
363
 
363
 
364
+        if (!objs[i].active) {
365
+            continue;
366
+        }
367
+
364
         spr_draw(objs[i].sprite, FLIP_NONE, objs[i].off_x >> POS_SCALE_OBJS, objs[i].off_y >> POS_SCALE_OBJS, 0, hiwater);
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
     }
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
 static void play_drum(enum drums drum) NONBANKED {
86
 static void play_drum(enum drums drum) NONBANKED {
69
     switch (drum) {
87
     switch (drum) {
70
         case dKick:
88
         case dKick:
98
 }
116
 }
99
 
117
 
100
 void snd_music_off(void) BANKED {
118
 void snd_music_off(void) BANKED {
119
+    CRITICAL {
120
+        music = NULL;
121
+    }
122
+}
123
+
124
+void snd_note_off(void) BANKED {
101
     play_note(SILENCE);
125
     play_note(SILENCE);
126
+    play_note2(SILENCE);
102
 }
127
 }
103
 
128
 
104
 static void play_current_note(void) NONBANKED {
129
 static void play_current_note(void) NONBANKED {
107
     }
132
     }
108
 
133
 
109
     START_ROM_BANK(bank);
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
     END_ROM_BANK();
150
     END_ROM_BANK();
121
 }
151
 }
122
 
152
 
123
 void snd_menu_music(void) BANKED {
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
 void snd_game_music(void) BANKED {
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
 void snd_gameover_music(void) BANKED {
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
 void snd_play(void) NONBANKED {
191
 void snd_play(void) NONBANKED {
150
     }
194
     }
151
 
195
 
152
     START_ROM_BANK(bank);
196
     START_ROM_BANK(bank);
197
+    CRITICAL {
153
         uint16_t diff = timer_get() - last_t;
198
         uint16_t diff = timer_get() - last_t;
154
         if (diff >= music->duration) {
199
         if (diff >= music->duration) {
155
             off++;
200
             off++;
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
             if (music->drums) {
219
             if (music->drums) {
167
                 if (music->drums[off] != dEND) {
220
                 if (music->drums[off] != dEND) {
168
                     play_drum(music->drums[off]);
221
                     play_drum(music->drums[off]);
171
                 }
224
                 }
172
             }
225
             }
173
         }
226
         }
227
+    }
174
     END_ROM_BANK();
228
     END_ROM_BANK();
175
 }
229
 }

+ 2
- 0
src/sound.h View File

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

+ 1
- 0
src/sound_game.c View File

40
 
40
 
41
 const struct music music_game = {
41
 const struct music music_game = {
42
     .notes = game_music,
42
     .notes = game_music,
43
+    .notes2 = NULL,
43
     .drums = NULL,
44
     .drums = NULL,
44
     .duration = 200,
45
     .duration = 200,
45
 };
46
 };

+ 1
- 6
src/sound_menu.c View File

29
 BANKREF(sound_menu)
29
 BANKREF(sound_menu)
30
 
30
 
31
 const enum notes menu_music[] = {
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
     SILENCE, SILENCE, END
32
     SILENCE, SILENCE, END
39
 };
33
 };
40
 
34
 
41
 const struct music music_menu = {
35
 const struct music music_menu = {
42
     .notes = menu_music,
36
     .notes = menu_music,
37
+    .notes2 = NULL,
43
     .drums = NULL,
38
     .drums = NULL,
44
     .duration = 200,
39
     .duration = 200,
45
 };
40
 };

+ 53
- 0
src/sound_over.c View File

108
     END
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
 #define DRUM_LOOP \
163
 #define DRUM_LOOP \
112
     dKi, dSI, dSI, dSI, dSI, dSI, dKi, dSI, \
164
     dKi, dSI, dSI, dSI, dSI, dSI, dKi, dSI, \
113
     dSn, dSI, dSI, dSI, dKi, dSI, dSI, dSI \
165
     dSn, dSI, dSI, dSI, dKi, dSI, dSI, dSI \
142
 
194
 
143
 const struct music music_over = {
195
 const struct music music_over = {
144
     .notes = over_notes,
196
     .notes = over_notes,
197
+    .notes2 = over_notes2,
145
     .drums = over_drums,
198
     .drums = over_drums,
146
     .duration = 120,
199
     .duration = 120,
147
 };
200
 };

+ 2
- 0
src/timer.c View File

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

Loading…
Cancel
Save