MIDI volume control problems

I’ve been having some problems setting the volume on MIDI playback. This is a (modified) code snippet I found on the web somewhere that does the trick but it’s not consistant:


public final void setGain(int volume) {
  if (volume < 0) {
    volume = 0;
  }
  else if (volume > 100) {
    volume = 100;
  }
  int value = (int) (MAX_MUSIC_VOLUME * (double) (volume / 100.0));
  MidiChannel[] cs = ((Synthesizer)sequencer).getChannels();
  for (int i=0; i < cs.length; i++) {
    cs[i].controlChange(7,value);
  }
}

It goes through each channel and sets the gain. I can’t seem to be able to find any other way of doing it. Occasionally I get some weird results, like half the channels have been set, but mostly it doesn’t seem to have much effect unless I’m running this code constantly. Thinking it was a timing issue I even set in a timer to wait before it runs this code but to little effect.
The other sucky thing is the music actually has to be playing to have access to the channels to do the control changes…

Anyone have any ideas?

Thanks!

Maybe the code that plays the MIDI music sends main volume controller events as well (maybe without you knowing)?

Erik

I tried using the same code in my game, and had identical problems. It also made the applet version crash now and then, too (not in applet viewer though).