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

commits at source.squeak.org commits at source.squeak.org
Sat Oct 23 22:52:29 UTC 2021


Eliot Miranda uploaded a new version of Sound to project The Trunk:
http://source.squeak.org/trunk/Sound-eem.83.mcz

==================== Summary ====================

Name: Sound-eem.83
Author: eem
Time: 23 October 2021, 3:52:26.745004 pm
UUID: bb4ab1c4-cd31-45f2-80b8-0b8a76870de8
Ancestors: Sound-ct.82

Avoid busy loop in playAndWaitUntilDone
Use Float64Array in FWT.

=============== Diff against Sound-ct.82 ===============

Item was changed:
  ----- Method: AbstractSound>>playAndWaitUntilDone (in category 'playing') -----
  playAndWaitUntilDone
  	"Play this sound to the sound ouput port and wait until it has finished playing before returning."
  
+ 	| duration timeTaken delay |
+ 	duration := [self duration] on: MessageNotUnderstood do: [:ex| SmallInteger maxVal].
+ 	timeTaken := Time utcMicrosecondClock.
  	self play.
+ 	timeTaken := Time utcMicrosecondClock - timeTaken // 1000.
+ 	duration > timeTaken ifTrue:
+ 		[delay := Delay forMilliseconds: (SoundPlayer bufferMSecs min: duration - timeTaken).
+ 		 [self samplesRemaining > 0] whileTrue: [delay wait]].
+ 	(Delay forMilliseconds: SoundPlayer bufferMSecs) wait  "ensure last buffer has been output"
+ 
+ 	"AndreasSystemProfiler spyOn: [PluckedSound bachFugue playAndWaitUntilDone]"!
- 	[self samplesRemaining > 0] whileTrue.
- 	(Delay forMilliseconds: 2 * SoundPlayer bufferMSecs) wait.  "ensure last buffer has been output"
- !

Item was changed:
  ----- Method: FWT>>doWaveDemo (in category 'testing') -----
  doWaveDemo  "FWT new doWaveDemo"
  	"Printing the above should yield a small number -- I get 1.1e-32"
  	| originalData |
  	self nSamples: 312 nLevels: 3.
  	self setAlpha: 0.0 beta: 0.0.
  
  	"Install a sine wave as sample data"
+ 	self samples: ((1 to: nSamples) collect: [:i | ((i-1) * 0.02 * Float pi) sin] as: Float64Array).
- 	self samples: ((1 to: nSamples) collect: [:i | ((i-1) * 0.02 * Float pi) sin]).
  	originalData := samples copy.
  	FFT new plot: (samples copyFrom: 1 to: nSamples) in: (0 at 0 extent: nSamples at 100).
  
  	"Transform forward and plot the decomposition"
  	self transformForward: true.
  	transform withIndexDo:
  		[:w :i |
  		FFT new plot: (w copyFrom: 1 to: w size-5)
  			in: (i-1\\2*320@(i+1//2*130) extent: (w size-5)@100)].
  
  	"Test copy out and read in the transform coefficients"
  	self coeffs: self coeffs.
  
  	"Ttransform back, plot the reconstruction, and return the error figure"
  	self transformForward: false.
  	FFT new plot: (samples copyFrom: 1 to: nSamples) in: (320 at 0 extent: nSamples at 100).
  	^ self meanSquareError: originalData!

Item was changed:
  ----- Method: FWT>>nSamples:nLevels: (in category 'initialization') -----
  nSamples: n nLevels: nLevs
  	"Initialize a wavelet transform."
  	"Note the sample array size must be N + 5, where N is a multiple of 2^nLevels"
  	| dyadSize |
  	(n // (1 bitShift: nLevs)) > 0 ifFalse: [self error: 'Data size error'].
  	(n \\ (1 bitShift: nLevs)) = 0 ifFalse: [self error: 'Data size error'].
  	nSamples := n.
+ 	samples := Float64Array new: n + 5.
- 	samples := Array new: n + 5.
  	nLevels := nLevs.
  	transform := Array new: nLevels*2.  "Transformed data is stored as a tree of coeffs"
  	dyadSize := nSamples.
  	1 to: nLevels do:
  		[:i |  dyadSize := dyadSize // 2.
+ 		transform at: 2*i-1 put: (Float64Array new: dyadSize + 5).
+ 		transform at: 2*i put: (Float64Array new: dyadSize + 5)]!
- 		transform at: 2*i-1 put: (Array new: dyadSize + 5).
- 		transform at: 2*i put: (Array new: dyadSize + 5)]!

Item was changed:
  ----- Method: FWT>>setAlpha:beta: (in category 'initialization') -----
  setAlpha: alph beta: bet
  	"Set alpha and beta, compute wavelet coeefs, and derive hFilter and lFilter"
  	| tcosa tcosb tsina tsinb |
  	alpha := alph.
  	beta := bet.
  
  	"WaveletCoeffs..."
  	"precalculate cosine of alpha and sine of beta"
  	tcosa := alpha cos.
  	tcosb := beta cos.
  	tsina := alpha sin.
  	tsinb := beta sin.
+ 	coeffs := Float64Array new: 6.
- 	coeffs := Array new: 6.
  	
  	"calculate first two wavelet coefficients a := a(-2) and b := a(-1)"
  	coeffs at: 1 put: ((1.0 + tcosa + tsina) * (1.0 - tcosb - tsinb)
  					+ (2.0 * tsinb * tcosa)) / 4.0.
  	coeffs at: 2 put: ((1.0 - tcosa + tsina) * (1.0 + tcosb - tsinb)
  					- (2.0 * tsinb * tcosa)) / 4.0.
  
  	"precalculate cosine and sine of alpha minus beta"
  	tcosa := (alpha - beta) cos.
  	tsina := (alpha - beta) sin.
  
  	"calculate last four wavelet coefficients c := a(0), d := a(1), e := a(2), and f := a(3)"
  	coeffs at: 3 put: (1.0 + tcosa + tsina) / 2.0.
  	coeffs at: 4 put: (1.0 + tcosa - tsina) / 2.0.
  	coeffs at: 5 put: 1.0 - (coeffs at: 1) - (coeffs at: 3).
  	coeffs at: 6 put: 1.0 - (coeffs at: 2) - (coeffs at: 4).
  
  	"MakeFiltersFromCoeffs..."
  	"Select the non-zero wavelet coefficients"
  	coeffs := coeffs copyFrom: (coeffs findFirst: [:c | c abs > 1.0e-14])
  						to: (coeffs findLast: [:c | c abs > 1.0e-14]).
  
  	"Form the low pass and high pass filters for decomposition"
  	hTilde := coeffs reversed collect: [:c | c / 2.0].
  	gTilde := coeffs collect: [:c | c / 2.0].
  	1 to: gTilde size by: 2 do:
  		[:i | gTilde at: i put: (gTilde at: i) negated].
  
  	"Form the low pass and high pass filters for reconstruction"
  	h := coeffs copy.
  	g := coeffs reversed.
  	2 to: g size by: 2 do:
  		[:i | g at: i put: (g at: i) negated]
  !



More information about the Squeak-dev mailing list