DEV Community

Sebastian Nozzi
Sebastian Nozzi

Posted on

Enabling Sound in Pharo Smalltalk

In this post I will show you how to enable sound playback in Pharo Smalltalk, which is not included by default.


The Pharo-VM is capable of playing sounds, but the classes to do so are not included in the image by default. You need to load the "PharoSound" package first.

There are two ways of doing this:

  1. Interactively, through the "Catalog Browser" or
  2. Programmatically, through code

Let's explore both ways.

Interactively

For the interactive variant, bring up a "World" menu by clicking anywhere on an empty space.

Then, under "Tools", select "Catalog Browser". Like this:

Opening the "Catalog Browser"

In the Catalog Browser, enter "sound" in the filter and press enter:

Searching for "sound" in the packages

Right-click on "Pharo Sound", and select "Install stable version" like this:

Installing stable package version

Since it has only be tested with Pharo 5, the system will warn you that it has not been tested with your current Pharo version. Accept the warning with "Yes" as it should work anyways with latest versions.

Accepting the warning

(Disclaimer: this was tested under Pharo 6.1/OSX)

Programmatically

The same done through the Catalog Browser can also be achieved directly by evaluating code. Evaluate this in a Playground window:

Metacello new
    smalltalkhubUser: 'Pharo' project: 'MetaRepoForPharo50';
    configuration: 'PharoSound';
    version: #stable;
    load.
Enter fullscreen mode Exit fullscreen mode

And that's it. Pharo now knows how to play sounds.

Making sure it works

To make sure it works you can open a Playground window, and try out some things ...

To play a basic sound, do the following:

SampledSound beep.
Enter fullscreen mode Exit fullscreen mode

To play a major chord using a synthesized clarinet sound, do this:

clarinet := FMSound clarinet.
(AbstractSound majorChordOn: clarinet from: #c4) play.
Enter fullscreen mode Exit fullscreen mode

To open a mini virtual-piano you can evaluate this:

PianoKeyboardMorph new openInWorld.
Enter fullscreen mode Exit fullscreen mode

You can load sounds from Wave-files (or AIFF) and play them like this:

sampledSound := (SampledSound fromWaveFileNamed: '/path/to/file.wav').
sampledSound play.
Enter fullscreen mode Exit fullscreen mode

How to learn what to do with sounds?

Explore the "AbstractSound" class and its subclasses. Start by the class-side. You will find many interesting things there. Smalltalk is best learnt by exploring.

Finally, I leave you with a fugue of J.S. Bach:

AbstractSound stereoBachFugue play
Enter fullscreen mode Exit fullscreen mode

Have fun with Smalltalk!

Top comments (1)

Collapse
 
martinceronio profile image
Martin Ceronio • Edited

This is so cool! Thanks for sharing.
You need to include instructions on how to stop all sounds. I am still listening to the fugue as I am typing this :-)

Oh yes, and just to confirm: I tried this on Pharo 7.0 64 bit on Linux and it works! (I did install some packages like osspd to enable /dev/dsp etc. while trying to get sound working in Squeak, and I imagine that is a prerequisite because I see Pharo uses the same .so objects for sound support on Linux).