120 likes | 296 Views
Using Squeak for media computation. Graphics and sound low-level manipulations in Squeak (Smalltalk). What is Squeak?. Re-implementation and extension of (original) Smalltalk-80, with VM written in Smalltalk
E N D
Using Squeak for media computation Graphics and sound low-level manipulations in Squeak (Smalltalk)
What is Squeak? • Re-implementation and extension of (original) Smalltalk-80, with VM written in Smalltalk • Runs on over 30 different platforms, including Linux, Windows, MacOS, Solaris…even Windows CE • Apple license allows commercial apps, but system fixes must be posted • Squeak has faster, more stable, cross-platform media than Java • Media: 3-D graphics, MIDI, Flash, MPEG, sound recording • Network: Web, POP/SMTP, zip compression/decompress • Beyond Smalltalk-80: Exceptions, namespaces
Heart of graphics in Squeak: Form • The class Form is what represents pictures in any Smalltalk. • To load it from a file, Form fromFileNamed: ‘myfile.gif’ (or .jpg or .bmp) • To display a Form in Smalltalk, you can just send it the message display • To display it in a “window” (Morph) in Squeak, use a SketchMorph • (SketchMorph withForm: form) openInWorld • To write it out to JPEG file, use form writeJPEGfileNamed: filename
Manipulating pixels in Squeak • Forms understand form colorAt: x@y which returns a Color • Colors understand methods like red, green, blue, lighter, darker, duller, brighter • Color will create new colors with the r:g:b: method • Forms understand form colorAt: x@y put: aColor
Negative example in Squeak form := Form fromFileNamed: 'C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\barbara.jpg'. 1 to: (form width) do: [:x | 1 to: (form height) do: [:y | color := form colorAt: x@y. newcolor := Color r: (1.0-color red) g: (1.0-color green) b: (1.0-color blue). "newcolor := Color r: (color luminance) g: (color luminance) b: (color luminance)." form colorAt: x@y put: newcolor]]. (SketchMorph withForm: form) openInWorld.
Sound Support in Squeak • Class AbstractSound is heart of sound support in Squeak • It knows how to write files • sound storeWAVonFileNamed: ‘fred.wav’ • sound storeAIFFonFileNamed: ‘fred.aif’ • Class SampledSound knows how to read recorded sounds • (SampledSound fromAIFFfileNamed: '1.aif') play • (SampledSound fromWaveFileNamed: 'c:\windows\media\chimes.wav') play
Manipulating Sounds: SoundBuffer • A SoundBuffer knows how to manipulate samples for conversion to sounds later • sr := SoundPlayer samplingRate. • anArray := SoundBuffer newMonoSampleCount: (sr * seconds) . • SoundBuffers are manipulated with at:put: and at: like any other array in Smalltalk. • You get the SoundBuffer for a SampledSound using the method samples
Converting SoundBuffers to SampledSounds |base sound| “Generate a SoundBuffer at the right frequency” base := SoundBufferGenerator forFreq: 440 amplitude: 3000 duration: 2. sound := SampledSound samples: base samplingRate: SoundPlayer samplingRate. sound viewSamples. "sound play."
Playing a sound backwards in Squeak source := SampledSound fromWaveFileNamed: 'C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\thisisatest2.wav'. target := source copy. samples := target samples. sourceIndex := source samples size. 1 to: (samples size) do: [:index | samples at: index put: (source samples at: sourceIndex). sourceIndex := sourceIndex - 1.]. target play.