[squeak-dev] Adjusting the volume of a sound as it's playing

Stéphane Rollandin lecteur at zogotounga.net
Sun Dec 8 15:14:47 UTC 2019


> @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
-------------- next part --------------
'From Squeak5.1 of 5 September 2016 [latest update: #16549] on 8 December 2019 at 3:59:31 pm'!

!MixedSound methodsFor: '*MuO-override' stamp: 'spfa 8/18/2019 12:09'!
mixSampleCount: n into: aSoundBuffer startingAt: startIndex leftVol: leftVol rightVol: rightVol
	"Play a number of sounds concurrently. The level of each sound can be set independently for the left and right channels."

	| snd left right |
	1 to: sounds size do: [:i |
		(soundDone at: i) ifFalse: [
			snd := sounds at: i.
			left := (leftVol * (leftVols at: i) * scaledVol / ScaleFactor) // ScaleFactor.
			right := (rightVol * (rightVols at: i) * scaledVol / ScaleFactor) // ScaleFactor.
			snd samplesRemaining > 0
				ifTrue: [
					snd mixSampleCount: n into: aSoundBuffer startingAt: startIndex leftVol: left rightVol: right]
				ifFalse: [soundDone at: i put: true]]].
! !
-------------- next part --------------
'From Squeak5.1 of 5 September 2016 [latest update: #16549] on 8 December 2019 at 3:59:27 pm'!

!RepeatingSound methodsFor: '*MuO-override' stamp: 'spfa 8/18/2019 11:59'!
mixSampleCount: n into: aSoundBuffer startingAt: startIndex leftVol: leftVol rightVol: rightVol
	"Play a collection of sounds in sequence."
	"(RepeatingSound new
		setSound: FMSound majorScale
		iterations: 2) play"

	| i count samplesNeeded |
	iteration <= 0 ifTrue: [^ self].
	i := startIndex.
	samplesNeeded := n.
	[samplesNeeded > 0] whileTrue: [
		count := sound samplesRemaining min: samplesNeeded.
		count = 0 ifTrue: [
			iterationCount == #forever
				ifFalse: [
					iteration := iteration - 1.
					iteration <= 0 ifTrue: [^ self]].  "done"
			sound reset.
			count := sound samplesRemaining min: samplesNeeded.
			count = 0 ifTrue: [^ self]].  "zero length sound"
		sound mixSampleCount: count
			into: aSoundBuffer
			startingAt: i
			leftVol: leftVol * scaledVol // ScaleFactor
			rightVol: rightVol * scaledVol // ScaleFactor.
		i := i + count.
		samplesNeeded := samplesNeeded - count].
! !
-------------- next part --------------
'From Squeak5.1 of 5 September 2016 [latest update: #16549] on 8 December 2019 at 4:06:21 pm'!

!AbstractSound methodsFor: '*MuO-override' stamp: 'spfa 8/16/2019 16:52'!
loudness
	"Answer the current volume setting for this sound."

	self hasVolumeEnvelope ifTrue: [^ self volumeEnvelope scale].

	^ scaledVol asFloat / ScaleFactor! !
-------------- next part --------------
'From Squeak5.2 of 13 December 2018 [latest update: #18229] on 8 December 2019 at 4:12:53 pm'!

!SequentialSound methodsFor: '*MuO-override' stamp: 'spfa 9/3/2019 10:46'!
mixSampleCount: n into: aSoundBuffer startingAt: startIndex leftVol: leftVol rightVol: rightVol
	"Play a collection of sounds in sequence."
	"PluckedSound chromaticScale play"

	| finalIndex i snd remaining count leftScaledVol rightScaledVol |
	currentIndex = 0 ifTrue: [^ self].  "already done"

	leftScaledVol := leftVol * scaledVol /// ScaleFactor.
	rightScaledVol := rightVol * scaledVol /// ScaleFactor.

	finalIndex := (startIndex + n) - 1.
	i := startIndex.
	[i <= finalIndex] whileTrue: [
		snd := (sounds at: currentIndex).
		[(remaining := snd samplesRemaining) <= 0] whileTrue: [
			"find next undone sound"
			currentIndex < sounds size
				ifTrue: [
					currentIndex := currentIndex + 1.
					snd := (sounds at: currentIndex)]
				ifFalse: [
					currentIndex := 0.
					^ self]].  "no more sounds"
		count := (finalIndex - i) + 1.
		remaining < count ifTrue: [count := remaining].
		snd mixSampleCount: count into: aSoundBuffer startingAt: i 
			leftVol: leftScaledVol
			rightVol: rightScaledVol.
		i := i + count].
! !


More information about the Squeak-dev mailing list