Skip to main content

saythanks Latest Version Build Status

Python miniaudio

This module provides:

  • the miniaudio cross platform sound playback, recording and conversion library
  • its decoders for wav, flac, vorbis and mp3
  • python bindings via cffi for much of the functions offered in those libraries:
    • getting audio file properties (such as duration, number of channels, sample rate)
    • reading and decoding audio files
    • streaming audio files
    • playback (via efficient asynchronous pull-API)
    • recording
    • streaming, recording and playback are done with generator functions

Requires Python 3.5 or newer. Also works on pypy3 (because it uses cffi).

The library is primarily distributed in source form so you need a C compiler to build and install this (note: the setup script takes care of the actual compilation process, no need to worry about compiling things yourself). For Linux and Mac this shouldn't be a problem. For Windows users, if the correct binary install is not available on pypi, you'll have to get it to compile as well which may be a bit of a hassle on this platform. You have to make sure that the required tools that allow you to compile Python extension modules are installed (Visual Studio or the VC++ build tools).

Software license for these Python bindings, miniaudio and the decoders: MIT

Todo

  • the various format conversion functions aren't properly exposed yet.

Examples

Most basic audio file playback

import miniaudio
stream = miniaudio.stream_file("samples/music.mp3")
device = miniaudio.PlaybackDevice()
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
device.close()

Playback using several other API functions

import miniaudio

def memory_stream(soundfile: miniaudio.DecodedSoundFile) -> miniaudio.PlaybackCallbackGeneratorType:
    required_frames = yield b""  # generator initialization
    current = 0
    samples = memoryview(soundfile.samples)     # avoid needless memory copying
    while current < len(samples):
        sample_count = required_frames * soundfile.nchannels
        output = samples[current:current + sample_count]
        current += sample_count
        print(".", end="", flush=True)
        required_frames = yield output

device = miniaudio.PlaybackDevice()
decoded = miniaudio.decode_file("samples/music.mp3")
print("The decoded file has {} frames at {} hz and takes {:.1f} seconds"
      .format(decoded.num_frames, decoded.sample_rate, decoded.duration))
stream = memory_stream(decoded)
next(stream)  # start the generator
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
device.close()

Playback of a file format that miniaudio can't decode by itself

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):

import subprocess
import miniaudio

channels = 2
sample_rate = 44100
sample_width = 2  # 16 bit pcm
filename = "samples/music.m4a"  # AAC encoded audio file

def stream_pcm(source):
    required_frames = yield b""  # generator initialization
    while True:
        required_bytes = required_frames * channels * sample_width
        sample_data = source.read(required_bytes)
        if not sample_data:
            break
        print(".", end="", flush=True)
        required_frames = yield sample_data

device = miniaudio.PlaybackDevice(ma_output_format=miniaudio.ma_format_s16,
                                  nchannels=channels, sample_rate=sample_rate)
ffmpeg = subprocess.Popen(["ffmpeg", "-v", "fatal", "-hide_banner", "-nostdin",
                           "-i", filename, "-f", "s16le", "-acodec", "pcm_s16le",
                           "-ac", str(channels), "-ar", str(sample_rate), "-"],
                          stdin=None, stdout=subprocess.PIPE)
stream = stream_pcm(ffmpeg.stdout)
next(stream)  # start the generator
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
device.close()
ffmpeg.terminate()

API

get_file_info(filename: str) -> miniaudio.SoundFileInfo
    Fetch some information about the audio file.

flac_get_file_info(filename: str) -> miniaudio.SoundFileInfo
    Fetch some information about the audio file (flac format).

flac_get_info(data: bytes) -> miniaudio.SoundFileInfo
    Fetch some information about the audio data (flac format).
    
flac_read_f32(data: bytes) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole flac audio file. Resulting sample format is 32 bits float.

flac_read_file_f32(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole flac audio file. Resulting sample format is 32 bits float.

flac_read_file_s16(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole flac audio file. Resulting sample format is 16 bits signed integer.

flac_read_file_s32(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole flac audio file. Resulting sample format is 32 bits signed integer.

flac_read_s16(data: bytes) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole flac audio data. Resulting sample format is 16 bits signed integer.

flac_read_s32(data: bytes) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole flac audio data. Resulting sample format is 32 bits signed integer.

flac_stream_file(filename: str, frames_to_read: int = 1024) -> Generator[array.array, NoneType, NoneType]
    Streams the flac audio file as interleaved 16 bit signed integer sample arrays segments.

decode(data: bytes, ma_output_format: int = 2, nchannels: int = 2, sample_rate: int = 44100) -> miniaudio.DecodedSoundFile
    Convenience function to decode any supported audio file in memory to raw PCM samples in your chosen format.

decode_file(filename: str, ma_output_format: int = 2, nchannels: int = 2, sample_rate: int = 44100) -> miniaudio.DecodedSoundFile
    Convenience function to decode any supported audio file to raw PCM samples in your chosen format.

stream_file(filename: str, ma_output_format: int = 2, nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024) -> 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.

stream_memory(data: bytes, ma_output_format: int = 2, nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024) -> 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.

mp3_get_file_info(filename: str) -> miniaudio.SoundFileInfo
    Fetch some information about the audio file (mp3 format).

mp3_get_info(data: bytes) -> miniaudio.SoundFileInfo
    Fetch some information about the audio data (mp3 format).

mp3_read_f32(data: bytes, want_nchannels: int = 0, want_sample_rate: int = 0) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole mp3 audio data. Resulting sample format is 32 bits float.

mp3_read_file_f32(filename: str, want_nchannels: int = 0, want_sample_rate: int = 0) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole mp3 audio file. Resulting sample format is 32 bits float.

mp3_read_file_s16(filename: str, want_nchannels: int = 0, want_sample_rate: int = 0) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole mp3 audio file. Resulting sample format is 16 bits signed integer.

mp3_read_s16(data: bytes, want_nchannels: int = 0, want_sample_rate: int = 0) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole mp3 audio data. Resulting sample format is 16 bits signed integer.

mp3_stream_file(filename: str, frames_to_read: int = 1024, want_nchannels: int = 0, want_sample_rate: int = 0) -> Generator[array.array, NoneType, NoneType]
    Streams the mp3 audio file as interleaved 16 bit signed integer sample arrays segments.

read_file(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole audio file. Resulting sample format is 16 bits signed integer.

vorbis_get_file_info(filename: str) -> miniaudio.SoundFileInfo
    Fetch some information about the audio file (vorbis format).

vorbis_get_info(data: bytes) -> miniaudio.SoundFileInfo
    Fetch some information about the audio data (vorbis format).

vorbis_read(data: bytes) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole vorbis audio data. Resulting sample format is 16 bits signed integer.

vorbis_read_file(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole vorbis audio file. Resulting sample format is 16 bits signed integer.

vorbis_stream_file(filename: str) -> Generator[array.array, NoneType, NoneType]
    Streams the ogg vorbis audio file as interleaved 16 bit signed integer sample arrays segments.

wav_get_file_info(filename: str) -> miniaudio.SoundFileInfo
    Fetch some information about the audio file (wav format).

wav_get_info(data: bytes) -> miniaudio.SoundFileInfo
    Fetch some information about the audio data (wav format).

wav_read_f32(data: bytes) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole wav audio data. Resulting sample format is 32 bits float.

wav_read_file_f32(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole wav audio file. Resulting sample format is 32 bits float.

wav_read_file_s16(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole wav audio file. Resulting sample format is 16 bits signed integer.

wav_read_file_s32(filename: str) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole wav audio file. Resulting sample format is 32 bits signed integer.

wav_read_s16(data: bytes) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole wav audio data. Resulting sample format is 16 bits signed integer.

wav_read_s32(data: bytes) -> miniaudio.DecodedSoundFile
    Reads and decodes the whole wav audio data. Resulting sample format is 32 bits signed integer.

wav_stream_file(filename: str, frames_to_read: int = 1024) -> Generator[array.array, NoneType, NoneType]
    Streams the WAV audio file as interleaved 16 bit signed integer sample arrays segments.

wav_write_file(filename: str, sound: miniaudio.DecodedSoundFile) -> None
    Writes the pcm sound to a WAV file


class PlaybackDevice(ma_output_format: int = 2, nchannels: int = 2, sample_rate: int = 44100, 
                     buffersize_msec: int = 200, device_id: Union[ffi.CData, None] = None)
   An audio device provided by miniaudio, for audio playback.
   
   close(self)
       Halt playback and close down the device.
   
   start(self, callback_generator: PlaybackCallbackGeneratorType) -> None
       Start the audio device: playback begins. The audio data is provided by the given audio_producer
       generator. The generator gets sent the required number of frames and should yield the sample data
       as raw bytes or as an array.array.  (it should already be started before passing it in)
   
   stop(self) -> None
       Halt playback.
       
       
class CaptureDevice(ma_input_format: int = 2, nchannels: int = 2, sample_rate: int = 44100,
                    buffersize_msec: int = 200, device_id: Union[ffi.CData, NoneType] = None)
    An audio device provided by miniaudio, for audio capture (recording).

    close(self)
        Halt capture and close down the device.
   
    start(self, callback_generator: CaptureCallbackGeneratorType) -> None
        Start the audio device: capture (recording) begins.
        The recorded audio data is sent to the given callback generator as raw bytes.
        (it should already be started before)
   
    stop(self) -> None
        Halt capture.


class DuplexStream(playback_format: int = 2, playback_channels: int = 2, capture_format: int = 2, 
                   capture_channels: int = 2, sample_rate: int = 44100, buffersize_msec: int = 200, 
                   playback_device_id: Union[ffi.CData, NoneType] = None, capture_device_id: Union[ffi.CData, NoneType] = None)
    Joins a capture device and a playback device.
    
    close(self)
        Halt capture and playback, and close down the device.

    start(self, callback_generator: DuplexCallbackGeneratorType) -> None
        Start the audio device: playback and capture begin.
        The audio data for playback is provided by the given callback generator, which is sent the
        recorded audio data at the same time.
        (it should already be started before passing it in)

    stop(self) -> None
        Halt capture and playback.
 
 
class WavFileReadStream(pcm_sample_gen: Generator[Union[bytes, array.array], int, NoneType], sample_rate: int, nchannels: int, ma_output_format: int, max_frames: int = 0)
    An IO stream that reads as a .wav file, and which gets its pcm samples from the provided producer

    close(self) -> None
        Flush and close the IO object.
    
    read(self, amount: int) -> Union[bytes, NoneType]
        Read bytes from the stream.


class Devices:
    Query the audio playback and record devices that miniaudio provides

    get_captures(self) -> List[Dict[str, Any]]
        Get a list of capture devices and some details about them
    
    get_playbacks(self) -> List[Dict[str, Any]]
        Get a list of playback devices and some details about them

Metadata

Release files for miniaudio 1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for miniaudio 1.4
File Size Uploaded
miniaudio-1.4.tar.gz 427.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for miniaudio 1.4
File Interpreter ABI Platform
miniaudio-1.4-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details
miniaudio-1.4-cp37-cp37m-win32.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-32 Details

Total release size: 858.9 kB

Release files / miniaudio-1.4.tar.gz

Download URL miniaudio-1.4.tar.gz
Size 427.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6f581f20073bce77d8512d47eec3bbcda931c58351d76faa680cae2f61abf640
BLAKE2b-256 checksum
How to use checksums
b47699c6d97bc447227057d31ed656b22e7a987760d96f2fc5a3170ade0049f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.3

Release files / miniaudio-1.4-cp37-cp37m-win_amd64.whl

Download URL miniaudio-1.4-cp37-cp37m-win_amd64.whl
Size 231.9 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
7656bce6f95234ed713029cf88fd987c644878e24dbe42a5965cf5188d9f2a33
BLAKE2b-256 checksum
How to use checksums
ae420d36cf22f2bcaa75f3f34cc6891fcbea5f9fbae1ea5c71532201f8619cd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release files / miniaudio-1.4-cp37-cp37m-win32.whl

Download URL miniaudio-1.4-cp37-cp37m-win32.whl
Size 199.7 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-32
SHA-256 checksum
How to use checksums
5654df20137f9f35b3714b96b04dd6a9e08593d8bb62ec21327350706ef34c66
BLAKE2b-256 checksum
How to use checksums
b0aecfe5498aa1ee9de6e9c544a4801c517225cb737d45c3c03fdf55960ae260
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

Release history Release notifications | RSS feed

1.71

31 release files

1.70

33 release files

1.61

60 release files

1.60

60 release files

1.59

52 release files

1.58

47 release files

1.57

37 release files

1.56

37 release files

1.55

37 release files

1.54

37 release files

1.53

37 release files

1.52

37 release files

1.51

37 release files

1.50

37 release files

1.46

37 release files

1.45

6 release files

1.44

4 release files

1.43

5 release files

1.42

5 release files

1.41

6 release files

1.40

6 release files

1.39

6 release files

1.38

6 release files

1.37

6 release files

1.36

6 release files

1.35

6 release files

1.34

6 release files

1.33

6 release files

1.32

6 release files

1.31

6 release files

1.30

6 release files

1.23

6 release files

1.22

6 release files

1.21

6 release files

1.20

6 release files

1.10

6 release files

1.9

5 release files

1.8

5 release files

1.7

6 release files

1.6

4 release files

1.5

3 release files

This release

1.4 This release

3 release files

1.3

3 release files

1.2.2

3 release files

1.2

1 release file

1.1

1 release file

1.0.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page