From 59085b091f2730c0b9c429ad3ff37d547323bb5b Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F" Date: Wed, 28 Sep 2022 09:41:02 +0200 Subject: [PATCH] Some bugfixes and useful function additions --- examples/Test SeriesBiquad.ipynb | 114 ---- examples/{Test SLM.ipynb => test_SLM.ipynb} | 0 examples/test_SeriesBiquad.ipynb | 82 +++ .../{Test input.ipynb => test_input.ipynb} | 64 +-- examples/test_output.ipynb | 138 +++++ examples/test_record.ipynb | 506 ++++++++++++++++++ src/lasp/device/lasp_daqconfig.cpp | 7 + src/lasp/device/lasp_daqconfig.h | 8 + src/lasp/device/lasp_rtaudiodaq.cpp | 2 +- src/lasp/device/lasp_streammgr.cpp | 2 + src/lasp/lasp_atomic.py | 2 +- src/lasp/lasp_record.py | 34 +- src/lasp/pybind11/lasp_daq.cpp | 2 +- src/lasp/pybind11/lasp_daqconfig.cpp | 1 + src/lasp/pybind11/lasp_pyindatahandler.cpp | 37 +- src/lasp/pybind11/lasp_streammgr.cpp | 1 + 16 files changed, 805 insertions(+), 195 deletions(-) delete mode 100644 examples/Test SeriesBiquad.ipynb rename examples/{Test SLM.ipynb => test_SLM.ipynb} (100%) create mode 100644 examples/test_SeriesBiquad.ipynb rename examples/{Test input.ipynb => test_input.ipynb} (82%) create mode 100644 examples/test_output.ipynb create mode 100644 examples/test_record.ipynb diff --git a/examples/Test SeriesBiquad.ipynb b/examples/Test SeriesBiquad.ipynb deleted file mode 100644 index c881fa3..0000000 --- a/examples/Test SeriesBiquad.ipynb +++ /dev/null @@ -1,114 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "from lasp import SeriesBiquad\n", - "import numpy as np" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2022-09-03 17:21:46+0200 DebugTrace-cpp 2.0.0a2 (g++ 12.2.0)\n", - "2022-09-03 17:21:46+0200 \n", - "2022-09-03 17:21:46+0200 Enter SeriesBiquad (lasp_biquadbank.cpp: 12)\n", - "2022-09-03 17:21:46+0200 Leave SeriesBiquad (lasp_biquadbank.cpp)\n" - ] - } - ], - "source": [ - "coefs = np.array([1.,0,0,1.,-.9,0])\n", - "bq = SeriesBiquad(coefs)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2022-09-03 17:21:47+0200 \n", - "2022-09-03 17:21:47+0200 Enter filter (lasp_biquadbank.cpp: 44)\n", - "2022-09-03 17:21:47+0200 Leave filter (lasp_biquadbank.cpp)\n" - ] - } - ], - "source": [ - "x = np.zeros(10, dtype=float)\n", - "x[0] = 1\n", - "\n", - "x2 = bq.filter(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[1. ],\n", - " [0.9 ],\n", - " [0.81 ],\n", - " [0.729 ],\n", - " [0.6561 ],\n", - " [0.59049 ],\n", - " [0.531441 ],\n", - " [0.4782969 ],\n", - " [0.43046721],\n", - " [0.38742049]])" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x2" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/examples/Test SLM.ipynb b/examples/test_SLM.ipynb similarity index 100% rename from examples/Test SLM.ipynb rename to examples/test_SLM.ipynb diff --git a/examples/test_SeriesBiquad.ipynb b/examples/test_SeriesBiquad.ipynb new file mode 100644 index 0000000..77b49e5 --- /dev/null +++ b/examples/test_SeriesBiquad.ipynb @@ -0,0 +1,82 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from lasp import SeriesBiquad\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "coefs = np.array([1.,0,0,1.,-.9,0])\n", + "bq = SeriesBiquad(coefs)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x = np.zeros(10, dtype=float)\n", + "x[0] = 1\n", + "\n", + "x2 = bq.filter(x)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "x2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "type(x2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/examples/Test input.ipynb b/examples/test_input.ipynb similarity index 82% rename from examples/Test input.ipynb rename to examples/test_input.ipynb index 2ceec77..78895bd 100644 --- a/examples/Test input.ipynb +++ b/examples/test_input.ipynb @@ -2,6 +2,7 @@ "cells": [ { "cell_type": "markdown", + "id": "740b4091", "metadata": {}, "source": [ "# Test DAQ input on a device" @@ -10,6 +11,7 @@ { "cell_type": "code", "execution_count": null, + "id": "ac06df04", "metadata": {}, "outputs": [], "source": [ @@ -19,6 +21,7 @@ { "cell_type": "code", "execution_count": null, + "id": "ce0dd691", "metadata": {}, "outputs": [], "source": [ @@ -30,6 +33,7 @@ { "cell_type": "code", "execution_count": null, + "id": "3cd242a8", "metadata": {}, "outputs": [], "source": [ @@ -39,18 +43,20 @@ { "cell_type": "code", "execution_count": null, + "id": "a5d878e9", "metadata": {}, "outputs": [], "source": [ "# Search for a device\n", "for i, d in enumerate(ds):\n", " print(f'{i}: ' + d.device_name)\n", - "d = ds[1]" + "d = ds[0]" ] }, { "cell_type": "code", "execution_count": null, + "id": "a456054e", "metadata": {}, "outputs": [], "source": [ @@ -61,6 +67,7 @@ { "cell_type": "code", "execution_count": null, + "id": "6d5b281e", "metadata": {}, "outputs": [], "source": [ @@ -75,6 +82,7 @@ { "cell_type": "code", "execution_count": null, + "id": "1ead3995", "metadata": {}, "outputs": [], "source": [ @@ -85,16 +93,27 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "id": "12db8306", + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "# Start a stream with a configuration\n", - "mgr.startStream(config)" + "mgr.startStream(config)\n", + "\n", + "def cb(data):\n", + " # raise RuntimeError('hh')\n", + " print(data.shape)\n", + "i = lasp.InDataHandler(mgr, cb)\n", + "import time\n", + "#time.sleep(4)" ] }, { "cell_type": "code", "execution_count": null, + "id": "4d9f0103", "metadata": {}, "outputs": [], "source": [ @@ -104,37 +123,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "mgr.stopAllStreams()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "def cb(data):\n", - " # raise RuntimeError('hh')\n", - " print(data.shape)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "i = lasp.InDataHandler(mgr, cb)\n", - "import time\n", - "#time.sleep(4)" - ] - }, - { - "cell_type": "code", - "execution_count": null, + "id": "61b7282f", "metadata": { "scrolled": true }, @@ -142,13 +131,6 @@ "source": [ "del i" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/examples/test_output.ipynb b/examples/test_output.ipynb new file mode 100644 index 0000000..738de7e --- /dev/null +++ b/examples/test_output.ipynb @@ -0,0 +1,138 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "740b4091", + "metadata": {}, + "source": [ + "# Test DAQ output on a device" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac06df04", + "metadata": {}, + "outputs": [], + "source": [ + "# !make -j -C ~/wip/mycode/lasp" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce0dd691", + "metadata": {}, + "outputs": [], + "source": [ + "import lasp\n", + "# Get handle to stream manager\n", + "mgr = lasp.StreamMgr.getInstance()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3cd242a8", + "metadata": {}, + "outputs": [], + "source": [ + "ds = mgr.getDeviceInfo()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5d878e9", + "metadata": {}, + "outputs": [], + "source": [ + "# Search for a device\n", + "for i, d in enumerate(ds):\n", + " print(f'{i}: ' + d.device_name)\n", + "d = ds[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6d5b281e", + "metadata": {}, + "outputs": [], + "source": [ + "# Create a configuration and enable some input channels\n", + "config = lasp.DaqConfiguration(d)\n", + "config.outchannel_config[0].enabled = True\n", + "config.outchannel_config[1].enabled = True\n", + "config.setAllInputEnabled(False)\n", + "# Choose a different number of frames per block\n", + "config.framesPerBlockIndex = 4" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ead3995", + "metadata": {}, + "outputs": [], + "source": [ + "print('Out channels:',d.noutchannels)\n", + "print('In channels:',d.ninchannels)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12db8306", + "metadata": {}, + "outputs": [], + "source": [ + "# Start a stream with a configuration\n", + "mgr.startStream(config)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "da5e8b81-bcd6-4587-8f59-74811a263eee", + "metadata": {}, + "outputs": [], + "source": [ + "siggen = lasp.Sine(1000)\n", + "siggen.setLevel(-20)\n", + "mgr.setSiggen(siggen)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d9f0103", + "metadata": {}, + "outputs": [], + "source": [ + "mgr.stopStream(lasp.StreamMgr.StreamType.output)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/test_record.ipynb b/examples/test_record.ipynb new file mode 100644 index 0000000..ad545a5 --- /dev/null +++ b/examples/test_record.ipynb @@ -0,0 +1,506 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "2406f830", + "metadata": {}, + "source": [ + "# Test DAQ input on a device" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "e3d3280f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "make: Entering directory '/home/anne/wip/mycode/lasp'\n", + "make[1]: Entering directory '/home/anne/wip/mycode/lasp'\n", + "make[2]: Entering directory '/home/anne/wip/mycode/lasp'\n", + "make[2]: Leaving directory '/home/anne/wip/mycode/lasp'\n", + "[ 42%] Built target lasp_dsp_lib\n", + "make[2]: Entering directory '/home/anne/wip/mycode/lasp'\n", + "make[2]: Leaving directory '/home/anne/wip/mycode/lasp'\n", + "[ 67%] Built target lasp_device_lib\n", + "make[2]: Entering directory '/home/anne/wip/mycode/lasp'\n", + "make[2]: Leaving directory '/home/anne/wip/mycode/lasp'\n", + "[100%] Built target lasp_cpp\n", + "make[1]: Leaving directory '/home/anne/wip/mycode/lasp'\n", + "make: Leaving directory '/home/anne/wip/mycode/lasp'\n" + ] + } + ], + "source": [ + " !make -j -C ~/wip/mycode/lasp" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d12f103c", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:01+0200 DebugTrace-cpp 2.0.0a2 (g++ 12.2.0)\n", + "2022-09-27 22:08:01+0200 \n", + "2022-09-27 22:08:01+0200 Enter getInstance (lasp_streammgr.cpp: 40)\n", + "2022-09-27 22:08:01+0200 | Enter StreamMgr (lasp_streammgr.cpp: 45)\n", + "2022-09-27 22:08:01+0200 | | Enter rescanDAQDevices (lasp_streammgr.cpp: 60)\n", + "2022-09-27 22:08:01+0200 | | | Enter rescanDAQDevices_impl (lasp_streammgr.cpp: 80)\n", + "2022-09-27 22:08:01+0200 | | | | Enter fillRtAudioDeviceInfo (lasp_rtaudiodaq.cpp: 20)\n", + "\n", + "RtApiAlsa::getDeviceInfo: snd_pcm_open error for device (hw:1,0), Device or resource busy.\n", + "\n", + "2022-09-27 22:08:01+0200 | | | | Leave fillRtAudioDeviceInfo (lasp_rtaudiodaq.cpp)\n", + "2022-09-27 22:08:01+0200 | | | Leave rescanDAQDevices_impl (lasp_streammgr.cpp)\n", + "2022-09-27 22:08:01+0200 | | Leave rescanDAQDevices (lasp_streammgr.cpp)\n", + "2022-09-27 22:08:01+0200 | Leave StreamMgr (lasp_streammgr.cpp)\n", + "2022-09-27 22:08:01+0200 Leave getInstance (lasp_streammgr.cpp)\n" + ] + } + ], + "source": [ + "import lasp\n", + "# Get handle to stream manager\n", + "mgr = lasp.StreamMgr.getInstance()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f44b1946", + "metadata": {}, + "outputs": [], + "source": [ + "ds = mgr.getDeviceInfo()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "db0f3bb7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0: Monitor of Starship/Matisse HD Audio Controller Analog Stereo\n", + "1: Starship/Matisse HD Audio Controller Analog Stereo\n", + "2: GP108 High Definition Audio Controller Digital Stereo (HDMI)\n", + "3: Monitor of GP108 High Definition Audio Controller Digital Stereo (HDMI)\n", + "4: default\n", + "5: hw:HDA NVidia,3\n", + "6: hw:HDA NVidia,7\n", + "7: hw:HDA NVidia,8\n", + "8: hw:HDA NVidia,9\n", + "9: hw:HDA NVidia,10\n", + "10: hw:HD-Audio Generic,0\n", + "11: hw:HD-Audio Generic,1\n", + "12: hw:HD-Audio Generic,2\n" + ] + } + ], + "source": [ + "# Search for a device\n", + "for i, d in enumerate(ds):\n", + " print(f'{i}: ' + d.device_name)\n", + "d = ds[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "f97c13d5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[512, 1024, 2048, 4096, 8192]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Check the available block sizes\n", + "d.availableFramesPerBlock" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5c84d454", + "metadata": {}, + "outputs": [], + "source": [ + "# Create a configuration and enable some input channels\n", + "config = lasp.DaqConfiguration(d)\n", + "config.inchannel_config[0].enabled = True\n", + "config.inchannel_config[1].enabled = True\n", + "# Choose a different number of frames per block\n", + "config.framesPerBlockIndex = 4" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "a020f86f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Out channels: 0\n", + "In channels: 2\n" + ] + } + ], + "source": [ + "print('Out channels:',d.noutchannels)\n", + "print('In channels:',d.ninchannels)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "034b5f5e", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:02+0200 \n", + "2022-09-27 22:08:02+0200 Enter startStream (lasp_streammgr.cpp: 209)\n", + "2022-09-27 22:08:02+0200 | Enter createDaq (lasp_daq.cpp: 18)\n", + "2022-09-27 22:08:02+0200 | | Enter Daq (lasp_daq.cpp: 37)\n", + "2022-09-27 22:08:02+0200 | | Leave Daq (lasp_daq.cpp)\n", + "2022-09-27 22:08:02+0200 | | \n", + "2022-09-27 22:08:02+0200 | | Enter RtAudioDaq (lasp_rtaudiodaq.cpp: 135)\n", + "2022-09-27 22:08:02+0200 | | | Enter samplerate (lasp_daq.cpp: 58)\n", + "2022-09-27 22:08:02+0200 | | | Leave samplerate (lasp_daq.cpp)\n", + "2022-09-27 22:08:02+0200 | | Leave RtAudioDaq (lasp_rtaudiodaq.cpp)\n", + "2022-09-27 22:08:02+0200 | Leave createDaq (lasp_daq.cpp)\n", + "2022-09-27 22:08:02+0200 | isInput = true\n", + "2022-09-27 22:08:02+0200 | isOutput = false\n", + "2022-09-27 22:08:02+0200 | \n", + "2022-09-27 22:08:02+0200 | Enter start (lasp_rtaudiodaq.cpp: 215)\n", + "2022-09-27 22:08:02+0200 | Leave start (lasp_rtaudiodaq.cpp)\n", + "2022-09-27 22:08:02+0200 Leave startStream (lasp_streammgr.cpp)\n" + ] + } + ], + "source": [ + "# Start a stream with a configuration\n", + "mgr.startStream(config)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "59326eda-f249-4a9b-9191-a784b01694b9", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:02+0200 \n", + "2022-09-27 22:08:02+0200 Enter InDataHandler (lasp_streammgr.cpp: 14)\n", + "2022-09-27 22:08:02+0200 Leave InDataHandler (lasp_streammgr.cpp)\n", + "2022-09-27 22:08:02+0200 \n", + "2022-09-27 22:08:02+0200 Enter ThreadedInDataHandler (lasp_threadedindatahandler.cpp: 13)\n", + "2022-09-27 22:08:02+0200 Leave ThreadedInDataHandler (lasp_threadedindatahandler.cpp)\n", + "2022-09-27 22:08:02+0200 \n", + "2022-09-27 22:08:02+0200 Enter PyIndataHandler (lasp_pyindatahandler.cpp: 78)\n", + "2022-09-27 22:08:02+0200 | Enter start (lasp_streammgr.cpp: 16)\n", + "2022-09-27 22:08:02+0200 | Leave start (lasp_streammgr.cpp)\n", + "2022-09-27 22:08:02+0200 Leave PyIndataHandler (lasp_pyindatahandler.cpp)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "." + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:02+0200 \n", + "2022-09-27 22:08:02+0200 Enter samplerate (lasp_daq.cpp: 58)\n", + "2022-09-27 22:08:02+0200 Leave samplerate (lasp_daq.cpp)\n", + "2022-09-27 22:08:02+0200 \n", + "2022-09-27 22:08:02+0200 Enter samplerate (lasp_daq.cpp: 58)\n", + "2022-09-27 22:08:02+0200 Leave samplerate (lasp_daq.cpp)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + ".....1....2.....3....4....5.....6....7.....8....9....10" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter ~PyIndataHandler (lasp_pyindatahandler.cpp: 87)\n", + "2022-09-27 22:08:12+0200 Leave ~PyIndataHandler (lasp_pyindatahandler.cpp)\n", + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter ~ThreadedInDataHandler (lasp_threadedindatahandler.cpp: 38)\n", + "2022-09-27 22:08:12+0200 Leave ~ThreadedInDataHandler (lasp_threadedindatahandler.cpp)\n", + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter ~InDataHandler (lasp_streammgr.cpp: 27)\n", + "2022-09-27 22:08:12+0200 Leave ~InDataHandler (lasp_streammgr.cpp)\n" + ] + } + ], + "source": [ + "rec = lasp.Recording('test', mgr, 10.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "5defa804", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter stopStream (lasp_streammgr.cpp: 287)\n", + "2022-09-27 22:08:12+0200 | Enter ~Daq (lasp_daq.cpp: 14)\n", + "2022-09-27 22:08:12+0200 | Leave ~Daq (lasp_daq.cpp)\n", + "2022-09-27 22:08:12+0200 Leave stopStream (lasp_streammgr.cpp)\n" + ] + } + ], + "source": [ + "mgr.stopStream(lasp.StreamMgr.StreamType.input)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "d12f8c4a", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter stopAllStreams (lasp_streammgr.cpp: 202)\n", + "2022-09-27 22:08:12+0200 Leave stopAllStreams (lasp_streammgr.cpp)\n" + ] + } + ], + "source": [ + "mgr.stopAllStreams()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "83b1713b", + "metadata": {}, + "outputs": [], + "source": [ + "def cb(data):\n", + " # raise RuntimeError('hh')\n", + " print(data.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "ff7c1e03", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter InDataHandler (lasp_streammgr.cpp: 14)\n", + "2022-09-27 22:08:12+0200 Leave InDataHandler (lasp_streammgr.cpp)\n", + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter ThreadedInDataHandler (lasp_threadedindatahandler.cpp: 13)\n", + "2022-09-27 22:08:12+0200 Leave ThreadedInDataHandler (lasp_threadedindatahandler.cpp)\n", + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter PyIndataHandler (lasp_pyindatahandler.cpp: 78)\n", + "2022-09-27 22:08:12+0200 | Enter start (lasp_streammgr.cpp: 16)\n", + "2022-09-27 22:08:12+0200 | Leave start (lasp_streammgr.cpp)\n", + "2022-09-27 22:08:12+0200 Leave PyIndataHandler (lasp_pyindatahandler.cpp)\n" + ] + } + ], + "source": [ + "i = lasp.InDataHandler(mgr, cb)\n", + "import time\n", + "#time.sleep(4)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "9ba54238", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter ~PyIndataHandler (lasp_pyindatahandler.cpp: 87)\n", + "2022-09-27 22:08:12+0200 Leave ~PyIndataHandler (lasp_pyindatahandler.cpp)\n", + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter ~ThreadedInDataHandler (lasp_threadedindatahandler.cpp: 38)\n", + "2022-09-27 22:08:12+0200 Leave ~ThreadedInDataHandler (lasp_threadedindatahandler.cpp)\n", + "2022-09-27 22:08:12+0200 \n", + "2022-09-27 22:08:12+0200 Enter ~InDataHandler (lasp_streammgr.cpp: 27)\n", + "2022-09-27 22:08:12+0200 Leave ~InDataHandler (lasp_streammgr.cpp)\n" + ] + } + ], + "source": [ + "del i" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "0f0edc36", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import numpy as np\n", + "a = np.array([1,2,3])\n", + "type(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "ee546114-a879-4ebe-a6eb-e2354b2247fd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dtype('int64')" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a.dtype" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "5d033d6d-3052-42ce-9441-faa917ece6a9", + "metadata": {}, + "outputs": [], + "source": [ + "from lasp import DaqChannel" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "bb99ec99-b921-4fb2-bd79-87db8dfcdb03", + "metadata": {}, + "outputs": [], + "source": [ + "d = DaqChannel()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "ad67d273-45aa-4358-9405-de5924f087a2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d.qty.value" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "53ab5c8f-f904-4f9f-b591-6a5dd1581ddd", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/src/lasp/device/lasp_daqconfig.cpp b/src/lasp/device/lasp_daqconfig.cpp index bdae42f..7df5c97 100644 --- a/src/lasp/device/lasp_daqconfig.cpp +++ b/src/lasp/device/lasp_daqconfig.cpp @@ -86,6 +86,13 @@ int DaqConfiguration::getLowestOutChannel() const { } return -1; } +vector DaqConfiguration::enabledInChannels() const { + vector res; + for(auto& ch: inchannel_config) { + if(ch.enabled) { res.push_back(ch);} + } + return res; +} #include "toml++/toml.h" #include diff --git a/src/lasp/device/lasp_daqconfig.h b/src/lasp/device/lasp_daqconfig.h index 3ceb42a..7411ed8 100644 --- a/src/lasp/device/lasp_daqconfig.h +++ b/src/lasp/device/lasp_daqconfig.h @@ -234,6 +234,14 @@ public: */ std::vector inchannel_config; + + /** + * @brief Return list of enabled input channels + * + * @return That. + */ + std::vector enabledInChannels() const; + /** * @brief Channel configuration for output channels */ diff --git a/src/lasp/device/lasp_rtaudiodaq.cpp b/src/lasp/device/lasp_rtaudiodaq.cpp index 55acf8e..5a56f81 100644 --- a/src/lasp/device/lasp_rtaudiodaq.cpp +++ b/src/lasp/device/lasp_rtaudiodaq.cpp @@ -267,7 +267,7 @@ public: unsigned int nFrames, double streamTime, RtAudioStreamStatus status) { - DEBUGTRACE_ENTER; + /* DEBUGTRACE_ENTER; */ using se = StreamStatus::StreamError; diff --git a/src/lasp/device/lasp_streammgr.cpp b/src/lasp/device/lasp_streammgr.cpp index 42c2eec..b42eee5 100644 --- a/src/lasp/device/lasp_streammgr.cpp +++ b/src/lasp/device/lasp_streammgr.cpp @@ -91,9 +91,11 @@ bool StreamMgr::inCallback(const DaqData &data) { std::scoped_lock lck(_inDataHandler_mtx); for (auto &handler : _inDataHandlers) { + bool res = handler->inCallback(data); if (!res) return false; + } return true; } diff --git a/src/lasp/lasp_atomic.py b/src/lasp/lasp_atomic.py index d9f35b9..6897683 100644 --- a/src/lasp/lasp_atomic.py +++ b/src/lasp/lasp_atomic.py @@ -32,7 +32,7 @@ class Atomic: self._lock = Lock() def checkType(self, val): - if not (type(val) == bool | type(val) == int): + if not (type(val) == bool or type(val) == int): raise RuntimeError("Invalid type for Atomic") def __iadd__(self, toadd): diff --git a/src/lasp/lasp_record.py b/src/lasp/lasp_record.py index db4abbe..0383a18 100644 --- a/src/lasp/lasp_record.py +++ b/src/lasp/lasp_record.py @@ -8,6 +8,7 @@ import logging import os import time import h5py +import numpy as np from .lasp_cpp import InDataHandler, StreamMgr from .lasp_atomic import Atomic @@ -108,11 +109,14 @@ class Recording: and make ready for storing data. """ daq = self.smgr.getDaq(StreamMgr.StreamType.input) - dtype = daq.dtype() - in_ch = daq.inchannel_config + in_ch = daq.enabledInChannels() blocksize = daq.framesPerBlock() self.blocksize = blocksize + # The array data type cannot + # datatype = daq.dataType() + dtype = np.dtype(data.dtype) + f = self.f # Set the bunch of attributes @@ -129,7 +133,7 @@ class Recording: nchannels = len(in_ch) self.ad = f.create_dataset('audio', (1, blocksize, nchannels), - dtype=data.dtype, + dtype=dtype, maxshape=( None, # This means, we can add blocks # indefinitely @@ -144,7 +148,7 @@ class Recording: # In V2, we do not store JSON metadata anymore, but just an enumeration # index to a physical quantity. - f.attrs['qtys_idx'] = [ch.qty for ch in in_ch] + f.attrs['qtys_enum_idx'] = [ch.qty.value for ch in in_ch] def inCallback(self, adata): """ @@ -161,7 +165,7 @@ class Recording: self.firstFrames(adata) self.__addTimeData(adata) - + return True def setDelete(self, val: bool): """ @@ -177,17 +181,12 @@ class Recording: remove the queue from the stream, etc. """ - assert self.stop() == True + self.stop <<= True logging.debug('Recording::finish()') - smgr = self.smgr + # Remove indata handler self.indh = None - # TODO: Fix when video - # if smgr.hasVideo(): - # smgr.removeCallback(self.vCallback, AvType.video_input) - # self.f['video_frame_positions'] = self.video_frame_positions - try: # Close the recording file self.f.close() @@ -217,7 +216,7 @@ class Recording: # Stop flag is raised. We stop recording here. return - curT = self.ablockno*self.blocksize/self.fs + curT = self.ablockno()*self.blocksize/self.fs # Increase the block counter self.ablockno += 1 @@ -230,9 +229,10 @@ class Recording: self.startDelay_passed = True # Reset the audio block counter and the recording time - self.ablockno = 1 + self.ablockno = Atomic(1) curT = 0 + ablockno = self.ablockno() recstatus = RecordStatus( curT=curT, done=False) @@ -252,10 +252,10 @@ class Recording: if self.progressCallback is not None: recstatus.done = True self.progressCallback(recstatus) - self.stop << True + self.stop <<= True return # Add the data to the file, and resize the audio data blocks - self.ad.resize(self.ablockno, axis=0) - self.ad[self.ablockno-1, :, :] = indata + self.ad.resize(ablockno, axis=0) + self.ad[ablockno-1, :, :] = indata diff --git a/src/lasp/pybind11/lasp_daq.cpp b/src/lasp/pybind11/lasp_daq.cpp index 7ddffb2..fa91280 100644 --- a/src/lasp/pybind11/lasp_daq.cpp +++ b/src/lasp/pybind11/lasp_daq.cpp @@ -32,7 +32,7 @@ void init_daq(py::module &m) { ss.def("errorMsg", &Daq::StreamStatus::errorMsg); /// Daq - daq.def("neninchannels", &Daq::neninchannels); + daq.def("neninchannels", &Daq::neninchannels, py::arg("include_monitor") = true); daq.def("nenoutchannels", &Daq::nenoutchannels); daq.def("samplerate", &Daq::samplerate); daq.def("dataType", &Daq::dataType); diff --git a/src/lasp/pybind11/lasp_daqconfig.cpp b/src/lasp/pybind11/lasp_daqconfig.cpp index 41fc8a0..e8113e9 100644 --- a/src/lasp/pybind11/lasp_daqconfig.cpp +++ b/src/lasp/pybind11/lasp_daqconfig.cpp @@ -93,4 +93,5 @@ void init_daqconfiguration(py::module &m) { &DaqConfiguration::outchannel_config); daqconfig.def("setAllInputEnabled", &DaqConfiguration::setAllInputEnabled); daqconfig.def("setAllOutputEnabled", &DaqConfiguration::setAllOutputEnabled); + daqconfig.def("enabledInChannels", &DaqConfiguration::enabledInChannels); } diff --git a/src/lasp/pybind11/lasp_pyindatahandler.cpp b/src/lasp/pybind11/lasp_pyindatahandler.cpp index 9950cd9..ab0a79a 100644 --- a/src/lasp/pybind11/lasp_pyindatahandler.cpp +++ b/src/lasp/pybind11/lasp_pyindatahandler.cpp @@ -48,7 +48,7 @@ template py::array_t getPyArray(const DaqData &d) { return py::array_t( py::array::ShapeContainer({d.nframes, d.nchannels}), // Shape - py::array::StridesContainer( // Strides + py::array::StridesContainer( // Strides {sizeof(T), sizeof(T) * d.nframes}), // Strides (in bytes) for each index @@ -99,37 +99,34 @@ public: py::gil_scoped_acquire acquire; try { - py::array binfo; + py::object bool_val; switch (d.dtype) { - case (DataType::dtype_int8): - binfo = getPyArray(d); - break; - case (DataType::dtype_int16): - binfo = getPyArray(d); - break; - case (DataType::dtype_int32): - binfo = getPyArray(d); - break; - case (DataType::dtype_fl32): - binfo = getPyArray(d); - break; - case (DataType::dtype_fl64): - binfo = getPyArray(d); - break; + case (DataType::dtype_int8): { + bool_val = cb(getPyArray(d)); + } break; + case (DataType::dtype_int16): { + bool_val = cb(getPyArray(d)); + } break; + case (DataType::dtype_int32): { + bool_val = cb(getPyArray(d)); + } break; + case (DataType::dtype_fl32): { + bool_val = cb(getPyArray(d)); + } break; + case (DataType::dtype_fl64): { + bool_val = cb(getPyArray(d)); + } break; default: throw std::runtime_error("BUG"); } // End of switch - py::object bool_val = cb(binfo); bool res = bool_val.cast(); if (!res) return false; - } catch (py::error_already_set &e) { cerr << "ERROR: Python raised exception from callback function: "; cerr << e.what() << endl; return false; - } catch (py::cast_error &e) { cerr << e.what() << endl; cerr << "ERROR: Python callback does not return boolean value." << endl; diff --git a/src/lasp/pybind11/lasp_streammgr.cpp b/src/lasp/pybind11/lasp_streammgr.cpp index 6e244f2..ad178a9 100644 --- a/src/lasp/pybind11/lasp_streammgr.cpp +++ b/src/lasp/pybind11/lasp_streammgr.cpp @@ -30,4 +30,5 @@ void init_streammgr(py::module &m) { smgr.def("getStreamStatus", &StreamMgr::getStreamStatus); smgr.def("isStreamRunningOK", &StreamMgr::isStreamRunningOK); smgr.def("isStreamRunning", &StreamMgr::isStreamRunning); + smgr.def("getDaq", &StreamMgr::getDaq, py::return_value_policy::reference); }