LameAudio¶
A 4-channel synthesizer for the LameStation.
audio : "LameAudio"
Demos for this object can be found in the /demos/audio/
folder of the SDK.
About¶
Initialization¶
Before LameAudio can be used, audio.Start
must be called once
and only once at the start of a program. LameAudio is designed so that only one
synthesizer is ever running, and any object may call it.
Each channel has its own parameters that affect the sound it makes.
audio.SetParam
can control any parameter in the Channel
parameter constant list. You can change all parameters at once with
audio.LoadPatch
, which is good if you change
instruments often.
This is what a patch looks like.
DAT
patch
byte $F, 127, 10, 100, 10, audio#_SAW
Each channel has a waveform and an envelope.
Waveform¶
The waveform generator is what actually creates the sound. Call
audio.SetFrequency
to play any frequency, or call
audio.SetNote
to play specific notes like on a piano.
Call audio.SetWaveform
to load one of the following
waveforms.
Value | Name | Waveform |
---|---|---|
0 | Square | ![]() |
1 | Sawtooth | ![]() |
2 | Triangle | ![]() |
3 | Sine | ![]() |
4 | Noise | ![]() |
5 | Sample | ![]() |
The Sample waveform is a 512B block of audio data, enough for one cycle. Call
audio.SetSample
with the address of a sample to load it.
Only one sample can be used at a time.
A collection of ready-made samples can be found in the /media/samples/
folder
of the SDK.
ADSR Envelope¶
The envelope controls the volume of the sound over time, allowing you to create sounds that hit sharply like bells, or have a long hold time like a wind instrument. You can control how it sounds with the ADSR parameters of the envelope.
ADSR stands for Attack, Decay, Sustain, and Release. You can configure ADSR
settings with audio.SetADSR
.
-
Attack (
0-127
) - The time from silence to full volume. -
Decay (
0-127
) - The time from the max value to... -
Sustain (
0-127
) - The volume at which the note is held while pressed. -
Release (
0-127
) - The time from sustain to silence.
The envelope can be toggled with audio.SetEnvelope
.
-
If enabled, you can play and stop notes with
audio.PlaySound
andaudio.StopSound
. -
If disabled, you can control channel volume manually using
audio.SetVolume
, which is useful for creating sound effects. You can also manually start and stop the envelope withaudio.StartEnvelope
andaudio.StopEnvelope
.
To silence all audio, use audio.StopAllSound
.
Sample Format¶
LameStation songs consist of two main sections: pattern and sequence data.
Sample Data¶
A LameAudio sample is a 512-byte sequence of arbitrary data. It can be anything you want as long as its 512 bytes.
Example pattern section
DAT
wav_data
byte <512 bytes...>
Since it's possible to use any address in memory, the running system can also create samples on the fly.
Sample Interface¶
The address of sample data can be passed directly to
audio.SetSample
, unless stored in another object. If so,
use this interface:
PUB Addr
return @wav_data
Functions¶
audio.LoadPatch¶
Load a new patch.
audio.LoadPatch(patchAddr)
- address - The address of the new patch to load.
You can change all parameters at once with audio.LoadPatch
. This is useful if
you change instruments often.
A patch is formatted as follows:
byte <attack>, <decay>, <sustain>, <release>, <waveform>
audio.PlaySound¶
Play sound on the specified channel.
audio.PlaySound(channel, note)
- channel - The oscillator channel (0-3).
Note
This requires the envelope to be enabled.
audio.SetADSR¶
Set all parameters of the ADSR envelope shape.
audio.SetADSR(channel, attack, decay, sustain, release)
-
channel - The oscillator channel (0-3).
-
attack - Time from silence to full volume (0-127).
-
decay - Time from full volume to sustain (0-127).
-
sustain - The volume sustained while the note is held (0-127).
-
release - Time from note release to silence (0-127).
audio.SetEnvelope¶
Toggle the ADSR envelope on the given audio channel.
audio.SetEnvelope(channel, enabled)
-
channel - The oscillator channel (0-3).
-
enabled - True enables the envelope; false disables it.
The envelope can be toggled with audio.SetEnvelope
.
-
If enabled, you can play and stop notes with
audio.PlaySound
andaudio.StopSound
. -
If disabled, you can control channel volume manually using
audio.SetVolume
, which is useful for creating sound effects. You can also manually start and stop the envelope withaudio.StartEnvelope
andaudio.StopEnvelope
.
audio.SetFrequency¶
Set the frequency increment of the given audio channel.
audio.SetFrequency(channel, value)
-
channel - The oscillator channel (0-3).
-
value - The new frequency increment to set (long).
This function controls the phase accumulator directly, allowing you to generate frequency possible with the synthesizer.
audio.SetNote¶
Set the note of the given audio channel.
audio.SetNote(channel, value)
-
channel - The oscillator channel (0-3).
-
value - The new note to set (0-127).
This function calculates and sets oscillator frequencies to musical note values.
audio.SetParam¶
Manually set a parameter of the audio channel.
audio.SetParam(channel, type, value)
-
channel - The oscillator channel (0-3).
-
type - _ATK, _DEC, _SUS, _REL, _WAV, _CONTROL.
-
value - Byte-sized value dependent on parameter (0-255).
audio.SetSample¶
Load a new sample.
audio.SetSample(address)
- address - The address of the new sample to load.
The Sample waveform is a 512B block of audio data, enough for one cycle. Call
audio.SetSample
with the address of a sample to load it. Only one sample can be
used at a time.
A collection of ready-made samples can be found in the /media/samples/
folder of
the SDK.
audio.SetVolume¶
Set the volume of the given channel.
audio.SetVolume(channel, value)
-
channel - The oscillator channel (0-3).
-
value - The new volume for the channel.
audio.SetWaveform¶
Set the waveform of the given audio channel.
audio.SetWaveform(channel, value)
-
channel - The oscillator channel (0-3).
-
waveform - Index of waveform to use.
audio.Start¶
Initialize the LameAudio library.
audio.Start
audio.StartEnvelope¶
Trigger start of envelope on the specified channel.
audio.StartEnvelope(channel)
- channel - The oscillator channel (0-3).
audio.StopAllSound¶
Immediately stop all sound on all channels.
audio.StopAllSound
audio.StopEnvelope¶
Trigger end of envelope on the specified channel.
audio.StopEnvelope(channel)
- channel - The oscillator channel (0-3).
audio.StopSound¶
Stop sound on the specified channel.
audio.StopSound(channel)
- channel - The oscillator channel (0-3).
Note
This function requires the envelope to be enabled.