speech-scoring/record_mic_speech.py

37 lines
943 B
Python

import pyaudio
import numpy as np
# from matplotlib import pyplot as plt
from spectro_gen import plot_stft
SAMPLE_RATE = 22050
N_SEC = 1.5
CHUNKSIZE = int(SAMPLE_RATE * N_SEC) # fixed chunk size
p_inp = pyaudio.PyAudio()
stream = p_inp.open(
format=pyaudio.paFloat32,
channels=2,
rate=SAMPLE_RATE,
input=True,
frames_per_buffer=CHUNKSIZE)
data = stream.read(CHUNKSIZE)
numpydata = np.frombuffer(data, dtype=np.float32)
multi_channel = np.abs(np.reshape(numpydata, (-1, 2))).mean(axis=1)
one_channel = np.asarray([multi_channel, -1 * multi_channel]).T.reshape(-1)
mean_channel_data = one_channel.tobytes()
plot_stft(one_channel, SAMPLE_RATE)
# plt.plot(one_channel)
# plt.show()
stream.stop_stream()
stream.close()
p_inp.terminate()
p_oup = pyaudio.PyAudio()
stream = p_oup.open(
format=pyaudio.paFloat32, channels=2, rate=SAMPLE_RATE, output=True)
stream.write(mean_channel_data)
stream.close()
p_oup.terminate()