Skip to content

Commit d6e7275

Browse files
committed
added playing & recording audio tutorial
1 parent 3183d73 commit d6e7275

8 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [How to Play and Record Audio in Python](https://www.thepythoncode.com/article/play-and-record-audio-sound-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- To record audio:
5+
```
6+
python audio_recorder.py --help
7+
```
8+
**Output:**
9+
```
10+
usage: audio_recorder.py [-h] [-o OUTPUT] [-d DURATION]
11+
12+
an Audio Recorder using Python
13+
14+
optional arguments:
15+
-h, --help show this help message and exit
16+
-o OUTPUT, --output OUTPUT
17+
Output file (with .wav)
18+
-d DURATION, --duration DURATION
19+
Duration to record in seconds (can be float)
20+
```
21+
For instance, you want to record 5 seconds and save it to `recorded.wav` file:
22+
```
23+
python audio_recorder.py -d 5 -o recorded.wav
24+
```
25+
- To play audio, there are 3 options (`audio_player_1.py` using [playsound](), `audio_player_2.py` using [pydub](), `audio_player_3.py` using [pyaudio]()):
26+
```
27+
python audio_player.py --help
28+
```
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from playsound import playsound
2+
3+
playsound("recorded.wav")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pyaudio
2+
import wave
3+
4+
filename = "audio_file.wav"
5+
6+
# set the chunk size of 1024 samples
7+
chunk = 1024
8+
9+
# open the audio file
10+
wf = wave.open(filename, "rb")
11+
12+
# initialize PyAudio object
13+
p = pyaudio.PyAudio()
14+
15+
# open stream object
16+
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
17+
channels=wf.getnchannels(),
18+
rate=wf.getframerate(),
19+
output=True)
20+
21+
# read data in chunks
22+
data = wf.readframes(chunk)
23+
24+
# writing to the stream (playing audio)
25+
while data:
26+
stream.write(data)
27+
data = wf.readframes(chunk)
28+
29+
# close stream
30+
stream.close()
31+
p.terminate()
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pydub import AudioSegment
2+
from pydub.playback import play
3+
4+
# read MP3 file
5+
song = AudioSegment.from_mp3("audio_file.mp3")
6+
# song = AudioSegment.from_wav("audio_file.wav")
7+
# you can also read from other formats such as MP4
8+
# song = AudioSegment.from_file("audio_file.mp4", "mp4")
9+
play(song)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import pyaudio
2+
import wave
3+
import argparse
4+
5+
parser = argparse.ArgumentParser(description="an Audio Recorder using Python")
6+
parser.add_argument("-o", "--output", help="Output file (with .wav)", default="recorded.wav")
7+
parser.add_argument("-d", "--duration", help="Duration to record in seconds (can be float)", default=5)
8+
9+
args = parser.parse_args()
10+
# the file name output you want to record into
11+
filename = args.output
12+
# number of seconds to record
13+
record_seconds = float(args.duration)
14+
15+
# set the chunk size of 1024 samples
16+
chunk = 1024
17+
# sample format
18+
FORMAT = pyaudio.paInt16
19+
# mono, change to 2 if you want stereo
20+
channels = 1
21+
# 44100 samples per second
22+
sample_rate = 44100
23+
24+
# initialize PyAudio object
25+
p = pyaudio.PyAudio()
26+
27+
# open stream object as input & output
28+
stream = p.open(format=FORMAT,
29+
channels=channels,
30+
rate=sample_rate,
31+
input=True,
32+
output=True,
33+
frames_per_buffer=chunk)
34+
35+
frames = []
36+
print("Recording...")
37+
for i in range(int(44100 / chunk * record_seconds)):
38+
data = stream.read(chunk)
39+
# if you want to hear your voice while recording
40+
# stream.write(data)
41+
frames.append(data)
42+
print("Finished recording.")
43+
# stop and close stream
44+
stream.stop_stream()
45+
stream.close()
46+
# terminate pyaudio object
47+
p.terminate()
48+
49+
# save audio file
50+
# open the file in 'write bytes' mode
51+
wf = wave.open(filename, "wb")
52+
# set the channels
53+
wf.setnchannels(channels)
54+
# set the sample format
55+
wf.setsampwidth(p.get_sample_size(FORMAT))
56+
# set the sample rate
57+
wf.setframerate(sample_rate)
58+
# write the frames as bytes
59+
wf.writeframes(b"".join(frames))
60+
# close the file
61+
wf.close()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
playsound
2+
pyaudio
3+
pydub
4+
ffmpeg-python

0 commit comments

Comments
 (0)