Lightweight, pure-stdlib audio utilities for Windows with background playback and scheduling.
Project description
miaudio
miaudio is a small, focused audio utility library designed for pure Python on Windows.
It aims to provide professional‑grade ergonomics while staying 100% dependency‑free.
It provides, among other things:
- Simple audio playback for WAV files using the Python standard library only.
- Background playback so your program can continue doing work while audio plays.
- Scheduled playback at a specific absolute time or after a delay.
- Playlists and sequences of files, with optional gaps and looping.
- Scoped playback with a context manager (
with play_while(...): ...). - Timeout‑limited playback (
play_for). - Beep utilities (
beep,beep_pattern) and system UI sounds. - Introspectable state (
is_playing) so you can build richer UIs. - Explicit stop control for stopping the currently playing sound.
- Defensive error handling with clear, documented exception types.
Important
miaudiois intentionally implemented with no third‑party dependencies and no external tools. It uses only the Python standard library and is currently supported on Windows via the built‑inwinsoundmodule. On unsupported platforms, the public APIs will raisePlatformNotSupportedError.
Quick start
from miaudio import play_audio, play_in_background, stop_audio
# Play a WAV file and block until it finishes
play_audio("C:/sounds/notification.wav")
# Play in the background (non‑blocking)
thread = play_in_background("C:/sounds/loop.wav", loop=False)
# Stop whatever is currently playing (if any)
stop_audio()
Installation
Once published on PyPI:
pip install miaudio
Until then, you can install from source:
pip install .
Core concepts
- Windows‑only implementation: Playback is implemented using
winsound. On non‑Windows platforms, aPlatformNotSupportedErroris raised. - WAV files only:
winsoundsupports uncompressed WAV files. Other formats (MP3, FLAC, etc.) are rejected withUnsupportedFormatError. - Thread‑safe control: Background playback and scheduling use
threading.Threadandthreading.Event, with a small synchronization layer to ensure that stop/schedule operations interact safely. - Fire‑and‑forget vs. controlled playback:
play_once,play_audio, andplay_in_backgroundcover the common cases; additional helpers (play_for,play_sequence,play_while) handle longer‑running, structured use cases.
Public API (overview)
The main functions and utilities exposed by miaudio are:
-
Basic playback
play_audio(path: str, *, loop: bool = False, block: bool = True) -> Noneplay_once(path: str) -> Noneplay_for(path: str, seconds: float) -> Nonestop_audio() -> None
-
Background & scheduling
play_in_background(path: str, *, loop: bool = False) -> threading.Threadplay_sequence(paths, *, gap_seconds: float = 0.0, loop_sequence: bool = False, block: bool = True)play_sequence_in_background(paths, *, gap_seconds: float = 0.0, loop_sequence: bool = False)play_after_delay(path: str, delay_seconds: float) -> threading.Threadplay_at_time(path: str, when: datetime.datetime) -> threading.Threadplay_while(path: str, *, loop: bool = False)(context manager)
-
Status & platform
is_supported_platform() -> boolis_playing() -> bool
-
Beep & system sounds
beep(frequency_hz: int = 800, duration_ms: int = 200) -> Nonebeep_pattern(pattern: Iterable[tuple[int, int, float]]) -> Noneplay_system_sound(kind: str = "asterisk") -> None
-
Exceptions
miaudio.PlatformNotSupportedErrormiaudio.AudioFileErrormiaudio.AudioFileNotFoundErrormiaudio.UnsupportedFormatErrormiaudio.PlaybackError
Threading model & stopping playback
Playback is coordinated via a module‑level lock and an internal stop event:
- At most one background playback thread is considered "active" at a time for
higher‑level helpers like
play_in_backgroundandplay_sequence. - Calling
stop_audio():- Clears any
winsoundplayback by issuingwinsound.PlaySound(None, 0). - Signals the internal event so cooperative background threads can stop.
- Clears any
While winsound itself is quite simple, miaudio wraps it to make:
- Parameter validation explicit and predictable.
- Exceptions strongly typed and well‑documented.
- Background, playlist, and scheduled playback easier to use reliably.
Usage examples
Basic blocking playback
from miaudio import play_audio
play_audio("C:/sounds/notification.wav")
Non‑blocking background playback
from miaudio import play_in_background, stop_audio
import time
thread = play_in_background("C:/sounds/music.wav", loop=True)
time.sleep(10) # do other work, or just wait
stop_audio() # stops the loop
thread.join() # wait for the background thread to exit
Play only for a limited time
from miaudio import play_for
# Play for at most 3.5 seconds, then stop automatically
play_for("C:/sounds/long.wav", seconds=3.5)
Playlist / sequence playback
from miaudio import play_sequence
tracks = [
"C:/sounds/intro.wav",
"C:/sounds/loop.wav",
"C:/sounds/outro.wav",
]
# Play all three, 0.25 seconds apart, then stop
play_sequence(tracks, gap_seconds=0.25, loop_sequence=False, block=True)
Playlist in the background, looping
from miaudio import play_sequence_in_background, stop_audio
import time
tracks = [
"C:/sounds/a.wav",
"C:/sounds/b.wav",
]
thread = play_sequence_in_background(tracks, gap_seconds=0.1, loop_sequence=True)
time.sleep(15)
stop_audio()
thread.join()
Scoped playback with a context manager
from miaudio import play_while
with play_while("C:/sounds/progress.wav", loop=True):
# Do some long running work here
do_expensive_work()
# Audio is now stopped, even if an exception was raised.
Simple beeps and patterns
from miaudio import beep, beep_pattern
beep() # quick default beep
beep(440, 500) # A4, 500 ms
pattern = [
(880, 150, 0.05),
(660, 150, 0.05),
(440, 250, 0.10),
]
beep_pattern(pattern)
Play a Windows system sound
from miaudio import play_system_sound
play_system_sound("exclamation")
play_system_sound("hand")
play_system_sound("question")
Schedule a sound five seconds from now
from miaudio import play_after_delay
play_after_delay("C:/sounds/alert.wav", delay_seconds=5.0)
Schedule a sound at a specific time
from datetime import datetime, timedelta
from miaudio import play_at_time
when = datetime.now() + timedelta(minutes=1)
play_at_time("C:/sounds/alert.wav", when=when)
Design goals
- Zero non‑stdlib dependencies: Everything is implemented using only the
Python standard library (
winsound,threading,pathlib,datetime, etc.). - Clear, explicit failures: The library fails fast with clear exception types when used on unsupported platforms or with unsupported formats.
- Simple, focused API: A small surface area that covers common use cases (play once, loop, background, schedule, playlists, stop).
- Predictable behavior under load: Helper functions are thread‑safe, and long‑running operations either block the caller or clearly document that they use background threads.
Limitations
- Currently supports only Windows via
winsound. - Currently supports only WAV files.
- Volume control, balance, and advanced audio processing are intentionally out of scope for this minimal, dependency‑free implementation.
Versioning
miaudio follows Semantic Versioning as closely as is
practical for a small utility library:
- MAJOR: incompatible API changes
- MINOR: backwards‑compatible feature additions
- PATCH: backwards‑compatible bug fixes and documentation updates
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file miaudio-0.1.0.tar.gz.
File metadata
- Download URL: miaudio-0.1.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d71ee5c3eb8d6df58fa68fe3ff40256b6bb44a81c4cbc52cb6a0bb165d29e5f
|
|
| MD5 |
633ebb702f114b51c7a34a86a78f2a37
|
|
| BLAKE2b-256 |
bb9497495918a0b8606807d2545d8915a89cb6642e78ce8dc9579173d3d0607f
|
File details
Details for the file miaudio-0.1.0-py3-none-any.whl.
File metadata
- Download URL: miaudio-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93c682c7e4e45fdaaa4fa7cfdfd17ca310483f0009a8603540701e7bf0bd7d68
|
|
| MD5 |
28bf1e5c15a9b04c8cafb8c509005082
|
|
| BLAKE2b-256 |
9bd07a048675186432a3e8fb00268c9abe79de022bb1f5899bed62697db7b9fc
|