Class MusicGenerationQueue

java.lang.Object
org.jjazz.rhythmmusicgeneration.api.MusicGenerationQueue
All Implemented Interfaces:
Runnable

public class MusicGenerationQueue extends Object implements Runnable
A thread to handle successive incoming music generation requests.

If several music generation requests arrive while a music generation task is already running, only the last request is kept. When generation task is done, a new music generation task is started with that last request.

A ChangeEvent is fired when a music generation task is complete and a result is available. Note that ChangeEvent is fired outside of the Swing EDT.

  • Constructor Details

    • MusicGenerationQueue

      public MusicGenerationQueue(int preUpdateBufferTimeMs, int postUpdateSleepTimeMs)
      Create the handler.
      Parameters:
      preUpdateBufferTimeMs - (milliseconds) Wait this time upon receiving the first request before starting the music generation
      postUpdateSleepTimeMs - (milliseconds) Wait this time before restarting a music generation
  • Method Details

    • add

      public void add(SongContext sgContext)
      Add a music generation request to this queue.
      Parameters:
      sgContext - Generate music for this context.
    • getLastAddedSongContext

      public SongContext getLastAddedSongContext()
    • isGeneratingMusic

      public boolean isGeneratingMusic()
      Check if queue is being generating music to produce a future Result.
      Returns:
    • isRunning

      public boolean isRunning()
    • start

      public void start()
      Start the thread which listens to requests.
      See Also:
    • getPreUpdateBufferTimeMs

      public int getPreUpdateBufferTimeMs()
      Wait this time from the first received change before triggering an update.

      This helps filtering out meaningless changes when a user action generates several changes (e.g. mouse drag).

      Returns:
    • getPostUpdateSleepTimeMs

      public int getPostUpdateSleepTimeMs()
      The minimum delay between 2 consecutive updates.

      This avoids too many sequencer changes in a short period of time, which can cause audio issues with notes muted/unmuted too many times.

      Returns:
    • stop

      public void stop()
      Stop the thread.
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • addChangeListener

      public void addChangeListener(ChangeListener listener)
      Be notified when a new result is available.

      Note that listener will be called from a distinct thread.

      Parameters:
      listener -
    • removeChangeListener

      public void removeChangeListener(ChangeListener listener)
    • getLastResult

      public MusicGenerationQueue.Result getLastResult()
      Get the result from the last generation task.

      Method should be called right after receiving a change event.

      Returns:
      Can be null if no request processed yet