Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default Java Midi player

    Greetings everyone, I am new to the community but have looked at many other posts and this seems like a very helpful place to newer programmers.

    So I am supposed to make a code to output "Twinkle Twinkle Little Star" using a loop/array..... I can do this just fine without trying to loop it with an array but when I do(this is the code I have posted) it just seems like the music scales up rather then play the music like I want it too....other then that it runs fine.
    Does anyone have any suggestions on what I could do?
    Also what would I have to do to play two instruments at the same time for this song?
    Thank you.

    Java Code:
    import javax.sound.midi.*;
    
    public class MusicB
    
    {
    
                //Method: playNote
    
                //Description: Uses the MidiSynthesizer to play a single note.
    
                //Arguments:     int note: note to play (0-127)
    
                //                      int velocity: the velocity or volume of the note (0-127)
    
                //                      int duration: length of the note in milliseconds
    
                //                      MidiChannel: the instruments to play the note
    
                  static void playNote(int note, int velocity,int duration, MidiChannel channels[])
    
                {
    
    
                            //start playing the note using instrument in slot 0
    
                            channels[0].noteOn(note, velocity);
    
                            //wait out the duration
    
                            try
    
                            {
    
                                        Thread.sleep(duration);
    
                            }
    
                            catch (InterruptedException e)
    
                            {
    
                            }
    
                            //stop playing the note
    
                            channels[0].noteOff(note);
    
                }
    
                public static void main(String[] args)
    
                {
    
                             // Initialize the synthesizer
    
                            Synthesizer synth = null;
    
                            try {
    
                                        synth = MidiSystem.getSynthesizer();
    
                            }
    
                            catch (MidiUnavailableException e) { }
    
                            try
    
                            {
    
                                        synth.open();
    
                            }
    
                            catch (MidiUnavailableException e)
    
                            {
    
                                        e.printStackTrace();
    
                                        System.exit(1);
    
                            }
    
                            // Define array for musical instruments / channels
    
                            int notes[] = {60, 60, 67, 67, 69, 69, 67, 65, 65, 64, 64, 62, 62, 61};
                            int velocities [] = new int [1];
                            int duration [] =  new int [1];
    
    
    
                            MidiChannel[] channels = synth.getChannels();
                            Instrument instruments[] = synth.getAvailableInstruments();
    						// Then pick an instrument to load.
    						// The following loads instrument number 101 into channel 0
    						synth.loadInstrument(instruments[0]);
    						channels[0].programChange(74);
    
    
                                                    velocities[0] = 127;
    
                                                    duration[0] = 400;
    
                                                    int stop = 0;
    
    
                            // Play notes using the default for the channels
    
                                                    while (stop < 14) {
    
    
    
                            playNote(notes[0], velocities[0], duration[0], channels);
                           stop ++;
                           notes[0] ++;
                           
                            
                    }
                }
    
    }

  2. #2
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
             int notes[] = {60, 60, 67, 67, 69, 69, 67, 65, 65, 64, 64, 62, 62, 61};
             int velocities [] = new int [1];
             int duration [] =  new int [1];
    
    
    
                            MidiChannel[] channels = synth.getChannels();
                            Instrument instruments[] = synth.getAvailableInstruments();
    						// Then pick an instrument to load.
    						// The following loads instrument number 101 into channel 0
    						synth.loadInstrument(instruments[0]);
    						channels[0].programChange(74);
    
    
                                                    velocities[0] = 127;
    
                                                    duration[0] = 400;
    
                                                    int stop = 0;
    
    
                            // Play notes using the default for the channels
    
                                                    while (stop < 14) {
    
    
    
                            playNote(notes[0], velocities[0], duration[0], channels);
                           stop ++;
                           notes[0] ++;
    I am pretty sure the error must be in this array or loop somewhere....the rest I believe to be right.

  3. #3
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Hello there, to be honest I haven't work with midi players before but I read you post and it attracted me, and a question occurred to my mind, in this part of you code
    playNote(notes[0], velocities[0], duration[0], channels);
    stop ++;
    notes[0] ++;
    aren't you supposed to move from one note to other ?
    does notes[0] ++ allow you to move from one element of the array to the second ? since I was taught to declare an int i=0 and add it by one each time in my while loop,
    Java Code:
    int i = 0;
    while (stop < 14) {
                            
    playNote(notes[0], velocities[0], duration[0], channels);
    stop ++;
    notes[i] ++;
     i++;
    }
    would that cause any problem ?
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  4. #4
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Thank you very much for your input!
    For the first part, I am trying to use the playNote(notes[0], velocities[0[, duration[0], channels);
    14 times and just allowing the variable "notes" to run my loop and move up one array value every time.... does that make sense?



    I see what you are saying, using the i++ idea changed it but I am still not getting the desired result.... now it sounds as if it plays the first note and then plays the second note for the next 13 notes....

    I used your i++ example and also changed it to this....(my int "stop" was meant to fulfill the i++ roll when I made it... I guess I just didn't put it in my code correctly!)
    Java Code:
         int stop = 0;
    
                            // Play notes using the default for the channels
    
                                                    while (stop < 14) {
    
                            playNote(notes[0], velocities[0], duration[0], channels);
                           
                            notes[stop] ++;
                            stop++;
    and they produce the same result so I think they perform the same function so that is what I am at right now.
    Last edited by Suende; 04-16-2011 at 06:49 PM.

  5. #5
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    I just spied something else, you are always playing the same note[0]
    playNote(notes[0], velocities[0], duration[0], channels);
    is what i understand right ? did you try to change the note[0] to note[stop] as well ?
    since changing notes[stop] means nothing when you recall notes[0] in calling the method. Did I explained my point clearly ?

    playNote(notes[stop], velocities[0], duration[0], channels);
    stop++
    ;
    Last edited by baf06; 04-16-2011 at 07:02 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    I am now in love with you. Worked great.
    That was so simple I can not believe I did not see it.
    Both of you thank you very very much for your help!

    Now if anyone knew how I would have it use more than one instrument.... haha That question is a little harder I think....Requires more knowledge of the midi player.

  7. #7
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    you are totally welcome :) at that point i can't help you unfortunately cause I told you I don't have much knowledge about midi :) but I managed to help you with what you were facing :P
    But you can sure get answers from Java docs
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  8. #8
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Haha I do not think many people are experienced with it...it is hard to even find anything on it on the web. That one I'll have to search a while more for.
    Do you know how I could make my "int notes" array read those exact same number but just from like a .txt file? I can find lots of ways to do things that that with strings, but not as much with ints...
    Is there a way to mark threads as solved or does a mod have to do that?

  9. #9
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
    Scanner scanner = new Scanner(new File("c:\\song.txt"));
    int [] notes = new int [14];
    int i = 0;
    while(scanner.hasNextInt()){
       notes[i++] = scanner.nextInt();
    }
    
                            int velocities = 127;
                            int duration = 500;
    
                            // Define array for musical instruments / channels
    
                            MidiChannel[] channels = synth.getChannels();
    
    
    
                            playNote(notes[i++], velocities, duration, channels);
    
                          
       System.exit(0);
        }
        }}
    build is successful and it even makes a sound....but it doesn't sound like ANY of the notes. Once again I think its some simple error I am not spotting.
    Last edited by Suende; 04-17-2011 at 08:08 AM.

  10. #10
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    in this you will face many errors I believe, one is that you have to reinitialize i or set another int, since
    int i = 0;
    while(scanner.hasNextInt()){
    notes[i++] = scanner.nextInt();
    }
    after this i will become 14, thus in
    playNote(notes[i++], velocities, duration, channels);
    it will start from 14, isn't this right ?
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  11. #11
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    I think you are correct, I originally had it written like
    Java Code:
    Scanner scanner = new Scanner(new File("c:\\song.txt"));
    int [] notes = new int [14];
    int i = 0;
    while(scanner.hasNextInt()){
       notes[i++] = scanner.nextInt();
    }
    
                            int velocities = 127;
                            int duration = 500;
    
                            // Define array for musical instruments / channels
    
                            MidiChannel[] channels = synth.getChannels();
    
    i = 0;
    
                            playNote(notes[i++], velocities, duration, channels);
    
                          
       System.exit(0);
        }
        }
    but that does the same thing, so I removed the "i = 0;" because it didn't seem to make a difference

  12. #12
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    you seem to perform one playNote since there is not loop over it, it only perform once, with one note, I think you have to put it in a for loop, try that.
    One more thing
    notes[i++] = scanner.nextInt();
    will start filling the array from index 1, thus making notes[0] null, manage to correct that
    Last edited by baf06; 04-17-2011 at 09:32 AM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  13. #13
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    Another solution for doing this is using recursion method, it is really helpful and easy to do in you case. But do what is easier for you of course :)
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  14. #14
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Java Code:
    Scanner scanner = new Scanner(new File("c:\\song.txt"));
    int [] notes = new int [14];
    int i = 0;
    while(scanner.hasNextInt()){
       notes[i] = scanner.nextInt();
       i++;
    }
    
                            int velocities = 127;
                            int duration = 500;
    
                            // Define array for musical instruments / channels
    
                            MidiChannel[] channels = synth.getChannels();
    i=0;
                            if (i == 14)
                                return;
                            else
    
                            playNote(notes[i], velocities, duration, channels);
    
                            i++;
                            return;
    look a little better now? hah Good catch with the array starting at 1 not 0...
    As you can tell I tried to use recursion but I still have the same error with only playing one note....
    Can't believe i didn't notice i forgot to add a loop on this program like I had on the other...but it was late, forgive me :)
    I see you must be in a very different time zone hah.

  15. #15
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    hehe yeh it is 8:10pm at my place :) you ? actually I don't know about midi, so maybe the problem is with the notes you are using, i don't know. I'll check your code again, but i can't see any thing wrong with it, check your file and the notes you are using.
    Last edited by baf06; 04-17-2011 at 05:59 PM.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  16. #16
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Hah here it is 9:30 AM!
    the midi should be okay....just copy pasted from the other code that works and no changes.
    the note should look like this right 63,34,64,65,34,23,45
    just as an example....so 14 numbers with "," between them?

  17. #17
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    a great difference :P yes that is right. I don't know actually why is that happening.
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  18. #18
    Suende's Avatar
    Suende is offline Member
    Join Date
    Apr 2011
    Posts
    22
    Rep Power
    0

    Default

    Hmm, well I am glad it is so close that both of can not see any error haha, those were all my objectives for this!...Thank you for your help baf06! <3

  19. #19
    baf06 is offline Member
    Join Date
    Jan 2011
    Location
    Beirut, Lebanon
    Posts
    90
    Rep Power
    0

    Default

    and You are very welcome :) just a private question if you don't mind, r u a girl or a male ?:) since I sent you a msg that we are born in the same day :P
    Click on REP and add to member reputation, if you find their advices/solutions effective.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,383
    Blog Entries
    7
    Rep Power
    20

    Default

    Maybe the OP is interested in jFugue; I use it myself and imho it's a great library.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Vlc player interfaces in java
    By sankar1227 in forum Networking
    Replies: 1
    Last Post: 08-06-2011, 08:39 PM
  2. How to make a Java mp3/mp4 player?
    By Java123 in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 10:08 PM
  3. MIDI - Music Player Help
    By bs3ac in forum New To Java
    Replies: 0
    Last Post: 08-29-2010, 01:03 AM
  4. how to play MIDI in java application?
    By oregon in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 02:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •