Browse Source

add drums to score screen music

Thomas B 1 month ago
parent
commit
bf412c2d46
5 changed files with 123 additions and 63 deletions
  1. 51
    6
      src/sound.c
  2. 35
    55
      src/sound.h
  3. 1
    0
      src/sound_game.c
  4. 1
    0
      src/sound_menu.c
  5. 35
    2
      src/sound_over.c

+ 51
- 6
src/sound.c View File

64
     }
64
     }
65
 }
65
 }
66
 
66
 
67
+static void play_drum(enum drums drum) NONBANKED {
68
+    switch (drum) {
69
+        case dKick:
70
+            NR41_REG = 0x2F; // length timer, higher value is shorter time (up to 0x3F)
71
+            NR42_REG = 0xF0; // initially full volume, no volume changes over time
72
+            NR43_REG = 0x11; // frequency distribution
73
+            NR44_REG = 0xC0; // trigger and enable length
74
+            break;
75
+
76
+        case dSnare:
77
+            NR41_REG = 0x00; // length timer, higher value is shorter time (up to 0x3F)
78
+            NR42_REG = 0xF1; // initially full volume, then fade sound out
79
+            NR43_REG = 0x46; // frequency distribution
80
+            NR44_REG = 0xC0; // trigger and enable length
81
+            break;
82
+
83
+        default:
84
+            break;
85
+    }
86
+}
87
+
67
 void snd_init(void) BANKED {
88
 void snd_init(void) BANKED {
68
     NR52_REG = 0x80; // sound on
89
     NR52_REG = 0x80; // sound on
69
     NR51_REG = 0xFF; // all channels on left and right
90
     NR51_REG = 0xFF; // all channels on left and right
80
 }
101
 }
81
 
102
 
82
 static void play_current_note(void) NONBANKED {
103
 static void play_current_note(void) NONBANKED {
104
+    if (!music) {
105
+        return;
106
+    }
107
+
83
     START_ROM_BANK(bank);
108
     START_ROM_BANK(bank);
84
-        play_note(music->notes[off]);
109
+    if (music->notes) {
110
+        if (music->notes[off] != END) {
111
+            play_note(music->notes[off]);
112
+        }
113
+    }
114
+    if (music->drums) {
115
+        if (music->drums[off] != dEND) {
116
+            play_drum(music->drums[off]);
117
+        }
118
+    }
85
     END_ROM_BANK();
119
     END_ROM_BANK();
86
 }
120
 }
87
 
121
 
118
         uint16_t diff = timer_get() - last_t;
152
         uint16_t diff = timer_get() - last_t;
119
         if (diff >= music->duration) {
153
         if (diff >= music->duration) {
120
             off++;
154
             off++;
121
-            if (music->notes[off] != END) {
122
-                play_note(music->notes[off]);
123
-            } else {
124
-                off = 0xFFFF;
125
-            }
126
             last_t += music->duration;
155
             last_t += music->duration;
156
+
157
+            if (music->notes) {
158
+                if (music->notes[off] != END) {
159
+                    play_note(music->notes[off]);
160
+                } else {
161
+                    off = 0xFFFF;
162
+                }
163
+            }
164
+
165
+            if (music->drums) {
166
+                if (music->drums[off] != dEND) {
167
+                    play_drum(music->drums[off]);
168
+                } else {
169
+                    off = 0xFFFF;
170
+                }
171
+            }
127
         }
172
         }
128
     END_ROM_BANK();
173
     END_ROM_BANK();
129
 }
174
 }

+ 35
- 55
src/sound.h View File

23
 #include <gbdk/platform.h>
23
 #include <gbdk/platform.h>
24
 
24
 
25
 enum notes {
25
 enum notes {
26
-    C0, Cd0, D0, Dd0, E0, F0, Fd0, G0, Gd0, A0, Ad0, B0, //  0 .. 11
27
-    C1, Cd1, D1, Dd1, E1, F1, Fd1, G1, Gd1, A1, Ad1, B1, // 12 .. 23
28
-    C2, Cd2, D2, Dd2, E2, F2, Fd2, G2, Gd2, A2, Ad2, B2, // 24 .. 35
29
-    C3, Cd3, D3, Dd3, E3, F3, Fd3, G3, Gd3, A3, Ad3, B3, // 36 .. 47
30
-    C4, Cd4, D4, Dd4, E4, F4, Fd4, G4, Gd4, A4, Ad4, B4, // 48 .. 59
31
-    C5, Cd5, D5, Dd5, E5, F5, Fd5, G5, Gd5, A5, Ad5, B5, // 60 .. 71
32
-    SILENCE, END,                                        // 72 .. 73
33
-    SIL = SILENCE,
34
-
35
-    Db0 = Cd0,
36
-    Eb0 = Dd0, Ed0 = F0,
37
-    Fb0 = E0,
38
-    Gb0 = Fd0,
39
-    Ab0 = Gd0,
40
-    Bb0 = Ad0,
41
-
42
-    Cb1 = B0,
43
-    Db1 = Cd1,
44
-    Eb1 = Dd1, Ed1 = F1,
45
-    Fb1 = E1,
46
-    Gb1 = Fd1,
47
-    Ab1 = Gd1,
48
-    Bb1 = Ad1,
49
-
50
-    Cb2 = B1,
51
-    Db2 = Cd2,
52
-    Eb2 = Dd2, Ed2 = F2,
53
-    Fb2 = E2,
54
-    Gb2 = Fd2,
55
-    Ab2 = Gd2,
56
-    Bb2 = Ad2,
57
-
58
-    Cb3 = B2,
59
-    Db3 = Cd3,
60
-    Eb3 = Dd3, Ed3 = F3,
61
-    Fb3 = E3,
62
-    Gb3 = Fd3,
63
-    Ab3 = Gd3,
64
-    Bb3 = Ad3,
65
-
66
-    Cb4 = B3,
67
-    Db4 = Cd4,
68
-    Eb4 = Dd4, Ed4 = F4,
69
-    Fb4 = E4,
70
-    Gb4 = Fd4,
71
-    Ab4 = Gd4,
72
-    Bb4 = Ad4,
73
-
74
-    Cb5 = B4,
75
-    Db5 = Cd5,
76
-    Eb5 = Dd5, Ed5 = F5,
77
-    Fb5 = E5,
78
-    Gb5 = Fd5,
79
-    Ab5 = Gd5,
80
-    Bb5 = Ad5,
26
+    C0 = 0, Cd0, D0, Dd0, E0, F0, Fd0, G0, Gd0, A0, Ad0, B0, //  0 .. 11
27
+    C1,     Cd1, D1, Dd1, E1, F1, Fd1, G1, Gd1, A1, Ad1, B1, // 12 .. 23
28
+    C2,     Cd2, D2, Dd2, E2, F2, Fd2, G2, Gd2, A2, Ad2, B2, // 24 .. 35
29
+    C3,     Cd3, D3, Dd3, E3, F3, Fd3, G3, Gd3, A3, Ad3, B3, // 36 .. 47
30
+    C4,     Cd4, D4, Dd4, E4, F4, Fd4, G4, Gd4, A4, Ad4, B4, // 48 .. 59
31
+    C5,     Cd5, D5, Dd5, E5, F5, Fd5, G5, Gd5, A5, Ad5, B5, // 60 .. 71
32
+    SILENCE, END, SIL = SILENCE,                             // 72 .. 73
33
+
34
+              Db0 = Cd0, Eb0 = Dd0, Ed0 = F0,
35
+    Fb0 = E0, Gb0 = Fd0, Ab0 = Gd0, Bb0 = Ad0,
36
+
37
+    Cb1 = B0, Db1 = Cd1, Eb1 = Dd1, Ed1 = F1,
38
+    Fb1 = E1, Gb1 = Fd1, Ab1 = Gd1, Bb1 = Ad1,
39
+
40
+    Cb2 = B1, Db2 = Cd2, Eb2 = Dd2, Ed2 = F2,
41
+    Fb2 = E2, Gb2 = Fd2, Ab2 = Gd2, Bb2 = Ad2,
42
+
43
+    Cb3 = B2, Db3 = Cd3, Eb3 = Dd3, Ed3 = F3,
44
+    Fb3 = E3, Gb3 = Fd3, Ab3 = Gd3, Bb3 = Ad3,
45
+
46
+    Cb4 = B3, Db4 = Cd4, Eb4 = Dd4, Ed4 = F4,
47
+    Fb4 = E4, Gb4 = Fd4, Ab4 = Gd4, Bb4 = Ad4,
48
+
49
+    Cb5 = B4, Db5 = Cd5, Eb5 = Dd5, Ed5 = F5,
50
+    Fb5 = E5, Gb5 = Fd5, Ab5 = Gd5, Bb5 = Ad5,
51
+};
52
+
53
+enum drums {
54
+    dKick = 0, dSnare,
55
+    dSilence,
56
+
57
+    dKi = dKick, dSn = dSnare,
58
+    dSI = dSilence,
59
+    dEND,
81
 };
60
 };
82
 
61
 
83
 struct music {
62
 struct music {
84
     enum notes * const notes;
63
     enum notes * const notes;
64
+    enum drums * const drums;
85
     uint16_t duration;
65
     uint16_t duration;
86
 };
66
 };
87
 
67
 

+ 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
+    .drums = NULL,
43
     .duration = 200,
44
     .duration = 200,
44
 };
45
 };

+ 1
- 0
src/sound_menu.c View File

40
 
40
 
41
 const struct music music_menu = {
41
 const struct music music_menu = {
42
     .notes = menu_music,
42
     .notes = menu_music,
43
+    .drums = NULL,
43
     .duration = 200,
44
     .duration = 200,
44
 };
45
 };

+ 35
- 2
src/sound_over.c View File

28
 
28
 
29
 BANKREF(sound_over)
29
 BANKREF(sound_over)
30
 
30
 
31
-const enum notes over_music[] = {
31
+const enum notes over_notes[] = {
32
     // fanfare
32
     // fanfare
33
     /*
33
     /*
34
      C3,  C3,  C3,  C3, Ab2, Ab2, Bb2, Bb2,
34
      C3,  C3,  C3,  C3, Ab2, Ab2, Bb2, Bb2,
108
     END
108
     END
109
 };
109
 };
110
 
110
 
111
+#define DRUM_LOOP \
112
+    dKi, dSI, dSI, dSI, dSI, dSI, dKi, dSI, \
113
+    dSn, dSI, dSI, dSI, dKi, dSI, dSI, dSI \
114
+
115
+const enum drums over_drums[] = {
116
+    DRUM_LOOP,
117
+    DRUM_LOOP,
118
+    DRUM_LOOP,
119
+    DRUM_LOOP,
120
+    DRUM_LOOP,
121
+    DRUM_LOOP,
122
+    DRUM_LOOP,
123
+    DRUM_LOOP,
124
+    DRUM_LOOP,
125
+    DRUM_LOOP,
126
+
127
+    DRUM_LOOP,
128
+    DRUM_LOOP,
129
+    DRUM_LOOP,
130
+    DRUM_LOOP,
131
+    DRUM_LOOP,
132
+    DRUM_LOOP,
133
+    DRUM_LOOP,
134
+    DRUM_LOOP,
135
+    DRUM_LOOP,
136
+    DRUM_LOOP,
137
+
138
+    dSI, dSI, dSI, dSI, dSI, dSI, dSI, dSI,
139
+    dSI, dSI, dSI, dSI, dSI, dSI, dSI, dSI,
140
+    dEND
141
+};
142
+
111
 const struct music music_over = {
143
 const struct music music_over = {
112
-    .notes = over_music,
144
+    .notes = over_notes,
145
+    .drums = over_drums,
113
     .duration = 120,
146
     .duration = 120,
114
 };
147
 };

Loading…
Cancel
Save