0

Here I have provide the code which was working properly before but suddenly its stop working. But if I change the sample rate 16000 to 44100 its working. But i am not able to understand what exactly happen that stop work in 16000 sample instead of 44100 rate.

Code:

  import pyaudio
  import wave
  import os
  form_1 = pyaudio.paInt16
  chans=1
  samp_rate = 16000  #44100
  chunk = 4096
  record_secs = 5    #record time
  dev_index = 2
  wav_output_filename = 'test1.wav'


  audio = pyaudio.PyAudio()

  #setup audio input stream
  stream=audio.open(format = form_1,rate=samp_rate,channels=chans, 
  input_device_index = dev_index, input=True, frames_per_buffer=chunk)
  print("recording")
  frames=[]

  for ii in range(0,int((samp_rate/chunk)*record_secs)):
       data=stream.read(chunk,exception_on_overflow = False)
       frames.append(data)

  print("finished recording")

  stream.stop_stream()
  stream.close()
  audio.terminate()

  #creates wave file with audio read in
  #Code is from the wave file audio tutorial as referenced below
  wavefile=wave.open(wav_output_filename,'wb')
  wavefile.setnchannels(chans)
  wavefile.setsampwidth(audio.get_sample_size(form_1))
  wavefile.setframerate(samp_rate)
  wavefile.writeframes(b''.join(frames))
  wavefile.close()

  #plays the audio file
  os.system("aplay test1.wav")

Error look like:

Traceback (most recent call last): File "/home/pi/Desktop/tryy.py", line 52, 
in <module> stream=audio.open(format = form_1,rate=samp_rate,channels=chans, 
input_device_index = dev_index, input=True, frames_per_buffer=chunk) File 
"/usr/lib/python3/dist-packages/pyaudio.py", line 750, in open stream = 
Stream(self, *args, **kwargs) File "/usr/lib/python3/dist- 
packages/pyaudio.py", line 441, in init self._stream = pa.open(**arguments) 
OSError: [Errno -9997] Invalid sample rate 
  • code which was working properly ... what exactly does the code do when it js working properly? .... how is it failing now? ... what changes did you make that may have caused the failure? ... suddenly its stop working indicates a possible hardware failure – jsotola Nov 12 '19 at 20:54
  • Hi jsotala I have clearly mentioned that above code is still working if I changed the frequency rate from 16000 to 44100. Then this code able to record the voice .But before the same code record voice using the same and same code but the sample frequency rate was 16000. Because I need 16000 sample recorded voice . – BUBAI NAYAK Nov 13 '19 at 12:28
  • So if changed the sample rate 44100 to 16000 on the above code I am getting some error.And error is look like – BUBAI NAYAK Nov 13 '19 at 12:38
  • Error look like: Traceback (most recent call last): File "/home/pi/Desktop/tryy.py", line 52, in <module> stream=audio.open(format = form_1,rate=samp_rate,channels=chans, input_device_index = dev_index, input=True, frames_per_buffer=chunk) File "/usr/lib/python3/dist-packages/pyaudio.py", line 750, in open stream = Stream(self, *args, **kwargs) File "/usr/lib/python3/dist-packages/pyaudio.py", line 441, in init self._stream = pa.open(**arguments) OSError: [Errno -9997] Invalid sample rate – BUBAI NAYAK Nov 13 '19 at 12:39
  • Your comment is unreadable. Please edit your question and add additional information to it. There is a link below the question named edit. Please use it. – Ingo Nov 14 '19 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.