Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'samples', filename)
def memory_stream(soundfile: miniaudio.DecodedSoundFile) -> miniaudio.AudioProducerType:
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_path("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()
def choose_device():
devices = miniaudio.Devices()
print("Available playback devices:")
playbacks = devices.get_playbacks()
for d in enumerate(playbacks, 1):
print("{num} = {name}".format(num=d[0], name=d[1]['name']))
choice = int(input("play on which device? "))
return playbacks[choice-1]
if __name__ == "__main__":
selected_device = choose_device()
stream = miniaudio.stream_file(samples_path("music.mp3"))
device = miniaudio.PlaybackDevice(device_id=selected_device["id"])
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
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"))
device = miniaudio.PlaybackDevice()
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
device.close()
return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'samples', filename)
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
filename = samples_path("music.m4a") # AAC encoded file
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()
import os
import miniaudio
def samples_path(filename):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'samples', filename)
if __name__ == "__main__":
devices = miniaudio.Devices()
selected_device = devices.get_playbacks()[0]
print("Playing back through {}".format(selected_device.name))
stream = miniaudio.stream_file(samples_path("music.mp3"))
device = miniaudio.PlaybackDevice(device_id=selected_device.id)
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
"""
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()
def stream_file(filename):
def progress_stream_wrapper(stream) -> miniaudio.AudioProducerType:
framecount = yield(b"")
try:
while True:
framecount = yield stream.send(framecount)
print(".", end="", flush=True)
except StopIteration:
return
stream = progress_stream_wrapper(miniaudio.stream_file(filename))
next(stream) # start the generator
device = miniaudio.PlaybackDevice()
print("playback device backend:", device.backend)
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
device.close()