百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pyminiaudio

> 编程语言
开源

Python 接口用于连接 miniaudio 音频播放、录制、解码和转换库

185 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Python 接口用于连接 miniaudio 音频播放、录制、解码和转换库

Python miniaudio

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:

  • audio operations run in the background
  • python bindings for most of the functions offered in the miniaudio library:
    • reading and decoding audio files
    • getting audio file properties (such as duration, number of channels, sample rate)
    • converting sample formats and frequencies
    • streaming large audio files
    • audio playback
    • audio recording
  • decoders for wav, flac, vorbis and mp3
  • Audio file and Icecast internet radio streaming
  • Python enums instead of just some integers for special values
  • several classes to represent the main functions of the library
  • generators for the Audio playback and recording
  • sample data is usually in the form of a Python array with appropriately sized elements depending on the sample width (rather than a raw block of bytes)
  • TODO: filters, waveform generators?

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

Synthesizer, modplayer?

If you like this library you may also be interested in my software FM synthesizer or my mod player which uses libxmp.

Examples

Basic audio file playback with info

python
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")

Decode and convert audio file

…

Playback of an unsupported file format

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

…

API

Note: everything below is automatically generated from comments in the source code files. Do not edit in this readme directly.

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 array type or bytes, memoryview or 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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言