heronarts / LX

Core library for 3D LED lighting engines
https://chromatik.co/
Other
41 stars 25 forks source link

Request for access to LXAudioOutput.mediaPath and/or playing audio via an InputStream #12

Closed ssilverman closed 6 years ago

ssilverman commented 6 years ago

It would be nice to have a getMediaPath() call so I don't have to use reflection to get access to the private variable. The root cause, actually, is only being able to use a File and not an InputStream when playing audio programmatically.

My workflow is to create a temporary file in the mediaPath directory, using java.nio.Files.copy() to copy the contents from an InputStream, and then starting the audio.

The core reason mediaPath is needed is because it's used as the root directory internally.

mcslee commented 6 years ago

Done here: https://github.com/heronarts/LX/commit/b04cadf3f1602274fcf88d7c2ae395586f3d327f

I also just made the setInputStream() methods public, so if you want to use an InputStream programmatically to play audio, you can.

Just note that it won't be saved in the project file.

mcslee commented 6 years ago

Also, FYI, you can get the mediaPath from your P3LX / LXStudio instance: https://github.com/heronarts/P3LX/blob/master/src/heronarts/p3lx/P3LX.java#L112

What you really want is applet.sketchPath - but the design pattern here uses setMediaPath() on the LXAudioOutput so that the LX library can be 100% free of dependencies upon P3LX.

I should perhaps consider cleaning this up so that there is a mediaPath on the root LX object, and then P3LX could just override that... something to think about.

But this should sort you out for the time being.

ssilverman commented 6 years ago

How will looping work with InputStream play? I'm guessing I'd still have to manage that myself?

mcslee commented 6 years ago

Can't 100% guarantee you what will happen there. Depends upon the type of input stream.

This is what will be attempted:

Trigger will be set to true https://github.com/heronarts/LX/blob/master/src/heronarts/lx/audio/LXAudioOutput.java#L131

Then the InputStream will try to be reset() https://github.com/heronarts/LX/blob/master/src/heronarts/lx/audio/LXAudioOutput.java#L114

If your input stream doesn't support marking and reseting, it won't work and will print an error.

Just curious - what are you doing here? What is the input stream that you're reading from? Or are you generating one? It sounded like originally you were reading it into a File for playback, which suggested the stream was already available in its entirety (from somewhere). Or were you also writing the file in real-time while playing it back?

ssilverman commented 6 years ago

I'm using resources from the project's pre-compiled JAR. The project doesn't run from a directory. Because LXAudioOutput only uses a File as input, I had to copy the audio file into a temporary file.

In fact, I have to take this approach anywhere a File is assumed in the API.

mcslee commented 6 years ago

Ah gotcha - that makes sense. You can see if the InputStream method works, but copying into a temp file seems like a reasonable enough solution as well.

Let me know if other File spots are really annoying - the only other one I can think of is loading project files.