How to stop the audio file with specific condition?

Meng's icon

Hi all

I have a new question about the audio file. Concretely, there is an integer as the input to the patcher, if the variable is greater than the threshold, an audio file will be played, and when it goes below the threshold, a different sound will play. I want to add a third condition, if the variable is equal to 0, stop whichever sound is playing at that moment, and when the value changes, it works as in a usual way. The current patcher is attached, hope it is helpful.

test.txt
txt
Source Audio's icon

Hi Mengwei,

Your question could have been like this :
I want to set threshold at some value, and if incoming number
is lower than threshold it should start playback on first sfplay.
If it is higher than threshold then it should start playback on second sfplay.
Only one player should be playing at a time, so starting one means stoping the other one.
( what if incoming number is equal to threshold ? ) I suggested using split object, remember ?
or at least >= 320 ....
If incoming number is 0, then any player that was playing should pause ??? or stop ???
And on next number greater than 0 , what should happen ?
1 - resume the playback on selected player if it was paused ?
2 - start playback on selected player if it was not paused but stoped ?
-----------
It would be all easier for You if players would not pause, but stop.
Then You could do it simply.
If You want pause - resume thing, than You have to store players state somewhere,
and than trigger the appropriate action when needed.
the states could be : 0 = player is stoped, 1 = Player is playing, 2 = player is paused.
That is because sfplay will do nothing if You tell it to resume in case it is not in paused state.
***Big mistake in Your patch is connecting both outlet of change object AND resume message to sfplay ***
Even if player was in paused state it will never resume, because change would send it 1, so it would
allways start playback from the beginning.
And what happened to resetting change object when player stops playback ?

I understand that You are just beginning with Max, but that does not mean that You can not make
a good plan on what You want to do.
So stop or pause on zero input ?
As soon as that gets cleared I'll be happy to patch something for You.
In the meantime have a look at "select" or short version "sel" object.
It will bang when input equals set argument, and pass the rest on second outlet.
Greetings !

Source Audio's icon
Max Patch
Copy patch and select New From Clipboard in Max.
Meng's icon

Hi "Source Audio"

I am really sorry that I didn't make my question clear to understand. But, it is amazing to see that you get what I want to ask! And yes, you are right, I should plan my job first before working on it. In this work, I didn't realize the difficulties and effects of using pause or stop in the sfplay~. Stop may be a simpler and better choice in this work. But I've found the patcher you uploaded by using pause and resume. I need some time to catch up so that I can look into details and the logic behind the patcher. Beginning with Max always makes me feel that what I am doing is out of control and not reasonable, so sometimes, I may lose my mind. I think it still needs time to get familiar with Max. But, it is always nice to have someone or somewhere to turn to. And thank you soooo much for your help!

Have a good night!

Source Audio's icon

Hi Mengwei, You don't need to be sorry about this or that, I am really glad to help,
as are all the users here on the forum.
It is just that sometimes a bit of criticism can be helpful,.
Yes,the logic to predict what is going to happen if I do this or that
is quite important.
I was lucky to start learning max in the very beginning of it's existance,
so everything was much easier.
Now one has to digg through wizzies, beaps, references etc
bevore there used to be PDF manual and very helpfull help files.
Anyway a good start is to check help files and reference of all objects,
because that is like vocabulary in a foreign language.
I had no time to put comments in the patch, but simplified
- there is a gate that switches between played/paused and stoped state.
Then start (1) or resume gets sent to the player, depending on players state .
Happy patching !

Meng's icon

I've carefully gone through the patcher you designed, and the logic sounds clear to me now. I have the habit of turning to help and reference file in Max, and in most cases, the help and reference pages work well. But sometimes, those information doesn't make sense or help solve a problem. So, some discussion and hints are really helpful in figuring out a problem at that circumstance. And again, thank you very much for your help!
Happy patching together! :-)

Source Audio's icon

You are welcome !
Now how about a next step - avoiding clicking and too abrupt sounding audio stop ?
Depending on played material, it could sound a bit hard to just stop audio.
So one has to ramp the volume down, and then send stop to player.
10 ms is a good starting value, which can be changed, depending on audio played.
Same when playback resumes from paused position, first start, then fade up.
Playing from beginning usually needs no fade, but one has to be sure that volume is up.

Meng's icon

Sounds interesting, your idea is to add a fade in and fade out stage about 10ms, which is changeable when the pause or resume is triggered. I will work on it now. Hope I can survive this time! Fighting!