synthio – Support for MIDI synthesis

Available on these boards
  • ARAMCON Badge 2019
  • ARAMCON2 Badge
  • ATMegaZero ESP32-S2
  • Adafruit CLUE nRF52840 Express
  • Adafruit Circuit Playground Bluefruit
  • Adafruit EdgeBadge
  • Adafruit Feather Bluefruit Sense
  • Adafruit Feather M4 CAN
  • Adafruit Feather M4 Express
  • Adafruit Feather RP2040
  • Adafruit Feather STM32F405 Express
  • Adafruit Feather nRF52840 Express
  • Adafruit FunHouse
  • Adafruit Grand Central M4 Express
  • Adafruit Hallowing M4 Express
  • Adafruit ItsyBitsy M4 Express
  • Adafruit ItsyBitsy RP2040
  • Adafruit ItsyBitsy nRF52840 Express
  • Adafruit Macropad RP2040
  • Adafruit MagTag
  • Adafruit Matrix Portal M4
  • Adafruit Metro ESP32S2
  • Adafruit Metro M4 Airlift Lite
  • Adafruit Metro M4 Express
  • Adafruit Metro nRF52840 Express
  • Adafruit Monster M4SK
  • Adafruit PyGamer
  • Adafruit PyPortal
  • Adafruit PyPortal Pynt
  • Adafruit PyPortal Titano
  • Adafruit Pybadge
  • Adafruit QT Py RP2040
  • Adafruit QT2040 Trinkey
  • Adafruit Trellis M4 Express
  • AloriumTech Evo M51
  • Arduino Nano 33 BLE
  • Arduino Nano RP2040 Connect
  • Artisense Reference Design RD00
  • AtelierDuMaker nRF52840 Breakout
  • BDMICRO VINA-D51
  • BLE-SS dev board Multi Sensor
  • BastBLE
  • BastWiFi
  • CP32-M4
  • Capable Robot Programmable USB Hub
  • CircuitBrains Deluxe
  • Cytron Maker Pi RP2040
  • DynOSSAT-EDU-OBC
  • Electronut Labs Blip
  • Electronut Labs Papyr
  • Feather ESP32S2 without PSRAM
  • FeatherS2
  • FeatherS2 PreRelease
  • Franzininho WIFI w/Wroom
  • Franzininho WIFI w/Wrover
  • HiiBot BlueFi
  • IkigaiSense Vita nRF52840
  • Kaluga 1
  • LILYGO TTGO T8 ESP32-S2 w/Display
  • MDBT50Q-DB-40
  • MEOWBIT
  • MakerDiary nRF52840 MDK
  • MakerDiary nRF52840 MDK USB Dongle
  • Makerdiary M60 Keyboard
  • Makerdiary Pitaya Go
  • Makerdiary nRF52840 M.2 Developer Kit
  • Mini SAM M4
  • Open Hardware Summit 2020 Badge
  • PCA10056 nRF52840-DK
  • PCA10059 nRF52840 Dongle
  • PCA10100 nRF52833 Dongle
  • Particle Argon
  • Particle Boron
  • Particle Xenon
  • PewPew M4
  • Pimoroni Keybow 2040
  • Pimoroni PGA2040
  • Pimoroni Pico LiPo (16MB)
  • Pimoroni Pico LiPo (4MB)
  • Pimoroni PicoSystem
  • Pimoroni Tiny 2040
  • PyCubedv04
  • PyCubedv04-MRAM
  • PyboardV1_1
  • Raspberry Pi Pico
  • Robo HAT MM1 M4
  • SAM E54 Xplained Pro
  • SAM32v26
  • STM32F412G_DISCO
  • STM32F4_DISCO
  • Saola 1 w/Wroom
  • Saola 1 w/Wrover
  • Seeeduino Wio Terminal
  • Silicognition LLC M4-Shim
  • SparkFun MicroMod ATP - RP2040
  • SparkFun MicroMod SAMD51
  • SparkFun MicroMod nRF52840
  • SparkFun Pro Micro RP2040
  • SparkFun Pro nRF52840 Mini
  • SparkFun Thing Plus - RP2040
  • SparkFun Thing Plus - SAMD51
  • Sprite_v2b
  • TG-Boards' Datalore IP M4
  • TG-Watch
  • THUNDERPACK_v11
  • THUNDERPACK_v12
  • Targett Module Clip w/Wroom
  • Targett Module Clip w/Wrover
  • Teknikio Bluebird
  • The Open Book Feather
  • TinkeringTech ScoutMakes Azul
  • TinyS2
  • UARTLogger II
  • micro:bit v2
  • microS2
  • nanoESP32-S2 w/Wrover
  • nanoESP32-S2 w/Wroom
  • nice!nano
  • stm32f411ce-blackpill-with-flash

synthio.from_file(file: BinaryIO, *, sample_rate: int = 11025)MidiTrack

Create an AudioSample from an already opened MIDI file. Currently, only single-track MIDI (type 0) is supported.

Parameters
  • file (typing.BinaryIO) – Already opened MIDI file

  • sample_rate (int) – The desired playback sample rate; higher sample rate requires more memory

Playing a MIDI file from flash:

import audioio
import board
import synthio

data = open("single-track.midi", "rb")
midi = synthio.from_file(data)
a = audioio.AudioOut(board.A0)

print("playing")
a.play(midi)
while a.playing:
  pass
print("stopped")
class synthio.MidiTrack(buffer: _typing.ReadableBuffer, tempo: int, *, sample_rate: int = 11025)

Simple square-wave MIDI synth

Create a MidiTrack from the given stream of MIDI events. Only “Note On” and “Note Off” events are supported; channel numbers and key velocities are ignored. Up to two notes may be on at the same time.

Parameters
  • buffer (ReadableBuffer) – Stream of MIDI events, as stored in a MIDI file track chunk

  • tempo (int) – Tempo of the streamed events, in MIDI ticks per second

  • sample_rate (int) – The desired playback sample rate; higher sample rate requires more memory

Simple melody:

import audioio
import board
import synthio

dac = audioio.AudioOut(board.SPEAKER)
melody = synthio.MidiTrack(b"\0\x90H\0*\x80H\0\6\x90J\0*\x80J\0\6\x90L\0*\x80L\0\6\x90J\0" +
                           b"*\x80J\0\6\x90H\0*\x80H\0\6\x90J\0*\x80J\0\6\x90L\0T\x80L\0" +
                           b"\x0c\x90H\0T\x80H\0\x0c\x90H\0T\x80H\0", tempo=640)
dac.play(melody)
print("playing")
while dac.playing:
  pass
print("stopped")
sample_rate :Optional[int]

32 bit value that tells how quickly samples are played in Hertz (cycles per second).

deinit(self)None

Deinitialises the MidiTrack and releases any hardware resources for reuse.

__enter__(self)MidiTrack

No-op used by Context Managers.

__exit__(self)None

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