import pyaudio, math, struct
import numpy as np
import matplotlib.pyplot as plt
#inital config
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECOND = float(input('seconds:' ))
p = pyaudio.PyAudio()
guess = 0
integral = 0
kp = 0.5
ki = 200
kd = 9999
dt = RATE**(-1)
ddt = RATE
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=chunk)
total_error = 0
previous_e = 0
#start processing
print("recording")
for i in range(0, int(RATE / chunk * RECORD_SECOND)):
byted = (b'')
#stream.read(chunk, exception_on_overflow = False)
for element in np.fromstring(stream.read(chunk), 'Int16'):
stream.read(chunk, exception_on_overflow = False)
error = -(guess - element)
integral += error*dt
derivative = (error - previous_e) / ddt
guess += int(round(kp*error + ki*integral + kd*derivative, 0))
byted += struct.pack('<h', -guess)
previous_e = error
stream.write(byted, chunk)
#close
stream.stop_stream()
stream.close()
p.terminate()
input('press enter to exit...')
If you run the above code, it will progress to the recording part.
"IOError: [Errno -9981] An Input Overflowed" error occurs.
I have tried to change various values related to chunk and rate to see why this problem occurs, but the same thing always happens. I wonder if there is an answer to this question.