Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3691320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:08:03+00:00 2026-05-19T04:08:03+00:00

I am creating an application where the user can add multiple MIDI notes to

  • 0

I am creating an application where the user can add multiple MIDI notes to a collection, from here they can press the “Play” button and the application will iterate through each note and play them through the speakers.

I’ve created a MIDIMessage class that holds details of the note the user has chose to add to the list, the details stored being the Pitch,Velocity,Delay and Channel.

Each of these messages is saved to an ArrayList with the type as MIDIMessage.

I then go on to attatch the iterator of the collection to an iterator object and play a sound whilst there is still an element in the collection that hasn’t been used.

For some reason even if I only add one note to the collection there is always two notes playing with exactly the same pitch, length and velocity.

Also each note plays at the same time no matter how many are present in the collection, I assumed there would be some sort of delay in between them.

Following is the code I am using at the moment:

MIDIMessage:

package javatest;

public class MIDIMessage
{
    private int pitch;
    private int velocity;
    private int channel;

    public MIDIMessage(int p, int v, int c)
    {
        pitch = p;
        velocity = v;
        channel = c;
    }

    public int GetPitch()
    {
        return this.pitch;
    }

    public int GetVelocity()
    {
        return this.velocity;
    }

    public int GetChannel()
    {
        return this.channel;
    }
}

Adding a note to the collection:

public void AddToList()
    {
        int channel = jComboBoxChannels.getSelectedIndex();
        int pitch = jComboBoxPitch.getSelectedIndex();
        int velocity = ((Integer)jSpinnerVelocity.getValue());    
        collection.add(new MIDIMessage(pitch,velocity,channel));
    }

Playing the notes:

try
        {
            jButton1.setEnabled(false);
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();
            Sequence sequence = new Sequence(Sequence.PPQ,1);
            Track track = sequence.createTrack();

            Iterator itr = collection.iterator();
            int i = 0;

            while(itr.hasNext())
            {
                MIDIMessage msg = (MIDIMessage)itr.next();

                ShortMessage noteOnMsg = new ShortMessage();
                //Signal/Channel/Pitch/Velocity
                noteOnMsg.setMessage(ShortMessage.NOTE_ON, msg.GetChannel(),msg.GetPitch(),msg.GetVelocity());

                ShortMessage noteOffMsg = new ShortMessage();
                //Signal/Channel/Pitch/Velocity
                noteOffMsg.setMessage(ShortMessage.NOTE_OFF,msg.GetChannel(),msg.GetPitch(),msg.GetVelocity());

                track.add(new MidiEvent(noteOnMsg,0));
                track.add(new MidiEvent(noteOffMsg,1));
                //i = i+1;
            }

            sequencer.setSequence(sequence);
            sequencer.setTempoInBPM(120);
            sequencer.setLoopCount(1);

            sequencer.start();

            Thread.sleep(1000);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:08:04+00:00Added an answer on May 19, 2026 at 4:08 am

    I’m not sure why the notes are played twice, but this is definitely wrong:

                track.add(new MidiEvent(noteOnMsg,0));
                track.add(new MidiEvent(noteOffMsg,1));
    

    As you can see here, a MidiEvent consists of the message and a MIDI tick. You always add a noteOnMsg on tick 0 and a noteOffMsg on tick 1, which explains why for multiple notes, they are all played at the same time. You want to do something like this instead:

    startTick = 0;
    
    while(itr.hasNext()) {
       [...]
       track.add(new MidiEvent(noteOnMsg,startTick));
       track.add(new MidiEvent(noteOffMsg,startTick + how_long_the_note_is_played));
       startTick += difference_between_this_note_and_the_next;
       [...]
    }
    

    I’m not sure what your delay means and if you can use it here, so I’m using descriptive variable names instead.

    Note that you’ll have to use MIDI ticks here – if you want to convert seconds in MIDI ticks, see this SO question (or rather its answer):

    [How long is one MIDI tick?]

    The formula is 60000 / (BPM * PPQ)
    (milliseconds).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a web application for work where the user has to enter the
When creating a web application, and lets say you have a User object denoting
When I'm creating a user for my web application, an SMTP email (using ASP.NET's
I am creating a basic image browsing application using Silverlight. Depending on the user's
For a web application, when creating the user which will connect to the MySQL
I'm creating an application that will store a hierarchical collection of items in an
I want to create migrations to add columns from my rails application (not through
I am currently creating a application and would like to add a AJAX feature,
I am creating a application which displays 8 thumbnails per page and it can
I want to integrate the iTunes library into my application so the user can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.