So I have a code I am writing where I use OpenCV for face detection and I am switching between 2 scenes depending on the distance the face is from the camera. But I have to put one audio file to play for each of these, and playing on draw() just bugs the audio.
I am using switch() for playing the 2 diferent graphics. Do you guys have any suggestions on how to make the audio switch as well?
(The code is long, but I can share if needed)
Hiya
Have a look at Example 4 here - just switch the keyboard trigger for your face detection.
Processing Sound Library, Example 4
This example shows how to make a simple keyboard-triggered sampler with the Sound
library. In this sketch 5 different short samples are loaded and played back at
different speeds, which also changes their perceived pitch by one or two octaves.
I tried that approach, but I think the audio keeps playing again and again, because the trigger is inside the draw.
I am kind of a begginer, so I tried making a new void for the face detection but it stop working ![:frowning_face: :frowning_face:](https://emoji.discourse-cdn.com/google_classic/frowning_face.png?v=12)
Hiya
you could use isPlaying to check if a sound is already playing
and stop for when you switch
then perhaps for an approach that might work - look at scene switching as used by games (also called game state)
eg in your draw loop have these steps
detect face
if face is near
- face is near code
- is face-is-near sound playing? if so do nothing, otherwise play face-is-near sound
- is face-is-far sound playing? if so stop it, otherwise do nothing
if face is far
- face is far code
- is face-is-far sound playing? if so do nothing, otherwise play face-is-far sound
- is face-is-near sound playing? if so stop it, otherwise do nothing
its fine having the face detection in a new void - you then call it at the right point in your draw loop
![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/google_classic/slight_smile.png?v=12)
2 Likes
Thank you so much!!! I was not aware of isPlaying and it worked perfectly!