-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
65 lines (49 loc) · 1.54 KB
/
audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import sounddevice as sd
import time
import audio
import waves
# Define audio stream parameters
SAMPLE_RATE = 48000
CHANNELS = 1
FORMAT_RECORD = "int16"
RECORD_BITS = 16
# Initialise the audio streams
def start(recording_block_size):
stream_play = sd.OutputStream(
samplerate=audio.SAMPLE_RATE,
channels=audio.CHANNELS)
stream_record = sd.InputStream(
samplerate=audio.SAMPLE_RATE,
channels=audio.CHANNELS,
dtype=audio.FORMAT_RECORD,
blocksize=recording_block_size)
time.sleep(0.1)
stream_record.start()
return stream_play, stream_record
# Reads the audio as a wave
def read_wave(stream, recording_block_size):
wave = stream.read(recording_block_size)[0].flatten()
return wave
# Plays the wave as sound
def play_wave(stream, wave):
# Convert the wave to the correct format
data = wave.astype(np.float32)
# Write the data to the playback stream
play_audio(stream, data)
# Plays audio data as bytes
def play_audio(stream, data):
with stream:
stream.write(data)
# Records the microphone audio
def record_audio(stream, seconds, recording_block_size, log=True):
if log:
print("Started recording")
wave = []
# Take the specified number of recording chunks
for i in range(0, int(audio.SAMPLE_RATE / recording_block_size * seconds)):
# Write the frame to the data
wave = waves.combine_waves(wave, read_wave(stream, recording_block_size))
if log:
print("Stopped recording")
return wave