#record pc audio python

13 messages · Page 1 of 1 (latest)

idle granite
#

l cant record audio it says ```OSError: [Errno -9998] Invalid number of channels````
here its the code

import wave

FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

# find the default output device
info = p.get_host_api_info_by_index(0)
num_devices = info.get('deviceCount')
for i in range(num_devices):
    device = p.get_device_info_by_host_api_device_index(0, i)
    if 'output' in device['name'].lower():
        default_device = device['index']
        break
else:
    raise RuntimeError("No output device found.")

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                input_device_index=default_device,
                frames_per_buffer=CHUNK)

frames = []

print("Recording...")
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("Recording finished.")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()```
polar pawn
#

are you accessing the right hardware?

minor sinew
cloud urchin
#

!warn @minor sinew we do not allow DMs, please read #👮rules. If you want to help them then do it in the server

unkempt hareBOT
#
Mod Warning

@minor sinew we do not allow DMs, please read #👮rules. If you want to help them then do it in the server

idle granite
serene mountain
#

Try changing this to 1: CHANNELS = 2

#

It could be that the soundcard you have doesn't support 2 channels simultaneously

#

Or isn't configured to do that out of the box

idle granite
polar pawn
#

@idle granite