We are about to switch to a new forum software. Until then we have removed the registration on this forum.
It sounds a bit strange, but basically I want a sound effect to play every time something special happens in my sketch, but the results vary from the soundfile only playing ones to cases where later attempts to play the file innterupts earlier instantations. I've tried both Minim and the processing Sound library, but I haven't been able to make it work. How could this be done?
Answers
you need to rewind files when they finish.
you'll need multiple objects using the same source file, i think.
post some code. anything else just requires us to guess what you've done.
This is the code:
Rewind does help, but it doesn't play the same file over itself. I'd like to be able to use multiple objects, but I potentially want to play around 1000 files at once, and loading the file multiple times drains memory quite drastically...
Declare an AudioPlayer[] global variable.
So you can store all of your loadFile() within setup() and use them everywhere else.
Demo below.
Kf
Thank you kfrajer! But while this does work, it might still run low on memory if I want to be certain that a large number of files are played at the same time. Is there no way to only load the file once?
Hmmm loading the file once and playing the same resource multiple times, not sure if it is possible. An analogy is that instead of having an army, you want to have a single soldier and tell it to go right while at the same time telling it to go left?
Maybe if you provide more details about the nature of your projects, your files and how many you want to play, then other forum goers could add additional comment on your challenge.
Kf
Well, basically I'm drawing multiple quadratic functions and whenever two of them intersect I want to play a sound, but when I've got about 50 of them it feels a bit like I'm wasting space if I load files for all of the lines separately. It does work when I do that though, so this may just be something I have to accept...
@Eiroth -- It is possible. Using the minim library, load a Sampler and allocate up to a certain number of "voices" for multiple simultaneous playback.
See this recent discussion for an example:
This seems very unlikely, unless your sound sample is quite long. At a certain time resolution there would be no perceptual difference between 50 or 100 overlapping samples and 1000.
Keep in mind that samples triggered in the same frame are identical -- you only need one. If you have 60 fps, and are triggering new overlapping samples at 1/60th sec rate, then your sample would need to be over 16 seconds long with new sampling triggered every single frame before you would need 1000 voices.
Even then I doubt you could tell much difference between that audio mess and the audio mess that you would get from triggering 500 voices at 30fps -- they might sound almost identical (although this is outside my expertise, and you would need to test it).
If you have a normal 2-3 second sample at 60 fps then 180 voices is the outer limit, and I would guess you need far less.
Thanks so much for the help guys! If anyone's interested, this is probably how the code turned out. It's pretty messy but it works: