Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Wednesday, 18 November 2015

Playing Sounds

One of the features in the recent release is support for playing sound files. We've actually had a "sound" device (on Hosted platforms) for some time, but so far it only actually worked with js-sniff, where the compiled code runs in a browser. This meant you could write games in Sniff and have sound when you embed them in a web page, but the sounds wouldn't actually play when you ran the code as a real program on your own machine!

We've fixed that, and now you can play sounds on Mac, Windows and Linux.

make player sound device
make fileData string

when start
.set fileData to "chainsaw.wav"
.tell player to "load sound"
..tell player to "play"
..wait 10 secs

The code is about as simple as it gets: we make a sound device (called player in this case), and tell the player the name of the sound file we want it to play. With that out of the way, we can just tell it to play whenever we want to hear that sound.

If you want to play multiple sounds then you can either create multiple devices, or create one device, and keep telling it to load different sounds. Use whichever approach makes most sense for your app.

Because each system handles sound file slightly differently there are minor variations in behaviour across platforms. On Mac and JS you should be able to use most types of sound files, while on windows and Linux it must be a WAV. On Windows if the file can't be loaded a default "ping" sound is played instead. The Linux version uses the external program "aplay" to actually play the sound. This is standard in most distributions. Some Linux variants give a warning that a return value is ignored when you compile the Sniff program - this is harmless, and we've fixed it in new versions so if you do see it, just ignore it.

That's about it - get out there, play some music, and add some sound effects to your Sniff games!

No comments:

Post a Comment