diff options
author | Takamichi Horikawa <takamichiho@gmail.com> | 2017-04-03 22:19:14 +0900 |
---|---|---|
committer | Takamichi Horikawa <takamichiho@gmail.com> | 2017-04-03 22:19:49 +0900 |
commit | fd698bc65313888c689877128d4b489c64f2e85f (patch) | |
tree | 0ad39ba217b1b43ffa1156f7ab81fcb6b186b9aa /fmdriver/fmdriver_common.c | |
parent | 4e4ba88b67cc9e891a69d65bbde460e1ba51a39e (diff) |
pmd: fix PPZ8 loop
Diffstat (limited to 'fmdriver/fmdriver_common.c')
-rw-r--r-- | fmdriver/fmdriver_common.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/fmdriver/fmdriver_common.c b/fmdriver/fmdriver_common.c index 26e6def..b919c3a 100644 --- a/fmdriver/fmdriver_common.c +++ b/fmdriver/fmdriver_common.c @@ -69,3 +69,35 @@ uint8_t fmdriver_ssg_freq2key(uint16_t freq) { if (octave > 8) return 0x8b; return (octave << 4) | note; } + +uint8_t fmdriver_ppz8_freq2key(uint32_t freq) { + if (!freq) return 0x00; + int octave = 16+4; + while (!(freq & 0x80000000u)) { + freq <<= 1; + octave--; + } + if (octave < 0) return 0x00; + if (octave > 8) return 0x8b; + // round(0x8000*(2**((i+0.5)/12))) + static const uint16_t freqtab[0xc] = { + 0x83c0, + 0x8b96, + 0x93e3, + 0x9cae, + 0xa5ff, + 0xafde, + 0xba53, + 0xc567, + 0xd124, + 0xdd94, + 0xeac1, + 0xf8b6 + }; + uint16_t freqhigh = freq >> 16; + int note = 0; + for (; note < 12; note++) { + if (freqhigh < freqtab[note]) break; + } + return (octave << 4) | note; +} |