text
stringlengths
0
897
We've been using `Sound.sineWave`. Try `Sound.triangleWave` next, which is pretty close to a sine wave, but not quite so pure.
```terminal
]s.waveform = Sound.triangleWave
]s.play
```
It's the same pitch as before, but now a little richer, less pure. (If you need to compare again, switch back to `Sound.sineWave` and play that again.) Next try `Sound.sawtoothWave`. (I'm not showing the commands for that — you know what you're doing by now — but please try it, since you can't really know what it so...
There is one more waveform which we haven't tried yet: `Sound.noiseWave`. This one is different from all the others; while the others have a fairly simple shape, this one looks like complex, random gibberish. And that's pretty much what it is. Random, chaotic air pressure changes like that don't sound like tones to ...
```terminal
]s.waveform = Sound.noiseWave
]s.freq = 5
]s.play
```
Of course you may want to try other frequencies too, and there's certainly no harm in using high frequencies with noise if you like the sound. It's totally up to you. With the right choice of parameters, you can approximate the sound of footsteps, impacts, wind, waves, and so on.
The `init` method is a shorthand for setting `duration`, `freq`, `envelope`, and `waveform` all at once, in that order. For just playing around on the REPL, assigning (or changing) these properties one at a time is probably nicer. But in code, you might want to do more of it at once, something like:
```terminal
]s = new Sound
]s.init 0.5, [2000,50], [1,0.25]
{"__isa": Sound, "duration": 0.5, "freq": [2000, 50], "waveform": [1
, -1], "envelope": [1, 0.25], "fadeOut": 0.01, "fadeIn": 0.01, "_han
dle": null}
]s.play
```
Note that each of the parameters to `Sound.init` has a default value; in the example above, we omitted the last one (waveform), and it used `Sound.sawtoothWave` by default.
The last topic to cover in synthesizing sounds is the `mix` method. This lets you take a second synthesized sound, and add it to the first, so that when you play them you essentially get both at once. For example:
```terminal
]pew = new Sound
]pew.init 0.5, [2000,10], [1,0]
]pew.play
]psh = new Sound
]psh.init 0.2, 5, [1,0], Sound.noiseWave
]psh.play
]pew.mix psh
]pew.play
```
In lines 1-3 above, we synthesize and test a "pew" sound (a rapid falling tone). Then in lines 4-6, we synthesize and test a noisy burst sound. Line 7 mixes the noisy burst into the pew sound. Now when we play that sound, we hear the noisy burst at the beginning.
I think you can see by now that Mini Micro's sound synthesizer is very versatile. With only a few lines of code, you can make a wide variety of sounds. You could make the audio for a game entirely with synthesized sounds, especially if you're going for a retro feel; or you could use synthesized sounds as stand-ins un...
## Making Music
One use for both digitized and sythesized sounds is making music. Of course if you just want a background score, you could load a sound file in OGG format and simply play it. But if you want to use synthesized sounds, or make music on the fly from code, then you will need to know what frequency to use for each note.
{i:"`noteFreq`"}
Mini Micro has an intrinsic global function just for that, called `noteFreq`. It takes a *note number*, and returns the proper frequency for that note.
note number
: a standard way of identifying notes over a 9-octave range, with each note assigned a unique number
Middle C (also known as C4, i.e. note C in octave 4) is note number 60, and the next highest note, C sharp, is note 61. After that is 62 (D), 63 (D sharp or E flat), and so on. When you include all the sharps and flats, there are 12 notes per octave, so high C (also known as C5) is note 72, and low C (i.e. C3) is not...
![Note names and numbers for octaves 3 through 5 of the musical scale.](NoteNumbers.svg)
By passing these note numbers to the `noteFreq` function, you can get the right frequency to make any note.
```terminal
]snd = new Sound
]snd.init 1, noteFreq(60)
{"__isa": Sound, "duration": 1, "freq": 261.625549, "waveform": [1,
-1], "envelope": 1, "fadeOut": 0.01, "fadeIn": 0.01, "_handle": null
}
]snd.play
]snd.freq = noteFreq(67)
]snd.play
```
The above plays a sawtooth wave middle C (note 60) followed by middle G (note 67).
With digitized sounds, the procedure is a little different. You first need to know what pitch the sound was recorded as. All of the digitized instruments in `/sys/sounds` were recorded at note 60 (C4). Then, divide the desired frequency by the original note frequency, and use this ratio as the `speed` parame...
```terminal
]snd = file.loadSound("/sys/sounds/celloLongC4.wav")
]snd.play // plays as note 60 (C4)
]snd.play 1, 0, noteFreq(67)/noteFreq(60) // plays as note 67 (G4)
```
Here's a little program from the Mini Micro Cheat Sheet that illustrates how to make a little song. The notes are defined by a list, where each element is a note number and duration.
{caption:"Playing a song with code. Charge!"}
```miniscript
// notes defined as: [note, duration]
notes = [[60, 0.1], [64, 0.1], [67, 0.1],
[72, 0.2], [67, 0.1], [72, 0.4]]
snd = new Sound
for n in notes
snd.init n[1], noteFreq(n[0])
snd.play
wait snd.duration
end for