• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Midi Sequencer problem  RSS feed

 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
After I covered about 400 pages from HeadFirst Java2nd Edition, I thought I might try to make a simple Piano using keyboard keys...
Well, I can't understand how the whole process takes place in a MidiSystem...I mean..I don't know how many times should a sequencer player be open ...
I want that whenever I press a Keyboard key, a specific note be sent to sintethizer so I can hear it...

This is how my code looks like...As I'm still at the beginning, maybe you could suggest me how should've been written the whole code, the pattern of the code!(KeyListeners,Inner classes and all the stuff)...
I'd also be deeply grateful if you could guide me to where this whole MidiSystem is well explained...
All I know is first I need to ask for a Sequencer from the MidiSystem, then I must open the Sequencer, create a new Sequence and then ask Sequence object to give me a new track.
This track will be populated with MidiEvents. A MidiEvent contains a ShortMessage object and the tick when that message should be called, I guess!

I've used a static utility method to create ShortMessages for the ON and OFF Messages type...and then just add those 2 MidiEvents(On and Off) to the track which I guess is empty right now...
Finally, I need to set the sequence based on the track I've just populated and start the player so I can hear the note...this is the whole thing I understood from Midi...

And this is my code:




The problem is...the first three notes play well but then the notes get to cover/overlap each other...and everything is a mess...
First time when I wrote the program, everything worked like charm..I even played "Silent night" on the keyboard but I had a bit of a lag between key presses and sound made by the sintethizer so I thought I should change something...but the change wasn't good ..now it doesn't work at all ...Please explain me how should I approach this so I wouldn't need to guesstimate how to do the program, in the future!
Thanks in advance and I'm sorry for the long post!

PS: The KeyboardKeys array contains all the keyboard I'm pressing on...their corresponding keys;
The MusicalNotes array contains all the notes from DO to DO the 2nd octave along with their sharpes forms ...I wouldn't been able to play Silent night if I hadn't all these notes implemented
I need to get rid of that overlap and any lag ...that would be awesome Cheers!!

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you are using the same tick for each note.

Say, for example, you want to play Middle C for 2 seconds.

Your first short message would be new ShortMessage(144,1,60,velocity)

The MidiEvent would be new MidiEvent(new ShortMessage(144,1,60,velocity),1)

The ShortMessage to stop the note would be new ShortMessage(128,1,60,velocity)

The MidiEvent would be new MidiEvent(new ShortMessage(128,1,60,velocity),3)
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS2: I modified the code like this and now works well but the lag still persist when I try to change the rhythm...press faster on the keyboard keys...there is a lag...
The modification: Just put these code:

in the setUpMusic(int i) method

As for the same tick...I made it like this on purpose ...I don't think this is the problem
Still, look at my new code!
Am I committed to open the player and create a brand new Sequence and Track for every note I want to hear? I don't get this whole process...the tick part doesn't stand to me...sorry!
I will follow your advice, though and coming back with reactions
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait...what are you trying to say to me...
The first code worked well if I modified the tick?
Let's say I have 4 notes. Their ticks must be like this?
Note 1

on- tick 1
off tick 3

Note 2

on-tick 3
off-tick 5

Note 3

on-tick 5
off-tick 7

...etc??
This is what you're trying to tell the stupid of me??This way I can get rid of lag??
I thought it's not mandatory to use different ticks as the notes are not looping...they are created for every keyEvent
If they were looping I would modified the ticks accordingly ...still needs a bit of clearance from your side Thanks again!
 
Alin Andrei
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works...with the new code and tick modification it works like charm! I modified the code like this:

track.add(Helper.MakeMusic(144,1,MusicalNotes[i],115,1));
track.add(Helper.MakeMusic(128, 1, MusicalNotes[i], 115, 2));

and is much better ...I guess first time, the note didn't have the time to "breathe" in only one tick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!