diff --git a/docs/get-started/first-program.md b/docs/get-started/first-program.md index 03ff6204..0f8b8cfb 100644 --- a/docs/get-started/first-program.md +++ b/docs/get-started/first-program.md @@ -58,3 +58,10 @@ frames of 16-bit stereo, so 100 of them make 25,600 frames. Put more effects in the chain with [Chain effects](../guides/chain-effects.md), or play a part in time with [Sequence a part](../guides/sequence-a-part.md). + +To hear something right away, run one of the two complete examples. Each +writes a WAV file you can open in any audio player: +[audioinstruments_simpletest.py](https://github.com/PyDevices/audiocomponents/blob/main/examples/audioinstruments_simpletest.py) +plays a tune on a Rhodes electric piano, and +[audioeffects_simpletest.py](https://github.com/PyDevices/audiocomponents/blob/main/examples/audioeffects_simpletest.py) +runs plucked notes through a tape echo and a hall. diff --git a/examples/audioeffects_simpletest.py b/examples/audioeffects_simpletest.py new file mode 100644 index 00000000..2f869b6d --- /dev/null +++ b/examples/audioeffects_simpletest.py @@ -0,0 +1,73 @@ +# SPDX-FileCopyrightText: 2026 Brad Barnett +# +# SPDX-License-Identifier: MIT +"""Run plucked notes through a tape echo and a hall, and save a WAV file. + +A synthio Synthesizer plays short notes, a TapeDelay repeats them and a +Reverb puts them in a hall. Each effect takes the one before it as its +input, and the program writes the end of the chain to a WAV file you can +open in any audio player. Change the names to hear any of the forty-five. + +Needs: audioeffects and audiodsp. Both are built into the PyDevices +MicroPython firmware; on CPython, pip install pydevices-audioeffects. + +Run it: + micropython audioeffects_simpletest.py + +Tested on: the unix port of MicroPython and CPython 3.12 on Linux. +""" + +import struct + +import audiocore +import audioeffects +import synthio + +# Change these to match your setup. +FILENAME = "echoes.wav" +NOTES = (60, 67, 64, 72) # MIDI note numbers +NOTE_SECONDS = 0.6 +SAMPLE_RATE = 22050 +ECHO_MS = 300 # time between the repeats + +# A synthesizer whose notes die away quickly, so the echoes stand out. +envelope = synthio.Envelope(attack_time=0.005, decay_time=0.2, sustain_level=0) +synth = synthio.Synthesizer(sample_rate=SAMPLE_RATE, envelope=envelope) + +# A tape echo fed by the synthesizer, then a hall fed by the echo. +echo = audioeffects.create("TapeDelay", synth, SAMPLE_RATE, + time_ms=ECHO_MS, feedback=0.4, mix=0.35) +hall = audioeffects.create("Reverb", echo.output, SAMPLE_RATE, + character="hall", mix=0.3) + +# Write the WAV header now; the data size is filled in at the end. +out = open(FILENAME, "wb") +out.write(b"RIFF\0\0\0\0WAVEfmt ") +out.write(struct.pack("