Skip to content

How_to_use_sound

hhzl edited this page May 15, 2025 · 9 revisions

Installation of the 'Sound' package

Feature require: 'Sound'.

First example

(AbstractSound noteSequenceOn: 
    PluckedSound default from: 
      #((c4 1 500) 
        (d4 1 500) 
        (e4 1 500) 
        (g4 1 500) 
        (a4 1 250)
        (a4 1 250) 
        (c5 1 500)
       ) 
) play

Source: http://wiki.squeak.org/squeak/152

The information about what to play is in this array of arrays:

#((c4 1 500) 
        (d4 1 500) 
        (e4 1 500) 
        (g4 1 500) 
        (a4 1 250)
        (a4 1 250) 
        (c5 1 500)
       ) 

If you do a printIt of it or open an inspector you get

#(#(#c4 1 500) 
  #(#d4 1 500) 
  #(#e4 1 500) 
  #(#g4 1 500) 
  #(#a4 1 250) 
  #(#a4 1 250) 
  #(#c5 1 500)) 

Each note is defined by a triplet starting with a symbol (e.g. #c4.) and the two integer values.The individual notes are given by a symbol,
Triplets of

  • note (a-g, optionally b or s for flat or sharp, and a number indicating octave)
  • duration, and loudness
  • loudness range is 0..1000;

The note may also be given as an integer value in Hertz as the class method of PluckedSound bachFugueVoice1On: aSound shows

bachFugueVoice1On: aSound
	"Voice one of a fugue by J. S. Bach."

	^ self noteSequenceOn: aSound from: #(
		(1047 0.15 268)
		(988  0.15 268)
		(1047 0.30 268)
		(784  0.30 268)
		(831  0.30 268)
...
		(622  0.15 268)
		(587  0.30 268)
		(784  0.60 268)
		(rest 0.3)
		(988  0.30 268)
...		(932  0.30 346)
		(622  0.15 346)
		(587  0.40 268)
		(rest 0.4)
		(587  0.40 268)
		(rest 0.4)
		(523  1.60 268)).

Explore the 'examples' category of class 'AbstractSound'. For example

FMSound majorChord play

How to generate a 'WAV' file

(AbstractSound noteSequenceOn: FMSound flute2 from: #(
(e5 0.5 500)
(d5 0.5 300)
(c5 0.5 350)
(d5 0.5 300)
(e5 0.5 500)
(e5 0.5 300)
(e5 1.0 350)
(d5 0.5 400)
(d5 0.5 300)
(d5 1.0 330)
(e5 0.5 400)
(g5 0.5 300)
(g5 1.0 500)
)) storeWAVOnFileNamed: 'maryhadalittlelamb.wav'

And play it with

 (SampledSound fromWaveFileNamed: 'maryhadalittlelamb.wav') play

Source: http://wiki.squeak.org/squeak/152

Class AbstractSound

Adapted from: http://wiki.squeak.org/squeak/2125

AbstractSound is a the superclass which represents sounds. It's abstract, which means that generally you'll be working with one of its subclasses instead of AbstractSound directly.

 AbstractSound new play

just gives an error.

See the first example above how to use it.

The usual way to use these methods is with classes that inherit from AbstractSound, such as on the Squeak wiki: SampledSound http://wiki.squeak.org/squeak/2125 or FMSound.

TODO:add list of implemented instruments/sounds here

Example with specifying the tune with Solfège (do re mi)

TODO: use a Dictionary object to map Solfège sound indication and use as illustration a very basic canon

Frère Jacques (Brother John)

to create a WAV version of https://en.wikipedia.org/wiki/File:Fr%C3%A8re_Jacques.ogg

Clone this wiki locally