# HG changeset patch # User Josef 'Jeff' Sipek # Date 1672377208 18000 # Fri Dec 30 00:13:28 2022 -0500 # Node ID 08a6a7fc77ca7643e81220d5263f0e024c15ca7a # Parent 0cce8e4cf6f402917b9044f4487d1060b20c534d tones: keep frequencies in tone tables in 8.8 fixed point format The phase calculation is already done using fixed point math, so we can trivially feed it tone frequencies in the same format. This results in the tones being closer to the desired frequencies. E.g., a E6 will sound as 1319 Hz (as it should) instead of 1344 Hz as before. Signed-off-by: Josef 'Jeff' Sipek diff --git a/fmfox.h b/fmfox.h --- a/fmfox.h +++ b/fmfox.h @@ -47,8 +47,8 @@ * Various tone sequences */ extern const __flash uint8_t sine_wave[WAVE_STEPS / 4 + 1]; -extern const __flash uint8_t tones_fixed[10]; -extern const __flash uint8_t tones_lfsr[16]; -extern const __flash uint8_t tones_morse[2]; +extern const __flash uint16_t tones_fixed[10]; +extern const __flash uint16_t tones_lfsr[16]; +extern const __flash uint16_t tones_morse[2]; #endif diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -378,7 +378,7 @@ tone = next_fixed_tone(tone); } - dphase = tones_fixed[tone] << 8; + dphase = tones_fixed[tone]; break; case STATE_PLAYING_LFSR: if (!steps) { @@ -386,7 +386,7 @@ tone = next_lfsr_tone(tone); } - dphase = tones_lfsr[tone] << 8; + dphase = tones_lfsr[tone]; break; case STATE_PLAYING_IDENT: if (!steps) { @@ -401,7 +401,7 @@ phase = 0; } - dphase = tones_morse[tone] << 8; + dphase = tones_morse[tone]; break; } diff --git a/tones.c b/tones.c --- a/tones.c +++ b/tones.c @@ -25,7 +25,7 @@ #define DIV_ROUND_UP(x, y) (((x) + (y) - 1) / (y)) #define HZ_TO_STEPS(freq) \ - DIV_ROUND_UP((freq), (OUT_SAMPLE_RATE / WAVE_STEPS)) + DIV_ROUND_UP((freq) * 256ul, (OUT_SAMPLE_RATE / WAVE_STEPS)) #define E4 HZ_TO_STEPS(330) #define G4 HZ_TO_STEPS(392) @@ -44,12 +44,12 @@ #define D7 HZ_TO_STEPS(2349) /* a fixed sequence of tones that play in order */ -const __flash uint8_t tones_fixed[] = { +const __flash uint16_t tones_fixed[] = { C6, C5, D6, D5, E6, E5, G6, G5, A6, A5, }; /* a set of tones for a 4-bit LFSR */ -const __flash uint8_t tones_lfsr[16] = { +const __flash uint16_t tones_lfsr[16] = { 0, /* never used since LFSRs are never 0 */ E4, G4, A4, C5, D5, E5, G5, A5, @@ -57,6 +57,6 @@ C7, D7, }; -const __flash uint8_t tones_morse[2] = { +const __flash uint16_t tones_morse[2] = { 0, D5, };