Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  MIDI Synthesizer being nuts when trying to do a crossfade  (Read 2848 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


Game Engineer


« Posted 2009-01-04 08:29:32 »

I'm trying to get the MIDI synthesizer working, and it seems to go completely nuts when I try to get a crossfade going by using multiple channels.

Pretty much it seems to ignore everything I tell it to do when I'm using more than one channel... which is odd, considering you're supposed to use 16. The Java MIDI instructions are cryptic and unhelpful to someone who doesn't know much about how MIDI works, so I'm having a lot of trouble getting exactly what the problem is.

It tends to result in when I change the instrument in any channel. Once I do, notes that were previously played never end naturally, they only do if I force them off, which makes a crossfade impossible.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
noteTime--;
if (noteTime <= 0)
{
   int otherChannel = noteChannel == 0 ? 1 : 0;
   midiChannels[otherChannel].allNotesOff();
   noteChannel = otherChannel;
   noteTime = (int)(Math.random() * 300 + 100);
   currentNote = (int)(Math.random()*50+5);
   currentInstrument = MUSIC_INSTRUMENTS[(int)(Math.random()*MUSIC_INSTRUMENTS.length)];
   midiChannels[noteChannel].programChange(currentInstrument);
}
midiChannels[noteChannel].noteOn(currentNote,150);

This code is basically to play random notes using random instruments for random lengths of time. Once a note runs out of time, the channel is swapped to the other channel (in order to create a crossfade) and the instrument is changed on that channel. So basically it might go like this:
- Play note 50 with instrument 100 on channel 0
- Stop everything playing on channel 1
- Play note 75 with instrument 25 on channel 1
- Stop everything playing on channel 0
- Play note 63 with instrument 111 on channel 0
etc.
So there are always two different notes/instruments playing simultaneously on separate channels. Because noteOn is only getting called on one channel, however, this allows the inactive channel to slowly fade out on its own, creating a nice cross fade.

It should do that, anyway. It seems that this works once only. Basically the first sound plays on channel 0, then I tell the instrument on channel 1 to change, and that messes it all up. If I never change the instrument and use the same one on both channels, it seems to work fine.

By "messes it all up" I mean that the second played sound (played right after the first instrument change) will play either forever at a grating single pitch (won't do any fading or pitch bending or anything like it normally would) or will play for sort of a random length of time.

So basically I can play the same instrument a bunch of times simultaneously with no issues, but once I change the instrument assigned to a channel it goes crazy.

See my work:
OTC Software
Offline ShannonSmith
« Reply #1 - Posted 2009-01-15 20:25:03 »

I don't think program changes were ever intended for mid song. You probably need to wait for the sound to die away completely on a channel before you do a program change. NoteOff doesn't mean the sound has stopped playing and a note may be actively decaying for quite a while and doing a program change will screw up the synthesizer. Why not pick 16 instruments that sound good at the start and then randomly pick a channel for the next note.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (121 views)
2013-05-17 21:29:12

alaslipknot (129 views)
2013-05-16 21:24:48

gouessej (159 views)
2013-05-16 00:53:38

gouessej (151 views)
2013-05-16 00:17:58

theagentd (164 views)
2013-05-15 15:01:13

theagentd (149 views)
2013-05-15 15:00:54

StreetDoggy (193 views)
2013-05-14 15:56:26

kutucuk (217 views)
2013-05-12 17:10:36

kutucuk (216 views)
2013-05-12 15:36:09

UnluckyDevil (219 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.122 seconds with 20 queries.