Seeking assistance to save audio as a numpy array

93 views Asked by At

I'm trying to recordaudio input stream and export that data to the .wav file. To achieve such result I am using soundevice library as well as scipy.io.wavfile module. I am having difficulty understanding how to accomplish this; if you could provide me with any advice, I would be very grateful.

def record(sample_rate: int, device_id: int):
input_latency = get_input_latency(device_id)
frame_size = 1024
audio_data = np.array(dtype ='int16')

audio_input_stream = sd.InputStream(samplerate = sample_rate, channels = 1,
    dtype = np.int16, device = device_id, latency = input_latency)

audio_input_stream.start()
while keyboardHandler.get_status() != "submit":
    np.append(audio_data, audio_input_stream.read(frames = frame_size))

Play.stop()
audio_input_stream.stop()
audio_input_stream.close()

return audio_data

it's very likely that my function above is totally wrong and I'm missing some things, however the error when trying to record is:

array() missing required argument 'object' (pos 0)

My thoughts on it are that the audio stream data itself is not correct, so therefore I can't append that data to the "audio_data" array. keyboard listener is a module that I made to control the whole recording situation using keyboard.

Any who, help would be really appreciated, Ineed this for my project.

1

There are 1 answers

0
MSS On

The error you are facing is due to line

audio_data = np.array(dtype ='int16')

When you try to initialize audio_data with an empty numpy array, you do not pass any arguments to np.array(dtype='int16'). You have two options here, pass an empty list to the function:

audio_data= np.array([], dtype='int16')

or use np.empty, which will create an empty numpy array:

audio_data= = np.empty(0, dtype='int16')

There is another way also:

def record(sample_rate: int, device_id: int):
  input_latency = get_input_latency(device_id)
  frame_size = 1024
  #audio_data = np.array(dtype ='int16')

  audio_input_stream = sd.InputStream(samplerate = sample_rate, channels = 1,
    dtype = np.int16, device = device_id, latency = input_latency)
  frames =[]
  audio_input_stream.start()
  while keyboardHandler.get_status() != "submit":
      frames.append( audio_input_stream.read(frame_size))
  audio_data = numpy.hstack(frames)

  Play.stop()
  audio_input_stream.stop()
  audio_input_stream.close()

  return audio_data