def convert_audio_miniaudio(in_file, out_file): channels = 1 sample_rate = 44100 src = miniaudio.decode_file( in_file) # , dither=miniaudio.DitherMode.TRIANGLE # DecodedSoundFile - Contains various properties and also the PCM frames of # a fully decoded audio file. tgt = miniaudio.DecodedSoundFile("result", 1, sample_rate, miniaudio.SampleFormat.SIGNED16, array.array('b')) converted_frames = miniaudio.convert_frames(src.sample_format, src.nchannels, src.sample_rate, src.samples.tobytes(), tgt.sample_format, tgt.nchannels, tgt.sample_rate) tgt.num_frames = int( len(converted_frames) / tgt.nchannels / tgt.sample_width) tgt.samples.frombytes(converted_frames) miniaudio.wav_write_file(out_file, tgt) logger.debug("wrote converted file to '%s'" % out_file) return False
import miniaudio 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 with miniaudio.PlaybackDevice() as device: 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: ")
""" 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)
import miniaudio 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/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()