audiofilewriter – Support for streaming audio to a WAV file

Available on these boards
  • 0xCB Gemini
  • 0xCB Helios
  • 42. Keebs Frood
  • 8086 RP2040 Interfacer
  • 8086 USB Interposer
  • ATMegaZero ESP32-S2
  • Adafruit Feather ESP32-S2 Reverse TFT
  • Adafruit Feather ESP32-S2 TFT
  • Adafruit Feather ESP32S2
  • Adafruit Feather RP2040
  • Adafruit Feather RP2040 Adalogger
  • Adafruit Feather RP2040 CAN
  • Adafruit Feather RP2040 DVI
  • Adafruit Feather RP2040 Prop-Maker
  • Adafruit Feather RP2040 RFM
  • Adafruit Feather RP2040 Scorpio
  • Adafruit Feather RP2040 ThinkInk
  • Adafruit Feather RP2040 USB Host
  • Adafruit Feather RP2350
  • Adafruit Feather RP2350 Adalogger
  • Adafruit Floppsy RP2040
  • Adafruit Fruit Jam
  • Adafruit FunHouse
  • Adafruit ItsyBitsy RP2040
  • Adafruit KB2040
  • Adafruit Macropad RP2040
  • Adafruit MagTag
  • Adafruit Metro ESP32S2
  • Adafruit Metro RP2040
  • Adafruit Metro RP2350
  • Adafruit QT Py ESP32S2
  • Adafruit QT Py RP2040
  • Adafruit QT2040 Trinkey
  • Adafruit Tinychad RP2350
  • Adafruit Vindie S2
  • Archi RP2040
  • Arduino Nano RP2040 Connect
  • Artisense Reference Design RD00
  • BBQ20KBD
  • BLOK
  • BPI-Bit-S2
  • BastWiFi
  • Bradán Lane STUDIO Explorer Badge
  • COSMO-Pico
  • Challenger NB RP2040 WiFi
  • Challenger RP2040 LTE
  • Challenger RP2040 LoRa
  • Challenger RP2040 SD/RTC
  • Challenger RP2040 SubGHz
  • Challenger RP2040 WiFi
  • Challenger RP2040 WiFi/BLE
  • Challenger+ RP2350 BConnect
  • Challenger+ RP2350 WiFi6/BLE5
  • CrumpS2
  • Cytron EDU PICO 2
  • Cytron EDU PICO W
  • Cytron IRIV IO Controller
  • Cytron MOTION 2350 Pro
  • Cytron Maker Nano RP2040
  • Cytron Maker Pi RP2040
  • Cytron Maker Uno RP2040
  • Datanoise PicoADK
  • Datanoise PicoADK V2
  • Deneyap Mini
  • Deneyap Mini v2
  • E-Fidget
  • ELECFREAKS PICO:ED
  • ESP 12k NodeMCU
  • ESP32-S2-DevKitC-1-N4
  • ESP32-S2-DevKitC-1-N4R2
  • ESP32-S2-DevKitC-1-N8R2
  • Electrolama minik
  • EncoderPad RP2040
  • FeatherS2
  • FeatherS2 Neo
  • FeatherS2 PreRelease
  • Fig Pi
  • Flipper Zero Wi-Fi Dev
  • Franzininho WIFI w/Wroom
  • Franzininho WIFI w/Wrover
  • Gravitech Cucumber M
  • Gravitech Cucumber MS
  • Gravitech Cucumber R
  • Gravitech Cucumber RS
  • HEIA-FR Picomo V2
  • HEIA-FR Picomo V3
  • HMI-DevKit-1.1
  • HXR.DK SAO Digital Multimeter
  • Hack Club Sprig
  • HexKyS2
  • IoTs2
  • Kaluga 1
  • LILYGO T-DISPLAY
  • LILYGO TTGO T8 ESP32-S2
  • LILYGO TTGO T8 ESP32-S2 w/Display
  • MORPHEANS MorphESP-240
  • Maker badge by Czech maker
  • Maple Computing Elite-Pi
  • Melopero Shake RP2040
  • MicroDev microS2
  • MixGo CE
  • Music Thing Modular Workshop Computer
  • Noise Nugget 2040
  • Oak Dev Tech BREAD2040
  • Oak Dev Tech Cast-Away RP2040
  • Oak Dev Tech PixelWing ESP32S2
  • Oak Dev Tech RPGA Feather
  • Orpheus Pico
  • PCBCupid Glyph Mini 2040
  • PROVES Kit v4
  • Pajenicko PicoPad
  • Pimoroni Badger 2040
  • Pimoroni Badger 2040 W
  • Pimoroni Badger 2350
  • Pimoroni Explorer
  • Pimoroni Inky Frame 5.7
  • Pimoroni Inky Frame 7.3
  • Pimoroni Interstate 75
  • Pimoroni Keybow 2040
  • Pimoroni Motor 2040
  • Pimoroni PGA2040
  • Pimoroni PGA2350
  • Pimoroni Pico LiPo (16MB)
  • Pimoroni Pico LiPo (4MB)
  • Pimoroni Pico Plus 2
  • Pimoroni Pico Plus 2 W
  • Pimoroni Pico dv Base
  • Pimoroni PicoSystem
  • Pimoroni Plasma 2040
  • Pimoroni Plasma 2040W
  • Pimoroni Plasma 2350
  • Pimoroni Plasma 2350W
  • Pimoroni Servo 2040
  • Pimoroni Tiny 2040 (2MB)
  • Pimoroni Tiny 2040 (8MB)
  • Pimoroni Tiny 2350
  • Pimoroni Tiny FX
  • PyKey 18 Numpad
  • PyKey 44 Ergo
  • PyKey 60
  • PyKey 87 TKL
  • RF.Guru RP2040
  • RP2.65-F
  • RP2040 Stamp
  • RP2350 Stamp
  • RP2350 Stamp XL
  • Raspberry Breadstick
  • Raspberry Pi Pico
  • Raspberry Pi Pico 2
  • Raspberry Pi Pico 2 W
  • Raspberry Pi Pico W
  • Red S2-WROOM
  • S2Mini
  • S2Pico
  • Saola 1 w/Wroom
  • Saola 1 w/Wrover
  • Seeeduino XIAO RP2040
  • Seeeduino XIAO RP2350
  • Silicognition LLC RP2040-Shim
  • SparkFun MicroMod RP2040 Processor
  • SparkFun Pro Micro RP2040
  • SparkFun Pro Micro RP2350
  • SparkFun Thing Plus - RP2040
  • SparkFun Thing Plus RP2350
  • Studiolab Pico Expander
  • TTGO T8 ESP32-S2-WROOM
  • Targett Module Clip w/Wroom
  • Targett Module Clip w/Wrover
  • TinyCircuits Thumby
  • TinyCircuits Thumby Color
  • TinyS2
  • VCC-GND Studio YD RP2040
  • W5100S-EVB-Pico
  • W5100S-EVB-Pico2
  • W5500-EVB-Pico
  • W5500-EVB-Pico2
  • WK-50 Trackball Keyboard
  • Waveshare ESP32-S2-Pico
  • Waveshare ESP32-S2-Pico-LCD
  • Waveshare RP2040-GEEK
  • Waveshare RP2040-LCD-0.96
  • Waveshare RP2040-LCD-1.28
  • Waveshare RP2040-One
  • Waveshare RP2040-PiZero
  • Waveshare RP2040-Plus (16MB)
  • Waveshare RP2040-Plus (4MB)
  • Waveshare RP2040-TOUCH-LCD-1.28
  • Waveshare RP2040-Tiny
  • Waveshare RP2040-Zero
  • Waveshare RP2350-GEEK
  • Waveshare RP2350-LCD-0.96
  • Waveshare RP2350-LCD-1.28
  • Waveshare RP2350-One
  • Waveshare RP2350-Plus
  • Waveshare RP2350-TOUCH-LCD-1.28
  • Waveshare RP2350-Tiny
  • Waveshare RP2350-Zero
  • WeAct Studio Pico
  • WeAct Studio Pico 16MB
  • WeAct Studio RP2350B Core
  • WisdPi Ardu2040M
  • WisdPi Tiny RP2040
  • nanoESP32-S2 w/Wrover
  • nanoESP32-S2 w/Wroom
  • nullbits Bit-C PRO
  • senseBox MCU-S2 ESP32S2
  • splitkb.com Liatris
  • takayoshiotake Octave RP2040
  • uGame22

class audiofilewriter.AudioFileWriter(file: BinaryIO, *, buffer_size: int = 32768)

Streams an audio source to a .wav file in the background.

AudioFileWriter is the inverse of audiocore.WaveFile: rather than being an audio source played by an audioio.AudioOut, it is a sink that drives an audio source (a microphone, synthio, or an audiofilters/ audiodelays/audiofreeverb/audiospeed effect chain) and writes the resulting PCM to a file as a WAV.

Recording runs on a background pump paced to the source’s real-time rate, so it does not block and does not require a Python read loop.

Create an AudioFileWriter that writes to file.

Parameters:
  • file (BinaryIO) – An already-open writable binary stream (a file opened in "wb" mode, or an io.BytesIO). The stream must support seeking so the WAV header sizes can be patched when recording stops. AudioFileWriter does not close it; the caller owns it.

  • buffer_size (int) – Size in bytes of the internal RAM ring that decouples file-write latency from the source. Larger values tolerate longer write stalls (e.g. a slow SD card) at the cost of RAM. Minimum valid value, and default, is 512. Must be at least the size of the source buffer.

The audio format (sample rate, channel count, bit depth) is taken from the source at play() time, so there are no format arguments here.

Recording synthio to SD:

import time
import synthio
from audiofilewriter import AudioFileWriter
import storage

SAMPLE_RATE = 16000
OUTPUT_PATH = "/sd/demo_file.wav"

C_major_scale = [60, 62, 64, 65, 67, 69, 71, 72, 71, 69, 67, 65, 64, 62, 60]
synth = synthio.Synthesizer(sample_rate=SAMPLE_RATE)

with open(OUTPUT_PATH, "wb") as f:
    writer = AudioFileWriter(f)
    writer.play(synth)
    for note in C_major_scale:
        synth.press(note)
        time.sleep(0.1)
        synth.release(note)
        time.sleep(0.10)
    writer.stop()
print("Done ->", OUTPUT_PATH)
deinit() None

Stops recording (patching the WAV header) and releases resources.

__enter__() AudioFileWriter

No-op used by Context Managers.

__exit__() None

Automatically deinitializes when exiting a context. See Lifetime and ContextManagers for more info.

play(sample: circuitpython_typing.AudioSample) None

Begin recording sample to the file. Does not block.

Writes a WAV header (in the format of sample) and starts the background pump. sample must be an 8-bit or 16-bit mono or stereo audio source. Use playing to tell when a finite source has finished, or call stop() to end recording of a continuous source (e.g. a mic).

stop() None

Stop recording, flush the RAM ring to the file, and patch the WAV header sizes. The file is left open for the caller to close.

playing: bool

True while recording is in progress. Becomes False on its own when a finite source finishes, or after stop(). (read-only)