Browse Source

try to optimize music playback output

Thomas B 1 month ago
parent
commit
9541c46394
1 changed files with 12 additions and 16 deletions
  1. 12
    16
      src/sound.c

+ 12
- 16
src/sound.c View File

45
 
45
 
46
 static volatile struct music const * music = NULL;
46
 static volatile struct music const * music = NULL;
47
 static volatile uint8_t bank;
47
 static volatile uint8_t bank;
48
+static volatile uint8_t duration;
48
 static volatile uint16_t off = 0;
49
 static volatile uint16_t off = 0;
49
 static volatile uint16_t last_t = 0;
50
 static volatile uint16_t last_t = 0;
50
 
51
 
59
     { .bank = BANK(sound_over), .snd = &music_over }, // SND_GAMEOVER
60
     { .bank = BANK(sound_over), .snd = &music_over }, // SND_GAMEOVER
60
 };
61
 };
61
 
62
 
63
+#define CALL_FREQ_HZ 256
64
+#define MIN(x, y) ((x < y) ? x : y)
65
+#define MAX(x, y) ((x > y) ? x : y)
66
+
62
 static void play_note(enum notes note) NONBANKED {
67
 static void play_note(enum notes note) NONBANKED {
63
     if (note < SILENCE) {
68
     if (note < SILENCE) {
64
         START_ROM_BANK(BANK(sound));
69
         START_ROM_BANK(BANK(sound));
65
             uint16_t freq = frequencies[note];
70
             uint16_t freq = frequencies[note];
66
         END_ROM_BANK();
71
         END_ROM_BANK();
67
 
72
 
68
-        NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
73
+        NR11_REG = 0x80 | duration; // 50% duty, higher value is shorter time (up to 0x3F)
69
         NR12_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
74
         NR12_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
70
         NR13_REG = freq & 0xFF; // given frequency
75
         NR13_REG = freq & 0xFF; // given frequency
71
-        NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
72
-    } else {
73
-        NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
74
-        NR12_REG = 0x00; // silence
75
-        NR13_REG = 0x00; // lowest frequency
76
-        NR14_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
76
+        NR14_REG = 0x80 | 0x40 | ((freq >> 8) & 0x07); // trigger, enable length, upper freq bits
77
     }
77
     }
78
 }
78
 }
79
 
79
 
83
             uint16_t freq = frequencies[note];
83
             uint16_t freq = frequencies[note];
84
         END_ROM_BANK();
84
         END_ROM_BANK();
85
 
85
 
86
-        NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
86
+        NR21_REG = 0x80 | duration; // 50% duty, higher value is shorter time (up to 0x3F)
87
         NR22_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
87
         NR22_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
88
         NR23_REG = freq & 0xFF; // given frequency
88
         NR23_REG = freq & 0xFF; // given frequency
89
-        NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
90
-    } else {
91
-        NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
92
-        NR22_REG = 0x00; // silence
93
-        NR23_REG = 0x00; // lowest frequency
94
-        NR24_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
89
+        NR24_REG = 0x80 | 0x40 | ((freq >> 8) & 0x07); // trigger, enable length, upper freq bits
95
     }
90
     }
96
 }
91
 }
97
 
92
 
105
             break;
100
             break;
106
 
101
 
107
         case dSnare:
102
         case dSnare:
108
-            NR41_REG = 0x00; // length timer, higher value is shorter time (up to 0x3F)
109
-            NR42_REG = (conf_get()->music_vol << 4) | 0x01; // initially full volume, then fade sound out
103
+            NR41_REG = 0x10; // length timer, higher value is shorter time (up to 0x3F)
104
+            NR42_REG = (conf_get()->music_vol << 4) | 0x02; // initially full volume, then fade sound out
110
             NR43_REG = 0x46; // frequency distribution
105
             NR43_REG = 0x46; // frequency distribution
111
             NR44_REG = 0xC0; // trigger and enable length
106
             NR44_REG = 0xC0; // trigger and enable length
112
             break;
107
             break;
146
     CRITICAL {
141
     CRITICAL {
147
         music = snds[snd].snd;
142
         music = snds[snd].snd;
148
         bank = snds[snd].bank;
143
         bank = snds[snd].bank;
144
+        duration = 0x3F - MIN((snds[snd].snd->duration >> 2) + 1, 0x3F);
149
         off = 0;
145
         off = 0;
150
         last_t = timer_get();
146
         last_t = timer_get();
151
     }
147
     }

Loading…
Cancel
Save