text
stringlengths
0
897
To help stretch those new coding muscles, here's one more sample program to try.
{caption: "String and number functions review"}
```miniscript
print "The next whole number after pi is " + ceil(pi)
print "...which is " + "Huge!".upper
print "Rounded to 2 decimal places, pi is " + round(pi, 2)
print "...which is " + "NOT So Huge".lower
print """Hello World"" has " + "Hello World".len + " characters"
print "...and the code point of the first one is " + "H".code
print "Your lucky number is: " + floor(rnd * 100)
```
As always, be sure to type carefully. Especially be careful with the embedded (and so doubled) quotation marks in line 5. (Review Day 1 if you've forgotten what that's about.)
A> **Chapter Review**
A> - You discovered how to do math with numbers and strings.
A> - You can invoke functions, including providing arguments, and using dot syntax.
A> - You browsed a list of built-in numeric and string functions.
A> - You learned that you don't need to memorize all the details, because it's OK to look them up when you need them.
{chapterHead: "Day 20: Sounds & Music", startingPageNum:237}
{width: "50%"}
![](Chapter20.svg)
Q> There is no recipe, there is no one way to do things — there is only your way. And if you can recognize that in yourself and accept and appreciate that in others, you can make magic.
Q>— Ara Katz (co-founder of Seed Health, Spring, and BeachMint)
A> **Chapter Objectives**
A> - Load and play digitized sounds from files on disk.
A> - Learn how to synthesize sounds from scratch.
A> - Use your new audio skills to create music from code.
So far, working with Mini Micro has been like watching a silent movie. Unless you happen to have printed `char(7)` at some point, your programs have not made a peep. Grab your earbuds or apologize to your neighbor, because that's going to change today!
## Playing Sound Files
There are two ways to make sounds in Mini Micro. The first one involves loading a digitized sound from disk, and simply playing it.
Mini Micro comes with a bunch of sound files in `/sys/sounds`. `cd` to that directory, then use `dir` to list them. Use the `view` command to hear what they sound like (remember to put quotes around the file name!). Many of the sounds are musical instruments, and we'll cover how to use these to make music in the las...
To play these sounds from code is easy: load the sound, and then play it.
```terminal
]snd = file.loadSound("/sys/sounds/bonus.wav")
]snd.play
```
{i:"`Sound` class"}
The result of `file.loadSound` is a `Sound` object, another built-in class in Mini Micro. This class has some properties that don't apply to file-based sounds, so for now let's just consider the following:
{caption:"Properties and methods for digitized sounds. *snd* is any `Sound` object.", colWidths:"150,*"}
|`snd.duration`|length of the sound, in seconds|
|`snd.loop`|if true, repeat the sound indefinitely until stopped|
|`snd.play` *volume=1*, *pan=0*, *speed=1*|play this sound|
|`snd.stop`|stop playing this sound|
|`snd.isPlaying`|returns 1 (true) if sound is playing, 0 (false) if not|
|`Sound.stopAll`|stop all currently playing sounds|
The `play` method is the only one with any parameters, though they are all optional. The parameters are:
- *volume*: how loud to play, from 0 (silent) to 1 (full volume).
- *pan*: adjusts the left/right stereo balance, from -1 (left speaker only) to 1 (right speaker only). 0 means to play in both speakers equally.
- *speed*: a factor that controls payback speed and pitch. 1 is normal speed, 0.5 is half speed, 2 is double speed, and so on. Faster speeds produce a higher pitch.
As an example, try this `for` loop:
{caption:"How to play the guitar."}
```miniscript
snd = file.loadSound("/sys/sounds/elecGuitarC4.wav")
for x in range(1, 2, 0.25)
snd.play 1, 0, x
wait snd.duration
end for
```
This plays the sound five times, at a different speed each time. Notice that when you play a sound at double speed, it sounds exactly one octave higher.
Of course you are not limited to the sounds that ship with Mini Micro. You can import your own sounds, in WAV or OGG format. The easiest way to do this is to click on the lower disk slot, use "Mount Folder..." to mount a folder in the host OS containing your sound files, and then access these in Mini Micro under the ...
## Synthesizing Sounds
Loading a sound file is one way of making sounds. The other way is to create sounds from scratch, using Mini Micro's sophisticated synthesizer.
To understand how synthesized sounds work, we first have to talk a bit about how sound works in general. Sound is repeated, tiny changes in air pressure over time. These air pressure variations cause little receptors in your ear to vibrate. Each receptor is tuned to a different *frequency*, which is how many times t...
Low frequencies, like 20 Hz, sound like very deep, low sounds, while high frequencies are high-pitched sounds. The relationship between frequency and pitch is not linear; to go up one octave in pitch, you must double the frequency. Conversely, to go down one octave in pitch, you would cut the frequency in half.
Speakers are simply hardware that produce these changes in air presure, by moving the speaker cone back and forth according to an electronic signal. Microphones do the reverse, responding to changes in air pressure by producing a signal that a computer can read. This is known as *digitizing* the sound, and is how mos...
If you were to take the signal from a microphone and graph it, to represent the air pressure changes visually, you would often see some pattern that repeats over and over, hundreds or thousands of times per second. That pattern is called the *waveform*.
waveform
: one cycle of a sound wave, representing the air pressure changes that are repeated *frequency* times per second
frequency
: how many times per second the waveform is repeated to produce a sound