[squeak-dev] MixedSound (& consequently stereo) is badly broken in both 32-bit and 64-bit versions

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


Ok, it looks like your choice of frequency let a bug surface.

In SampledSound>>#reset, the computation of scaledIncrement depends on 
the sampling rate. But scaledIncrement is also supposed to be somewhat 
commensurate with ScaledIndexOverflow which has a fixed value set in the 
class-side initialize.

So I guess the whole LargePositiveIntegers avoidance scheme is bogus. If 
you get rid of it altogether, which in 
SampledSound>>#mixSampleCount:into:startingAt:leftVol:rightVol: 
translates as follow (primitive uncommented):

-----

mixSampleCount: n into: aSoundBuffer startingAt: startIndex leftVol: 
leftVol rightVol: rightVol
	"Mix the given number of samples with the samples already in the given 
buffer starting at the given index. Assume that the buffer size is at 
least (index + count) - 1."

	| lastIndex outIndex sample i s |
"	<primitive:'primitiveMixSampledSound' module:'SoundGenerationPlugin'>
	<var: #aSoundBuffer declareC: 'short int *aSoundBuffer'>
	<var: #samples declareC: 'short int *samples'>
"
	lastIndex := (startIndex + n) - 1.
	outIndex := startIndex.    "index of next stereo output sample pair"

	[(outIndex <= samplesSize) and: [outIndex <= lastIndex]] whileTrue: [
		sample := ((samples at: outIndex) * scaledVol) // ScaleFactor.
		leftVol > 0 ifTrue: [
			i := (2 * outIndex) - 1.
			s := (aSoundBuffer at: i) + ((sample * leftVol) // ScaleFactor).
			s >  32767 ifTrue: [s :=  32767].  "clipping!"
			s < -32767 ifTrue: [s := -32767].  "clipping!"
			aSoundBuffer at: i put: s].
		rightVol > 0 ifTrue: [
			i := 2 * outIndex.
			s := (aSoundBuffer at: i) + ((sample * rightVol) // ScaleFactor).
			s >  32767 ifTrue: [s :=  32767].  "clipping!"
			s < -32767 ifTrue: [s := -32767].  "clipping!"
			aSoundBuffer at: i put: s].

		scaledVolIncr ~= 0 ifTrue: [
			scaledVol := scaledVol + scaledVolIncr.
			((scaledVolIncr > 0 and: [scaledVol >= scaledVolLimit]) or:
			 [scaledVolIncr < 0 and: [scaledVol <= scaledVolLimit]])
				ifTrue: [  "reached the limit; stop incrementing"
					scaledVol := scaledVolLimit.
					scaledVolIncr := 0]].

		outIndex := outIndex + 1].
	count := count - n.

-----

then you get a nice sine wave (which still play badly in my Squeak image 
but now only because it is too slow to generate without a primitive).

Stef


More information about the Squeak-dev mailing list