asobolev 7 月之前
父節點
當前提交
93f1a21a0f
共有 2 個文件被更改,包括 32 次插入6 次删除
  1. 29 6
      controllers/sound.ipynb
  2. 3 0
      controllers/sound.py

+ 29 - 6
controllers/sound.ipynb

@@ -286,6 +286,7 @@
     "                time.sleep(0.005)\n",
     "                \n",
     "        stream.stop()\n",
+    "        stream.close()\n",
     "        print('Sound stopped')\n",
     "\n",
     "        \n",
@@ -328,6 +329,7 @@
     "            self.stream.write(self.data[start_idx:end_idx])\n",
     "            \n",
     "        self.stream.stop()\n",
+    "        self.stream.close()\n",
     "        \n",
     "        \n",
     "class SoundControllerPR:\n",
@@ -426,7 +428,8 @@
     "    def stop(self):\n",
     "        for t in self.timers:\n",
     "            t.cancel()\n",
-    "        self.stream.stop()"
+    "        self.stream.stop()\n",
+    "        self.stream.close()"
    ]
   },
   {
@@ -476,25 +479,45 @@
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 89,
    "metadata": {},
    "outputs": [],
    "source": [
-    "sc = SoundControllerPR(1, SoundControllerPR.default_cfg)"
+    "sound_cfg = {\n",
+    "    \"device\": [1, 24],\n",
+    "    \"n_channels\": 14,\n",
+    "    \"sounds\": {\n",
+    "        \"noise\": {\"amp\": 0.01, \"duration\": 10.0, \"channels\": [6]},  # 69 DBA (amp 0.01, volume 0.1)\n",
+    "        \"target\": {\"freq\": 660, \"amp\": 0.3, \"duration\": 0.015, \"window\": 0.005, \"iti\": 0.1, \"number\": 10, \"channels\": [5]}\n",
+    "    },\n",
+    "    \"sample_rate\": 44100,\n",
+    "    \"volume\": 0.1,\n",
+    "    \"file_path\": \"sounds.csv\"\n",
+    "}"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 87,
    "metadata": {},
    "outputs": [],
    "source": [
-    "sc.play_non_blocking('target', 0)"
+    "sc = SoundControllerPR(1, sound_cfg)"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 88,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "sc.noise()  # 5 is 3\n",
+    "#sc.play_non_blocking('noise', 0)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 85,
    "metadata": {},
    "outputs": [],
    "source": [

+ 3 - 0
controllers/sound.py

@@ -194,6 +194,7 @@ class SoundController:
                 time.sleep(0.005)
                 
         stream.stop()
+        stream.close()
         print('Sound stopped')
 
         
@@ -236,6 +237,7 @@ class ContinuousSoundStream:
             self.stream.write(self.data[start_idx:end_idx])
             
         self.stream.stop()
+        self.stream.close()
         
         
 class SoundControllerPR:
@@ -335,3 +337,4 @@ class SoundControllerPR:
         for t in self.timers:
             t.cancel()
         self.stream.stop()
+        self.stream.close()