Here, it is explained that you can instantiate a pyaudio object and get a list of device ids at an interactive python prompt like this:
import pyaudio
p = pyaudio.PyAudio()[p.get_device_info_by_index(i) for i in range(p.get_device_count())]
The above code basically initializes the PyAudio object, and then we open up a stream object that allows us to record from the microphone using stream.read() method. After we finish recording, we use the built-in wave module to write that WAV audio file into the disk.,When you set input=True in the p.open() method, you will be able to use stream.read() to read from the microphone. Also, when you set output=True, you'll be able to use stream.write() to write to the speaker.,A great challenge for you is to combine this with a screen recorder, and you'll come up with a Python tool that records your voice and screen simultaneously. You will need to use a thread that records audio and another one for the screen recorder, good luck with that!,Many of the applications out there record your voice as well as playing sounds, if you want to do that as well, then you came into the right place, in this tutorial, we will be using different Python libraries to play and record audio in Python.
Let's install the required libraries for this tutorial:
pip3 install playsound pyaudio pydub ffmpeg - python
First, we gonna start with the most straightforward module here, playsound:
from playsound
import playsound
playsound("audio_file.mp3")
Another alternative is to use the Pydub library:
from pydub import AudioSegment from pydub.playback import play # read MP3 file song = AudioSegment.from_mp3("audio_file.mp3") # song = AudioSegment.from_wav("audio_file.wav") # you can also read from other formats such as MP4 # song = AudioSegment.from_file("audio_file.mp4", "mp4") play(song)
Play and record audio without resorting to CPython extensions,SoundCard is a library for playing and recording audio without resorting to a CPython extension. Instead, it is implemented using the wonderful CFFI and the native audio libraries of Linux, Windows and macOS.,SoundCard is cross-platform, and supports Linux/pulseaudio, Mac/coreaudio, and Windows/WASAPI. While the programming interface is identical across platforms, sound card naming schemes and default block sizes can vary between devices and platforms.,In addition, pulseaudio/Linux defines channel -1 as the mono mix of all channels for both playback and recording. CoreAudio/macOS defines channel -1 as silence for both playback and recording.
Here is how you get to your Speakers and Microphones:
import soundcard as sc
# get a list of all speakers:
speakers = sc.all_speakers()
# get the current default speaker on your system:
default_speaker = sc.default_speaker()
# get a list of all microphones:
mics = sc.all_microphones()
# get the current default microphone on your system:
default_mic = sc.default_microphone()
# search for a sound card by substring:
>>> sc.get_speaker('Scarlett')
<Speaker Focusrite Scarlett 2i2 (2 channels)>
>>> one_mic = sc.get_microphone('Scarlett')
<Microphone Focusrite Scalett 2i2 (2 channels)>
# fuzzy-search to get the same results:
one_speaker = sc.get_speaker('FS2i2')
one_mic = sc.get_microphone('FS2i2')
All of these functions return Speaker and Microphone objects, which can be used for playback and recording. All data passed in and out of these objects are frames × channels Numpy arrays.
import numpy
>>> print(default_speaker)
<Speaker Focusrite Scarlett 2i2 (2 channels)>
>>> print(default_mic)
<Microphone Focusrite Scarlett 2i2 (2 channels)>
# record and play back one second of audio:
data = default_mic.record(samplerate=48000, numframes=48000)
default_speaker.play(data/numpy.max(data), samplerate=48000)
# alternatively, get a `Recorder` and `Player` object
# and play or record continuously:
with default_mic.recorder(samplerate=48000) as mic, \
default_speaker.player(samplerate=48000) as sp:
for _ in range(100):
data = mic.record(numframes=1024)
sp.play(data)
Additionally, it might help to experiment with advice from here: https://askubuntu.com/questions/707171/how-can-i-fix-choppy-audio and edit your /etc/pulse/default.pa file to replace the line saying
load - module module - udev - detect
with
load - module module - udev - detect tsched = 0
and then do not forget to restart pulseaudio with
pulseaudio - k
A: PulseAudio is not installed by default on the Raspberry Pi OS Lite distribution (https://www.raspberrypi.org/software/operating-systems/). In order to use soundcard, you have to install PulseAudio first, and edit the configuration (with a fix to avoid the main output to be in mono-only).
sudo apt install - y python3 - pip python3 - numpy pulseaudio sudo nano / usr / share / pulseaudio / alsa - mixer / profile - sets / default.conf # comment the block[Mapping analog - mono] with ';' pulseaudio - D python3 - m pip install soundcard