Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6153

Record audio on Ubuntu server through PyAudio

$
0
0

Requirements:

  1. Record audio in real-time through microphone on Ubuntu 20.04 machine
  2. Stream the audio to a system running on another machine

Current State: I am at first step currently. I am using PyAudio to record my voice through microphone on Ubuntu machine. Please see the below code snippet:

import waveimport sysimport pyaudioCHUNK = 1024FORMAT = pyaudio.paInt16CHANNELS = 1RATE = 16000RECORD_SECONDS = 5with wave.open('output.wav', 'wb') as wf:    p = pyaudio.PyAudio()    wf.setnchannels(CHANNELS)    wf.setsampwidth(p.get_sample_size(FORMAT))    wf.setframerate(RATE)    stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, input_device_index=1)    print('Recording...')    for _ in range(0, RATE // CHUNK * RECORD_SECONDS):        wf.writeframes(stream.read(CHUNK))    print('Done')    stream.close()    p.terminate()

Issue: I am getting the below errors on Ubuntu machine. The resulting output.wav file has no audio recorded in it.

ALSA lib confmisc.c:767:(parse_card) cannot find card '0'ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directoryALSA lib confmisc.c:392:(snd_func_concat) error evaluating stringsALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directoryALSA lib confmisc.c:1246:(snd_func_refer) error evaluating nameALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefaultALSA lib confmisc.c:767:(parse_card) cannot find card '0'ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directoryALSA lib confmisc.c:392:(snd_func_concat) error evaluating stringsALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directoryALSA lib confmisc.c:1246:(snd_func_refer) error evaluating nameALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM sysdefaultALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.frontALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rearALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfeALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.sideALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmiALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmiALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modemALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modemALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phonelineALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phonelineALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockCannot connect to server socket err = No such file or directoryCannot connect to server request channeljack server is not running or cannot be startedJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlockALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field portALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field portALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for cardALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for cardALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for cardALSA lib confmisc.c:767:(parse_card) cannot find card '0'ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directoryALSA lib confmisc.c:392:(snd_func_concat) error evaluating stringsALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directoryALSA lib confmisc.c:1246:(snd_func_refer) error evaluating nameALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directoryALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM dmixRecording...Done

My Ubuntu machine has following devices. I think recording could happen if I could use the default device (PulseAudio sound server).

$ arecord -Ldefault    Playback/recording through the PulseAudio sound servernull    Discard all samples (playback) or generate zero samples (capture)samplerate    Rate Converter Plugin Using Samplerate Libraryspeexrate    Rate Converter Plugin Using Speex Resamplerjack    JACK Audio Connection Kitoss    Open Sound Systempulse    PulseAudio Sound Serverupmix    Plugin for channel upmix (4,6,8)vdownmix    Plugin for channel downmix (stereo) with a simple spacialization

Currently, PyAudio sees 2 devices and uses device at index 1 as the default input device.

Python 3.9.19 (main, May  6 2024, 19:43:03) [GCC 11.2.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license" for more information.>>> import pyaudio>>> p = pyaudio.PyAudio()ALSA lib confmisc.c:767:(parse_card) cannot find card '0'ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory...<same errors as above>...ALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directoryALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM dmix>>>>>> p.get_device_count()2>>> p.get_default_input_device_info(){'index': 1, 'structVersion': 2, 'name': 'default', 'hostApi': 0, 'maxInputChannels': 32, 'maxOutputChannels': 32, 'defaultLowInputLatency': 0.008707482993197279, 'defaultLowOutputLatency': 0.008707482993197279, 'defaultHighInputLatency': 0.034829931972789115, 'defaultHighOutputLatency': 0.034829931972789115, 'defaultSampleRate': 44100.0}>>> 

Kindly advise how to fix this issue.


Viewing all articles
Browse latest Browse all 6153

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>