Skip to content

Commit

Permalink
daq,audio: latch state closer to IO
Browse files Browse the repository at this point in the history
  • Loading branch information
nzjrs committed May 14, 2021
1 parent d4fed28 commit bc27940
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
37 changes: 23 additions & 14 deletions flyvr/audio/io_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def send(self, data):

def EveryNCallback(self):
with self._data_lock:
tns = time.time_ns()
tns = None

if self.cha_type is "input":
if not self.digital:
Expand All @@ -359,6 +359,24 @@ def EveryNCallback(self):
self._data, self.num_samples_per_chan * self.num_channels,
byref(self.read), byref(numBytesPerSamp), None)

# latch the current timing info as close to the read call completion as possible
self.flyvr_shared_state.DAQ_INPUT_NUM_SAMPLES_READ += self._data.shape[0]
row = [self.flyvr_shared_state.FICTRAC_FRAME_NUM,
self.flyvr_shared_state.DAQ_OUTPUT_NUM_SAMPLES_WRITTEN,
self.flyvr_shared_state.DAQ_INPUT_NUM_SAMPLES_READ,
self.flyvr_shared_state.SOUND_OUTPUT_NUM_SAMPLES_WRITTEN,
self.flyvr_shared_state.VIDEO_OUTPUT_NUM_FRAMES,
self.flyvr_shared_state.TIME_NS]

# save the data
self.flyvr_shared_state.logger.log(self.samples_dset_name, self._data)

# save the sync info
self.flyvr_shared_state.logger.log(self.samples_sync_dset_name,
np.array(row, dtype=np.int64))

tns = row[5]

elif self.cha_type is "output":

if self._data_generator is None:
Expand Down Expand Up @@ -404,36 +422,27 @@ def EveryNCallback(self):
sound_output_num_samples_written=row[3],
video_output_num_frames=row[4],
# and a time for replay experiments
time_ns=tns)
time_ns=row[5])

self.WriteAnalogF64(self._data.shape[0], 0, DAQmx_Val_WaitInfinitely, DAQmx_Val_GroupByScanNumber,
self._data, daq.byref(self.read), None)

self.flyvr_shared_state.DAQ_OUTPUT_NUM_SAMPLES_WRITTEN += self._data.shape[0]
self._last_chunk = chunk

tns = row[5]
else:
self.WriteDigitalLines(self._data.shape[0], False, DAQmx_Val_WaitInfinitely,
DAQmx_Val_GroupByScanNumber, self._data, None, None)

tns = time.time_ns()

# send the data to a control if requested.
if self.data_recorders is not None:
for data_rec in self.data_recorders:
if self._data is not None:
data_rec.send((self._data, tns))

if self.cha_type == "input":
self.flyvr_shared_state.logger.log(self.samples_dset_name, self._data)

self.flyvr_shared_state.DAQ_INPUT_NUM_SAMPLES_READ += self._data.shape[0]
self.flyvr_shared_state.logger.log(self.samples_sync_dset_name,
np.array([self.flyvr_shared_state.FICTRAC_FRAME_NUM,
self.flyvr_shared_state.DAQ_OUTPUT_NUM_SAMPLES_WRITTEN,
self.flyvr_shared_state.DAQ_INPUT_NUM_SAMPLES_READ,
self.flyvr_shared_state.SOUND_OUTPUT_NUM_SAMPLES_WRITTEN,
self.flyvr_shared_state.VIDEO_OUTPUT_NUM_FRAMES,
tns], dtype=np.int64))

self._newdata_event.set()

return 0 # The function should return an integer
Expand Down
3 changes: 2 additions & 1 deletion flyvr/audio/sound_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ def callback(outdata, frames, time_info, status):
exc_info=True)
raise sd.CallbackAbort

self.flyvr_shared_state.SOUND_OUTPUT_NUM_SAMPLES_WRITTEN += frames
# latch the current timing info as close to the write call (below) as possible

self.flyvr_shared_state.SOUND_OUTPUT_NUM_SAMPLES_WRITTEN += frames
# same order as SampleChunk.SYNCHRONIZATION_INFO_FIELDS
row = [self.flyvr_shared_state.FICTRAC_FRAME_NUM,
self.flyvr_shared_state.DAQ_OUTPUT_NUM_SAMPLES_WRITTEN,
Expand Down

0 comments on commit bc27940

Please sign in to comment.