How to use the miniaudio.DitherMode.TRIANGLE function in miniaudio

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 / examples / demo1.py View on Github external
"""
Simplest example of decoding and playing an audio file
"""

import os
import miniaudio


def samples_path(filename):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'samples', filename)


stream = miniaudio.stream_file(samples_path("music.mp3"), dither=miniaudio.DitherMode.TRIANGLE)
device = miniaudio.PlaybackDevice()
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
device.close()
github irmen / pyminiaudio / examples / streamingconvert.py View on Github external
"""
Convert samples in another format, in streaming fashion rather than all at once.
"""

import os
import array
import io
import miniaudio


def samples_path(filename):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'samples', filename)


# for demonstration purposes, just use a fully decoded file as a simulated streaming data source
decoded = miniaudio.decode_file(samples_path("music.ogg"), dither=miniaudio.DitherMode.TRIANGLE)
print("source:", decoded)
src = io.BytesIO(decoded.samples.tobytes())


# streaming conversion to other sound format frames

def produce_data(src: io.BytesIO, nchannels: int, samplewidth: int) -> miniaudio.PlaybackCallbackGeneratorType:
    desired_frames = yield b""  # generator initialization
    while True:
        desired_bytes = desired_frames * nchannels * samplewidth
        data = src.read(desired_bytes)
        if not data:
            break
        print(".", end="", flush=True)
        desired_frames = yield data
github irmen / pyminiaudio / examples / convert.py View on Github external
"""
Convert an audio file to WAV and different sample formats.
"""

import os
import array
import miniaudio


def samples_path(filename):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'samples', filename)


src = miniaudio.decode_file(samples_path("music.ogg"), dither=miniaudio.DitherMode.TRIANGLE)
print("Source: ", src)

result = miniaudio.DecodedSoundFile("result", 1, 22050, miniaudio.SampleFormat.UNSIGNED8, array.array('b'))
converted_frames = miniaudio.convert_frames(src.sample_format, src.nchannels, src.sample_rate, src.samples.tobytes(),
                                            result.sample_format, result.nchannels, result.sample_rate)
# note: currently it is not possible to provide a dithermode to convert_frames()

result.num_frames = int(len(converted_frames) / result.nchannels / result.sample_width)
result.samples.frombytes(converted_frames)


miniaudio.wav_write_file("converted.wav", result)
print("Converted sound written to ./converted.wav")

output_info = miniaudio.get_file_info("converted.wav")
print(output_info)
github irmen / pyminiaudio / examples / play.py View on Github external
try:
            while True:
                framecount = yield stream.send(framecount)
                print(".", end="", flush=True)
        except StopIteration:
            return

    output_format = info.sample_format
    try:
        filestream = miniaudio.stream_file(filename, output_format=output_format, sample_rate=info.sample_rate)
    except miniaudio.MiniaudioError as x:
        print("Cannot create optimal stream:", x)
        print("Creating stream with different sample format!")
        output_format = miniaudio.SampleFormat.SIGNED16
        filestream = miniaudio.stream_file(filename, output_format=output_format, sample_rate=info.sample_rate,
                                           dither=miniaudio.DitherMode.TRIANGLE)

    stream = progress_stream_wrapper(filestream)
    next(stream)   # start the generator
    device = miniaudio.PlaybackDevice(output_format=output_format, sample_rate=info.sample_rate)
    print("playback device backend:", device.backend, device.format.name, device.sample_rate, "hz")
    device.start(stream)
    input("Audio file playing in the background. Enter to stop playback: ")
    device.close()
github irmen / pyminiaudio / examples / streamingconvert.py View on Github external
desired_frames = yield b""  # generator initialization
    while True:
        desired_bytes = desired_frames * nchannels * samplewidth
        data = src.read(desired_bytes)
        if not data:
            break
        print(".", end="", flush=True)
        desired_frames = yield data


producer = produce_data(src, decoded.nchannels, decoded.sample_width)
next(producer)    # start the generator

converter = miniaudio.StreamingConverter(decoded.sample_format, decoded.nchannels, decoded.sample_rate,
                                         miniaudio.SampleFormat.UNSIGNED8, 1, 12000, producer,
                                         miniaudio.DitherMode.TRIANGLE)

print("Stream format conversion of source:")
framechunks = []
while True:
    framedata = converter.read(4000)
    if not framedata:
        break
    print("got chunk of size", len(framedata))
    framechunks.append(framedata)

print("\nGot", len(framechunks), "total frame chunks")

# convert the frames to bytes and write it to a file
samples = array.array('B')
for f in framechunks:
    samples.extend(f)