Also you should use the python struct module to decode the buffer.
import struct buff_size = 512 # 'H' is for unsigned 16 bit integer, try 'h' also sample_buff = struct.unpack('H' * buf_size, raw_buf)
The easiest way is to use a library that does the decoding for you. There are several Python libraries available, my favorite is the soundfile module:
import soundfile as sf
signal, samplerate = sf.read('/some_path/my_audio.wav')
Convenience function to decode any supported audio file in memory to raw PCM samples in your chosen format.,Convenience function to decode any supported audio file to raw PCM samples in your chosen format.,Convenience generator function to decode and stream any supported audio file in memory as chunks of raw PCM samples in the chosen format. If you send() a number into the generator rather than just using next() on it, you'll get that given number of frames, instead of the default configured amount. This is particularly useful to plug this stream into an audio device callback that wants a variable number of frames per call.,Convenience generator function to decode and stream any supported audio file as chunks of raw PCM samples in the chosen format. If you send() a number into the generator rather than just using next() on it, you'll get that given number of frames, instead of the default configured amount. This is particularly useful to plug this stream into an audio device callback that wants a variable number of frames per call.
Most basic audio file playback
import miniaudio
stream = miniaudio.stream_file("samples/music.mp3")
with miniaudio.PlaybackDevice() as device:
device.start(stream)
input("Audio file playing in the background. Enter to stop playback: ")
This example uses ffmpeg as an external tool to decode an audio file in a format that miniaudio itself can't decode (m4a/aac in this case):
import subprocess import miniaudio channels = 2 sample_rate = 44100 sample_width = 2 # 16 bit pcm filename = "samples/music.m4a" # AAC encoded audio file 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 with miniaudio.PlaybackDevice(output_format = miniaudio.SampleFormat.SIGNED16, nchannels = channels, sample_rate = sample_rate) as device: 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: ") ffmpeg.terminate()
Last Updated : 25 Jul, 2022
To use pyttsx3 we have to install espeak and ffmpeg first.
sudo apt update sudo apt install espeak sudo apt install ffmpeg
Additionally, we need to install the latest version of pyttsx3
python3 - m pip install pyttsx3
We can confirm the installation by importing the module.
import pyttsx3