How to use miniaudio - 10 common examples

To help you get started, we’ve selected a few miniaudio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github irmen / pyminiaudio / miniaudio.py View on Github external
def wav_get_file_info(filename: str) -> SoundFileInfo:
    """Fetch some information about the audio file (wav format)."""
    filenamebytes = _get_filename_bytes(filename)
    wav = ffi.new("drwav*")
    if not lib.drwav_init_file(wav, filenamebytes, ffi.NULL):
        raise DecodeError("could not open/decode file")
    try:
        duration = wav.totalPCMFrameCount / wav.sampleRate
        sample_width = wav.bitsPerSample // 8
        is_float = wav.translatedFormatTag == lib.DR_WAVE_FORMAT_IEEE_FLOAT
        return SoundFileInfo(filename, FileFormat.WAV, wav.channels, wav.sampleRate,
                             _format_from_width(sample_width, is_float), duration, wav.totalPCMFrameCount)
    finally:
        lib.drwav_uninit(wav)
github irmen / pyminiaudio / miniaudio.py View on Github external
def wav_get_file_info(filename: str) -> SoundFileInfo:
    """Fetch some information about the audio file (wav format)."""
    filenamebytes = _get_filename_bytes(filename)
    wav = ffi.new("drwav*")
    if not lib.drwav_init_file(wav, filenamebytes, ffi.NULL):
        raise DecodeError("could not open/decode file")
    try:
        duration = wav.totalPCMFrameCount / wav.sampleRate
        sample_width = wav.bitsPerSample // 8
        is_float = wav.translatedFormatTag == lib.DR_WAVE_FORMAT_IEEE_FLOAT
        return SoundFileInfo(filename, FileFormat.WAV, wav.channels, wav.sampleRate,
                             _format_from_width(sample_width, is_float), duration, wav.totalPCMFrameCount)
    finally:
        lib.drwav_uninit(wav)
github irmen / pyminiaudio / miniaudio.py View on Github external
def flac_get_file_info(filename: str) -> SoundFileInfo:
    """Fetch some information about the audio file (flac format)."""
    filenamebytes = _get_filename_bytes(filename)
    flac = lib.drflac_open_file(filenamebytes, ffi.NULL)
    if not flac:
        raise DecodeError("could not open/decode file")
    try:
        duration = flac.totalPCMFrameCount / flac.sampleRate
        sample_width = flac.bitsPerSample // 8
        return SoundFileInfo(filename, FileFormat.FLAC, flac.channels, flac.sampleRate,
                             _format_from_width(sample_width), duration, flac.totalPCMFrameCount)
    finally:
        lib.drflac_close(flac)
github irmen / pyminiaudio / miniaudio.py View on Github external
def flac_get_file_info(filename: str) -> SoundFileInfo:
    """Fetch some information about the audio file (flac format)."""
    filenamebytes = _get_filename_bytes(filename)
    flac = lib.drflac_open_file(filenamebytes, ffi.NULL)
    if not flac:
        raise DecodeError("could not open/decode file")
    try:
        duration = flac.totalPCMFrameCount / flac.sampleRate
        sample_width = flac.bitsPerSample // 8
        return SoundFileInfo(filename, FileFormat.FLAC, flac.channels, flac.sampleRate,
                             _format_from_width(sample_width), duration, flac.totalPCMFrameCount)
    finally:
        lib.drflac_close(flac)
github irmen / pyminiaudio / miniaudio.py View on Github external
self.sample_format = sample_format
        self.sample_format_name = ffi.string(lib.ma_get_format_name(sample_format.value)).decode()
        self.sample_width = _width_from_format(sample_format)
        self.num_frames = num_frames
        self.duration = duration
        self.file_format = file_format

    def __str__(self) -> str:
        return "<{clazz}: '{name}' {nchannels} ch, {sample_rate} hz, {sample_format.name}, " \
               "{num_frames} frames={duration:.2f} sec.>".format(clazz=self.__class__.__name__, **(vars(self)))

    def __repr__(self) -> str:
        return str(self)


class DecodedSoundFile(SoundFileInfo):
    """Contains various properties and also the PCM frames of a fully decoded audio file."""
    def __init__(self, name: str, nchannels: int, sample_rate: int,
                 sample_format: SampleFormat, samples: array.array) -> None:
        num_frames = len(samples) // nchannels
        duration = num_frames / sample_rate
        super().__init__(name, FileFormat.UNKNOWN, nchannels, sample_rate, sample_format, duration, num_frames)
        self.samples = samples


class MiniaudioError(Exception):
    """When a miniaudio specific error occurs."""
    pass


class DecodeError(MiniaudioError):
    """When something went wrong during decoding an audio file."""
github irmen / pyminiaudio / miniaudio.py View on Github external
def flac_get_info(data: bytes) -> SoundFileInfo:
    """Fetch some information about the audio data (flac format)."""
    flac = lib.drflac_open_memory(data, len(data), ffi.NULL)
    if not flac:
        raise DecodeError("could not open/decode data")
    try:
        duration = flac.totalPCMFrameCount / flac.sampleRate
        sample_width = flac.bitsPerSample // 8
        return SoundFileInfo("", FileFormat.FLAC, flac.channels, flac.sampleRate,
                             _format_from_width(sample_width), duration, flac.totalPCMFrameCount)
    finally:
        lib.drflac_close(flac)
github irmen / synthesizer / synthplayer / soundapi / miniaudio.py View on Github external
def __init__(self, samplerate: int = 0, samplewidth: int = 0, nchannels: int = 0, frames_per_chunk: int = 0) -> None:
        super().__init__(samplerate, samplewidth, nchannels, frames_per_chunk, 0)
        self.mixed_chunks = self.mixer.chunks()
        output_format = {
            1: miniaudio.SampleFormat.UNSIGNED8,
            2: miniaudio.SampleFormat.SIGNED16,
            3: miniaudio.SampleFormat.SIGNED24,
            4: miniaudio.SampleFormat.SIGNED32
        }[self.samplewidth]
        buffersize_msec = self.nchannels * 1000 * self.frames_per_chunk // self.samplerate
        self.mixed_chunks = self.mixer.chunks()
        self.device = miniaudio.PlaybackDevice(output_format, self.nchannels, self.samplerate, buffersize_msec)
        stream = self.generator()
        next(stream)  # start generator
        self.device.start(stream)
github irmen / synthesizer / synthplayer / soundapi / miniaudio.py View on Github external
def __init__(self, samplerate: int = 0, samplewidth: int = 0, nchannels: int = 0, frames_per_chunk: int = 0) -> None:
        super().__init__(samplerate, samplewidth, nchannels, frames_per_chunk, 0)
        self.mixed_chunks = self.mixer.chunks()
        output_format = {
            1: miniaudio.SampleFormat.UNSIGNED8,
            2: miniaudio.SampleFormat.SIGNED16,
            3: miniaudio.SampleFormat.SIGNED24,
            4: miniaudio.SampleFormat.SIGNED32
        }[self.samplewidth]
        buffersize_msec = self.nchannels * 1000 * self.frames_per_chunk // self.samplerate
        self.mixed_chunks = self.mixer.chunks()
        self.device = miniaudio.PlaybackDevice(output_format, self.nchannels, self.samplerate, buffersize_msec)
        stream = self.generator()
        next(stream)  # start generator
        self.device.start(stream)
github irmen / pyminiaudio / tests / test_miniaudio.py View on Github external
def test_stop_callback_playback(backends, jackd_server):
    stop_callback = mock.Mock()

    playback = miniaudio.PlaybackDevice(backends=backends)
    gen = dummy_generator()
    next(gen)
    playback.start(gen, stop_callback)

    assert playback.running is True
    # Simulate an unexpected stop.
    miniaudio.lib.ma_device_stop(playback._device)

    stop_callback.assert_called_once()
    assert playback.running is False
github irmen / pyminiaudio / tests / test_miniaudio.py View on Github external
def test_devices():
    devs = miniaudio.Devices()
    devs.get_playbacks()
    devs.get_captures()