shithub: choc

Download patch

ref: 116b698e31c8637248ca1610e2faa717d005b6f2
parent: d051bd5d55a998f2ff4f74f718e23c98cbf49b88
parent: 0cada1cf6fd7b5cdcf41b5638b35d779b430efb8
author: Simon Howard <fraggle+github@gmail.com>
date: Sat Nov 1 17:07:38 EDT 2014

Merge pull request #466 from khokh2001/opl-fix2

opl note limitation and octave overflow fixes

Adjust how the OPL MIDI code behaves at extreme MIDI note values
(high/low octaves) to better match how the Doom DMX library decides
on the OPL register value (thanks Alexey Khokholov / khokh20010.

--- a/src/i_oplmusic.c
+++ b/src/i_oplmusic.c
@@ -700,10 +700,10 @@
 static unsigned int FrequencyForVoice(opl_voice_t *voice)
 {
     genmidi_voice_t *gm_voice;
-    unsigned int freq_index;
+    signed int freq_index;
     unsigned int octave;
     unsigned int sub_index;
-    unsigned int note;
+    signed int note;
 
     note = voice->note;
 
@@ -719,10 +719,15 @@
 
     // Avoid possible overflow due to base note offset:
 
-    if (note > 0x7f)
+    while (note < 0)
     {
-        note = voice->note;
+        note += 12;
     }
+    
+    while (note > 95)
+    {
+        note -= 12;
+    }
 
     freq_index = 64 + 32 * note + voice->channel->bend;
 
@@ -734,6 +739,11 @@
         freq_index += (voice->current_instr->fine_tuning / 2) - 64;
     }
 
+    if (freq_index < 0)
+    {
+    	freq_index = 0;
+    }
+    
     // The first 7 notes use the start of the table, while
     // consecutive notes loop around the latter part.
 
@@ -755,14 +765,7 @@
 
     if (octave >= 7)
     {
-        if (sub_index < 5)
-        {
-            octave = 7;
-        }
-        else
-        {
-            octave = 6;
-        }
+        octave = 7;
     }
 
     // Calculate the resulting register value to use for the frequency.