Adjusting the volume of a sound as it's playing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 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

Reply | Threaded
Open this post in threaded view
|

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

David T. Lewis
This thread died out in late August, and was mentioned today in
the "Squeak and the SoundPlugin in cog VM" thread.

@Stef, could you please attach a fileout with the fixed method? I
think that it is just one method change, so I can put that in the
inbox and make sure it gets included in trunk.

Thanks!
Dave

On Sun, Aug 18, 2019 at 12:07:24PM +0200, St??phane Rollandin wrote:

>
> >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
>

Reply | Threaded
Open this post in threaded view
|

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

Stéphane Rollandin
> @Stef, could you please attach a fileout with the fixed method? I
> think that it is just one method change, so I can put that in the
> inbox and make sure it gets included in trunk.
There are actually three methods, one for MixedSound, one for
RepeatingSound and one for SequentialSound. You may want to recategorize
them.

I also attached a modification for AbstractSound>>#loudness which uses
the amplitude envelope when it is present. I do not remember why I did
this, but I'm confident that works :) But please do not take my code for
granted and review it.

Stef



MixedSound-mixSampleCountintostartingAtleftVolrightVol.st (1K) Download Attachment
RepeatingSound-mixSampleCountintostartingAtleftVolrightVol.st (1K) Download Attachment
AbstractSound-loudness.st (460 bytes) Download Attachment
SequentialSound-mixSampleCountintostartingAtleftVolrightVol.st (1K) Download Attachment