From 222596b93ec50c6503fef7632279d7e0d6a0985d Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 28 May 2021 21:02:23 +0200 Subject: Add audio configuration macros --- src/apu.c | 125 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 40 deletions(-) diff --git a/src/apu.c b/src/apu.c index 8dd4ce5..eb898c1 100644 --- a/src/apu.c +++ b/src/apu.c @@ -1,23 +1,89 @@ +/* +Copyright (c) 2021 Bad Diode + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE. +*/ + // -// REG_TM0D frequency buffer size -// | | | -// V V V -// -// Timer = 62610 = 65536 - (16777216 / 5734), buf = 96 -// Timer = 63940 = 65536 - (16777216 / 10512), buf = 176 -// Timer = 64282 = 65536 - (16777216 / 13379), buf = 224 -// Timer = 64612 = 65536 - (16777216 / 18157), buf = 304 -// Timer = 64738 = 65536 - (16777216 / 21024), buf = 352 -// Timer = 64909 = 65536 - (16777216 / 26758), buf = 448 -// Timer = 65004 = 65536 - (16777216 / 31536), buf = 528 -// Timer = 65073 = 65536 - (16777216 / 36314), buf = 608 -// Timer = 65118 = 65536 - (16777216 / 40137), buf = 672 -// Timer = 65137 = 65536 - (16777216 / 42048), buf = 704 +// Audio quality configuration. // -// Source: https://deku.gbadev.org/program/sound1.html -#define AUDIO_FREQ 18157 -#define AUDIO_BUF_LEN 304 -#define AUDIO_TIMER 64612 +// The AUDIO_FREQ, AUDIO_BUF_LEN and AUDIO_TIMER depend on each other. The pitch +// table is generated such as the samples are pitched with respect to C4. When +// the pitch table is multiplied by the sampling rate of the original sample, +// the resulting value will be the increment per VSYNC, which must be shifted +// AUDIO_INC_PRECISION to obtain a (20.12) fixed-point increment value. +#if defined(AUDIO_HIFI) +#define AUDIO_FREQ 40137 +#define AUDIO_BUF_LEN 672 +#define AUDIO_TIMER 65118 +#define AUDIO_INC_PRECISION 6 +static u16 pitch_table[120] = { + 53, 56, 59, 63, 67, 71, 75, 79, + 84, 89, 95, 100, 106, 113, 119, 126, + 134, 142, 151, 159, 169, 179, 190, 201, + 213, 226, 239, 253, 269, 285, 302, 319, + 339, 359, 380, 403, 427, 452, 479, 507, + 538, 570, 604, 639, 678, 718, 761, 806, + 854, 905, 958, 1015, 1076, 1140, 1208, 1279, + 1356, 1436, 1522, 1612, 1708, 1810, 1917, 2031, + 2152, 2280, 2416, 2559, 2712, 2873, 3044, 3225, + 3417, 3620, 3835, 4063, 4305, 4561, 4832, 5119, + 5424, 5746, 6088, 6450, 6834, 7240, 7671, 8127, + 8610, 9122, 9665, 10239, 10848, 11493, 12177, 12901, + 13668, 14481, 15342, 16254, 17221, 18245, 19330, 20479, + 21697, 22987, 24354, 25802, 27337, 28962, 30684, 32509, + 34442, 36490, 38660, 40959, 43394, 45975, 48709, 51605, +}; +#elif defined(AUDIO_LOWFI) +#define AUDIO_FREQ 5734 +#define AUDIO_BUF_LEN 96 +#define AUDIO_TIMER 62610 +#define AUDIO_INC_PRECISION 4 +static u16 pitch_table[120] = { + 93, 98, 104, 111, 117, 124, 132, 139, + 148, 157, 166, 176, 186, 197, 209, 222, + 235, 249, 264, 279, 296, 314, 332, 352, + 373, 395, 419, 444, 470, 498, 528, 559, + 593, 628, 665, 705, 747, 791, 839, 888, + 941, 997, 1057, 1119, 1186, 1257, 1331, 1411, + 1494, 1583, 1678, 1777, 1883, 1995, 2114, 2239, + 2373, 2514, 2663, 2822, 2989, 3167, 3356, 3555, + 3767, 3991, 4228, 4479, 4746, 5028, 5327, 5644, + 5979, 6335, 6712, 7111, 7534, 7982, 8456, 8959, + 9492, 10056, 10654, 11288, 11959, 12670, 13424, 14222, + 15068, 15964, 16913, 17919, 18984, 20113, 21309, 22576, + 23919, 25341, 26848, 28445, 30136, 31928, 33827, 35838, + 37969, 40227, 42619, 45153, 47838, 50683, 53697, 56890, + 60273, 63857, 67654, 71677, 75939, 80454, 85238, 90307, +}; +#else +#define AUDIO_FREQ 18157 +#define AUDIO_BUF_LEN 304 +#define AUDIO_TIMER 64612 +#define AUDIO_INC_PRECISION 5 +static u16 pitch_table[120] = { + 59, 62, 66, 70, 74, 78, 83, 88, + 93, 99, 105, 111, 118, 125, 132, 140, + 148, 157, 166, 176, 187, 198, 210, 222, + 236, 250, 264, 280, 297, 315, 333, 353, + 374, 396, 420, 445, 472, 500, 529, 561, + 594, 630, 667, 707, 749, 793, 841, 891, + 944, 1000, 1059, 1122, 1189, 1260, 1335, 1414, + 1498, 1587, 1682, 1782, 1888, 2000, 2119, 2245, + 2379, 2520, 2670, 2829, 2997, 3175, 3364, 3564, + 3776, 4001, 4239, 4491, 4758, 5041, 5341, 5658, + 5995, 6351, 6729, 7129, 7553, 8002, 8478, 8982, + 9517, 10083, 10682, 11317, 11990, 12703, 13459, 14259, + 15107, 16005, 16957, 17965, 19034, 20166, 21365, 22635, + 23981, 25407, 26918, 28519, 30215, 32011, 33915, 35931, + 38068, 40332, 42730, 45271, 47963, 50815, 53837, 57038, +}; +#endif typedef struct Audio { s8 mix_buffer[AUDIO_BUF_LEN * 2]; @@ -51,27 +117,6 @@ typedef struct AudioChannel { u8 filter_len; } AudioChannel; -// Calculated as ((261.6 / 18157) << 17) for C4. If multiplied by sampling rate -// we will have a u32 (15.17) fixed-point number. This should be enough to -// accurately portray samples up to 75300 Hz. -static u16 pitch_table[120] = { - 59, 62, 66, 70, 74, 78, 83, 88, - 93, 99, 105, 111, 118, 125, 132, 140, - 148, 157, 166, 176, 187, 198, 210, 222, - 236, 250, 264, 280, 297, 315, 333, 353, - 374, 396, 420, 445, 472, 500, 529, 561, - 594, 630, 667, 707, 749, 793, 841, 891, - 944, 1000, 1059, 1122, 1189, 1260, 1335, 1414, - 1498, 1587, 1682, 1782, 1888, 2000, 2119, 2245, - 2379, 2520, 2670, 2829, 2997, 3175, 3364, 3564, - 3776, 4001, 4239, 4491, 4758, 5041, 5341, 5658, - 5995, 6351, 6729, 7129, 7553, 8002, 8478, 8982, - 9517, 10083, 10682, 11317, 11990, 12703, 13459, 14259, - 15107, 16005, 16957, 17965, 19034, 20166, 21365, 22635, - 23981, 25407, 26918, 28519, 30215, 32011, 33915, 35931, - 38068, 40332, 42730, 45271, 47963, 50815, 53837, 57038, -}; - IWRAM_CODE void build_adsr(AudioChannel *chan, u16 adsr) { @@ -177,7 +222,7 @@ update_channel(AudioChannel *c, u8 *data, u16 length, u8 pitch, u16 adsr, if (length > 256) { sampling_rate = 44100; } - c->inc = (pitch_table[c->pitch] * sampling_rate) >> 5; + c->inc = (pitch_table[c->pitch] * sampling_rate) >> AUDIO_INC_PRECISION; build_adsr(c, adsr); } -- cgit v1.2.1