Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

python - I have trouble working with pyaudio . I can't record voice with this lib

a few days ago i tried to install pyaudio in pycharm by pip install pyaudio and i have a problem with visual c++ tool because i used old version of this and somebody told my to instal pipwin (pip install pipwin) and install pyaudio with pipwin (pipwin install pyaudio) . that was ok. i can play .WAV file but i can't record the audio

import pyaudio
import wave

chunk = 1024  # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16  # 16 bits per sample
channels = 2
fs = 44100  # Record at 44100 samples per second
seconds = 3
filename = "output.wav"

p = pyaudio.PyAudio()  # Create an interface to PortAudio

print('Recording')

stream = p.open(format=sample_format,
                channels=channels,
                rate=fs,
                frames_per_buffer=chunk,
                input=True )                #line 19 error

frames = []  # Initialize array to store frames

# Store data in chunks for 3 seconds
for i in range(0, int(fs / chunk * seconds)):
    data = stream.read(chunk)
    frames.append(data)

# Stop and close the stream
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()

print('Finished recording')

# Save the recorded data as a WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()

and my errors are

  File "F:/record_voice_pyaudio/RecordVoice.py", line 19, in <module>
    input=True)
  File "C:UsersArdinAppDataLocalProgramsPythonPython37-32libsite-packagespyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "C:UsersArdinAppDataLocalProgramsPythonPython37-32libsite-packagespyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels

question from:https://stackoverflow.com/questions/66055625/i-have-trouble-working-with-pyaudio-i-cant-record-voice-with-this-lib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

problem solved . i used a microphone without any speaker, actually my mic have just one channel so i change it to

channels = 1

and problem solved.

if anybody have same problem first check the microphone that be enable for your compiler and second check the microphone ability


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...