text
stringlengths
0
897
Note that in the context of Mini Micro, *waveform* refers to just *one* cycle of the air pressure changes, which is repeated many times per second. In other contexts, people may use "waveform" to refer to a graph of the entire sound, from beginning to end.
So to define a simple sound, we need to know:
1. What the waveform looks like.
2. How many times per second that waveform should be repeated (i.e. the frequency).
3. How long the sound should last.
4. How the volume should change over the course of the sound.
That third one, how long the sound should last, is called the *duration* of the sound; and that last one is called the *envelope*.
envelope
: how the amplitude (volume) of a sound varies from the beginning to the end of the sound
And now you have all the background you need to understand sound synthesis in Mini Micro. The `Sound` class has some additional properties that correspond to the concepts above.
{caption:"Properties and methods related to synthesizing sounds from scratch.", colWidths:"120,*"}
|`snd.duration`|length of the sound, in seconds|
|`snd.freq`|frequency (repeats of `waveform` per second)|
|`snd.envelope`|how the sound amplitude varies over the course of the sound|
|`snd.fadeIn`|length of fade-in period when sound begins, in seconds|
|`snd.fadeOut`|length of fade-out period when sound begins, in seconds|
|`snd.init` *duration, freq, envelope, waveform*|shortcut for setting the four key properties|
|`snd.mix` *sound2, level|add another synthesized sound into this one|
Let's begin, as usual, by experimenting on the REPL command line.
```terminal
]s = new Sound
]s.duration = 1
]s.freq = 440
]s.envelope = 1
]s.waveform = Sound.sineWave
]s.play
```
You should hear a clean, pure note (for the musically inclined, this is A above middle C) that lasts for 1 second. Let's go over each of these properties in turn.
First, `duration`. This one is easy enough: it's how long the sound lasts, in seconds. Try changing it and hear the difference.
```terminal
]s.duration = 1.5
]s.play
]s.duration = 0.5
]s.play
```
Next, frequency. You probably have a good idea of this by now, but go ahead and play with it a bit to be sure.
```terminal
]s.freq = 220
]s.play
]s.freq = 880
]s.play
```
However, you aren't limited to a single, constant frequency for the whole sound. If you give Mini Micro a list of frequencies, it will smoothly interpolate between them. Try these:
```terminal
]s.duration = 2
]s.freq = [220, 880]
]s.play
]s.freq = [880, 220]
]s.play
]s.freq = [220,300] * 8
]s.play
```
That last one is using list replication; go ahead and print out `s.freq` to make sure you understood that correctly. And you can hear what it does to the sound: the pitch varies between 220 and 300 Hz, 8 times over the 2-second duration of the sound.
Envelope is a little trickier. Here, Mini Micro is expecting either 1, which indicates a constant amplitude for the sound; or a list of values between 0 and 1, which indicate how the amplitude should vary over the course of the sound. For example, if you use an envelope of `[0,1]`, then the sound will start at an amp...
```terminal
]s.freq = 440
]s.envelope = [0,1]
]s.play
]s.envelope = [1,0]
]s.play
]s.envelope = [0,1,0]
]s.play
]s.envelope = [1,0,1]
]s.play
```
You can use as many values in your `envelope` list as you like; the synthesizer will evenly space these amplitude changes over the length of the sound. And of course you don't have to stick to only values of 0 and 1; you can use any values in between. For example, using list replication again:
```terminal
]s.envelope = [1, 0.25] * 8
]s.play
```
It should be noted that the envelope isn't the *only* factor that affects the amplitude of the sound over time; there are also `fadeIn` and `fadeOut` properties. These are a simple way to make the sound fade in (go from zero to full amplitude) at the start of the sound, and fade out (go from full amplitude down to zer...
Finally, let's consider the waveform. In the examples so far we've been using a sine wave, which is a pure, smooth variation in air pressure over time. Such a smooth waveform strongly excites only one of the frequency detectors in your ear, producing a sound that is clean and pure (and so rather boring).
You can specify a waveform as a list of values from -1 to 1, and Mini Micro will interpolate over that list for each cycle of the sound. But the `Sound` class also has five common waveforms built right in, as shown below.
{width:"97%"}
![Waveforms available on the `Sound` class.](SoundWaves.svg)