I've covered sound input several times, but this time I've been able to input and output stably with Rasipi4, so I decided to summarize it. The important thing is as shown in Reference 1. Last year, I was playing with something like Reference 2. I'm thinking of doing something a little different this year. 【reference】 1.PyAudio Input overflowed 2. [Scipy] I played with FFT, STFT and wavelet transform ♬ ~ ⑦ Real-time spectrogram; Speed up
In other words, if you write the following magic when reading, overflow will not occur and you can record endlessly.
# -*- coding:utf-8 -*-
import pyaudio
import matplotlib.pyplot as plt
import numpy as np
import wave
import struct
Save the read sound as a wav file with the following function.
def savewav(sig,sk):
RATE = 44100 #Sampling frequency
#Sine wave-Convert from 32768 to 32767 integer value(to signed 16bit pcm)
swav = [(int(32767*x)) for x in sig] #32767
#Binary
binwave = struct.pack("h" * len(swav), *swav)
#Export sine wave as wav file
w = wave.Wave_write("./wine/"+str(sk)+".wav")
params = (1, 2, RATE, len(binwave), 'NONE', 'not compressed')
w.setparams(params)
w.writeframes(binwave)
w.close()
Sound input / output is set to True. Since voice (sentence) is input, record a long time (102400/44100 = 2.3sec).
RATE=44100
p=pyaudio.PyAudio()
N=100
CHUNK=1024*N
stream=p.open(format = pyaudio.paInt16,
channels = 1,
rate = RATE,
frames_per_buffer = CHUNK,
input = True,
output = True) #Set input and output to True at the same time
And finally, continuous recording has started. Exception_on_overflow is set to False. sig is divided by 32768.
sk=0
while stream.is_active():
input = stream.read(CHUNK, exception_on_overflow = False)
print(len(input))
sig =[]
sig = np.frombuffer(input, dtype="int16") / 32768
savewav(sig,sk)
fig, (ax1,ax2) = plt.subplots(2,1,figsize=(1.6180 * 4, 4*2))
lns1=ax1.plot(sig[0:1024] ,".-",color="red")
ax1.set_xticks(np.linspace(0, 882, 3))
ax1.set_ylabel("sig0")
ax1.set_title('short plot')
lns2=ax2.plot(sig[0:CHUNK], "-",color="blue")
ax2.set_xticks(np.linspace(0, 44100*2, 5))
ax2.set_ylabel("sig1")
ax2.set_title('long plot')
ax1.grid()
ax2.grid()
plt.pause(0.5)
plt.savefig("./wine/sound_{}.png ".format(sk))
plt.close()
sk+=1
output = stream.write(input)
Ah, uh, eh. .. Continuous measurement result of x-axis memory; 44100 = 1sec
・ Continuous measurement is now possible
・ I want to link R-python with sound.
Recommended Posts