Browse Source

experiment with sample volume settings

Thomas B 1 month ago
parent
commit
417212233f
10 changed files with 129 additions and 115 deletions
  1. 2
    20
      src/config.ba0.c
  2. 11
    2
      src/config.h
  3. 4
    8
      src/main.c
  4. 1
    1
      src/main.h
  5. 4
    2
      src/maps.c
  6. 103
    73
      src/sample.c
  7. 0
    2
      src/sample.h
  8. 3
    4
      src/sound.c
  9. 0
    2
      src/sound.h
  10. 1
    1
      src/timer.c

+ 2
- 20
src/config.ba0.c View File

@@ -26,14 +26,7 @@
26 26
 #include "sound.h"
27 27
 #include "config.h"
28 28
 
29
-struct config_mem {
30
-    struct config config;
31
-    struct scores scores[SCORE_NUM * 2];
32
-
33
-    uint32_t crc; // needs to be last
34
-};
35
-
36
-static struct config_mem mem;
29
+struct config_mem mem;
37 30
 
38 31
 BANKREF(config)
39 32
 
@@ -60,23 +53,12 @@ void conf_init(void) BANKED {
60 53
 
61 54
     if (calc_crc() != mem.crc) {
62 55
         mem.config.debug_flags = 0;
63
-        mem.config.sfx_vol = 0x0F;
56
+        mem.config.sfx_vol = 0x03;
64 57
         mem.config.music_vol = 0x07;
65 58
         score_reset();
66 59
     }
67
-
68
-    snd_vol_sfx = mem.config.sfx_vol;
69
-    snd_vol_music = mem.config.music_vol;
70 60
 }
71 61
 
72 62
 void conf_write_crc(void) BANKED {
73 63
     mem.crc = calc_crc();
74 64
 }
75
-
76
-struct scores *conf_scores(void) BANKED {
77
-    return mem.scores;
78
-}
79
-
80
-struct config *conf_get(void) BANKED {
81
-    return &mem.config;
82
-}

+ 11
- 2
src/config.h View File

@@ -44,9 +44,18 @@ struct config {
44 44
 
45 45
 void conf_init(void) BANKED;
46 46
 void conf_write_crc(void) BANKED;
47
-struct scores *conf_scores(void) BANKED;
48
-struct config *conf_get(void) BANKED;
49 47
 
50 48
 BANKREF_EXTERN(config)
51 49
 
50
+struct config_mem {
51
+    struct config config;
52
+    struct scores scores[SCORE_NUM * 2];
53
+
54
+    uint32_t crc; // needs to be last
55
+};
56
+
57
+extern struct config_mem mem;
58
+#define conf_scores() (mem.scores)
59
+#define conf_get() (&mem.config)
60
+
52 61
 #endif // __CONFIG_H__

+ 4
- 8
src/main.c View File

@@ -44,8 +44,8 @@ uint8_t debug_special_value = 0;
44 44
 BANKREF(main)
45 45
 
46 46
 const struct conf_entry conf_entries[CONF_ENTRY_COUNT] = {
47
-    { .name = "sfx-vol",  .var = &snd_vol_sfx,   .max = 0x0F }, // 0
48
-    { .name = "musi-vol", .var = &snd_vol_music, .max = 0x0F }, // 1
47
+    //{ .name = "sfx-vol",  .var = &mem.config.sfx_vol,   .max = 3 },
48
+    { .name = "musi-vol", .var = &mem.config.music_vol, .max = 15 },
49 49
 };
50 50
 
51 51
 const struct debug_entry debug_entries[DEBUG_ENTRY_COUNT] = {
@@ -106,14 +106,14 @@ static void about_screen(void) NONBANKED {
106 106
 static void conf_screen(void) NONBANKED {
107 107
     HIDE_WIN;
108 108
 
109
+    debug_menu_index = 0;
110
+
109 111
     move_win(MINWNDPOSX, MINWNDPOSY);
110 112
     hide_sprites_range(SPR_NUM_START, MAX_HARDWARE_SPRITES);
111 113
     win_conf();
112 114
 
113 115
     SHOW_WIN;
114 116
 
115
-    debug_menu_index = 0;
116
-
117 117
     while (1) {
118 118
         key_read();
119 119
 
@@ -141,8 +141,6 @@ static void conf_screen(void) NONBANKED {
141 141
                 } else {
142 142
                     *conf_entries[debug_menu_index].var = conf_entries[debug_menu_index].max;
143 143
                 }
144
-                conf_get()->music_vol = snd_vol_music;
145
-                conf_get()->sfx_vol = snd_vol_sfx;
146 144
                 conf_write_crc();
147 145
             END_ROM_BANK();
148 146
             win_conf();
@@ -153,8 +151,6 @@ static void conf_screen(void) NONBANKED {
153 151
                 } else {
154 152
                     *conf_entries[debug_menu_index].var = 0;
155 153
                 }
156
-                conf_get()->music_vol = snd_vol_music;
157
-                conf_get()->sfx_vol = snd_vol_sfx;
158 154
                 conf_write_crc();
159 155
             END_ROM_BANK();
160 156
             win_conf();

+ 1
- 1
src/main.h View File

@@ -42,7 +42,7 @@ struct debug_entry {
42 42
 
43 43
 BANKREF_EXTERN(main)
44 44
 
45
-#define CONF_ENTRY_COUNT 2
45
+#define CONF_ENTRY_COUNT 1
46 46
 extern const struct conf_entry conf_entries[CONF_ENTRY_COUNT];
47 47
 
48 48
 extern uint8_t debug_menu_index;

+ 4
- 2
src/maps.c View File

@@ -273,6 +273,7 @@ void win_debug(void) NONBANKED {
273 273
                   title_map_map, 0, BANK(title_map), title_map_MAP_ATTRIBUTES, BANK(title_map));
274 274
 
275 275
     // TODO paging when more options added
276
+    uint8_t off = (10 - DEBUG_ENTRY_COUNT) / 2;
276 277
 
277 278
     str_center("Debug Menu", 0, 0);
278 279
 
@@ -297,7 +298,7 @@ void win_debug(void) NONBANKED {
297 298
             n_len += 2;
298 299
         END_ROM_BANK();
299 300
 
300
-        str(name_buff, (LINE_WIDTH - n_len) * 2, (i * 2) + 3, (debug_menu_index == i) ? 1 : 0);
301
+        str(name_buff, (LINE_WIDTH - n_len) * 2, (i * 2) + 3 + off, (debug_menu_index == i) ? 1 : 0);
301 302
     }
302 303
 }
303 304
 
@@ -307,6 +308,7 @@ void win_conf(void) NONBANKED {
307 308
                   title_map_map, 0, BANK(title_map), title_map_MAP_ATTRIBUTES, BANK(title_map));
308 309
 
309 310
     // TODO paging when more options added
311
+    uint8_t off = (10 - CONF_ENTRY_COUNT) / 2;
310 312
 
311 313
     str_center("Conf Menu", 0, 0);
312 314
 
@@ -327,7 +329,7 @@ void win_conf(void) NONBANKED {
327 329
             n_len += 2;
328 330
         END_ROM_BANK();
329 331
 
330
-        str(name_buff, (LINE_WIDTH - n_len) * 2, (i * 2) + 3, (debug_menu_index == i) ? 1 : 0);
332
+        str(name_buff, (LINE_WIDTH - n_len) * 2, (i * 2) + 3 + off, (debug_menu_index == i) ? 1 : 0);
331 333
     }
332 334
 }
333 335
 

+ 103
- 73
src/sample.c View File

@@ -23,6 +23,9 @@
23 23
  * See <http://www.gnu.org/licenses/>.
24 24
  */
25 25
 
26
+#include "banks.h"
27
+#include "config.h"
28
+#include "gb/hardware.h"
26 29
 #include "sfx_shoot.h"
27 30
 #include "sfx_expl_orb.h"
28 31
 #include "sfx_expl_ship.h"
@@ -30,12 +33,9 @@
30 33
 
31 34
 BANKREF(sample)
32 35
 
33
-static volatile uint8_t play_bank = 1;
34
-static volatile const uint8_t *play_sample = 0;
35
-static volatile uint16_t play_length = 0;
36
-static volatile uint8_t playing = 0;
37
-
38
-uint8_t snd_vol_sfx = 0x00;
36
+static uint8_t play_bank = 1;
37
+static const uint8_t *play_sample = 0;
38
+static uint16_t play_length = 0;
39 39
 
40 40
 struct sfxs {
41 41
     uint8_t bank;
@@ -53,85 +53,115 @@ void sample_play(enum SFXS sfx) BANKED {
53 53
     if (sfx >= SFX_COUNT) {
54 54
         return;
55 55
     }
56
+    if (conf_get()->sfx_vol == 0) {
57
+        return;
58
+    }
56 59
 
57 60
     CRITICAL {
58 61
         play_bank = sfxs[sfx].bank;
59 62
         play_sample = sfxs[sfx].smp;
60 63
         play_length = sfxs[sfx].len;
61
-        playing = 1;
62 64
     }
63 65
 }
64 66
 
65 67
 uint8_t sample_running(void) BANKED {
66
-    return playing;
68
+    return (play_length > 0) ? 1 : 0;
67 69
 }
68 70
 
71
+#if 1
72
+
73
+// TODO C version has a slight 'beep' always? and much worse at lower volumes?
74
+
75
+void sample_isr(void) NONBANKED {
76
+    if (play_length == 0) {
77
+        return;
78
+    }
79
+
80
+    NR51_REG = 0xBB; // turn CH3 off in left and right pan
81
+    NR30_REG = 0x00; // turn DAC off
82
+
83
+    START_ROM_BANK(play_bank);
84
+        // load waveforms
85
+        for (uint8_t i = 0; i < 16; i++) {
86
+            _AUD3WAVERAM[i] = *(play_sample++);
87
+        }
88
+    END_ROM_BANK();
89
+
90
+    NR30_REG = 0x80; // turn DAC on
91
+    NR31_REG = 0xFE; // length of wave, 2nd shortest
92
+    NR32_REG = (4 - conf_get()->sfx_vol) << 5;
93
+    NR33_REG = 0x00; // low freq bits are zero
94
+    NR34_REG = 0xC7; // start, no loop, high freq bits are 111
95
+    NR51_REG = 0xFF; // turn all channels on
96
+
97
+    play_length--;
98
+}
99
+
100
+#else
101
+
102
+// TODO ASM version has less beep at full volume, but also beeping at lower volumes?
103
+
69 104
 void sample_isr(void) NONBANKED NAKED {
70 105
     __asm
71
-    ld hl, #_play_length    ; something left to play?
72
-    ld a, (hl+)
73
-    or (hl)
74
-    jp z, done
75
-
76
-    ld hl, #_play_sample
77
-    ld a, (hl+)
78
-    ld h, (hl)
79
-    ld l, a                 ; HL = current position inside the sample
80
-
81
-    ; load new waveform
82
-    ld a, (#__current_bank) ; save bank and switch
83
-    ld e, a
84
-    ld a, (#_play_bank)
85
-    ld (_rROMB0), a
86
-
87
-    ldh a, (_NR51_REG)
88
-    ld c, a
89
-    and #0b10111011
90
-    ldh (_NR51_REG), a
91
-
92
-    xor a
93
-    ldh (_NR30_REG), a
94
-
95
-    .irp ofs,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
96
-    ld a, (hl+)
97
-    ldh (__AUD3WAVERAM+ofs), a
98
-    .endm
99
-
100
-    ld a, #0x80
101
-    ldh (_NR30_REG), a
102
-    ld a, #0xFE             ; length of wave
103
-    ldh (_NR31_REG), a
104
-    ld a, (_snd_vol_sfx)    ; volume
105
-    swap a                  ; shift vol to upper bits
106
-    ldh (_NR32_REG), a
107
-    xor a                   ; low freq bits are zero
108
-    ldh (_NR33_REG), a
109
-    ld a, #0xC7             ; start; no loop; high freq bits are 111
110
-    ldh (_NR34_REG), a
111
-
112
-    ld a, c
113
-    ldh (_NR51_REG), a
114
-
115
-    ld a, e                 ; restore bank
116
-    ld (_rROMB0), a
117
-
118
-    ld a, l                 ; save current position
119
-    ld (#_play_sample), a
120
-    ld a, h
121
-    ld (#_play_sample+1), a
122
-
123
-    ld hl, #_play_length    ; decrement length variable
124
-    ld a, (hl)
125
-    sub #1
126
-    ld (hl+), a
127
-    ld a, (hl)
128
-    sbc #0
129
-    ld (hl), a
130
-    ret
131
-
132
-done:
133
-    ld a, #0
134
-    ld (_playing), a
135
-    ret z
106
+        ld hl, #_play_length    ; something left to play?
107
+        ld a, (hl+)
108
+        or (hl)
109
+        ret z
110
+
111
+        ld hl, #_play_sample
112
+        ld a, (hl+)
113
+        ld h, (hl)
114
+        ld l, a                 ; HL = current position inside the sample
115
+
116
+                                ; load new waveform
117
+        ld a, (#__current_bank) ; save bank and switch
118
+        ld e, a
119
+        ld a, (#_play_bank)
120
+        ld (_rROMB0), a
121
+
122
+        ldh a, (_NR51_REG)
123
+        ld c, a
124
+        and #0b10111011
125
+        ldh (_NR51_REG), a
126
+
127
+        xor a
128
+        ldh (_NR30_REG), a
129
+
130
+        .irp ofs,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
131
+            ld a, (hl+)
132
+            ldh (__AUD3WAVERAM+ofs), a
133
+        .endm
134
+
135
+        ld a, #0x80
136
+        ldh (_NR30_REG), a
137
+        ld a, #0xFE             ; length of wave
138
+        ldh (_NR31_REG), a
139
+        ld a, #0x20             ; volume
140
+        ldh (_NR32_REG), a
141
+        xor a                   ; low freq bits are zero
142
+        ldh (_NR33_REG), a
143
+        ld a, #0xC7             ; start; no loop; high freq bits are 111
144
+        ldh (_NR34_REG), a
145
+
146
+        ld a, c
147
+        ldh (_NR51_REG), a
148
+
149
+        ld a, e                 ; restore bank
150
+        ld (_rROMB0), a
151
+
152
+        ld a, l                 ; save current position
153
+        ld (#_play_sample), a
154
+        ld a, h
155
+        ld (#_play_sample+1), a
156
+
157
+        ld hl, #_play_length    ; decrement length variable
158
+        ld a, (hl)
159
+        sub #1
160
+        ld (hl+), a
161
+        ld a, (hl)
162
+        sbc #0
163
+        ld (hl), a
164
+        ret
136 165
     __endasm;
137 166
 }
167
+#endif

+ 0
- 2
src/sample.h View File

@@ -37,6 +37,4 @@ void sample_isr(void);
37 37
 
38 38
 BANKREF_EXTERN(sample)
39 39
 
40
-extern uint8_t snd_vol_sfx;
41
-
42 40
 #endif // __SAMPLE_H__

+ 3
- 4
src/sound.c View File

@@ -25,6 +25,7 @@
25 25
  */
26 26
 
27 27
 #include "banks.h"
28
+#include "config.h"
28 29
 #include "timer.h"
29 30
 #include "sound_menu.h"
30 31
 #include "sound_game.h"
@@ -47,8 +48,6 @@ static volatile uint8_t bank;
47 48
 static volatile uint16_t off = 0;
48 49
 static volatile uint16_t last_t = 0;
49 50
 
50
-uint8_t snd_vol_music = 0x00;
51
-
52 51
 struct snds {
53 52
     uint8_t bank;
54 53
     struct music const * snd;
@@ -67,7 +66,7 @@ static void play_note(enum notes note) NONBANKED {
67 66
         END_ROM_BANK();
68 67
 
69 68
         NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
70
-        NR12_REG = (snd_vol_music << 4) | 0x00; // given volume, no change
69
+        NR12_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
71 70
         NR13_REG = freq & 0xFF; // given frequency
72 71
         NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
73 72
     } else {
@@ -85,7 +84,7 @@ static void play_note2(enum notes note) NONBANKED {
85 84
         END_ROM_BANK();
86 85
 
87 86
         NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
88
-        NR22_REG = (snd_vol_music << 4) | 0x00; // given volume, no change
87
+        NR22_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
89 88
         NR23_REG = freq & 0xFF; // given frequency
90 89
         NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
91 90
     } else {

+ 0
- 2
src/sound.h View File

@@ -86,6 +86,4 @@ void snd_play(void);
86 86
 
87 87
 BANKREF_EXTERN(sound)
88 88
 
89
-extern uint8_t snd_vol_music;
90
-
91 89
 #endif // __SOUND_H__

+ 1
- 1
src/timer.c View File

@@ -21,7 +21,7 @@
21 21
 #include "sound.h"
22 22
 #include "timer.h"
23 23
 
24
-static volatile uint16_t count = 0;
24
+static uint16_t count = 0;
25 25
 
26 26
 static void timer_isr(void) NONBANKED {
27 27
     if ((count & 0x03) == 0) {

Loading…
Cancel
Save