example/mod: add frequency to cent conversion

This commit is contained in:
Zack Buhman 2025-04-20 10:50:10 -05:00
parent 37af2993cf
commit 503ed9ca94

View File

@ -7,6 +7,82 @@
#include "mod/mod.hpp"
#include "mod/getfunk/getfunk.mod.h"
/*
Octave 1: 856, C
*/
struct oct_fns
{
int oct;
int fns;
};
struct oct_fns oct_fns(int x)
{
const uint16_t _cent[] = {
0x0,
0x3d,
0x7d,
0xc2,
0x10a,
0x157,
0x1a8,
0x1fe,
0x25a,
0x2ba,
0x321,
0x38d,
};
switch (x) {
case 856: return {2, _cent[0]}; // C
case 808: return {1, _cent[11]}; // C#
case 762: return {1, _cent[10]}; // D
case 720: return {1, _cent[9]}; // D#
case 678: return {1, _cent[8]}; // E
case 640: return {1, _cent[7]}; // F
case 604: return {1, _cent[6]}; // F#
case 570: return {1, _cent[5]}; // G
case 538: return {1, _cent[4]}; // G#
case 508: return {1, _cent[3]}; // A
case 480: return {1, _cent[2]}; // A#
case 453: return {1, _cent[1]}; // B
case 428: return {1, _cent[0]}; // C
case 404: return {0, _cent[11]}; // D#
case 381: return {0, _cent[10]}; // D
case 360: return {0, _cent[9]}; // D#
case 339: return {0, _cent[8]}; // E
case 320: return {0, _cent[7]}; // F
case 302: return {0, _cent[6]}; // F#
case 285: return {0, _cent[5]}; // G
case 269: return {0, _cent[4]}; // G#
case 254: return {0, _cent[3]}; // A
case 240: return {0, _cent[2]}; // A#
case 226: return {0, _cent[1]}; // B
case 214: return {0, _cent[0]}; // C
case 202: return {-1, _cent[11]}; // D#
case 190: return {-1, _cent[10]}; // D
case 180: return {-1, _cent[9]}; // D#
case 170: return {-1, _cent[8]}; // E
case 160: return {-1, _cent[7]}; // F
case 151: return {-1, _cent[6]}; // F#
case 143: return {-1, _cent[5]}; // G
case 135: return {-1, _cent[4]}; // G#
case 127: return {-1, _cent[3]}; // A
case 120: return {-1, _cent[2]}; // A#
case 113: return {-1, _cent[1]}; // B
case 107: return {-1, _cent[0]}; // C
default:
return {-1, -1};
}
}
void wait()
{
uint32_t ffst = system.FFST;
@ -70,7 +146,7 @@ void main()
wait(); aica_sound.common.dmea0_mrwinh = aica::dmea0_mrwinh::MRWINH(0b1101);
wait(); aica_sound.channel[0].KYONB(1);
wait(); aica_sound.channel[0].LPCTL(1);
wait(); aica_sound.channel[0].LPCTL(0);
wait(); aica_sound.channel[0].PCMS(1); // 8-bit pcm
wait(); aica_sound.channel[0].LSA(0);
wait(); aica_sound.channel[0].LEA(sample_bytes);
@ -80,7 +156,7 @@ void main()
wait(); aica_sound.channel[0].AR(0x1f);
wait(); aica_sound.channel[0].OCT(-2);
wait(); aica_sound.channel[0].FNS(0);
wait(); aica_sound.channel[0].FNS(0x1a8);
wait(); aica_sound.channel[0].DISDL(0xf);
wait(); aica_sound.channel[0].DIPAN(0x0);
@ -97,5 +173,41 @@ void main()
wait(); aica_sound.channel[0].KYONEX(1);
while (1);
int division = 0;
struct pattern * pattern = &mod.patterns[3];
constexpr uint32_t timer_a_interrupt = (1 << 6);
int started = 0;
while (1) {
wait();
if (!started || (aica_sound.common.SCIPD() & timer_a_interrupt)) {
started = 1;
aica_sound.common.scire = timer_a_interrupt;
aica_sound.common.tactl_tima =
aica::tactl_tima::TACTL(6) // increment once every 128 samples
| aica::tactl_tima::TIMA(256 - 128) // interrupt after 128 counts
;
struct channel * c = &pattern->division[division][0];
if (c->parameter != 0) {
struct oct_fns of = oct_fns(c->parameter);
wait(); aica_sound.channel[0].KYONB(0);
wait(); aica_sound.channel[0].KYONEX(1);
wait(); aica_sound.channel[0].OCT(of.oct - 1);
wait(); aica_sound.channel[0].FNS(of.fns);
wait(); aica_sound.channel[0].KYONB(1);
wait(); aica_sound.channel[0].KYONEX(1);
}
division += 1;
if (division >= 64) {
break;
division = 0;
}
}
}
}