DIAMOND RUSH N73 REBUILD - MIDI SOUNDS OUT OF KEY
=================================================

PROBLEM
-------
Melody and bass lines in the game's MIDI music sound out of key (wrong
notes / unpitched percussion).

CAUSE (verified from the data)
------------------------------
- The MIDI sequences in snd.f put pitched melody and bass notes on MIDI
  channel index 9 (GM channel 10) in 17 of the 21 sequences. Sequence 19
  alone has 174 such notes, with pitches in the melodic range.
- Desktop synthesizers (Gervill / javax.sound.midi) always play channel
  index 9 as the General MIDI drum kit. A pitched note sent there is
  played as a drum sample at a fixed pitch, so the melody sounds out of
  key.
- Verified: note 60 (C4) sent on channel 0 gives a piano tone at 262 Hz
  (C4), with harmonics at 523 and 785 Hz. The same note sent on channel 9
  gives drum energy (strongest peak 207 Hz), with almost no energy at 262 Hz.
- The synthesizer's tuning is correct: A4 renders at exactly 440 Hz at
  both 44100 Hz and 48000 Hz. The sequence data itself is in key (all 21
  files are C major, no key-signature or tuning messages). The pitch bends
  are deliberate slides and are not the cause.

FIX
---
In src/javax/microedition/media/Manager.java:

1. In createPlayer, wrap the sequence:

     return new MidiPlayer(moveDrumChannel(MidiSystem.getSequence(in)));

2. Add this method to class Manager:

  /* The game's MIDI files put pitched melody and bass notes on MIDI channel index 9 (GM channel 10).
     Desktop synthesizers play that channel as a drum kit, so those notes come out as unpitched
     percussion and sound out of key. The phone played them as instruments, so move channel 9 to the
     otherwise unused channel 15. Set -Ddiamond.drumChannel=true to keep the GM drum behaviour. */
  static Sequence moveDrumChannel(Sequence s) throws InvalidMidiDataException {
   if(Boolean.getBoolean("diamond.drumChannel"))return s;
   for(Track t:s.getTracks()){
    for(int i=0;i<t.size();i++){
     MidiMessage m=t.get(i).getMessage();
     if(m instanceof ShortMessage){
      ShortMessage sm=(ShortMessage)m;
      if(sm.getChannel()==9)sm.setMessage(sm.getCommand(),15,sm.getData1(),sm.getData2());
     }
    }
   }
   return s;
  }

Or simply replace Manager.java with the attached Manager.java, or apply
midi_drum_channel_fix.patch.

WHY CHANNEL 15
--------------
No sequence in snd.f uses channel index 15, so moving channel 9 there
cannot collide with another part. Note and controller events are moved
along with the program change.

VERIFICATION DONE
-----------------
- All 21 sequences in snd.f parsed: no channel-9 events remain after the
  fix, and the note count is unchanged in every sequence (for example
  seq 19: 865 notes before and after).
- Not yet verified: how it sounds on Windows with the desktop synthesizer.
  Please listen to a melodic level (e.g. Angkor 4 or Scotland).

KNOWN OPEN POINTS
-----------------
- Sequences 9 and 12 hold a single repeated note on channel 9, which may
  be an intended drum hit. If one now sounds wrong, limit the fix to the
  sequences that need it.
- Sequences with no program change on channel 9 (e.g. 2, 5, 7, 8, 9, 15,
  17) will now play piano instead of the drum kit. The original phone
  voice for those is unknown from the data.
- Build note: this javac rejected --release 8 in the test sandbox. The
  Windows build (build.py, JDK 17) should work unchanged.
- Gervill is the only synthesizer tested. Other desktop synths may map
  channel 9 the same way.

FILES
-----
- MIDI_OUT_OF_KEY_FIX.txt (this file)
- midi_drum_channel_fix.patch
- Manager.java (patched file)
