python interface to the miniaudio audio playback, recording, decoding and conversion library
python interface to the miniaudio audio playback, recording, decoding and conversion library
Multiplatform audio playback, recording, decoding and sample format conversion for Linux (including Raspberry Pi), Windows, Mac and others.
Installation for most users: via Pypi, Raspberry Pi builds via PiWheels.
This is a Pythonic interface to the cross-platform miniaudio C library:
array with appropriately sized elements
depending on the sample width (rather than a raw block of bytes)Requires Python 3.10 or newer. Also works on pypy3 (because it uses cffi).
The miniaudio C library version is 0.11.25.
Software license for these Python bindings, miniaudio and the decoders: MIT
If you like this library you may also be interested in my software FM synthesizer or my mod player which uses libxmp.
import miniaudio
# Get file info
info = miniaudio.get_file_info("samples/music.mp3")
print(f"Playing: {info.nchannels} channels, {info.sample_rate} Hz, {info.duration:.1f}s")
# Stream and play
stream = miniaudio.stream_file("samples/music.mp3")
with miniaudio.PlaybackDevice() as device:
device.start(stream)
input("Playing... press Enter to stop")…This example uses ffmpeg as an external tool to decode an audio file in a format that miniaudio itself can't decode (m4a/aac in this case):
…enum class Backend
names: WASAPI DSOUND WINMM COREAUDIO SNDIO AUDIO4 OSS PULSEAUDIO ALSA JACK AAUDIO OPENSL WEBAUDIO CUSTOM NULL
Operating system audio backend to use (only a subset will be available)
enum class ChannelMixMode
names: RECTANGULAR SIMPLE CUSTOMWEIGHTS
How to mix channels when converting
enum class DeviceType
names: PLAYBACK CAPTURE DUPLEX
Type of audio device
enum class DitherMode
names: NONE RECTANGLE TRIANGLE
How to dither when converting
enum class FileFormat
names: UNKNOWN WAV FLAC MP3 VORBIS
Audio file format
enum class SampleFormat
names: UNKNOWN UNSIGNED8 SIGNED16 SIGNED24 SIGNED32 FLOAT32
Sample format in memory
enum class SeekOrigin
names: START CURRENT END
How to seek() in a source
enum class ThreadPriority
names: IDLE LOWEST LOW NORMAL HIGH HIGHEST REALTIME
The priority of the worker thread (default=HIGHEST)
function convert_frames (from_fmt: miniaudio.SampleFormat, from_numchannels: int, from_samplerate: int, sourcedata: bytes, to_fmt: miniaudio.SampleFormat, to_numchannels: int, to_samplerate: int) -> bytearray
Convert audio frames in source sample format with a certain number of channels, to another sample format and possibly down/upmixing the number of channels as well.
function convert_sample_format (from_fmt: miniaudio.SampleFormat, sourcedata: bytes, to_fmt: miniaudio.SampleFormat, dither: miniaudio.DitherMode = ) -> bytearray
Convert a raw buffer of pcm samples to another sample format. The result is returned as another raw pcm sample buffer
function decode (data: bytes, output_format: miniaudio.SampleFormat = , nchannels: int = 2, sample_rate: int = 44100, dither: miniaudio.DitherMode = ) -> miniaudio.DecodedSoundFile
Convenience function to decode any supported audio file in memory to raw PCM samples in your chosen format.
function decode_file (filename: str, output_format: miniaudio.SampleFormat = , nchannels: int = 2, sample_rate: int = 44100, dither: miniaudio.DitherMode = ) -> miniaudio.DecodedSoundFile
Convenience function to decode any supported audio file to raw PCM samples in your chosen format.
function flac_get_file_info (filename: str) -> miniaudio.SoundFileInfo
Fetch some information about the audio file (flac format).
function flac_get_info (data: bytes) -> miniaudio.SoundFileInfo
Fetch some information about the audio data (flac format).
function flac_read_f32 (data: bytes) -> miniaudio.DecodedSoundFile
Reads and decodes the whole flac audio file. Resulting sample format is 32 bits float.
function flac_read_file_f32 (filename: str) -> miniaudio.DecodedSoundFile
Reads and decodes the whole flac audio file. Resulting sample format is 32 bits float.
function flac_read_file_s16 (filename: str) -> miniaudio.DecodedSoundFile
Reads and decodes the whole flac audio file. Resulting sample format is 16 bits signed integer.
function flac_read_file_s32 (filename: str) -> miniaudio.DecodedSoundFile
Reads and decodes the whole flac audio file. Resulting sample format is 32 bits signed integer.
function flac_read_s16 (data: bytes) -> miniaudio.DecodedSoundFile
Reads and decodes the whole flac audio data. Resulting sample format is 16 bits signed integer.
function flac_read_s32 (data: bytes) -> miniaudio.DecodedSoundFile
Reads and decodes the whole flac audio data. Resulting sample format is 32 bits signed integer.
function flac_stream_file (filename: str, frames_to_read: int = 1024, seek_frame: int = 0) -> Generator[array.array, NoneType, NoneType]
Streams the flac audio file as interleaved 16 bit signed integer sample arrays segments. This uses a fixed chunk size and cannot be used as a generic miniaudio decoder input stream. Consider using stream_file() instead.
function get_enabled_backends () -> Set[miniaudio.Backend]
Returns the set of available backends by the compilation environment for the underlying miniaudio C library
function get_file_info (filename: str) -> miniaudio.SoundFileInfo
Fetch some information about the audio file.
function is_backend_enabled (backend: miniaudio.Backend) -> bool
Determines whether or not the given backend is available by the compilation environment for the underlying miniaudio C library
function is_loopback_supported (backend: miniaudio.Backend) -> bool
Determines whether or not loopback mode is support by a backend.
function lib_version () -> str
Returns the version string of the underlying miniaudio C library
function mp3_get_file_info (filename: str) -> miniaudio.SoundFileInfo
Fetch some information about the audio file (mp3 format).
function mp3_get_info (data: bytes) -> miniaudio.SoundFileInfo
Fetch some information about the audio data (mp3 format).
function mp3_read_f32 (data: bytes) -> miniaudio.DecodedSoundFile
Reads and decodes the whole mp3 audio data. Resulting sample format is 32 bits float.
function mp3_read_file_f32 (filename: str) -> miniaudio.DecodedSoundFile
Reads and decodes the whole mp3 audio file. Resulting sample format is 32 bits float.
function mp3_read_file_s16 (filename: str) -> miniaudio.DecodedSoundFile
Reads and decodes the whole mp3 audio file. Resulting sample format is 16 bits signed integer.
function mp3_read_s16 (data: bytes) -> miniaudio.DecodedSoundFile
Reads and decodes the whole mp3 audio data. Resulting sample format is 16 bits signed integer.
function mp3_stream_file (filename: str, frames_to_read: int = 1024, seek_frame: int = 0) -> Generator[array.array, NoneType, NoneType]
Streams the mp3 audio file as interleaved 16 bit signed integer sample arrays segments. This uses a fixed chunk size and cannot be used as a generic miniaudio decoder input stream. Consider using stream_file() instead.
function read_file (filename: str, convert_to_16bit: bool = False) -> miniaudio.DecodedSoundFile
Reads and decodes the whole audio file. Miniaudio will attempt to return the sound data in exactly the same format as in the file. Unless you set convert_convert_to_16bit to True, then the result is always a 16 bit sample format.
function stream_any (source: miniaudio.StreamableSource, source_format: miniaudio.FileFormat = , output_format: miniaudio.SampleFormat = , nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024, dither: miniaudio.DitherMode = , seek_frame: int = 0) -> Generator[array.array, int, NoneType]
Convenience function that returns a generator to decode and stream any source of encoded audio data (such as a network stream). Stream result is chunks of raw PCM samples in the chosen format. If you send() a number into the generator rather than just using next() on it, you'll get that given number of frames, instead of the default configured amount. This is particularly useful to plug this stream into an audio device callback that wants a variable number of frames per call.
function stream_file (filename: str, output_format: miniaudio.SampleFormat = , nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024, dither: miniaudio.DitherMode = , seek_frame: int = 0) -> Generator[array.array, int, NoneType]
Convenience generator function to decode and stream any supported audio file as chunks of raw PCM samples in the chosen format. If you send() a number into the generator rather than just using next() on it, you'll get that given number of frames, instead of the default configured amount. This is particularly useful to plug this stream into an audio device callback that wants a variable number of frames per call.
function stream_memory (data: bytes, output_format: miniaudio.SampleFormat = , nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024, dither: miniaudio.DitherMode = ) -> Generator[array.array, int, NoneType]
Convenience generator function to decode and stream any supported audio file in memory as chunks of raw PCM samples in the chosen format. If you send() a number into the generator rather than just using next() on it, you'll get that given number of frames, instead of the default configured amount. This is particularly useful to plug this stream into an audio device callback that wants a variable number of frames per call.
function stream_raw_pcm_memory (pcmdata: array.array | memoryview | bytes, nchannels: int, sample_width: int, frames_to_read: int = 4096) -> Generator[bytes | array.array, int, NoneType]
Convenience generator function to stream raw pcm audio data from memory. Usually you don't need to use this as the library provides many other streaming options that work on much smaller, encoded, audio data. However, in the odd case that you only have already decoded raw pcm data you can use this generator as a stream source. The data can be provided in
arraytype orbytes,memoryviewor even a numpy array. Be sure to also specify the correct number of channels that the audio data has, and the sample with in bytes.
function ``stream_with_callbacks (sample_stream: Generator[bytes | array.array, int, NoneType], progress_callback: Callabl
No open issues yet, or sync has not completed.