Can MouseMotionDetector code feed data to SourceDataLine?

I’ve been playing around with javax.audio.sampled coding, and managed to do a couple things like load wav data into a float array and play it back, either forwards or backwards (triggered by button presses). Also managed to give each playback instance its own thread which allows the various sounds to overlap. The fidelity seems pretty nice.

What I’m now wondering about is whether one can use the MouseMotionDetector calls to feed data to a SourceDataLine for playback. I was thinking of doing this: move the mouse over a rectangle, and use the x coordinates of successive MouseMotionDetection calls to grab corresponding sections of my floats array and ship it off for playback. (This would include the needed linear interpolations as the mouse moves at different speeds.)

It seems like the number of potential problems is rather large & I’m having some trouble conceptualizing this. I would assume I’d open a line before I start moving the mouse around, and send data to that same line with each MouseMotionDetection call. But if each of these is a separate thread, it seems that concurrency problems are inevitable. Should I try to make some sort of FIFO buffer? Haven’t gone about that before. Any recommendations as to the data structure to accomplish this?

So, there would be a “blocking” while loop draining the FIFO in its own thread while the MouseMotionDetector threads are loading up the FIFO? What’s to keep these from themselves causing concurrency problems at the loading end? Hmmm. Maybe need to back up a couple steps, send the x coords & times to the fifo instead of the float data…hmmm.

Current code with the mouse sounds pretty chaotic. I’m pretty sure that there is concurrency confusion/chaos going on. It’s an interesting playback but not what I’m going for.

I came up with this:

there is a separate playback thread that consults state variables such as the X-location of the mouse and whether to play or not

the MouseMotionListener updates the state variables but is otherwise entirely decoupled from the playback thread

It seems to work. I can load a sound (wav file) into a float array, then, start it playing. The lazy gui has an orange “playhead” cursor on a rectangle. By placing the mouse in front or behind the “playhead” cursor, the cursor changes direction in an attempt to follow the mouse. There is a sort of rubber band effect–the closer the mouse to the playback head, the slower the playback head moves (pitch drops), the further away, the faster the playback (pitch rises).

Clear as mud, I’m sure. But kind of cool to actually do some crude audio functions, finally.