Recording functional now.

This commit is contained in:
Nicholas Stănescu 2025-11-28 11:02:48 +01:00
parent e0f5eaa86d
commit 79042eeb50

View File

@ -623,19 +623,20 @@
"source": [ "source": [
"import sounddevice as sd\n", "import sounddevice as sd\n",
"\n", "\n",
"device_index = 15 # your input device index\n", "#device_index = 15 # your input device index\n",
"Fs = 48000 # sample rate\n", "#Fs = 48000 # sample rate\n",
"\n", "\n",
"# Open 5channel float32 input stream on that device\n", "# Open 5channel float32 input stream on that device\n",
"stream = sd.InputStream(\n", "#stream = sd.InputStream(\n",
" device=device_index, # or device=(device_index, None) for in/out\n", "# device=device_index, # or device=(device_index, None) for in/out\n",
" channels=5,\n", "# channels=5,\n",
" samplerate=Fs,\n", "# samplerate=Fs,\n",
" dtype='float32', # 16bit would be 'int16'\n", "# dtype='float32', # 16bit would be 'int16'\n",
" # or True, depending on how you use it\n", " # or True, depending on how you use it\n",
")\n", "#)\n",
"\n", "\n",
"stream.start()\n" "#stream.start()\n",
"#stream.stop()"
] ]
}, },
{ {
@ -657,11 +658,16 @@
"outputs": [], "outputs": [],
"execution_count": null, "execution_count": null,
"source": [ "source": [
"Fs = 48000 # Sampling frequency\n", "Fs = 48000\n",
"N = 5*Fs # 2 seconds of audio data\n", "duration = 30\n",
"samples,_ = stream.read(N)\n", "N = int(Fs * duration)\n",
"samples_reshaped = np.array(samples)\n", "\n",
"print(samples_reshaped.shape)" "sd.default.device = 15 # or a substring of its name\n",
"sd.default.samplerate = Fs\n",
"\n",
"samples = sd.rec(N, samplerate=Fs, channels=5)\n",
"sd.wait()\n",
"print(samples.shape)"
] ]
}, },
{ {
@ -708,16 +714,22 @@
"execution_count": null, "execution_count": null,
"source": [ "source": [
"### Student Version ###\n", "### Student Version ###\n",
"samples_reshaped = np.array(samples)\n",
"fig, ax = plt.subplots(5,1,figsize=(20,30))\n", "fig, ax = plt.subplots(5,1,figsize=(20,30))\n",
"\n", "\n",
"t = np.arange(0,5,1/Fs)\n", "t = np.arange(0,N/Fs,1/Fs)\n",
"ax[0].plot(t,samples_reshaped[:,0])\n", "ax[0].plot(t,samples_reshaped[:,0])\n",
"ax[1].plot(t,samples_reshaped[:,1])\n", "ax[1].plot(t,samples_reshaped[:,1])\n",
"ax[2].plot(t,samples_reshaped[:,2])\n", "ax[2].plot(t,samples_reshaped[:,2])\n",
"ax[3].plot(t,samples_reshaped[:,3])\n", "ax[3].plot(t,samples_reshaped[:,3])\n",
"ax[4].plot(t,samples_reshaped[:,4])\n", "ax[4].plot(t,samples_reshaped[:,4])\n",
"for ax_in in ax:\n",
" ax_in.set_xlabel('Time (s)')\n",
" ax_in.set_ylabel('Amplitude')\n",
"fig.show()\n", "fig.show()\n",
"\n", "\n",
"from scipy.io import wavfile\n",
"wavfile.write(\"./audio_beacon_67676767_10s_driving.wav\", Fs, samples_reshaped.astype(np.float32))\n",
"# TODO: Plot the data for each microphone" "# TODO: Plot the data for each microphone"
] ]
}, },