Package: miniaudio
Python miniaudioMultiplatform audio playback, recording, decoding and sample format conversion for
Linux (including Raspberri Pi), Windows, Mac and others.Installation for most users: viaPypi, Raspberri Pi builds viaPiWheels.This is a Pythonic interface to the cross-platformminiaudioC library:audio operations run in the backgroundpython bindings for most of the functions offered in the miniaudio library:reading and decoding audio filesgetting audio file properties (such as duration, number of channels, sample rate)converting sample formats and frequenciesstreaming large audio filesaudio playbackaudio recordingdecoders for wav, flac, vorbis and mp3Audio file and Icecast internet radio streamingPython enums instead of just some integers for special valuesseveral classes to represent the main functions of the librarygenerators for the Audio playback and recordingsample data is usually in the form of a Pythonarraywith appropriately sized elements
depending on the sample width (rather than a raw block of bytes)TODO: filters, waveform generators?Requires Python 3.6 or newer.  Also works on pypy3 (because it uses cffi).Software license for these Python bindings, miniaudio and the decoders: MITSynthesizer, modplayer?If you like this library you may also be interested in mysoftware FM synthesizeror mymod playerwhich uses libxmp.ExamplesMost basic audio file playbackimportminiaudiostream=miniaudio.stream_file("samples/music.mp3")withminiaudio.PlaybackDevice()asdevice:device.start(stream)input("Audio file playing in the background. Enter to stop playback: ")Playback of an unsupported file formatThis 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):importsubprocessimportminiaudiochannels=2sample_rate=44100sample_width=2# 16 bit pcmfilename="samples/music.m4a"# AAC encoded audio filedefstream_pcm(source):required_frames=yieldb""# generator initializationwhileTrue:required_bytes=required_frames*channels*sample_widthsample_data=source.read(required_bytes)ifnotsample_data:breakprint(".",end="",flush=True)required_frames=yieldsample_datawithminiaudio.PlaybackDevice(output_format=miniaudio.SampleFormat.SIGNED16,nchannels=channels,sample_rate=sample_rate)asdevice: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 generatordevice.start(stream)input("Audio file playing in the background. Enter to stop playback: ")ffmpeg.terminate()APINote: everything below is automatically generated from comments in the source code files. Do not edit in this readme directly.enum classBackendnames:WASAPIDSOUNDWINMMCOREAUDIOSNDIOAUDIO4OSSPULSEAUDIOALSAJACKAAUDIOOPENSLWEBAUDIOCUSTOMNULLOperating system audio backend to use (only a subset will be available)enum classChannelMixModenames:RECTANGULARSIMPLECUSTOMWEIGHTSHow to mix channels when convertingenum classDeviceTypenames:PLAYBACKCAPTUREDUPLEXType of audio deviceenum classDitherModenames:NONERECTANGLETRIANGLEHow to dither when convertingenum classFileFormatnames:UNKNOWNWAVFLACMP3VORBISAudio file formatenum classSampleFormatnames:UNKNOWNUNSIGNED8SIGNED16SIGNED24SIGNED32FLOAT32Sample format in memoryenum classSeekOriginnames:STARTCURRENTHow to seek() in a sourceenum classThreadPrioritynames:IDLELOWESTLOWNORMALHIGHHIGHESTREALTIMEThe priority of the worker thread (default=HIGHEST)functionconvert_frames  (from_fmt: miniaudio.SampleFormat, from_numchannels: int, from_samplerate: int, sourcedata: bytes, to_fmt: miniaudio.SampleFormat, to_numchannels: int, to_samplerate: int) -> bytearrayConvert 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.functionconvert_sample_format  (from_fmt: miniaudio.SampleFormat, sourcedata: bytes, to_fmt: miniaudio.SampleFormat, dither: miniaudio.DitherMode = <DitherMode.NONE: 0>) -> bytearrayConvert a raw buffer of pcm samples to another sample format. The result is returned as another
raw pcm sample bufferfunctiondecode  (data: bytes, output_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, nchannels: int = 2, sample_rate: int = 44100, dither: miniaudio.DitherMode = <DitherMode.NONE: 0>) -> miniaudio.DecodedSoundFileConvenience function to decode any supported audio file in memory to raw PCM samples in your
chosen format.functiondecode_file  (filename: str, output_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, nchannels: int = 2, sample_rate: int = 44100, dither: miniaudio.DitherMode = <DitherMode.NONE: 0>) -> miniaudio.DecodedSoundFileConvenience function to decode any supported audio file to raw PCM samples in your chosen format.functionflac_get_file_info  (filename: str) -> miniaudio.SoundFileInfoFetch some information about the audio file (flac format).functionflac_get_info  (data: bytes) -> miniaudio.SoundFileInfoFetch some information about the audio data (flac format).functionflac_read_f32  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole flac audio file. Resulting sample format is 32 bits float.functionflac_read_file_f32  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole flac audio file. Resulting sample format is 32 bits float.functionflac_read_file_s16  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole flac audio file. Resulting sample format is 16 bits signed integer.functionflac_read_file_s32  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole flac audio file. Resulting sample format is 32 bits signed integer.functionflac_read_s16  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole flac audio data. Resulting sample format is 16 bits signed integer.functionflac_read_s32  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole flac audio data. Resulting sample format is 32 bits signed integer.functionflac_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.functionget_enabled_backends  () -> Set[miniaudio.Backend]Returns the set of available backends by the compilation environment for the underlying miniaudio
C libraryfunctionget_file_info  (filename: str) -> miniaudio.SoundFileInfoFetch some information about the audio file.functionis_backend_enabled  (backend: miniaudio.Backend) -> boolDetermines whether or not the given backend is available by the compilation environment for the
underlying miniaudio C libraryfunctionis_loopback_supported  (backend: miniaudio.Backend) -> boolDetermines whether or not loopback mode is support by a backend.functionlib_version  () -> strReturns the version string of the underlying miniaudio C libraryfunctionmp3_get_file_info  (filename: str) -> miniaudio.SoundFileInfoFetch some information about the audio file (mp3 format).functionmp3_get_info  (data: bytes) -> miniaudio.SoundFileInfoFetch some information about the audio data (mp3 format).functionmp3_read_f32  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole mp3 audio data. Resulting sample format is 32 bits float.functionmp3_read_file_f32  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole mp3 audio file. Resulting sample format is 32 bits float.functionmp3_read_file_s16  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole mp3 audio file. Resulting sample format is 16 bits signed integer.functionmp3_read_s16  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole mp3 audio data. Resulting sample format is 16 bits signed integer.functionmp3_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.functionread_file  (filename: str, convert_to_16bit: bool = False) -> miniaudio.DecodedSoundFileReads 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.functionstream_any  (source: miniaudio.StreamableSource, source_format: miniaudio.FileFormat = <FileFormat.UNKNOWN: 0>, output_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024, dither: miniaudio.DitherMode = <DitherMode.NONE: 0>, 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.functionstream_file  (filename: str, output_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024, dither: miniaudio.DitherMode = <DitherMode.NONE: 0>, 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.functionstream_memory  (data: bytes, output_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, nchannels: int = 2, sample_rate: int = 44100, frames_to_read: int = 1024, dither: miniaudio.DitherMode = <DitherMode.NONE: 0>) -> 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.functionstream_raw_pcm_memory  (pcmdata: Union[array.array, memoryview, bytes], nchannels: int, sample_width: int, frames_to_read: int = 4096) -> Generator[Union[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 inarraytype 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.functionstream_with_callbacks  (sample_stream: Generator[Union[bytes, array.array], int, NoneType], progress_callback: Optional[Callable[[int], NoneType]] = None, frame_process_method: Optional[Callable[[Union[bytes, array.array]], Union[bytes, array.array]]] = None, end_callback: Optional[Callable] = None) -> Generator[Union[bytes, array.array], int, NoneType]Convenience generator function to add callback and processing functionality to another stream. You
can specify : > A callback function that gets called during play and takes an int for the number of
frames played.  > A function that can be used to process raw data frames before they are yielded
back (takes an array.array or bytes, returns an array.array or bytes) *Note: if the processing
method is slow it will result in audio glitchiness  > A callback function that gets called when the
stream ends playing.functionvorbis_get_file_info  (filename: str) -> miniaudio.SoundFileInfoFetch some information about the audio file (vorbis format).functionvorbis_get_info  (data: bytes) -> miniaudio.SoundFileInfoFetch some information about the audio data (vorbis format).functionvorbis_read  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole vorbis audio data. Resulting sample format is 16 bits signed integer.functionvorbis_read_file  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole vorbis audio file. Resulting sample format is 16 bits signed integer.functionvorbis_stream_file  (filename: str, seek_frame: int = 0) -> Generator[array.array, NoneType, NoneType]Streams the ogg vorbis audio file as interleaved 16 bit signed integer sample arrays segments.
This uses a variable unconfigurable chunk size and cannot be used as a generic miniaudio decoder
input stream. Consider using stream_file() instead.functionwav_get_file_info  (filename: str) -> miniaudio.SoundFileInfoFetch some information about the audio file (wav format).functionwav_get_info  (data: bytes) -> miniaudio.SoundFileInfoFetch some information about the audio data (wav format).functionwav_read_f32  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole wav audio data. Resulting sample format is 32 bits float.functionwav_read_file_f32  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole wav audio file. Resulting sample format is 32 bits float.functionwav_read_file_s16  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole wav audio file. Resulting sample format is 16 bits signed integer.functionwav_read_file_s32  (filename: str) -> miniaudio.DecodedSoundFileReads and decodes the whole wav audio file. Resulting sample format is 32 bits signed integer.functionwav_read_s16  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole wav audio data. Resulting sample format is 16 bits signed integer.functionwav_read_s32  (data: bytes) -> miniaudio.DecodedSoundFileReads and decodes the whole wav audio data. Resulting sample format is 32 bits signed integer.functionwav_stream_file  (filename: str, frames_to_read: int = 1024, seek_frame: int = 0) -> Generator[array.array, NoneType, NoneType]Streams the WAV 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.functionwav_write_file  (filename: str, sound: miniaudio.DecodedSoundFile)Writes the pcm sound to a WAV filefunctionwidth_from_format  (sampleformat: miniaudio.SampleFormat) -> intreturns the sample width in bytes, of the given sample format.classCaptureDeviceCaptureDevice  (self, input_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, nchannels: int = 2, sample_rate: int = 44100, buffersize_msec: int = 200, device_id: Optional[_cffi_backend._CDataBase] = None, callback_periods: int = 0, backends: Optional[List[miniaudio.Backend]] = None, thread_prio: miniaudio.ThreadPriority = <ThreadPriority.HIGHEST: 0>, app_name: str = '')An audio device provided by miniaudio, for audio capture (recording).methodclose  (self)Halt playback or capture and close down the device. If you use the device as a context manager,
it will be closed automatically.methodstart  (self, callback_generator: Generator[NoneType, Union[bytes, array.array], NoneType])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)methodstop  (self)Halt playback or capture.classDecodeErrorDecodeError  (self, /, *args, **kwargs)When something went wrong during decoding an audio file.classDecodedSoundFileDecodedSoundFile  (self, name: str, nchannels: int, sample_rate: int, sample_format: miniaudio.SampleFormat, samples: array.array)Contains various properties and also the PCM frames of a fully decoded audio file.classDevicesDevices  (self, backends: Optional[List[miniaudio.Backend]] = None)Query the audio playback and record devices that miniaudio providesmethodget_captures  (self) -> List[Dict[str, Any]]Get a list of capture devices and some details about themmethodget_playbacks  (self) -> List[Dict[str, Any]]Get a list of playback devices and some details about themclassDuplexStreamDuplexStream  (self, playback_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, playback_channels: int = 2, capture_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, capture_channels: int = 2, sample_rate: int = 44100, buffersize_msec: int = 200, playback_device_id: Optional[_cffi_backend._CDataBase] = None, capture_device_id: Optional[_cffi_backend._CDataBase] = None, callback_periods: int = 0, backends: Optional[List[miniaudio.Backend]] = None, thread_prio: miniaudio.ThreadPriority = <ThreadPriority.HIGHEST: 0>, app_name: str = '')Joins a capture device and a playback device.methodclose  (self)Halt playback or capture and close down the device. If you use the device as a context manager,
it will be closed automatically.methodstart  (self, callback_generator: Generator[Union[bytes, array.array], Union[bytes, array.array], NoneType])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)methodstop  (self)Halt playback or capture.classIceCastClientIceCastClient  (self, url: str, update_stream_title: Callable[[ForwardRef('IceCastClient'), str], NoneType] = None, ssl_context: 'ssl.SSLContext' = None)A simple client for IceCast audio streams as miniaudio streamable source. If the stream has Icy
MetaData, the stream_title attribute will be updated with the actual title taken from the metadata.
You can also provide a callback to be called when a new stream title is available. The downloading
of the data from the internet is done in a background thread and it tries to keep a (small) buffer
filled with available data to read. You can optionally provide a custom ssl.SSLContext in the
ssl_context parameter, if you need to change the way SSL connections are configured (certificates,
checks, etc).methodclose  (self)Stop the stream, aborting the background downloading.methodread  (self, num_bytes: int) -> bytesRead a chunk of data from the stream.methodseek  (self, offset: int, origin: miniaudio.SeekOrigin) -> boolOverride this if the stream supports seeking. Note: seek support is sometimes not needed if you
give the file type to a decoder upfront. You can ignore this method then.classMiniaudioErrorMiniaudioError  (self, /, *args, **kwargs)When a miniaudio specific error occurs.classPlaybackDevicePlaybackDevice  (self, output_format: miniaudio.SampleFormat = <SampleFormat.SIGNED16: 2>, nchannels: int = 2, sample_rate: int = 44100, buffersize_msec: int = 200, device_id: Optional[_cffi_backend._CDataBase] = None, callback_periods: int = 0, backends: Optional[List[miniaudio.Backend]] = None, thread_prio: miniaudio.ThreadPriority = <ThreadPriority.HIGHEST: 0>, app_name: str = '')An audio device provided by miniaudio, for audio playback.methodclose  (self)Halt playback or capture and close down the device. If you use the device as a context manager,
it will be closed automatically.methodstart  (self, callback_generator: Generator[Union[bytes, array.array], int, NoneType])Start the audio device: playback begins. The audio data is provided by the given callback
generator. The generator gets sent the required number of frames and should yield the sample data as
raw bytes, a memoryview, an array.array, or as a numpy array with shape (numframes, numchannels).
The generator should already be started before passing it in.methodstop  (self)Halt playback or capture.classSoundFileInfoSoundFileInfo  (self, name: str, file_format: miniaudio.FileFormat, nchannels: int, sample_rate: int, sample_format: miniaudio.SampleFormat, duration: float, num_frames: int, sub_format: int = None)Contains various properties of an audio file.classStreamableSourceStreamableSource  (self, /, *args, **kwargs)Base class for streams of audio data bytes. Can be used as a contextmanager, to properly call
close().methodclose  (self)Override this to properly close the stream and free resources.methodread  (self, num_bytes: int) -> Union[bytes, memoryview]override this to provide data bytes to the consumer of the streammethodseek  (self, offset: int, origin: miniaudio.SeekOrigin) -> boolOverride this if the stream supports seeking. Note: seek support is sometimes not needed if you
give the file type to a decoder upfront. You can ignore this method then.classWavFileReadStreamWavFileReadStream  (self, pcm_sample_gen: Generator[Union[bytes, array.array], int, NoneType], sample_rate: int, nchannels: int, output_format: miniaudio.SampleFormat, max_frames: int = 0)An IO stream that reads as a .wav file, and which gets its pcm samples from the provided producermethodclose  (self)Close the filemethodread  (self, amount: int = 9223372036854775807) -> Optional[bytes]Read up to the given amount of bytes from the file.