0

I would like to use the "out" parameter to write a recorded signal to a given numpy array but I don't get any audio appended to the array I am passing it. Should a samplerate be defined if I want it to use the default samplerate? There is no specific example usage in the documentation so if there any suggestions about proper usage please see the example below:

import sounddevice as sd
import numpy as np
import time

input_chs = [1,2]
rec_array = np.zeros([1, len(input_chs)])
sd.rec(out=rec_array, mapping=input_chs)
time.sleep(1)
sd.stop()
print(rec_array)  # Returns original rec_array

I also tried

rec_array = sd.rec(out=rec_array, mapping=input_chs)

but the results were the same.

1 Answers1

2

The recorded audio data is not appended to the out array. The out array is filled with the recorded data, up to its size, then the recording stops (assuming that you use sd.wait() or blocking=True).

You are passing an out array with only one row, that means that only one frame will be recorded. You should use int(desired_duration_in_seconds * samplerate) rows for your out array.

In other words, you must know beforehand how long the recording is supposed to be. Then you can use sd.wait() for the recording to be finished.

If you want to make a recording where you don't know the duration beforehand, you should have a look at the example https://github.com/spatialaudio/python-sounddevice/blob/master/examples/rec_unlimited.py.

Should a samplerate be defined if I want it to use the default samplerate?

If you want to use the default, you don't have to explicitly specify it.

Matthias
  • 4,524
  • 2
  • 31
  • 50
  • I appreciate the quick and thorough reply, that functionality makes more sense. Thank you for making this useful tool! – hanzorama Mar 06 '19 at 17:40
  • No problem, I'm glad if I could help! If an answer is helpful to you, you should consider up-voting (by clicking the up array) and/or "accepting" the answer (by clicking the check mark). This is the way of saying "thank you" on stackoverflow! – Matthias Mar 06 '19 at 21:06