Recording somewhat functional now.

This commit is contained in:
Nicholas Stănescu 2025-11-28 09:55:55 +01:00
parent 71bca8d47f
commit e0f5eaa86d

View File

@ -602,10 +602,8 @@
"outputs": [], "outputs": [],
"execution_count": null, "execution_count": null,
"source": [ "source": [
"sounddevice_handle = sounddevice()\n", "for i, device in enumerate(sounddevice.query_devices()):\n",
"for i in range(sounddevice_handle.get_device_count()):\n", " print(i, device['name'])\n"
" device_info = sounddevice_handle.get_device_info_by_index(i)\n",
" print(i, device_info['name'])\n"
] ]
}, },
{ {
@ -623,19 +621,21 @@
"outputs": [], "outputs": [],
"execution_count": null, "execution_count": null,
"source": [ "source": [
"# Initialize sounddevice again\n", "import sounddevice as sd\n",
"sounddevice_handle = sounddevice()\n",
"\n", "\n",
"# Specify the device index and sampling frequency\n", "device_index = 15 # your input device index\n",
"device_index = 1 # Replace with the index of your microphone device\n", "Fs = 48000 # sample rate\n",
"Fs = 44100 # or 48000, depending on the field setup. Try to use 48000 when making your own recordings.\n",
"\n", "\n",
"# Open the audio stream with 5 channels, 16-bit audio format (paInt16), and the specified sample rate\n", "# Open 5channel float32 input stream on that device\n",
"stream = sounddevice_handle.open(input_device_index=device_index,\n", "stream = sd.InputStream(\n",
" device=device_index, # or device=(device_index, None) for in/out\n",
" channels=5,\n", " channels=5,\n",
" format='float32',\n", " samplerate=Fs,\n",
" rate=Fs,\n", " dtype='float32', # 16bit would be 'int16'\n",
" input=True)" " # or True, depending on how you use it\n",
")\n",
"\n",
"stream.start()\n"
] ]
}, },
{ {
@ -657,9 +657,11 @@
"outputs": [], "outputs": [],
"execution_count": null, "execution_count": null,
"source": [ "source": [
"Fs = 44100 # Sampling frequency\n", "Fs = 48000 # Sampling frequency\n",
"N = 2*Fs # 2 seconds of audio data\n", "N = 5*Fs # 2 seconds of audio data\n",
"samples,_ = stream.read(N)" "samples,_ = stream.read(N)\n",
"samples_reshaped = np.array(samples)\n",
"print(samples_reshaped.shape)"
] ]
}, },
{ {
@ -687,7 +689,6 @@
"execution_count": null, "execution_count": null,
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"\n",
"# TODO: Reshape the data into a matrix with 5 columns (one for each microphone)" "# TODO: Reshape the data into a matrix with 5 columns (one for each microphone)"
] ]
}, },
@ -707,6 +708,15 @@
"execution_count": null, "execution_count": null,
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"fig, ax = plt.subplots(5,1,figsize=(20,30))\n",
"\n",
"t = np.arange(0,5,1/Fs)\n",
"ax[0].plot(t,samples_reshaped[:,0])\n",
"ax[1].plot(t,samples_reshaped[:,1])\n",
"ax[2].plot(t,samples_reshaped[:,2])\n",
"ax[3].plot(t,samples_reshaped[:,3])\n",
"ax[4].plot(t,samples_reshaped[:,4])\n",
"fig.show()\n",
"\n", "\n",
"# TODO: Plot the data for each microphone" "# TODO: Plot the data for each microphone"
] ]