Sound cracks when recording with pyaudio

Asked 2 years ago, Updated 2 years ago, 97 views

When recording using pyaudio,
There is a crack in the sound for some reason.
This has not happened in other recording applications (such as voice recorders that were included as standard)

I suspect this code is the cause.
I'm a beginner, so I don't know why this happens.
Please let me know if you understand.

import pyaudio
import matplotlib.pyplot asplot
import wave 
import numpy as np
if__name__=='__main__':
    main()
"""
defaudiostop (audio, stream, CHUNK, FORMAT, CHANNELS, RATE, RECORD_SECONDS):
    stream.stop_stream()
    stream.close()
    audio.terminate()
defaudio_save (audio, stream, CHUNK, FORMAT, CHANNELS, RATE, RECORD_SECONDS):
    wf=wave.open("rokuon5.wav", 'wb')
    wf.setnchannels (CHANNELS)
    wf.setsampwidth(audio.get_sample_size(FORMAT))
    wf.setframerate (RATE)
    wf.writeframes(b''.join(frames))
    wf.close()




defread_plot_data (stream, frames, CHUNK, FORMAT, CHANNELS, RATE, RECORD_SECONDS):
    data=stream.read(CHUNK)
    frames.append(data)

Audiodata=np.frombuffer(data,dtype='int16')# Convert to numpy
print(type(audiodata))
#plot.plot(audiodata)
#plot.draw()
#plot.pause (0.001)
#plot.cla()

CHUNK=1024
FORMAT=pyaudio.paInt16#int16 type
CHANNELS=1#Stereo
RATE=44100#441.kHz RECORD_SECONDS=3

if name=='main':
audio=pyaudio.PyAudio()
stream=audio.open(format=FORMAT,
) #record_time=10,
rate = RATE,
channels = CHANNELS,
input_device_index=0,
input = True,
frames_per_buffer=CHUNK)
frames=[]
for_in range (int(RATE/CHUNK* RECORD_SECONDS))—
try:
read_plot_data(stream, frames, CHUNK, FORMAT, CHANNELS, RATE, RECORD_SECONDS)
except KeyboardInterrupt:
break

audiostop (audio, stream, CHUNK, FORMAT, CHANNELS, RATE, RECORD_SECONDS)
audio_save (audio, stream, CHUNK, FORMAT, CHANNELS, RATE, RECORD_SECONDS)

python audio

2022-09-30 16:10

1 Answers

Resolved It was apparently the cause of the playback side

When I played it in a different way, it came off beautifully


2022-09-30 16:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.