Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 3649 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

« Posted 2009-01-04 07: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.

if (noteTime <= 0)
   int otherChannel = noteChannel == 0 ? 1 : 0;
   noteChannel = otherChannel;
   noteTime = (int)(Math.random() * 300 + 100);
   currentNote = (int)(Math.random()*50+5);
   currentInstrument = MUSIC_INSTRUMENTS[(int)(Math.random()*MUSIC_INSTRUMENTS.length)];

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
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 19: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  

EgonOlsen (78 views)
2018-06-10 19:43:48

EgonOlsen (58 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (260 views)
2018-05-13 18:23:11

nelsongames (158 views)
2018-04-24 18:15:36

nelsongames (157 views)
2018-04-24 18:14:32

ivj94 (898 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (811 views)
2018-03-24 14:43:53

Solater (175 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
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!