From 0e150423b11867558b5dbed648910efb80810e5e Mon Sep 17 00:00:00 2001 From: John Stowers Date: Wed, 16 Dec 2020 21:43:02 +0100 Subject: [PATCH] add a sample non-OL dynamic random experiment flyvr.exe --config rig.yml -p playlists\audio_daq_paused.yml -e experiments\paired_random_audio_daq.py this starts a python experiment which randomly chooses corresponding pairs (in the order they were written in the playlist) from the audio and daq backend, and plays them simultaenously. note: the rig.yml configuration file should have * the audio_in and audio_out channels defined * you should disable the next signaling with remote_2P_next_disable --- experiments/paired_random_audio_daq.py | 50 ++++++++++++++++++++++++++ playlists/audio_daq_paused.yml | 13 +++++++ 2 files changed, 63 insertions(+) create mode 100644 experiments/paired_random_audio_daq.py create mode 100644 playlists/audio_daq_paused.yml diff --git a/experiments/paired_random_audio_daq.py b/experiments/paired_random_audio_daq.py new file mode 100644 index 00000000..703a3fb5 --- /dev/null +++ b/experiments/paired_random_audio_daq.py @@ -0,0 +1,50 @@ +import time +import random + +from flyvr.control.experiment import Experiment + +# this experiment randomly chooses only corresponding playlist items across +# the audio and daq backend. for example, if the daq playlist as items with +# identifiers 'd1', 'd2', 'd3', and 'd4', and the audio playlist has items +# with identifiers 'a1', 'a2', and 'a3', then every SWITCH_SECONDS we random +# choose to play simultaneously only (`d1' and 'a1'), ('d2' and 'a2'), or +# ('d3' and 'a3') + +SWITCH_SECONDS = 5 + + +class _MyExperiment(Experiment): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self._exp_started = False + self._last_switch = None + self._n_common_playlist_items = 0 + + def process_state(self, state): + if self._exp_started: + t = time.time() + if (t - self._last_switch) > SWITCH_SECONDS: + idx = random.randint(0, self._n_common_playlist_items - 1) + + item_daq = self.configured_playlist_items[Experiment.BACKEND_DAQ][idx] + self.play_playlist_item(Experiment.BACKEND_DAQ, item_daq) + item_audio = self.configured_playlist_items[Experiment.BACKEND_AUDIO][idx] + self.play_playlist_item(Experiment.BACKEND_AUDIO, item_audio) + + self.log.info('switched to daq:%s and audio:%s' % (item_daq, item_audio)) + + self._last_switch = t + else: + if self.is_started() and \ + self.is_backend_ready(Experiment.BACKEND_AUDIO) and \ + self.is_backend_ready(Experiment.BACKEND_DAQ): + self._exp_started = True + self._last_switch = time.time() - SWITCH_SECONDS # switch next loop + + self._n_common_playlist_items = min(len(self.configured_playlist_items[Experiment.BACKEND_DAQ]), + len(self.configured_playlist_items[Experiment.BACKEND_AUDIO])) + assert self._n_common_playlist_items > 0 + + +experiment = _MyExperiment() diff --git a/playlists/audio_daq_paused.yml b/playlists/audio_daq_paused.yml new file mode 100644 index 00000000..bef3e96c --- /dev/null +++ b/playlists/audio_daq_paused.yml @@ -0,0 +1,13 @@ +playlist: + daq: + - _options: {random_mode: 'none', paused: true} + - sq20hz: {name: 'square', sample_rate: 10000, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 20, max_value: 10.0, min_value: -10.0, amplitude: 2.0, phase: 0.0} + - sq40hz: {name: 'square', sample_rate: 10000, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 40, max_value: 10.0, min_value: -10.0, amplitude: 4.0, phase: 0.0} + - sq60hz: {name: 'square', sample_rate: 10000, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 60, max_value: 10.0, min_value: -10.0, amplitude: 6.0, phase: 0.0} + - sq80hz: {name: 'square', sample_rate: 10000, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 80, max_value: 10.0, min_value: -10.0, amplitude: 8.0, phase: 0.0} + audio: + - _options: {random_mode: 'none', paused: true} + - sin200hz: {name: 'sin', sample_rate: 44100, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 200, max_value: 10.0, min_value: -10.0, amplitude: 1.0, phase: 0.0} + - sin400hz: {name: 'sin', sample_rate': 44100, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 400, max_value: 10.0, min_value: -10.0, amplitude: 1.0, phase: 0.0} + - sin600hz: {name: 'sin', sample_rate: 44100, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 600, max_value: 10.0, min_value: -10.0, amplitude: 1.0, phase: 0.0} + - sin800hz: {name: 'sin', sample_rate: 44100, duration: 1000, pre_silence: 0, post_silence: 500, attenuator: null, frequency: 800, max_value: 10.0, min_value: -10.0, amplitude: 1.0, phase: 0.0} \ No newline at end of file