Class MusicGenerationQueue
java.lang.Object
org.jjazz.rhythmmusicgeneration.api.MusicGenerationQueue
- All Implemented Interfaces:
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
A result from a music generation. -
Constructor Summary
ConstructorDescriptionMusicGenerationQueue
(int preUpdateBufferTimeMs, int postUpdateSleepTimeMs) Create the handler. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(SongContext sgContext) Add a music generation request to this queue.void
addChangeListener
(ChangeListener listener) Be notified when a new result is available.Get the result from the last generation task.int
The minimum delay between 2 consecutive updates.int
Wait this time from the first received change before triggering an update.boolean
Check if queue is being generating music to produce a future Result.boolean
void
removeChangeListener
(ChangeListener listener) void
run()
void
start()
Start the thread which listens to requests.void
stop()
Stop the thread.
-
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 generationpostUpdateSleepTimeMs
- (milliseconds) Wait this time before restarting a music generation
-
-
Method Details
-
add
Add a music generation request to this queue.- Parameters:
sgContext
- Generate music for this context.
-
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. -
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() -
addChangeListener
Be notified when a new result is available.Note that listener will be called from a distinct thread.
- Parameters:
listener
-
-
removeChangeListener
-
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
-