From e8882d58c9c4db8992b0620565e671979cd6a696 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Wed, 22 Aug 2018 00:51:52 +0300 Subject: Rotate octave on high out of range tone frequencies --- README.md | 1 + src/adlmidi_opl3.cpp | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b10577b..37773aa 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ To build that example you will need to have installed SDL2 library. * XMIDI now supports loops * Added working implementation of TMB's velocity offset * Added support for full-panning stereo option (Thanks to [Christopher Snowhill](https://github.com/kode54) for a work!) + * Fixed inability to play high notes due physical tone frequency out of range on the OPL3 chip ## 1.3.3 2018-06-19 * Fixed an inability to load another custom bank without of library re-initialization diff --git a/src/adlmidi_opl3.cpp b/src/adlmidi_opl3.cpp index 94602ba..5ea2053 100644 --- a/src/adlmidi_opl3.cpp +++ b/src/adlmidi_opl3.cpp @@ -266,33 +266,37 @@ void OPL3::noteOff(size_t c) void OPL3::noteOn(size_t c, double hertz) // Hertz range: 0..131071 { size_t chip = c / 23, cc = c % 23; - uint32_t x = 0x2000; + uint32_t octave = 0; - if(hertz < 0 || hertz > 131071) // Avoid infinite loop + if(hertz < 0) return; while(hertz >= 1023.5) { - hertz /= 2.0; // Calculate octave - x += 0x400; + hertz /= 2.0; // Calculate octave + if(octave < 0x1C00) + octave += 0x400; } - x += static_cast(hertz + 0.5); + octave += static_cast(hertz + 0.5); uint32_t chn = g_channelsMap[cc]; if(cc >= 18) { m_regBD[chip ] |= (0x10 >> (cc - 18)); writeRegI(chip , 0x0BD, m_regBD[chip ]); - x &= ~0x2000u; //x |= 0x800; // for test } + else + { + octave += 0x2000u; /* Key-ON [KON] */ + } if(chn != 0xFFF) { - writeRegI(chip , 0xA0 + chn, (x & 0xFF)); - writeRegI(chip , 0xB0 + chn, (x >> 8)); - m_keyBlockFNumCache[c] = (x >> 8); + writeRegI(chip , 0xA0 + chn, (octave & 0xFF)); + writeRegI(chip , 0xB0 + chn, (octave >> 8)); + m_keyBlockFNumCache[c] = (octave >> 8); } } -- cgit v1.2.3