Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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 3061 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.

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 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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Nickropheliac (16 views)
2014-08-31 22:59:12

TehJavaDev (24 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (73 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!