From 08550442afb4867cdf9873ca8272fd5a02e05433 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 26 May 2021 17:31:51 +0200 Subject: Add pitch table for audio resampling at 44100Hz --- src/main.c | 11 +++++++---- src/uxn/devices/apu.c | 44 +++++++++++++++++--------------------------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/main.c b/src/main.c index a5a0628..fa82999 100644 --- a/src/main.c +++ b/src/main.c @@ -126,11 +126,8 @@ audio_talk(Device *d, u8 b0, u8 w) { // Disable the channel before updating. c->data = NULL; - // Initialization. - c->inc = (44100 << 12) / AUDIO_FREQ; - c->pos = 0; - // Data. + c->pos = 0; c->length = mempeek16(d->dat, 0xa); c->length <<= 12; // fixed point. c->data = &d->mem[mempeek16(d->dat, 0xc)]; @@ -149,8 +146,14 @@ audio_talk(Device *d, u8 b0, u8 w) { // Pitch c->pitch = d->dat[0xf] & 0x7f; + + // Initialization. + c->inc = pitch_table_44100[c->pitch]; + txt_position(0, 0); txt_printf("note: %d \n", c->pitch); + txt_printf("inc: %ld \n", c->inc); + txt_printf("length: %ld \n", c->length >> 12); } } diff --git a/src/uxn/devices/apu.c b/src/uxn/devices/apu.c index 2b1c8ff..96b9058 100644 --- a/src/uxn/devices/apu.c +++ b/src/uxn/devices/apu.c @@ -1,32 +1,22 @@ -#define SAMPLE_FREQUENCY 44100 -#define NOTE_PERIOD (SAMPLE_FREQUENCY * 0x4000 / 11025) -#define ADSR_STEP (SAMPLE_FREQUENCY / 0xf) - -static u16 pitch_table[] = { - 12173, 11490, 10845, 10237, 9662, 9120, 8608, 8125, - 7669, 7238, 6832, 6448, 6086, 5745, 5422, 5118, - 4831, 4560, 4304, 4062, 3834, 3619, 3416, 3224, - 3043, 2872, 2711, 2559, 2415, 2280, 2152, 2031, - 1917, 1809, 1708, 1612, 1521, 1436, 1355, 1279, - 1207, 1140, 1076, 1015, 958, 904, 854, 806, - 760, 718, 677, 639, 603, 570, 538, 507, - 479, 452, 427, 403, 380, 359, 338, 319, - 301, 285, 269, 253, 239, 226, 213, 201, - 190, 179, 169, 159, 150, 142, 134, 126, - 119, 113, 106, 100, 95, 89, 84, 79, - 75, 71, 67, 63, 59, 56, 53, 50, - 47, 44, 42, 39, 37, 35, 33, 31, - 29, 28, 26, 25, 23, 22, 21, 19, - 18, 17, 16, 15, 14, 14, 13, 12, - 2, +// Calculated as (44100 << 12) / 18157 for C4 +static u32 pitch_table_44100[] = { + 310, 329, 348, 369, 391, 414, 439, 465, + 493, 522, 553, 586, 621, 658, 697, 739, + 783, 829, 879, 931, 987, 1045, 1107, 1173, + 1243, 1317, 1395, 1478, 1566, 1659, 1758, 1863, + 1974, 2091, 2215, 2347, 2487, 2634, 2791, 2957, + 3133, 3319, 3517, 3726, 3948, 4182, 4431, 4695, + 4974, 5269, 5583, 5915, 6267, 6639, 7034, 7452, + 7896, 8365, 8863, 9390, 9948, 10539, 11166, 11830, + 12534, 13279, 14069, 14905, 15792, 16731, 17726, 18780, + 19896, 21079, 22333, 23661, 25068, 26559, 28138, 29811, + 31584, 33462, 35452, 37560, 39793, 42159, 44666, 47322, + 50136, 53118, 56276, 59623, 63168, 66924, 70904, 75120, + 79587, 84319, 89333, 94645, 100273, 106236, 112553, 119246, + 126337, 133849, 141808, 150241, 159174, 168639, 178667, 189291, + 200547, 212472, 225107, 238492, 252674, 267699, 283617, 300482, }; -s8 square_wave[] = { - (s8)0x00 + (s8)0x80, (s8)0xFF + (s8)0x80 -}; - -#include "text.h" - // // REG_TM0D frequency buffer size // | | | -- cgit v1.2.1