Bugfix (delete measurement when no data is in it) and cleanup of recording code
This commit is contained in:
parent
e9f500d460
commit
878da3369b
@ -33,7 +33,7 @@ class Atomic:
|
|||||||
|
|
||||||
def checkType(self, val):
|
def checkType(self, val):
|
||||||
if not (type(val) == bool or type(val) == int):
|
if not (type(val) == bool or type(val) == int):
|
||||||
raise RuntimeError("Invalid type for Atomic")
|
raise ValueError("Invalid type for Atomic")
|
||||||
|
|
||||||
def __iadd__(self, toadd):
|
def __iadd__(self, toadd):
|
||||||
self.checkType(toadd)
|
self.checkType(toadd)
|
||||||
|
@ -7,6 +7,7 @@ import dataclasses, logging, os, time, h5py, threading
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from .lasp_atomic import Atomic
|
from .lasp_atomic import Atomic
|
||||||
|
from enum import Enum, auto, unique
|
||||||
from .lasp_cpp import InDataHandler, StreamMgr
|
from .lasp_cpp import InDataHandler, StreamMgr
|
||||||
from .lasp_version import LASP_VERSION_MAJOR, LASP_VERSION_MINOR
|
from .lasp_version import LASP_VERSION_MAJOR, LASP_VERSION_MINOR
|
||||||
import uuid
|
import uuid
|
||||||
@ -18,6 +19,16 @@ class RecordStatus:
|
|||||||
done: bool = False
|
done: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class RecordingState(Enum):
|
||||||
|
"""Enumeration for the recording state"""
|
||||||
|
|
||||||
|
Waiting = auto()
|
||||||
|
Recording = auto()
|
||||||
|
AllDataStored = auto()
|
||||||
|
Finished = auto()
|
||||||
|
Error = auto()
|
||||||
|
|
||||||
|
|
||||||
class Recording:
|
class Recording:
|
||||||
"""
|
"""
|
||||||
Class used to perform a recording. Recording data can come in from a
|
Class used to perform a recording. Recording data can come in from a
|
||||||
@ -53,83 +64,88 @@ class Recording:
|
|||||||
if ext not in fn:
|
if ext not in fn:
|
||||||
fn += ext
|
fn += ext
|
||||||
|
|
||||||
self.smgr = streammgr
|
self._smgr = streammgr
|
||||||
self.metadata = None
|
self._metadata = None
|
||||||
|
|
||||||
|
self._recState = RecordingState.Waiting
|
||||||
|
|
||||||
if startDelay < 0:
|
if startDelay < 0:
|
||||||
raise RuntimeError("Invalid start delay value. Should be >= 0")
|
raise RuntimeError("Invalid start delay value. Should be >= 0")
|
||||||
|
|
||||||
self.startDelay = startDelay
|
self._startDelay = startDelay
|
||||||
|
|
||||||
# Flag used to indicate that we have passed the start delay
|
|
||||||
self.startDelay_passed = False
|
|
||||||
|
|
||||||
# The amount of seconds (float) that is to be recorded
|
# The amount of seconds (float) that is to be recorded
|
||||||
self.rectime = rectime
|
self._requiredRecordingLength = rectime
|
||||||
|
|
||||||
# The file name to store data to
|
# The file name to store data to
|
||||||
self.fn = fn
|
self._fn = fn
|
||||||
|
|
||||||
self.curT_rounded_to_seconds = 0
|
# Counter of the number of blocks that have been recorded
|
||||||
|
self._recordedBlocks = 0
|
||||||
|
|
||||||
# Counter of the number of blocks
|
# Counter of the overall number of blocks that have passed (including
|
||||||
self.ablockno = Atomic(0)
|
# the blocks that passed during waiting prior to recording)
|
||||||
|
self._allBlocks = 0
|
||||||
|
|
||||||
# Stop flag, set when recording is finished.
|
# Stop flag, set when recording is finished.
|
||||||
self.stop = Atomic(False)
|
self._stop = Atomic(False)
|
||||||
|
|
||||||
# Mutex, on who is working with the H5py data
|
# Mutex, on who is working with the H5py data and the class settings
|
||||||
self.file_mtx = threading.Lock()
|
self._rec_mutex = threading.Lock()
|
||||||
|
|
||||||
self.progressCallback = progressCallback
|
self._progressCallback = progressCallback
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Open the file
|
# Open the file
|
||||||
self.f = h5py.File(self.fn, "w", "stdio")
|
self._h5file = h5py.File(self._fn, "w", "stdio")
|
||||||
self.f.flush()
|
self._h5file.flush()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error creating measurement file {e}")
|
logging.error(f"Error creating measurement file {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
# This flag is used to delete the file on finish(), and can be used
|
# This flag is used to delete the file on finish(), and can be used
|
||||||
# when a recording is canceled.
|
# when a recording is canceled. It is set to True at start, as the file will be deleted when no data is in it.
|
||||||
self.deleteFile = False
|
self._deleteFile = True
|
||||||
|
|
||||||
# Try to obtain stream metadata
|
# Try to obtain stream metadata
|
||||||
streamstatus = streammgr.getStreamStatus(StreamMgr.StreamType.input)
|
streamstatus = streammgr.getStreamStatus(StreamMgr.StreamType.input)
|
||||||
if not streamstatus.runningOK():
|
if not streamstatus.runningOK():
|
||||||
raise RuntimeError(
|
raise RuntimeError("Stream is not running properly. Cannot start recording")
|
||||||
"Stream is not running properly. Please first start the stream"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.ad = None
|
# Audio dataset
|
||||||
|
self._ad = None
|
||||||
|
|
||||||
logging.debug("Starting record....")
|
logging.debug("Starting record....")
|
||||||
|
|
||||||
self.indh = InDataHandler(streammgr, self.inCallback, self.resetCallback)
|
self._indataHandler = InDataHandler(
|
||||||
|
streammgr, self.inCallback, self.resetCallback
|
||||||
|
)
|
||||||
|
|
||||||
if wait:
|
if wait:
|
||||||
logging.debug("Stop recording with CTRL-C")
|
logging.debug("Stop recording with CTRL-C")
|
||||||
try:
|
try:
|
||||||
while not self.stop():
|
while not self._stop():
|
||||||
time.sleep(0.01)
|
time.sleep(0.01)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.debug("Keyboard interrupt on record")
|
logging.debug("Keyboard interrupt on record")
|
||||||
finally:
|
finally:
|
||||||
self.finish()
|
self.finish()
|
||||||
|
|
||||||
|
def curT(self):
|
||||||
|
"""Return currently recorded time as float"""
|
||||||
|
|
||||||
def resetCallback(self, daq):
|
def resetCallback(self, daq):
|
||||||
"""
|
"""
|
||||||
Function called with initial stream data.
|
Function called with initial stream data.
|
||||||
"""
|
"""
|
||||||
with self.file_mtx:
|
with self._rec_mutex:
|
||||||
in_ch = daq.enabledInChannels()
|
in_ch = daq.enabledInChannels()
|
||||||
blocksize = daq.framesPerBlock()
|
blocksize = daq.framesPerBlock()
|
||||||
self.blocksize = blocksize
|
self._blocksize = blocksize
|
||||||
self.nchannels = daq.neninchannels()
|
self._nchannels = daq.neninchannels()
|
||||||
self.fs = daq.samplerate()
|
self._fs = daq.samplerate()
|
||||||
|
|
||||||
f = self.f
|
f = self._h5file
|
||||||
|
|
||||||
f.attrs["LASP_VERSION_MAJOR"] = LASP_VERSION_MAJOR
|
f.attrs["LASP_VERSION_MAJOR"] = LASP_VERSION_MAJOR
|
||||||
f.attrs["LASP_VERSION_MINOR"] = LASP_VERSION_MINOR
|
f.attrs["LASP_VERSION_MINOR"] = LASP_VERSION_MINOR
|
||||||
@ -145,7 +161,7 @@ class Recording:
|
|||||||
# Add the start delay here, as firstFrames() is called right after the
|
# Add the start delay here, as firstFrames() is called right after the
|
||||||
# constructor is called. time.time() returns a floating point
|
# constructor is called. time.time() returns a floating point
|
||||||
# number of seconds after epoch.
|
# number of seconds after epoch.
|
||||||
f.attrs["time"] = time.time() + self.startDelay
|
f.attrs["time"] = time.time() + self._startDelay
|
||||||
|
|
||||||
# In V2, we do not store JSON metadata anymore, but just an enumeration
|
# In V2, we do not store JSON metadata anymore, but just an enumeration
|
||||||
# index to a physical quantity.
|
# index to a physical quantity.
|
||||||
@ -156,10 +172,69 @@ class Recording:
|
|||||||
# f.attrs['qtys'] = [ch.qty.to_json() for ch in in_ch]
|
# f.attrs['qtys'] = [ch.qty.to_json() for ch in in_ch]
|
||||||
f.flush()
|
f.flush()
|
||||||
|
|
||||||
def firstFrames(self, adata):
|
def inCallback(self, adata):
|
||||||
|
"""
|
||||||
|
This method is called when a block of audio data from the stream is
|
||||||
|
available. It should return either True or False.
|
||||||
|
|
||||||
|
When returning False, it will stop the stream.
|
||||||
|
|
||||||
|
"""
|
||||||
|
if self._stop():
|
||||||
|
logging.debug("Stop flag set, early return in inCallback")
|
||||||
|
# Stop flag is raised. We do not add any data anymore.
|
||||||
|
return True
|
||||||
|
|
||||||
|
with self._rec_mutex:
|
||||||
|
|
||||||
|
self._allBlocks += 1
|
||||||
|
|
||||||
|
match self._recState:
|
||||||
|
case RecordingState.Waiting:
|
||||||
|
if (
|
||||||
|
self._allBlocks * self._blocksize / self._fs
|
||||||
|
> self._startDelay
|
||||||
|
):
|
||||||
|
self._recState = RecordingState.Recording
|
||||||
|
|
||||||
|
case RecordingState.Recording:
|
||||||
|
if self._ad is None:
|
||||||
|
self.__addFirstFramesToFile(adata)
|
||||||
|
else:
|
||||||
|
self.__addTimeDataToFile(adata)
|
||||||
|
|
||||||
|
# Increase the block counter
|
||||||
|
self._recordedBlocks += 1
|
||||||
|
|
||||||
|
recstatus = RecordStatus(curT=self.recordedTime, done=False)
|
||||||
|
|
||||||
|
if self.recordedTime >= self._requiredRecordingLength:
|
||||||
|
self._recState = RecordingState.AllDataStored
|
||||||
|
self._stop <<= True
|
||||||
|
recstatus.done = True
|
||||||
|
|
||||||
|
if self._progressCallback is not None:
|
||||||
|
self._progressCallback(recstatus)
|
||||||
|
|
||||||
|
case RecordingState.AllDataStored:
|
||||||
|
pass
|
||||||
|
|
||||||
|
case RecordingState.Finished:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def recordedTime(self):
|
||||||
|
"""Return recorded time (not rounded) as float"""
|
||||||
|
if self._ad is None:
|
||||||
|
return 0.0
|
||||||
|
return self._recordedBlocks * self._blocksize / self._fs
|
||||||
|
|
||||||
|
def __addFirstFramesToFile(self, adata):
|
||||||
"""
|
"""
|
||||||
Set up the dataset in which to store the audio data. This will create
|
Set up the dataset in which to store the audio data. This will create
|
||||||
the attribute `self.ad`
|
the attribute `self.ad` and flip around the _deleteFile flag.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
adata: Numpy array with data from DAQ
|
adata: Numpy array with data from DAQ
|
||||||
@ -170,40 +245,24 @@ class Recording:
|
|||||||
# datatype = daq.dataType()
|
# datatype = daq.dataType()
|
||||||
dtype = np.dtype(adata.dtype)
|
dtype = np.dtype(adata.dtype)
|
||||||
|
|
||||||
self.ad = self.f.create_dataset(
|
assert self._ad is None
|
||||||
|
|
||||||
|
self._ad = self._h5file.create_dataset(
|
||||||
"audio",
|
"audio",
|
||||||
(1, self.blocksize, self.nchannels),
|
(1, self._blocksize, self._nchannels),
|
||||||
dtype=dtype,
|
dtype=dtype,
|
||||||
maxshape=(
|
maxshape=(
|
||||||
None, # This means, we can add blocks
|
None, # This means, we can add blocks
|
||||||
# indefinitely
|
# indefinitely
|
||||||
self.blocksize,
|
self._blocksize,
|
||||||
self.nchannels,
|
self._nchannels,
|
||||||
),
|
),
|
||||||
compression="gzip",
|
compression="gzip",
|
||||||
)
|
)
|
||||||
self.f.flush()
|
self._ad[0, :, :] = adata
|
||||||
|
|
||||||
def inCallback(self, adata):
|
self._h5file.flush()
|
||||||
"""
|
self._deleteFile = False
|
||||||
This method is called when a block of audio data from the stream is
|
|
||||||
available. It should return either True or False.
|
|
||||||
|
|
||||||
When returning False, it will stop the stream.
|
|
||||||
|
|
||||||
"""
|
|
||||||
if self.stop():
|
|
||||||
logging.debug("Stop flag set, early return in inCallback")
|
|
||||||
# Stop flag is raised. We do not add any data anymore.
|
|
||||||
return True
|
|
||||||
|
|
||||||
with self.file_mtx:
|
|
||||||
|
|
||||||
if self.ad is None:
|
|
||||||
self.firstFrames(adata)
|
|
||||||
|
|
||||||
self.__addTimeData(adata)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def setDelete(self, val: bool):
|
def setDelete(self, val: bool):
|
||||||
"""
|
"""
|
||||||
@ -211,8 +270,8 @@ class Recording:
|
|||||||
the recording. Typically used for cleaning up after canceling a
|
the recording. Typically used for cleaning up after canceling a
|
||||||
recording.
|
recording.
|
||||||
"""
|
"""
|
||||||
with self.file_mtx:
|
with self._rec_mutex:
|
||||||
self.deleteFile = val
|
self._deleteFile = val
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
"""
|
"""
|
||||||
@ -222,86 +281,55 @@ class Recording:
|
|||||||
"""
|
"""
|
||||||
logging.debug("Recording::finish()")
|
logging.debug("Recording::finish()")
|
||||||
|
|
||||||
self.stop <<= True
|
self._stop <<= True
|
||||||
|
|
||||||
with self.file_mtx:
|
|
||||||
self.f.flush()
|
with self._rec_mutex:
|
||||||
|
if self._recState == RecordingState.Finished:
|
||||||
|
raise RuntimeError('Recording has already finished')
|
||||||
|
|
||||||
|
self._h5file.flush()
|
||||||
# Remove indata handler, which also should remove callback function
|
# Remove indata handler, which also should remove callback function
|
||||||
# from StreamMgr. This, however does not have to happen
|
# from StreamMgr. This, however does not have to happen
|
||||||
# instantaneously. For which we have to implement extra mutex
|
# instantaneously. For which we have to implement extra mutex
|
||||||
# guards in this class
|
# guards in this class
|
||||||
del self.indh
|
del self._indataHandler
|
||||||
self.indh = None
|
self._indataHandler = None
|
||||||
|
|
||||||
# Remove handle to dataset otherwise the h5 file is not closed
|
# Remove handle to dataset otherwise the h5 file is not closed
|
||||||
# properly.
|
# properly.
|
||||||
del self.ad
|
del self._ad
|
||||||
self.ad = None
|
self._ad = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Close the recording file
|
# Close the recording file
|
||||||
self.f.close()
|
self._h5file.close()
|
||||||
del self.f
|
del self._h5file
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error closing file: {e}")
|
logging.error(f"Error closing file: {e}")
|
||||||
|
|
||||||
logging.debug("Recording ended")
|
logging.debug("Recording ended")
|
||||||
if self.deleteFile:
|
if self._deleteFile:
|
||||||
self.__deleteFile()
|
self.__deleteFile()
|
||||||
|
self._recState = RecordingState.Finished
|
||||||
|
|
||||||
def __deleteFile(self):
|
def __deleteFile(self):
|
||||||
"""
|
"""
|
||||||
Cleanup the recording file.
|
Cleanup the recording file.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
os.remove(self.fn)
|
os.remove(self._fn)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Error deleting file: {self.fn}: {str(e)}")
|
logging.error(f"Error deleting file: {self._fn}: {str(e)}")
|
||||||
|
|
||||||
def __addTimeData(self, indata):
|
def __addTimeDataToFile(self, indata):
|
||||||
"""
|
"""
|
||||||
Called by handleQueue() and adds new time data to the storage file.
|
Called by handleQueue() and adds new time data to the storage file.
|
||||||
"""
|
"""
|
||||||
# logging.debug('Recording::__addTimeData()')
|
|
||||||
|
|
||||||
curT = self.ablockno() * self.blocksize / self.fs
|
ablockno = self._recordedBlocks
|
||||||
|
|
||||||
# Increase the block counter
|
|
||||||
self.ablockno += 1
|
|
||||||
|
|
||||||
if curT < self.startDelay and not self.startDelay_passed:
|
|
||||||
# Start delay has not been passed
|
|
||||||
return
|
|
||||||
elif curT >= 0 and not self.startDelay_passed:
|
|
||||||
# Start delay passed, switch the flag!
|
|
||||||
self.startDelay_passed = True
|
|
||||||
|
|
||||||
# Reset the audio block counter and the recording time
|
|
||||||
self.ablockno = Atomic(1)
|
|
||||||
curT = 0
|
|
||||||
|
|
||||||
ablockno = self.ablockno()
|
|
||||||
recstatus = RecordStatus(curT=curT, done=False)
|
|
||||||
|
|
||||||
if self.progressCallback is not None:
|
|
||||||
self.progressCallback(recstatus)
|
|
||||||
|
|
||||||
curT_rounded_to_seconds = int(curT)
|
|
||||||
if curT_rounded_to_seconds > self.curT_rounded_to_seconds:
|
|
||||||
self.curT_rounded_to_seconds = curT_rounded_to_seconds
|
|
||||||
print(f"{curT_rounded_to_seconds}", end="", flush=True)
|
|
||||||
else:
|
|
||||||
print(".", end="", flush=True)
|
|
||||||
|
|
||||||
if self.rectime is not None and curT > self.rectime:
|
|
||||||
# We are done!
|
|
||||||
if self.progressCallback is not None:
|
|
||||||
recstatus.done = True
|
|
||||||
self.progressCallback(recstatus)
|
|
||||||
self.stop <<= True
|
|
||||||
return
|
|
||||||
|
|
||||||
# Add the data to the file, and resize the audio data blocks
|
# Add the data to the file, and resize the audio data blocks
|
||||||
self.ad.resize(ablockno, axis=0)
|
self._ad.resize(ablockno + 1, axis=0)
|
||||||
self.ad[ablockno - 1, :, :] = indata
|
self._ad[ablockno, :, :] = indata
|
||||||
self.f.flush()
|
self._h5file.flush()
|
||||||
|
Loading…
Reference in New Issue
Block a user