[squeak-dev] The Trunk: Sound-eem.72.mcz

Stéphane Rollandin lecteur at zogotounga.net
Sat Sep 19 15:09:59 UTC 2020


> Actually there is another conceptual problem with
> MixedSound>>add:pan:volume:
> 
> It does not check whether the added sound is mono or stereo.
> 
> As a consequence, when using MixedSound>>add: (which applies a pan of
> 0.5) with a stereo sound as argument, that sound overall volume gets
> decreased because the corresponding leftVols and rightVols values are
> set to obey the pan law.
> 
> But I would expect the values added to leftVols and rightVols to be
> ScaleFactor so that the newly added sound is mixed as is, with no change
> in volume.
> 
> To see (well, hear) the problem, use the attached stereo sound as follow:
> 
> 	snd := SampledSound fromWaveFileNamed: 'sound.wav'.
> 
> 	ms := MixedSound new.
> 	ms add: snd.
> 
> 	snd play. "correct volume"
> 	ms play. "lower volume"


Fix attached.

It keeps the same behaviour as before if the added sound is mono, or if 
the pan argument is not 0.5

In the other cases, that is for adding a stereo sound centered in the 
mix, it just bypasses any panning calculation.

Note that I am not proposing this fix to be included as is, only tested 
and examined, because it also changes the way panning is computed by 
implementing a -4.5db power law (rationale here: 
http://www.cs.cmu.edu/~music/icm-online/readings/panlaws/index.html) 
which is the one I use in muO.

Stef

-------------- next part --------------
'From Squeak5.3 of 3 March 2020 [latest update: #19431] on 19 September 2020 at 5:03:18 pm'!

!MixedSound methodsFor: '*MuO-override' stamp: 'spfa 9/19/2020 15:21'!
add: aSound
	"Add the given sound with a pan setting of centered and no attenuation."

	aSound isStereo ifFalse: [
		^ self add: aSound pan: 0.5 volume: 1.0.].

	"If aSound is already stereo, there is no point in messing with its panning"
	sounds := sounds copyWith: aSound.
	leftVols := leftVols copyWith: ScaleFactor.
	rightVols := rightVols copyWith: ScaleFactor.
	^ aSound
! !

!MixedSound methodsFor: '*MuO-override' stamp: 'spfa 9/19/2020 15:22'!
add: aSound pan: leftRightPan
	"Add the given sound with the given left-right panning and no attenuation."

	(leftRightPan closeTo: 0.5) ifTrue: [^ self add: aSound; yourself].

	self add: aSound pan: leftRightPan volume: 1.0.
! !

!MixedSound methodsFor: '*MuO-override' stamp: 'spfa 9/19/2020 17:03'!
add: aSound pan: leftRightPan volume: volume
	"Add the given sound with the given left-right pan, where 0.0 is full left, 1.0 is full right, and 0.5 is centered. The loudness of the sound will be scaled by volume, which ranges from 0 to 1.0."

	"Note that MuO uses a -4.5 dB pan law"

	|  vol pvs |

	(aSound isStereo and: [leftRightPan closeTo: 0.5]) ifTrue: [
		"0.5 for pan is interpreted as 'do nothing' if aSound is already stereo (see senders)"
		vol := (ScaleFactor * volume) rounded.
		sounds := sounds copyWith: aSound.
		leftVols := leftVols copyWith: vol.  
		rightVols := rightVols copyWith: vol..
		^ self]..

	vol := (volume max: 0.0) min: 1.0.
	sounds := sounds copyWith: aSound.

	pvs := self class panValuesFor: leftRightPan.
	leftVols := leftVols copyWith: (pvs first * vol) rounded.
	rightVols := rightVols copyWith: (pvs second * vol) rounded
! !


!MixedSound class methodsFor: '*MuO' stamp: 'spfa 9/19/2020 17:00'!
db45PanAt: pNumber

	"pNumber ranges from -1 (45 degrees left) to 1 (45 degrees right)"

	"Uses -4.5 dB pan law
ref: http://www.cs.cmu.edu/~music/icm-online/readings/panlaws/index.html"

	| theta |

	theta := (pNumber + 1) * Float pi / 4.

	^ {
		"left" (theta cos * (Float halfPi - theta) / Float halfPi) sqrt.
		"right" (theta sin * theta / Float halfPi) sqrt
	}! !

!MixedSound class methodsFor: '*MuO' stamp: 'spfa 9/19/2020 17:00'!
panValuesFor: aPos

	"aPos ranges from 0.0 to 1.0 (Squeak convention)"

	"MuO uses a -4.5 dB pan law"

	^  (self db45PanAt: (aPos * 2) - 1) collect: [:ea | 
		((ea * ScaleFactor) rounded max: 0) min: ScaleFactor]! !


!MixedSound reorganize!
('accessing' duration sounds)
('composition' +)
('copying' copySounds postCopy)
('initialization' initialize)
('sound generation' doControl mixSampleCount:into:startingAt:leftVol:rightVol: reset samplesRemaining stopGracefully)
('*MuO-override' add: add:pan: add:pan:volume: isStereo)
('*MuO' addSansPanning: asSoundElementsWithDelay:forEdition: musicalNotesWithDelay: nominalDuration pitch: soundEditorClass |)
!



More information about the Squeak-dev mailing list