Eliot Miranda uploaded a new version of Sound to project The Trunk:
http://source.squeak.org/trunk/Sound-eem.75.mcz ==================== Summary ==================== Name: Sound-eem.75 Author: eem Time: 22 September 2020, 11:47:06.322437 am UUID: a4d7e0e8-269c-432f-ac18-25d2d7d1ff44 Ancestors: Sound-eem.74 In deference to strong opinions voice on squeak-dev reimplement teh 64-bit alternative for SampledSound>>mixSampleCount:into:startingAt:leftVol:rightVol:, and consequently fix a non-obvious regression in the 64-bit alternative. =============== Diff against Sound-eem.74 =============== Item was changed: ----- Method: AbstractSound class>>translatedPrimitives (in category 'primitive generation') ----- translatedPrimitives ^#( (FMSound mixSampleCount:into:startingAt:leftVol:rightVol:) (PluckedSound mixSampleCount:into:startingAt:leftVol:rightVol:) (LoopedSampledSound mixSampleCount:into:startingAt:leftVol:rightVol:) (SampledSound mixSampleCount:into:startingAt:leftVol:rightVol:) - (SampledSound _64bitMixSampleCount:into:startingAt:leftVol:rightVol:) (ReverbSound applyReverbTo:startingAt:count:) ). ! Item was removed: - ----- Method: SampledSound>>_64bitMixSampleCount:into:startingAt:leftVol:rightVol: (in category 'playing') ----- - _64bitMixSampleCount: 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 sampleIndex sample i s | - <inline: #always> - <var: #aSoundBuffer type: #'short int *'> - <var: #samples type: #'short int *'> - - lastIndex := (startIndex + n) - 1. - outIndex := startIndex. "index of next stereo output sample pair" - sampleIndex := indexHighBits + (scaledIndex >> IncrementFractionBits). - [(sampleIndex <= samplesSize) and: [outIndex <= lastIndex]] whileTrue: - [sample := ((samples at: sampleIndex) * 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]]. - - scaledIndex := scaledIndex + scaledIncrement. - - sampleIndex := indexHighBits + (scaledIndex >> IncrementFractionBits). - outIndex := outIndex + 1]. - count := count - n - ! Item was changed: ----- Method: SampledSound>>mixSampleCount:into:startingAt:leftVol:rightVol: (in category 'playing') ----- 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 sampleIndex sample i s overflow | <primitive:'primitiveMixSampledSound' module: 'SoundGenerationPlugin'> <var: #aSoundBuffer type: #'short int *'> <var: #samples type: #'short int *'> + lastIndex := startIndex + n - 1. - SmallInteger maxVal > 16r3FFFFFFF ifTrue: "In 64-bits we don't have to worry about 2^15 * 2^15 overflow" - [^self _64bitMixSampleCount: n into: aSoundBuffer startingAt: startIndex leftVol: leftVol rightVol: rightVol]. - lastIndex := (startIndex + n) - 1. outIndex := startIndex. "index of next stereo output sample pair" sampleIndex := indexHighBits + (scaledIndex >> IncrementFractionBits). + [sampleIndex <= samplesSize and: [outIndex <= lastIndex]] whileTrue: + [sample := (samples at: sampleIndex) * scaledVol // ScaleFactor. + leftVol > 0 ifTrue: + [i := 2 * outIndex - 1. + s := (aSoundBuffer at: i) + (sample * leftVol // ScaleFactor). + "clip the result!!!!" + aSoundBuffer at: i put: (s > 32767 ifTrue: [32767] ifFalse: [s < -32767 ifTrue: [-32767] ifFalse: [s]])]. + rightVol > 0 ifTrue: + [i := 2 * outIndex. + s := (aSoundBuffer at: i) + (sample * rightVol // ScaleFactor). + "clip the result!!!!" + aSoundBuffer at: i put: (s > 32767 ifTrue: [32767] ifFalse: [s < -32767 ifTrue: [-32767] ifFalse: [s]])]. - [(sampleIndex <= samplesSize) and: [outIndex <= lastIndex]] whileTrue: [ - sample := ((samples at: sampleIndex) * 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 + ifTrue: [scaledVol >= scaledVolLimit] + ifFalse: [scaledVol <= scaledVolLimit]) + ifTrue: "reached the limit; stop incrementing" + [scaledVol := scaledVolLimit. - 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]]. scaledIndex := scaledIndex + scaledIncrement. + "In 64-bits we don't have to worry about scaled indexes overflowing, + and in the primitive this is guard evaluated at compile time." + (SmallInteger maxVal <= 16r3FFFFFFF + and: [scaledIndex >= ScaledIndexOverflow]) ifTrue: + [overflow := scaledIndex >> IncrementFractionBits. - scaledIndex >= ScaledIndexOverflow ifTrue: [ - overflow := scaledIndex >> IncrementFractionBits. indexHighBits := indexHighBits + overflow. scaledIndex := scaledIndex - (overflow << IncrementFractionBits)]. sampleIndex := indexHighBits + (scaledIndex >> IncrementFractionBits). outIndex := outIndex + 1]. + "But we still have to update indexHighBits and scaledIndex correctly outside the loop..." + SmallInteger maxVal > 16r3FFFFFFF ifTrue: + [overflow := scaledIndex >> IncrementFractionBits. + indexHighBits := indexHighBits + overflow. + scaledIndex := scaledIndex - (overflow << IncrementFractionBits)]. count := count - n! |
Free forum by Nabble | Edit this page |