Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def samples_path(filename):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), 'samples', filename)
def memory_stream(npa: numpy.ndarray) -> miniaudio.PlaybackCallbackGeneratorType:
required_frames = yield b"" # generator initialization
frames = 0
while frames < len(npa):
print(".", end="", flush=True)
frames_end = frames + required_frames
required_frames = yield npa[frames:frames_end]
frames = frames_end
device = miniaudio.PlaybackDevice()
decoded = miniaudio.decode_file(samples_path("music.wav"))
# convert the sample data into a numpy array with shape (numframes, numchannels):
npa = numpy.array(decoded.samples, dtype=numpy.int16).reshape((-1, decoded.nchannels))
stream = memory_stream(npa)
next(stream) # start the generator
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
device.close()
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()