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 4107352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:29:57+00:00 2026-05-20T21:29:57+00:00

A user has the ability to generate a set of MIDI events which are

  • 0

A user has the ability to generate a set of MIDI events which are in turn added to an ArrayList and then iterated through and added to a Track object, waiting to be played. I’m trying to remove all of the events that are in a track, I do this by iterating through all of the MIDI Events in a collection and use the “CreateNoteOnEvent”/”CreateNoteOffEvent” in the .remove() method.

For some reason the method always returns as false, as in it can’t find the event to delete or that it has failed to delete it.

Am I going about it the right way? I just assumed I delete the events in the same way that I add them.

Thanks!

Remove:

Iterator itr = collection.iterator();

        try
        {
            if(sequencer.isRunning())
            {
                Stop();
            }
            while (itr.hasNext())
            {
                MIDIMessageContainer msg = (MIDIMessageContainer) itr.next();

                if(!track.remove(CreateNoteOnEvent(msg.GetChannel(), msg.GetKey(), msg.GetTick(), msg.GetVelocity())))
                {
                    Logger.Add("MIDI Event not removed");
                    return false;
                }
                if(!track.remove(CreateNoteOffEvent(msg.GetChannel(), msg.GetKey(), msg.GetTick(), msg.GetVelocity())))
                {
                    Logger.Add("MIDI Event not removed");
                    return false;
                }
            }

            collection.clear();
            return true;
        }

CreateNoteOnEvent:

private static MidiEvent CreateNoteOnEvent(int channel, int pitch, long tick, int velocity)
    {
        ShortMessage noteOnMessage = new ShortMessage();

        try
        {
            noteOnMessage.setMessage(ShortMessage.NOTE_ON, channel, pitch, velocity);
        }
        catch (Exception e)
        {
            Logger.Add(e.getMessage());
        }

        MidiEvent event = new MidiEvent(noteOnMessage, tick);
        return event;
    }

CreateNoteOffEvent:

 private static MidiEvent CreateNoteOffEvent(int channel, int pitch, long tick, int velocity)
    {
        ShortMessage noteOffMessage = new ShortMessage();

        try
        {
            noteOffMessage.setMessage(ShortMessage.NOTE_OFF, channel, pitch, velocity);
        }
        catch (Exception e)
        {
            Logger.Add(e.getMessage());
        }

        MidiEvent event = new MidiEvent(noteOffMessage, tick + 1);
        return event;
    }
  • 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-20T21:29:57+00:00Added an answer on May 20, 2026 at 9:29 pm

    After digging around the documentation I found that you can infact return a MidiEvent in the track if you specify the index, my first implementation of a solution went like this:

    for(int i = 0; i < track.size(); i++)
    {
           if(!track.remove(track.get(i)))
           {
                  Logger.Add("MIDI Event not removed");
           }
    }
    

    The problem with this code is the loop is checking it’s condition on the length of the Track, this will change as events are removed so it will eventually fail the condition and break out of the loop in turn leaving several events in the track still.

    This is the working solution:

    _sequence.deleteTrack(_track);
    _track = _sequence.createTrack();
    

    Simple but very effective, instead of iterating through a loop that could go on for a fair while I read through the documentation and decided upon this method instead. Although I have not profiled it’s performance I have a feeling that this is a better solution than looping, especially when the number of iterations can be fairly high.

    I hope it helps someone, I spent hours scratching my head over this.

    Thanks for the additional feedback!

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

Sidebar

Related Questions

I have a gridview which has rows of 'tickets'. A user has the ability
I've got a WinForm where the user has the ability to drag an item
I am working on an application where a user has the ability to leave
I'm currently trying to create a php script were the user has the ability
I'm working on an application with an embedded UIWebView. The user has the ability
User has many Tracks, through Favorite. Favorite has some extra per-user meta-data about the
I have an SSRS report that contains several sub-reports. The user has the ability
I've got the following scenario: User has the ability to drag-and-drop a control onto
On one page of my website the user has the ability to choose and
Take this following code from an example HR system. The user has the ability

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.