2 years ago

#54282

test-img

heavenly ruler

I am running a code in pyaudio which works normaly but when i run it on sudo(I am on linux) OSError: [Errno -9997]Invalid sample rate It does not work

When I run this code normally it works and when I run on Sudo(i need to run on Sudo) it shows this. Please help me. I need to use keyboard import so i need to run this on sudo. EDit(someone told me to paste full error this it it. I Need to write some more words or i cant post as it is saying my post mostly codes so pls ignore the edit i am just an idiot.Sorry to bother u if u are reading this Have a nice day if you read so far )

Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2721
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2842
Traceback (most recent call last):
  File "/home/bear/Documents/code/audiotest/audio.py", line 51, in <module>
    a = Recorder()
  File "/home/bear/Documents/code/audiotest/audio.py", line 32, in __init__
    self.stream = self.p.open(format=FORMAT,
  File "/usr/local/lib/python3.8/dist-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9997] Invalid sample rate

OSError: [Errno -9997] Invalid sample rate)


import pyaudio
import math
import struct
Threshold = 100
SHORT_NORMALIZE = (1.0/32768.0)
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
swidth = 2

f_name_directory = r'C/home/bear/Documents/code/audiotest/'

class Recorder:

    @staticmethod
    def rms(frame):
        count = len(frame) / swidth
        format = "%dh" % (count)
        shorts = struct.unpack(format, frame)

        sum_squares = 0.0
        for sample in shorts:
            n = sample * SHORT_NORMALIZE
            sum_squares += n * n
        rms = math.pow(sum_squares / count, 0.5)

        return rms * 1000

    def __init__(self):
        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format=FORMAT,
                                  channels=CHANNELS,
                                  rate=RATE,
                                  input=True,
                                  output=True,
                                  frames_per_buffer=chunk)

    
    

    def listen(self):
        print('Listening beginning')
        while True:
            input = self.stream.read(chunk)
            rms_val = self.rms(input)
            print(rms_val)
            if rms_val > Threshold:
                break

a = Recorder()

a.listen()

python

linux

ubuntu

sudo

pyaudio

0 Answers

Your Answer

Accepted video resources