This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help planning structure of randomly changing MIDI loop  RSS feed

 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am playing around with the HeadFirst Beatbox. I have an idea for a modification, but my attempts at implementing have been unsuccessful (and as I get closer and closer I feel more and more like I am doing it wrong).

Without unnecessary detail, the program presents an GUI for designing a Midi sequence. There is a button to the side that causes a sequence to be built and started in a loop (buildTrackAndStart()) and another that interrupts the playing beat( via sequencer.stop();)

What I would like is for the sequence to randomly change every time through the loop. I tried the follwing setup, which failed miserably:
buildTrackAndStart() runs, then calls a copy of itself, buildTrackAndStartover() which in turn calls back to buildTrackAndStart() I threw in an appropriate snippet to make the random changes in each of the methods

The trouble is that the sequences never allow each other to finish. They interrupt each other on the first note over and over.

My nexy thought is to add a Midievent to the end of each sequence which will itself call the other buildTrackAnd*() This will be a little bit of a pain (because I don't know how to do it yet) so I wonder if I am going in the right direction anyway. Is there a better method for doing this?

Any thoughts appreciated...
-Adam
[ December 13, 2005: Message edited by: Adam Price ]
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam, post some code snippets so we have a better idea of what's going on. From what little I can glean though, I don't think you should be calling the method recursively:


buildTrackAndStart() runs, then calls a copy of itself, buildTrackAndStartover() which in turn calls back to buildTrackAndStart()


This is an example of indirect recursion.
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This scans a 16x16 grid of checkboxes to see what midi instruments should fire (rows) on what beats (columns). I want to randomly tweak it each time through the loop with :



The tweak works fine - the trouble is finding a way to have the method keep generating and playing sequences incorporating the tweak (at least until I poke the stop button, which calls the method:


for the full code, before my attempts to add this random loop, see my other thread Modified Head First Beatbox
[ December 13, 2005: Message edited by: Adam Price ]
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam, I don't have any experience with the sound classes but it seems to me like you're going to have to bone up on threads. The sequencer has an isRunning() method that returns true if it's running a sequence. This suggests to me that it is spawning a thread that plays the sequence. I think you're going to have set up a loop to poll the sequencer to see if it's still running (it'd be nice if there was a way to just listen for a "loop done" event, wouldn't it?) and only build and start a new sequence after the current one completes running.

pseudocode:


[ December 14, 2005: Message edited by: Junilu Lacar ]
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Junilu Lacar:






ok - here's my new flow:


It works in the barest sense - it plays the loop with random changes now and then, just as it should. Three problems though:

1. The grid of checkboxes is never redrawn to show which ones have been checked. I have tried forcing a repaint of the panel they are in and of the whole frame - no luck
2. The stop button doesn't work - it toggles fine before I start the sequence, but once it gets going, it can't seem to get a toggle in edgewise.
3. I am new to OOP, so maybe my expectations are wrong, but this flow feels very procedural, particularly the part where I make it wait by doing nothing while the sequence is running. Is this flow inf good style, or is a a gross hack?

Thanks for input!
 
Junilu Lacar
Sheriff
Posts: 11155
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The empty while loop is eating up all the clock cycles and not letting the buttons get in on the action. Like I said before, you're going to have to bone up on threads: http://javaalmanac.com/egs/java.lang/PauseThread.html

As for being hacky or non-OOP, thinking in terms objects is just a way of abstracting and organizing a solution to a problem. At some point, however, you're going to have to go down to a level where all you have left is a sequence of commands that need to be executed by the computer. After all, statements like the for-loop, if-then-else, and while statements all come from procedural-type languages, don't they?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!