Adjusting the volume of a sound as it's playing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Adjusting the volume of a sound as it's playing

Jim Rosenberg
Apologies in advance if this has been fixed in more recent versions of
Squeak; I'm developing at the moment under Squeak 4.3.

I have a project in which I'm adding sound to a longstanding body of
artistic work implemented in Squeak. I'm going to need several sounds
playing at once; typically there will be one or more instances of
RepeatingSound and probably just one (or zero) MixedSound. When the
MixedSound comes on I want to turn down the volume on the other sounds. How
to do this. It has been driving me crazy. I was getting absolutely nothing
to work to actually change the volume.

There seems to be the right method in AbstractSound --
adjustVolumeTo:overMSecs: -- but in Squeak 4.3 this seems to not be
properly overridden in MixedSound or RepeatingSound. I was getting
absolutely nothing to work until I stumbled onto changing leftVols and
rightVols by brute force at an explorer. This DOES WORK.

leftVols and rightVols are instance variables in MixedSound, so it seems to
me they should be changed by MixedSound's own implementation of
adjustVolumeTo:overMSecs:. RepeatingSound should have its own
implementation which just passes on to its sound.

Maybe there's a better way to do this, but given that AbstractSound does
have the right method, it seems like overriding here is the right thing to
do.

Comments?

-Thanks, Jim

Reply | Threaded
Open this post in threaded view
|

Re: Adjusting the volume of a sound as it's playing

Stéphane Rollandin
> There seems to be the right method in AbstractSound --
> adjustVolumeTo:overMSecs: -- but in Squeak 4.3 this seems to not be
> properly overridden in MixedSound or RepeatingSound. I was getting
> absolutely nothing to work until I stumbled onto changing leftVols and
> rightVols by brute force at an explorer. This DOES WORK.
>
> leftVols and rightVols are instance variables in MixedSound, so it seems to
> me they should be changed by MixedSound's own implementation of
> adjustVolumeTo:overMSecs:. RepeatingSound should have its own
> implementation which just passes on to its sound.

For the volume control to work in MixedSound, it seems you would have to
replace the lines

        left := (leftVol * (leftVols at: i)) // ScaleFactor.
        right := (rightVol * (rightVols at: i)) // ScaleFactor.

with

        left := (leftVol * (leftVols at: i) * scaledVol) // ScaleFactor.
        right := (rightVol * (rightVols at: i) * scaledVol) // ScaleFactor.

in MixedSound>>#mixSampleCount:into:startingAt:leftVol:rightVol:

Does that work?

Stef

Reply | Threaded
Open this post in threaded view
|

Re: Adjusting the volume of a sound as it's playing

Stéphane Rollandin
In reply to this post by Jim Rosenberg
>  RepeatingSound should have its own
> implementation which just passes on to its sound.

Maybe just replace

                sound mixSampleCount: count
                        into: aSoundBuffer
                        startingAt: i
                        leftVol: leftVol
                        rightVol: rightVol.

with

                sound mixSampleCount: count
                        into: aSoundBuffer
                        startingAt: i
                        leftVol: leftVol * scaledVol
                        rightVol: rightVol * scaledVol.

in RepeatingSound>>#mixSampleCount:into:startingAt:leftVol:rightVol:

Is that ok?

Stef

Reply | Threaded
Open this post in threaded view
|

Re: Adjusting the volume of a sound as it's playing

Stéphane Rollandin
>>   RepeatingSound should have its own
>> implementation which just passes on to its sound.
>
> Maybe just replace
>
> sound mixSampleCount: count
> into: aSoundBuffer
> startingAt: i
> leftVol: leftVol
> rightVol: rightVol.
>
> with
>
> sound mixSampleCount: count
> into: aSoundBuffer
> startingAt: i
> leftVol: leftVol * scaledVol
> rightVol: rightVol * scaledVol.
>

Hmmm...

Make it

                sound mixSampleCount: count
                        into: aSoundBuffer
                        startingAt: i
                        leftVol: leftVol * scaledVol // ScaleFactor
                        rightVol: rightVol * scaledVol // ScaleFactor.

Stef

Reply | Threaded
Open this post in threaded view
|

Re: Adjusting the volume of a sound as it's playing

Stéphane Rollandin
In reply to this post by Stéphane Rollandin

> For the volume control to work in MixedSound, it seems you would have to
> replace the lines
>
> left := (leftVol * (leftVols at: i)) // ScaleFactor.
> right := (rightVol * (rightVols at: i)) // ScaleFactor.
>
> with
>
> left := (leftVol * (leftVols at: i) * scaledVol) // ScaleFactor.
> right := (rightVol * (rightVols at: i) * scaledVol) // ScaleFactor.
>
> in MixedSound>>#mixSampleCount:into:startingAt:leftVol:rightVol:

scaledVol is not in the [0,1] range, so that would be

        left := (leftVol * (leftVols at: i) * scaledVol // ScaleFactor) //
ScaleFactor.
        right := (rightVol * (rightVols at: i) * scaledVol // ScaleFactor) //
ScaleFactor.

Stef