|
@@ -126,12 +126,90 @@
|
|
|
"execution_count": null,
|
|
|
"metadata": {},
|
|
|
"outputs": [],
|
|
|
- "source": []
|
|
|
+ "source": [
|
|
|
+ "def test():\n",
|
|
|
+ " import sounddevice as sd\n",
|
|
|
+ " tone1 = get_pure_tone(440, 0.05, 44100) * 0.5\n",
|
|
|
+ "\n",
|
|
|
+ " sd.default.samplerate = 44100\n",
|
|
|
+ " stream = sd.OutputStream(samplerate=44100, channels=2, dtype='float32')\n",
|
|
|
+ " stream.start()\n",
|
|
|
+ "\n",
|
|
|
+ " for i in range(5):\n",
|
|
|
+ " stream.write(np.column_stack((tone1, tone1)))\n",
|
|
|
+ " time.sleep(0.2)\n",
|
|
|
+ " stream.stop()"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "selector = Value('f', 0)\n",
|
|
|
+ "is_running = Value(c_bool, True)\n",
|
|
|
+ "#sc = ProcessWithLogAndControls(target=sound_controller, args=(selector, is_running), name=\"sounds\")\n",
|
|
|
+ "sc = ProcessWithLogAndControls(target=test, args=(), name=\"sounds\")\n",
|
|
|
+ "sc.control_panel()"
|
|
|
+ ]
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "cell_type": "code",
|
|
|
+ "execution_count": null,
|
|
|
+ "metadata": {},
|
|
|
+ "outputs": [],
|
|
|
+ "source": [
|
|
|
+ "def get_pure_tone(freq, duration, sample_rate=44100):\n",
|
|
|
+ " x = np.linspace(0, duration * freq * 2*np.pi, int(duration*sample_rate), dtype=np.float32)\n",
|
|
|
+ " return np.sin(x)\n",
|
|
|
+ "\n",
|
|
|
+ "def sound_controller(selector, is_running, latency=0.25):\n",
|
|
|
+ " \"\"\"\n",
|
|
|
+ " selector mp.Value object to set the sound to be played\n",
|
|
|
+ " is_running mp.Value object to stop the loop\n",
|
|
|
+ " \"\"\"\n",
|
|
|
+ " print('Loading SC..')\n",
|
|
|
+ " \n",
|
|
|
+ " tone1 = get_pure_tone(440, 0.05, 44100) * 0.5\n",
|
|
|
+ " tone2 = get_pure_tone(880, 0.05, 44100) * 0.5\n",
|
|
|
+ " silence = np.zeros(len(tone1), dtype='float32') \n",
|
|
|
+ "\n",
|
|
|
+ " sounds = {\n",
|
|
|
+ " 0: np.column_stack((silence, silence)),\n",
|
|
|
+ " 1: np.column_stack((tone1, tone1)),\n",
|
|
|
+ " 2: np.column_stack((tone2, tone2))\n",
|
|
|
+ " }\n",
|
|
|
+ "\n",
|
|
|
+ " stream = sd.OutputStream(samplerate=44100, channels=2, dtype='float32')\n",
|
|
|
+ " stream.start()\n",
|
|
|
+ " \n",
|
|
|
+ " print('Creating sounds..')\n",
|
|
|
+ " \n",
|
|
|
+ " next_beat = time.time() + latency\n",
|
|
|
+ " beat_times = collections.deque(maxlen=10000)\n",
|
|
|
+ "\n",
|
|
|
+ " \n",
|
|
|
+ " while is_running.value:\n",
|
|
|
+ " print('Running loop..')\n",
|
|
|
+ " \n",
|
|
|
+ " t0 = time.time()\n",
|
|
|
+ " if t0 < next_beat:\n",
|
|
|
+ " continue\n",
|
|
|
+ "\n",
|
|
|
+ " beat_times.append(t0)\n",
|
|
|
+ " #sd.play(self.tone) # has underrun errors, maybe try blocking?\n",
|
|
|
+ " stream.write(sounds[int(selector.value)])\n",
|
|
|
+ " next_beat += latency\n",
|
|
|
+ " \n",
|
|
|
+ " stream.stop()\n",
|
|
|
+ " print('Sound controller stopped')"
|
|
|
+ ]
|
|
|
}
|
|
|
],
|
|
|
"metadata": {
|
|
|
"kernelspec": {
|
|
|
- "display_name": "Python 3",
|
|
|
+ "display_name": "Python 3 (ipykernel)",
|
|
|
"language": "python",
|
|
|
"name": "python3"
|
|
|
},
|
|
@@ -145,7 +223,7 @@
|
|
|
"name": "python",
|
|
|
"nbconvert_exporter": "python",
|
|
|
"pygments_lexer": "ipython3",
|
|
|
- "version": "3.8.10"
|
|
|
+ "version": "3.8.5"
|
|
|
}
|
|
|
},
|
|
|
"nbformat": 4,
|