def __init__(self, port, receiver_ip, receiver_port, user): ServerThread.__init__(self, port) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.receiver_ip = receiver_ip self.receiver_port = receiver_port self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.user = user
def __init__(self, port=18000, slhost='127.0.0.1:9951', **kwargs): ServerThread.__init__(self, port, **kwargs) self.ping = False self.tempo = 120 self.prefix = 'osc.udp://' self.slhost = self.prefix + slhost self.host = self.prefix + '127.0.0.1:' + str(port)
def __init__(self, server, destport=10024, srcport=11111, debug=False, **kwargs): """Class initialiser.""" self.timeout = self.settable['timeout'] = 500 self.server = self.settable['server'] = server self.destport = self.settable['destport'] = destport # add built-in custom command shortcuts self.shortcuts.update({ '/': 'osc', }) # add built-in custom command aliases kwargs.setdefault('use_ipython', True) super().__init__(**kwargs) self.aliases.update({ 'mute': 'osc /lr/mix/on 0', 'unmute': 'osc /lr/mix/on 1', 'mainvol': '/lr/mix/fader', }) self.srcport = srcport self.debug = debug self.osc_commands = parse_commands() self.osc_command_names = sorted([cmd.address.lstrip('/') for cmd in self.osc_commands.values()]) self.queue = queue.Queue() self.osc = ServerThread(self.srcport) self.osc.add_method(None, None, self.osc_recv) # hooks self.register_preloop_hook(self.start_osc_server) self.register_postloop_hook(self.stop_osc_server)
def __init__(self, name='osc2midi', midi='jack', port=9999, target=None): ServerThread.__init__(self, port) self.events = { 'CTRL': CtrlEvent, 'NOTEON': NoteOnEvent, 'NOTEOFF': NoteOffEvent, 'PROGRAM': ProgramEvent, 'PITCHBEND': PitchbendEvent, 'AFTERTOUCH': AftertouchEvent, 'SYSEX': SysExEvent } self.eventTypes = [] self.name = name self.midi = midi self.midiPorts = { 'in' : [name + '_in'], 'out': [name + '_out'] } if type(target) == int: self.target = '127.0.0.1:' + str(target) elif type(target) == str: self.target = target else: self.target = None midiServer.config( backend = self.midi, client_name = self.name, in_ports = self.midiPorts['in'], out_ports = self.midiPorts['out'] )
def __init__(self, incoming, outgoing, sparseOutput=None, config={}, device_source='Muse', software_source='muselsl', debug_outputs=True, verbose=False): self.incoming = incoming self.outgoing = outgoing self.eeg_chunk_length = 12 self.threshold = 30 self.conc_level = 50 self.inc = 2 self._osc_server = ServerThread(incoming['port']) print('OSC server initialized at port {}.'.format(incoming['port'])) if not isinstance(self.outgoing, tuple): self.outgoing = [self.outgoing] self._output_threads = [] for out in self.outgoing: self._output_threads.append(Address(out['address'], out['port'])) print('OSC client initialized at {}:{}.'.format( out['address'], out['port'])) self._init_processing(config) self.incremented_value = 50
def __init__(self, muse_port, muse_id, server): self.muse_port = muse_port self.muse_id = muse_id # Configuring the Muse OSC client ServerThread.__init__(self, muse_port) logger.info('Muse %s connected to museIO on port %s' % (muse_id, muse_port)) # configure the Spacebrew client self.brew = SpacebrewApp(muse_id, server=server) self.osc_paths = [ '/muse/eeg', '/muse/eeg/quantization', # '/muse/eeg/dropped_samples', '/muse/acc', # '/muse/acc/dropped_samples', '/muse/batt', # '/muse/drlref', #'/muse/elements/raw_fft0', #'/muse/elements/raw_fft1', #'/muse/elements/raw_fft2', #'/muse/elements/raw_fft3', '/muse/elements/low_freqs_absolute', '/muse/elements/delta_absolute', '/muse/elements/theta_absolute', '/muse/elements/alpha_absolute', '/muse/elements/beta_absolute', '/muse/elements/gamma_absolute', #'/muse/elements/delta_relative', #'/muse/elements/theta_relative', #'/muse/elements/alpha_relative', #'/muse/elements/beta_relative', #'/muse/elements/gamma_relative', #'/muse/elements/delta_session_score', #'/muse/elements/theta_session_score', #'/muse/elements/alpha_session_score', #'/muse/elements/beta_session_score', #'/muse/elements/gamma_session_score', '/muse/elements/touching_forehead', '/muse/elements/horseshoe', '/muse/elements/is_good', '/muse/elements/blink', '/muse/elements/jaw_clench', '/muse/elements/experimental/concentration', '/muse/elements/experimental/mellow' ] for osc_path in self.osc_paths: spacebrew_name = self.calculate_spacebrew_name(osc_path) self.brew.add_publisher(spacebrew_name, 'string') logger.debug('Spacebrew publisher %s added for muse with ID %s' % (spacebrew_name, self.muse_id)) # Connect to spacebrew self.brew.start() logger.debug('Initialization completed for muse with ID %s' % self.muse_id)
def add_method(self, oscSetup ): that = self def action( path, args ): data = dict(zip(oscSetup.names,args)) data['_path'] = path oscSetup.callback( data ) that.callback( data ) ServerThread.add_method( self, oscSetup.path, oscSetup.types, action )
def __init__(self, name, signaler, daemon_address, debug): ServerThread.__init__(self) self.name = name self.signaler = signaler self.daemon_address = daemon_address self.debug = debug self.server_capabilities = "" global instance instance = self
def send(self, target, *message): """ Send osc message over udp """ if target.isdigit(): target = '127.0.0.1:' + target if 'osc.udp://' not in target: target = 'osc.udp://' + target ServerThread.send(self, target, *message)
def __init__(self): self.acc_lock = th.RLock() self.acc_list = [] self.blink_lock = th.RLock() self.blink_list = deque() self.jaw_lock = th.RLock() self.jaw_list = deque() self.senstate_lock = th.RLock() self.senstate = [] ServerThread.__init__(self, 5001)
def __init__(self, namespace, **kwargs): """ Server contructor Args: namespace (str): osc method addresses will be prefixed with /namespace disabled with an empty string """ ServerThread.__init__(self, reg_methods=False, **kwargs) self.namespace = namespace if namespace == '' or namespace[0] == '/' else '/' + namespace self.osc_methods = {} self.add_method(None, None, self.route_osc)
def __init__(self, muse_port, muse_id, server): # Configuring the Muse OSC client ServerThread.__init__(self, muse_port) # configure the Spacebrew client self.brew = SpacebrewApp(muse_id, server=server) self.osc_paths = [ {'address': "/muse/eeg", 'arguments': 4}, {'address': "/muse/eeg/quantization", 'arguments': 4}, {'address': "/muse/eeg/dropped_samples", 'arguments': 1}, {'address': "/muse/acc", 'arguments': 3}, {'address': "/muse/acc/dropped_samples", 'arguments': 1}, {'address': "/muse/batt", 'arguments': 4}, {'address': "/muse/drlref", 'arguments': 2}, {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4}, {'address': "/muse/elements/delta_absolute", 'arguments': 4}, {'address': "/muse/elements/theta_absolute", 'arguments': 4}, {'address': "/muse/elements/alpha_absolute", 'arguments': 4}, {'address': "/muse/elements/beta_absolute", 'arguments': 4}, {'address': "/muse/elements/gamma_absolute", 'arguments': 4}, {'address': "/muse/elements/delta_relative", 'arguments': 4}, {'address': "/muse/elements/theta_relative", 'arguments': 4}, {'address': "/muse/elements/alpha_relative", 'arguments': 4}, {'address': "/muse/elements/beta_relative", 'arguments': 4}, {'address': "/muse/elements/gamma_relative", 'arguments': 4}, {'address': "/muse/elements/delta_session_score", 'arguments': 4}, {'address': "/muse/elements/theta_session_score", 'arguments': 4}, {'address': "/muse/elements/alpha_session_score", 'arguments': 4}, {'address': "/muse/elements/beta_session_score", 'arguments': 4}, {'address': "/muse/elements/gamma_session_score", 'arguments': 4}, {'address': "/muse/elements/touching_forehead", 'arguments': 1}, {'address': "/muse/elements/horseshoe", 'arguments': 4}, {'address': "/muse/elements/is_good", 'arguments': 4}, {'address': "/muse/elements/blink", 'arguments': 1}, {'address': "/muse/elements/jaw_clench", 'arguments': 1}, {'address': "/muse/elements/experimental/concentration", 'arguments': 1}, {'address': "/muse/elements/experimental/mellow", 'arguments': 1} ] self.paths = [path['address'] for path in self.osc_paths] for path in self.paths: spacebrew_name = path.split('/')[-1] self.brew.add_publisher(spacebrew_name, "string") # connect to spacebrew self.brew.start()
def __init__(self, namespace, **kwargs): """ Server contructor Args: namespace (str): osc method addresses will be prefixed with /namespace disabled with an empty string """ ServerThread.__init__(self, reg_methods=False, **kwargs) self.namespace = namespace if namespace == '' or namespace[ 0] == '/' else '/' + namespace self.osc_methods = {} self.add_method(None, None, self.route_osc)
def __init__(self, muse_port, began_at): # Configuring the Muse OSC client ServerThread.__init__(self, muse_port) self.began_at = began_at self.osc_paths = [ {'address': "/muse/eeg", 'arguments': 4}, {'address': "/muse/eeg/quantization", 'arguments': 4}, {'address': "/muse/eeg/dropped_samples", 'arguments': 1}, {'address': "/muse/acc", 'arguments': 3}, {'address': "/muse/acc/dropped_samples", 'arguments': 1}, {'address': "/muse/batt", 'arguments': 4}, {'address': "/muse/drlref", 'arguments': 2}, {'address': "/muse/elements/low_freqs_absolute", 'arguments': 4}, {'address': "/muse/elements/delta_absolute", 'arguments': 4}, {'address': "/muse/elements/theta_absolute", 'arguments': 4}, {'address': "/muse/elements/alpha_absolute", 'arguments': 4}, {'address': "/muse/elements/beta_absolute", 'arguments': 4}, {'address': "/muse/elements/gamma_absolute", 'arguments': 4}, {'address': "/muse/elements/delta_relative", 'arguments': 4}, {'address': "/muse/elements/theta_relative", 'arguments': 4}, {'address': "/muse/elements/alpha_relative", 'arguments': 4}, {'address': "/muse/elements/beta_relative", 'arguments': 4}, {'address': "/muse/elements/gamma_relative", 'arguments': 4}, {'address': "/muse/elements/delta_session_score", 'arguments': 4}, {'address': "/muse/elements/theta_session_score", 'arguments': 4}, {'address': "/muse/elements/alpha_session_score", 'arguments': 4}, {'address': "/muse/elements/beta_session_score", 'arguments': 4}, {'address': "/muse/elements/gamma_session_score", 'arguments': 4}, {'address': "/muse/elements/touching_forehead", 'arguments': 1}, {'address': "/muse/elements/horseshoe", 'arguments': 4}, {'address': "/muse/elements/is_good", 'arguments': 4}, {'address': "/muse/elements/blink", 'arguments': 1}, {'address': "/muse/elements/jaw_clench", 'arguments': 1}, {'address': "/muse/elements/experimental/concentration", 'arguments': 1}, {'address': "/muse/elements/experimental/mellow", 'arguments': 1} ] self.paths = [path['address'] for path in self.osc_paths]
class qlcStopper(object): def __init__(self, port, qlcappport): self.port = port if self.port is not None: self.server = ServerThread(self.port) self.server.register_methods(self) self.server.start() @make_method('/Stop', None) def allStopQlc(self, path, args): for path in paths: self.server.send(qlcappport, path, 0)
class FFTServer(): def __init__(self, incoming, outgoing, sparseOutput=None, config={}, device_source='Muse', software_source='muselsl', debug_outputs=True, verbose=False): self.incoming = incoming self.outgoing = outgoing self.eeg_chunk_length = 12 self.threshold = 30 self.conc_level = 50 self.inc = 2 self._osc_server = ServerThread(incoming['port']) print('OSC server initialized at port {}.'.format(incoming['port'])) if not isinstance(self.outgoing, tuple): self.outgoing = [self.outgoing] self._output_threads = [] for out in self.outgoing: self._output_threads.append(Address(out['address'], out['port'])) print('OSC client initialized at {}:{}.'.format( out['address'], out['port'])) self._init_processing(config) self.incremented_value = 50 def _init_processing(self, config): DEFAULT_CONFIG = { 'fs': 256., 'n_channels': 5, 'raw_buffer_len': 3 * 256, 'filt_buffer_len': 3 * 256, 'window_len': 256, 'step': int(256 / 10), 'filter': ([1], [1]), 'filter_bank': {}, 'psd_window_len': 256., 'psd_buffer_len': 10 } config = {**DEFAULT_CONFIG, **config} self.fs = config['fs'] self.n_channels = config['n_channels'] self.eeg_ch_remap = None self.n_channels = 4 raw_buffer_len = int(config['raw_buffer_len']) filt_buffer_len = int(config['filt_buffer_len']) self.eeg_buffer = ut.NanBuffer(raw_buffer_len, self.n_channels) self.filt_eeg_buffer = ut.CircularBuffer(filt_buffer_len, self.n_channels) self.hpfilt_eeg_buffer = ut.CircularBuffer(filt_buffer_len, self.n_channels) self.smooth_eeg_buffer = ut.CircularBuffer(filt_buffer_len, self.n_channels) if config['filter']: if isinstance(config['filter'], tuple): b = config['filter'][0] a = config['filter'][1] elif isinstance(config['filter'], dict): b, a = ut.get_filter_coeff(self.fs, **config['filter']) zi = np.tile(signal.lfilter_zi(b, a), (self.n_channels, 1)).T self.bandpass_filt = {'b': b, 'a': a, 'zi': zi} if config['hpfilter']: b = config['hpfilter'][0] a = config['hpfilter'][1] zi = np.tile(signal.lfilter_zi(b, a), (self.n_channels, 1)).T self.hp_filt = {'b': b, 'a': a, 'zi': zi} if config['lpfilter']: b = config['lpfilter'][0] a = config['lpfilter'][1] zi = np.tile(signal.lfilter_zi(b, a), (self.n_channels, 1)).T self.lp_filt = {'b': b, 'a': a, 'zi': zi} if config['filter_bank']: self.filter_bank = {} for name, coeff in config['filter_bank'].items(): zi = np.tile(signal.lfilter_zi(coeff[0], coeff[1]), (self.n_channels, 1)).T self.filter_bank[name] = { 'b': coeff[0], 'a': coeff[1], 'zi': zi } self.window_len = int(config['window_len']) self.step = int(config['step']) psd_buffer_len = int(config['psd_buffer_len']) self.psd_buffer = ut.CircularBuffer(psd_buffer_len, 129, self.n_channels) decayRate = 0.997 self.hists = { 'delta': ut.Histogram(1000, self.n_channels, bounds=(0, 50), min_count=80, decay=decayRate), 'theta': ut.Histogram(1000, self.n_channels, bounds=(0, 30), min_count=80, decay=decayRate), 'alpha': ut.Histogram(1000, self.n_channels, bounds=(0, 20), min_count=80, decay=decayRate), 'beta': ut.Histogram(1000, self.n_channels, bounds=(0, 10), min_count=80, decay=decayRate), 'gamma': ut.Histogram(1000, self.n_channels, bounds=(0, 10), min_count=80, decay=decayRate) } self.firstWindowProc = True def _update_eeg_liblo_osc(self, path, args): sample = np.array(args).reshape(1, -1) self._process_eeg(sample[:, :self.n_channels], 0) def _process_eeg(self, samples, timestamp): self.eeg_buffer.update(samples) filt_samples = samples if config['filter']: filt_samples, self.bandpass_filt['zi'] = signal.lfilter( self.bandpass_filt['b'], self.bandpass_filt['a'], samples, axis=0, zi=self.bandpass_filt['zi']) self.filt_eeg_buffer.update(filt_samples) if config['hpfilter']: filt_samples, self.hp_filt['zi'] = signal.lfilter( self.hp_filt['b'], self.hp_filt['a'], filt_samples, axis=0, zi=self.hp_filt['zi']) self.hpfilt_eeg_buffer.update(filt_samples) if config['lpfilter']: smooth_eeg_samples, self.lp_filt['zi'] = signal.lfilter( self.lp_filt['b'], self.lp_filt['a'], filt_samples, axis=0, zi=self.lp_filt['zi']) else: smooth_eeg_samples = filt_samples self.smooth_eeg_buffer.update(smooth_eeg_samples) if config['filter_bank']: filter_bank_samples = {} for name, filt_dict in self.filter_bank.items(): filter_bank_samples[name], self.filter_bank[name][ 'zi'] = signal.lfilter(filt_dict['b'], filt_dict['a'], filt_samples, axis=0, zi=self.filter_bank[name]['zi']) low_freq_chs = filter_bank_samples['delta'][0, [ 0, 2 ]] #+ filter_bank_samples['theta'][0, [0, 1]] window = self.smooth_eeg_buffer.extract(self.window_len) if self.smooth_eeg_buffer.pts % 3 == 0: self._send_output_vec(smooth_eeg_samples, timestamp, 'muse/eeg') if self.eeg_buffer.pts > self.step: self.eeg_buffer.pts = 0 if config['lpfilter']: window = self.smooth_eeg_buffer.extract(self.window_len) psd_raw_buffer = self.eeg_buffer.extract(self.window_len) psd, f = ut.fft_continuous(psd_raw_buffer, n=int(self.fs), psd=True, log='psd', fs=self.fs, window='hamming') self.psd_buffer.update(np.expand_dims(psd, axis=0)) mean_psd = np.nanmean(self.psd_buffer.extract(), axis=0) bandPowers, bandNames = ut.compute_band_powers(mean_psd, f, relative=True) ratioPowers, ratioNames = ut.compute_band_ratios(bandPowers) if (self.firstWindowProc): self.band_powers = bandPowers self.band_names = bandNames self.ratio_powers = ratioPowers self.ratio_names = ratioNames self.scores = np.zeros((len(self.band_names), self.n_channels)) self.firstWindowProc = False for i, (name, hist) in enumerate(self.hists.items()): self.band_powers = bandPowers self.ratio_powers = ratioPowers self.combine_bands = ut.get_combined_bands(self.band_powers) self.concentration = ut.compute_concentration( ut.get_combined_bands(self.band_powers)) if self.conc_level >= 0 and self.conc_level <= 98: self.conc_level += ut.concentration_incrementer( ut.compute_concentration( ut.get_combined_bands(self.band_powers)), self.threshold) band_names = ['delta', 'theta', 'alpha', 'beta', 'gamma'] self._send_bands(self.combine_bands, timestamp, band_names) self._send_outputs(self.band_powers, timestamp, 'bands') self._send_outputs(self.ratio_powers, timestamp, 'ratios') self._send_float(self.concentration, timestamp, 'concentration') print(self.concentration) self._send_float(100 - self.concentration, timestamp, 'arousal') self._send_float(self.conc_level, timestamp, 'concentration_incremented') self._send_float(100 - self.conc_level, timestamp, 'arousal_incremented') def inc_inc(self, num): self.incremented_value += num def _send_outputs(self, output, timestamp, name): for out in self._output_threads: for c in range(self.n_channels): new_output = [('f', x) for x in output[:, c]] message = Message('/{}{}'.format(name, c), *new_output) send(out, message) def _send_output_vec(self, output, timestamp, name): for out in self._output_threads: new_output = [('f', x) for x in output[0, :]] message = Message('/{}'.format(name), *new_output) send(out, message) def _send_output(self, output, timestamp, name): for out in self._output_threads: if (np.array(output).size == 1): new_output = [('f', np.asscalar(output))] message = Message('/{}'.format(name), *new_output) send(out, message) def _send_float(self, output, timestamp, name): for out in self._output_threads: new_output = [('f', output)] message = Message('/{}'.format(name), *new_output) send(out, message) def _send_bands(self, output, timestamp, names): for out in self._output_threads: for i in range(5): new_output = [('f', output[i])] message = Message('/{}'.format(names[i]), *new_output) send(out, message) def start(self): self.started = True self._osc_server.add_method('/muse/eeg', None, self._update_eeg_liblo_osc) self._osc_server.start() def stop(self): self.started = False
def __init__(self, incoming, outgoing, sparseOutput=None, config={}, device_source='Muse', software_source='muselsl', debug_outputs=True, verbose=False): self.incoming = incoming self.outgoing = outgoing self.sparseOutput = sparseOutput self.device_source = device_source self.software_source = software_source self.debug_outputs = debug_outputs self.verbose = verbose self.eeg_chunk_length = 12 # 1. Initialize inlet if isinstance(self.incoming, str): # LSL inlet print('Looking for the {} stream...'.format(incoming)) self._stream = resolve_byprop('type', incoming, timeout=2) if len(self._stream) == 0: raise (RuntimeError('Can\'t find {} stream.'.format(incoming))) print('Aquiring data from the \'{}\' stream...'.format(incoming)) self._inlet = StreamInlet(self._stream[0], max_chunklen=self.eeg_chunk_length) self._info_in = self._inlet.info() else: # OSC port if USE_LIBLO: self._osc_server = ServerThread(incoming['port']) print('OSC server initialized at port {}.'.format( incoming['port'])) else: self._dispatcher = dispatcher.Dispatcher() print('python-osc dispatcher initialized.') # 2. Initialize outlets if not isinstance(self.outgoing, tuple): self.outgoing = [self.outgoing] self._output_threads = [] for out in self.outgoing: if isinstance(out, str): # LSL outlet raise NotImplementedError elif isinstance(out, dict): # OSC port if USE_LIBLO: self._output_threads.append( Address(out['address'], out['port'])) else: raise NotImplementedError # self._client = udp_client.SimpleUDPClient( # outgoing['address'], outgoing['port']) print('OSC client initialized at {}:{}.'.format( out['address'], out['port'])) if (self.sparseOutput != None): if not isinstance(self.sparseOutput, tuple): self.sparseOutput = [self.sparseOutput] self._sparseOutput_threads = [] for out in self.sparseOutput: if isinstance(out, str): # LSL outlet raise NotImplementedError elif isinstance(out, dict): # OSC port if USE_LIBLO: self._sparseOutput_threads.append( Address(out['address'], out['port'])) else: raise NotImplementedError print('OSC sparse output client initialized at {}:{}.'. format(out['address'], out['port'])) # 3. Initialize internal buffers and variables self._init_processing(config)
t = 0 def print_diff(): global t, error, bpm if t == 0: t = time() return if bpm != seq.bpm: bpm = seq.bpm error = 0 t = time() return nt = time() error += 60. / bpm - (nt - t) t = nt print("Cumulated error: %s%.3f ms (%5.2f" % (' ' if str(error)[0] != '-' else '', round(1000000 * error) / 1000, abs(100 * error / (60. / bpm))) + "%) at " + str(bpm) + " bpm") sequencer_monitor = ServerThread(port=9900) sequencer_monitor.add_method('/tic', None, print_diff) sequencer_monitor.start() seq.send(':/Sequencer/Sequence/Enable', 'metronom') seq.play() seq.start()
class XAirCmdApp(cmd.Cmd): allow_cli_args = False abbrev = True intro = "Enter OSC command (Control-C or Control-D to quit)..." prompt = 'xair> ' # legalChars = u'/' + cmd.Cmd.legalChars def __init__(self, server, destport=10024, srcport=11111, debug=False, **kwargs): """Class initialiser.""" self.timeout = self.settable['timeout'] = 500 self.server = self.settable['server'] = server self.destport = self.settable['destport'] = destport # add built-in custom command shortcuts self.shortcuts.update({ '/': 'osc', }) # add built-in custom command aliases kwargs.setdefault('use_ipython', True) super().__init__(**kwargs) self.aliases.update({ 'mute': 'osc /lr/mix/on 0', 'unmute': 'osc /lr/mix/on 1', 'mainvol': '/lr/mix/fader', }) self.srcport = srcport self.debug = debug self.osc_commands = parse_commands() self.osc_command_names = sorted([cmd.address.lstrip('/') for cmd in self.osc_commands.values()]) self.queue = queue.Queue() self.osc = ServerThread(self.srcport) self.osc.add_method(None, None, self.osc_recv) # hooks self.register_preloop_hook(self.start_osc_server) self.register_postloop_hook(self.stop_osc_server) def osc_recv(self, path, args, types, addr): log.debug("OSC RECV (%s, %s): %s %s [%s]", addr.hostname, addr.port, path, types, ", ".join(repr(arg) for arg in args)) self.queue.put((path, args, types, addr)) def do_osc(self, line): if not line: return self.help_osc() try: oscaddr, rawargs = line.split(None, 1) except ValueError: oscaddr = line rawargs = '' oscaddr = '/' + oscaddr.lstrip('/') # ~ if not oscaddr.startswith('/'): # ~ self.perror("OSC address must start with a slash", traceback_war=False) # ~ return oscargs = [] for arg in shlex.split(rawargs, posix=False): if arg.strip(): try: oscargs.append(ast.literal_eval(arg)) except: # noqa:E722 oscargs.append(arg) log.debug("OSC SEND -> (%s, %s): %s %s", self.server, self.destport, oscaddr, "".join("%r" % arg for arg in oscargs)) self.osc.send((self.server, self.destport), oscaddr, *oscargs) try: path, args, types, addr = self.queue.get(timeout=self.timeout / 1000) except queue.Empty: self.p_warn("No reply within timeout ({:d} msec).".format(self.timeout)) else: self.p_ok("{} {} [{}]".format(path, types, ", ".join(repr(arg) for arg in args))) def help_osc(self): self.poutput("osc ADDR [arg1 [arg2] ... [argn]]") def complete_osc(self, text, line, begidx, endidx): log.debug((text, line, begidx, endidx)) #if not text.startswith('/'): # text = '/' + text return self.delimiter_complete(text, line, begidx, endidx, self.osc_command_names, '/') def p_ok(self, msg): self.poutput(msg, color=Fore.GREEN) def p_warn(self, msg): self.poutput(msg, color=Fore.YELLOW) def start_osc_server(self) -> None: self.osc.start() def stop_osc_server(self) -> None: self.osc.stop() self.poutput('') def postparse(self, parse_result) -> None: log.debug("postparse: %r", list(parse_result)) return parse_result
def __init__(self,port=5001): ServerThread.__init__(self, port)
def __init__(self, muse_port, callback_functions): ServerThread.__init__(self, muse_port) self.callback_functions = callback_functions
def __init__(self, host, mode): ServerThread.__init__(self, 8998 + int(random() * 9000), mode) self.host = host
def stop(self): ServerThread.stop(self) midiServer.quit()
class qlcDelayer(object): def __init__(self, port, qlcappport): self.port = port if self.port is not None: self.server = ServerThread(self.port) self.server.register_methods(self) self.server.start() @make_method(None, 'i') def sendToQlc(self, path, args): sleep(.001) self.server.send(qlcappport, path, args[0]) @make_method(None, 'fff') # /BC/1 R G B @make_method(None, 'iii') # /BC/1 R G B def sendToQlcRgb(self, path, args): if '/Segment' in path: sleep(.001) self.server.send(qlcappport, path.replace('/Segment', '/Red/Segment'), args[0]) self.server.send(qlcappport, path.replace('/Segment', '/Green/Segment'), args[1]) self.server.send(qlcappport, path.replace('/Segment', '/Blue/Segment'), args[2])
def __init__(self, muse_port, muse_id, server): # Configuring the Muse OSC client ServerThread.__init__(self, muse_port) # configure the Spacebrew client self.brew = SpacebrewApp(muse_id, server=server) self.osc_paths = [{ 'address': "/muse/eeg", 'arguments': 4 }, { 'address': "/muse/eeg/quantization", 'arguments': 4 }, { 'address': "/muse/eeg/dropped_samples", 'arguments': 1 }, { 'address': "/muse/acc", 'arguments': 3 }, { 'address': "/muse/acc/dropped_samples", 'arguments': 1 }, { 'address': "/muse/batt", 'arguments': 4 }, { 'address': "/muse/drlref", 'arguments': 2 }, { 'address': "/muse/elements/low_freqs_absolute", 'arguments': 4 }, { 'address': "/muse/elements/delta_absolute", 'arguments': 4 }, { 'address': "/muse/elements/theta_absolute", 'arguments': 4 }, { 'address': "/muse/elements/alpha_absolute", 'arguments': 4 }, { 'address': "/muse/elements/beta_absolute", 'arguments': 4 }, { 'address': "/muse/elements/gamma_absolute", 'arguments': 4 }, { 'address': "/muse/elements/delta_relative", 'arguments': 4 }, { 'address': "/muse/elements/theta_relative", 'arguments': 4 }, { 'address': "/muse/elements/alpha_relative", 'arguments': 4 }, { 'address': "/muse/elements/beta_relative", 'arguments': 4 }, { 'address': "/muse/elements/gamma_relative", 'arguments': 4 }, { 'address': "/muse/elements/delta_session_score", 'arguments': 4 }, { 'address': "/muse/elements/theta_session_score", 'arguments': 4 }, { 'address': "/muse/elements/alpha_session_score", 'arguments': 4 }, { 'address': "/muse/elements/beta_session_score", 'arguments': 4 }, { 'address': "/muse/elements/gamma_session_score", 'arguments': 4 }, { 'address': "/muse/elements/touching_forehead", 'arguments': 1 }, { 'address': "/muse/elements/horseshoe", 'arguments': 4 }, { 'address': "/muse/elements/is_good", 'arguments': 4 }, { 'address': "/muse/elements/blink", 'arguments': 1 }, { 'address': "/muse/elements/jaw_clench", 'arguments': 1 }, { 'address': "/muse/elements/experimental/concentration", 'arguments': 1 }, { 'address': "/muse/elements/experimental/mellow", 'arguments': 1 }] self.paths = [path['address'] for path in self.osc_paths] for path in self.paths: spacebrew_name = path.split('/')[-1] self.brew.add_publisher(spacebrew_name, "string") # connect to spacebrew self.brew.start()
def __init__(self, ip, port, start_muse_io, callback_functions): ServerThread.__init__(self, port) self.callback_functions = callback_functions if start_muse_io: _start_muse_io(port)
]) error = 0 t = 0 def print_diff(): global t, error, bpm if t == 0: t = time() return if bpm != seq.bpm: bpm=seq.bpm error = 0 t = time() return nt = time() error += 60. / bpm - (nt - t) t = nt print("Cumulated error: %s%.3f ms (%5.2f" % (' ' if str(error)[0]!='-' else '',round(1000000 * error)/1000, abs(100 * error / (60. / bpm))) + "%) at " + str(bpm) + " bpm") sequencer_monitor = ServerThread(port=9900) sequencer_monitor.add_method('/tic', None, print_diff) sequencer_monitor.start() seq.send(':/Sequencer/Sequence/Enable', 'metronom') seq.play() seq.start()
def __init__(self, port, qlcappport): self.port = port if self.port is not None: self.server = ServerThread(self.port) self.server.register_methods(self) self.server.start()
class FFTServer(): """Server to receive EEG data and stream classifier outputs. Attributes: See args. Args: incoming (str or dict): incoming data stream. If provided as a string, look for an LSL stream with the corresponding type. If provided as dict with fields `address` and `port`, open an OSC port at that address and port. outgoing (str or dict): outgoing data stream. If provided as a string, stream to an LSL stream with the corresponding type. If provided as dict with fields `address` and `port`, stream to an OSC port at that address and port. Keyword Args: config (dict): dictionary containing the configuration and preprocessing parameters, e.g. (these are the default values): config = {'fs': 256., 'n_channels': 4, 'raw_buffer_len': 3 * fs, 'filt_buffer_len': 3 * fs, 'window_len': fs, 'step': int(fs / 10), 'filter': ([1], [1]), 'psd_window_len': 256., 'psd_buffer_len': 10} device_source (str): Device from which the data is coming from. 'muse' or 'vive' streaming_source (str): Software source of the data stream: 'muselsl' 'musedirect' 'musemonitor' debug_outputs (bool): if True, send debug outputs (not used by VR experience) verbose (bool): if True, print status whenever new data is received or sent. """ def __init__(self, incoming, outgoing, sparseOutput=None, config={}, device_source='Muse', software_source='muselsl', debug_outputs=True, verbose=False): self.incoming = incoming self.outgoing = outgoing self.sparseOutput = sparseOutput self.device_source = device_source self.software_source = software_source self.debug_outputs = debug_outputs self.verbose = verbose self.eeg_chunk_length = 12 # 1. Initialize inlet if isinstance(self.incoming, str): # LSL inlet print('Looking for the {} stream...'.format(incoming)) self._stream = resolve_byprop('type', incoming, timeout=2) if len(self._stream) == 0: raise (RuntimeError('Can\'t find {} stream.'.format(incoming))) print('Aquiring data from the \'{}\' stream...'.format(incoming)) self._inlet = StreamInlet(self._stream[0], max_chunklen=self.eeg_chunk_length) self._info_in = self._inlet.info() else: # OSC port if USE_LIBLO: self._osc_server = ServerThread(incoming['port']) print('OSC server initialized at port {}.'.format( incoming['port'])) else: self._dispatcher = dispatcher.Dispatcher() print('python-osc dispatcher initialized.') # 2. Initialize outlets if not isinstance(self.outgoing, tuple): self.outgoing = [self.outgoing] self._output_threads = [] for out in self.outgoing: if isinstance(out, str): # LSL outlet raise NotImplementedError elif isinstance(out, dict): # OSC port if USE_LIBLO: self._output_threads.append( Address(out['address'], out['port'])) else: raise NotImplementedError # self._client = udp_client.SimpleUDPClient( # outgoing['address'], outgoing['port']) print('OSC client initialized at {}:{}.'.format( out['address'], out['port'])) if (self.sparseOutput != None): if not isinstance(self.sparseOutput, tuple): self.sparseOutput = [self.sparseOutput] self._sparseOutput_threads = [] for out in self.sparseOutput: if isinstance(out, str): # LSL outlet raise NotImplementedError elif isinstance(out, dict): # OSC port if USE_LIBLO: self._sparseOutput_threads.append( Address(out['address'], out['port'])) else: raise NotImplementedError print('OSC sparse output client initialized at {}:{}.'. format(out['address'], out['port'])) # 3. Initialize internal buffers and variables self._init_processing(config) def _init_processing(self, config): """Initialize internal buffers and variables for EEG processing. Args: config (dict): dictionary containing various parameters. See DEFAULT_CONFIG below for default values. fs (float): sampling frequency n_channels (int): number of channels raw_buffer_len (int): raw data buffer length filt_buffer_len (int): filtered data buffer length window_len (int): processing window length step (int): number of samples between two consecutive windows to process filter (tuple or dict): filtering parameters. If provided as a tuple, the first and second elements should be the `b` and `a` coefficients of a filter. If provided as a dictionary, the fields `order`, `l_freq`, `h_freq` and `method` are required; the function pre.get_filter_coeff() will then be used to compute the coefficients. If None, don't use a filter (windows will be extracted from the raw buffer). psd_window_len (int): length of the window to use for PSD psd_buffer_len (int): PSD buffer length """ DEFAULT_CONFIG = { 'fs': 256., 'n_channels': 5, 'raw_buffer_len': 3 * 256, 'filt_buffer_len': 3 * 256, 'window_len': 256, 'step': int(256 / 10), 'filter': ([1], [1]), 'filter_bank': {}, 'psd_window_len': 256., 'psd_buffer_len': 10 } config = {**DEFAULT_CONFIG, **config} self.fs = config['fs'] self.n_channels = config['n_channels'] # Initialize EEG channel remapping parameters self.eeg_ch_remap = None if self.device_source.lower() == 'vive': self.eeg_ch_remap = [3, 1, 2, 3, 4] self.n_channels = 5 if self.software_source.lower() == 'musedirect': self.eeg_ch_remap[-1] = 5 self.n_channels = 5 if self.device_source.lower() == 'leroy': self.eeg_ch_remap = None self.n_channels = 4 if self.device_source.lower() == 'muse': self.eeg_ch_remap = None self.n_channels = 4 if self.device_source.lower() == 'vivehr': self.eeg_ch_remap = [3, 1, 2, 3, 0] self.n_channels = 5 # Initialize the EEG buffers raw_buffer_len = int(config['raw_buffer_len']) filt_buffer_len = int(config['filt_buffer_len']) self.eeg_buffer = ut.NanBuffer(raw_buffer_len, self.n_channels) self.filt_eeg_buffer = ut.CircularBuffer(filt_buffer_len, self.n_channels) self.hpfilt_eeg_buffer = ut.CircularBuffer(filt_buffer_len, self.n_channels) self.smooth_eeg_buffer = ut.CircularBuffer(filt_buffer_len, self.n_channels) self.eyeH_buffer = ut.CircularBuffer(100, 1) # Initialize the EEG filter if config['filter']: if isinstance(config['filter'], tuple): b = config['filter'][0] a = config['filter'][1] elif isinstance(config['filter'], dict): b, a = ut.get_filter_coeff(self.fs, **config['filter']) zi = np.tile(signal.lfilter_zi(b, a), (self.n_channels, 1)).T self.bandpass_filt = {'b': b, 'a': a, 'zi': zi} if config['hpfilter']: b = config['hpfilter'][0] a = config['hpfilter'][1] zi = np.tile(signal.lfilter_zi(b, a), (self.n_channels, 1)).T self.hp_filt = {'b': b, 'a': a, 'zi': zi} if config['lpfilter']: b = config['lpfilter'][0] a = config['lpfilter'][1] zi = np.tile(signal.lfilter_zi(b, a), (self.n_channels, 1)).T self.lp_filt = {'b': b, 'a': a, 'zi': zi} # Initialize the filter bank if config['filter_bank']: self.filter_bank = {} for name, coeff in config['filter_bank'].items(): zi = np.tile(signal.lfilter_zi(coeff[0], coeff[1]), (self.n_channels, 1)).T self.filter_bank[name] = { 'b': coeff[0], 'a': coeff[1], 'zi': zi } # Initialize processing parameters self.window_len = int(config['window_len']) self.step = int(config['step']) # Initialize processing buffers psd_buffer_len = int(config['psd_buffer_len']) self.psd_buffer = ut.CircularBuffer(psd_buffer_len, 129, self.n_channels) # Initialize scoring histograms decayRate = 0.997 self.hists = { 'delta': ut.Histogram(1000, self.n_channels, bounds=(0, 50), min_count=80, decay=decayRate), 'theta': ut.Histogram(1000, self.n_channels, bounds=(0, 30), min_count=80, decay=decayRate), 'alpha': ut.Histogram(1000, self.n_channels, bounds=(0, 20), min_count=80, decay=decayRate), 'beta': ut.Histogram(1000, self.n_channels, bounds=(0, 10), min_count=80, decay=decayRate), 'gamma': ut.Histogram(1000, self.n_channels, bounds=(0, 10), min_count=80, decay=decayRate) } self.eyeH_hist = ut.Histogram(500, 1, bounds=(0, 10000), min_count=80, decay=decayRate) self.emg_hist = ut.Histogram(500, 1, bounds=(0, 10), min_count=80, decay=decayRate) self.blinkwait = 0 self.blink = 0 self.firstWindowProc = True self.band_names = 0 self.band_powers = 0 self.ratio_powers = 0 self.ratio_names = 0 # Used for calm score self.slow_calm_score = 0 self.slow_alpha_score = 0 self.eye_mov_percent_buffer = ut.CircularBuffer(256, 1) self.slow_calm_score_buffer = ut.CircularBuffer(512, 1) self.increments_buffer = ut.CircularBuffer(512, 1) self.low_freq_chs_buffer = ut.CircularBuffer(150, 2) self.low_freq_chs_std = 1 ###################################################################### # BODY Motion Processing, Accelerometer, Gyro raw_buffer_len = 150 filt_buffer_len = 150 self.acc_window_len = 50 self.acc_buffer = ut.NanBuffer(raw_buffer_len, 3) self.filt0_buffer = ut.CircularBuffer(filt_buffer_len, 3) self.heart_buffer = ut.CircularBuffer(150, 1) self.breath_buffer = ut.CircularBuffer(500, 1) # Initialize the Body Filters if config['filter0']: b = config['filter0'][0] a = config['filter0'][1] zi = np.tile(signal.lfilter_zi(b, a), (3, 1)).T self.filter0 = {'b': b, 'a': a, 'zi': zi} if config['filter1']: b = config['filter1'][0] a = config['filter1'][1] zi = np.tile(signal.lfilter_zi(b, a), (3, 1)).T self.filter1 = {'b': b, 'a': a, 'zi': zi} if config['filter2']: b = config['filter2'][0] a = config['filter2'][1] zi = signal.lfilter_zi(b, a) self.filter2 = {'b': b, 'a': a, 'zi': zi} if config['filter3']: b = config['filter3'][0] a = config['filter3'][1] zi = np.tile(signal.lfilter_zi(b, a), (3, 1)).T self.filter3 = {'b': b, 'a': a, 'zi': zi} if config['filter4']: b = config['filter4'][0] a = config['filter4'][1] zi = signal.lfilter_zi(b, a) self.filter4 = {'b': b, 'a': a, 'zi': zi} if config['filter5']: b = config['filter5'][0] a = config['filter5'][1] zi = signal.lfilter_zi(b, a) self.filter5 = {'b': b, 'a': a, 'zi': zi} if config['filter6']: b = config['filter6'][0] a = config['filter6'][1] zi = signal.lfilter_zi(b, a) self.filter6 = {'b': b, 'a': a, 'zi': zi} if config['filter7']: b = config['filter7'][0] a = config['filter7'][1] zi = signal.lfilter_zi(b, a) self.filter7 = {'b': b, 'a': a, 'zi': zi} def _update_eeg_liblo_osc(self, path, args): """Collect new EEG data point(s) from pyliblo OSC and process. Args: path (str): OSC path listened to args (list): received values """ if self.verbose: print('Receiving OSC packet!') sample = np.array(args).reshape(1, -1) self._process_eeg(sample[:, :self.n_channels], 0) def _update_eeg_python_osc(self, unused_addr, args, *chs): """Collect new EEG data point(s) from python-osc and process. Args: path (str): OSC path listened to args (list): received values """ if self.verbose: print('Receiving OSC packet!') sample = np.array(chs).reshape(1, -1) self._process_eeg(sample[:, :self.n_channels], 0) def _update_acc_liblo_osc(self, path, args): if self.verbose: print('Receiving ACC packet!') sample = np.array(args).reshape(1, -1) self._process_acc(sample[:, :3], 0) def _update_gyro_liblo_osc(self, path, args): if self.verbose: print('Receiving GYRO packet!') sample = np.array(args).reshape(1, -1) self._process_gyro(sample[:, :3], 0) def _process_eeg(self, samples, timestamp): """Process EEG. Process EEG. Includes buffering, filtering, windowing and pipeline. Args: samples (numpy.ndarray): new EEG samples to process timestamp (float): timestamp Returns: output (scalar): output of the pipeline """ # Re-map if self.eeg_ch_remap: samples = samples[:, self.eeg_ch_remap] self.eeg_buffer.update(samples) self._send_outputs(samples, timestamp, 'raw_eeg') # Apply filtes filt_samples = samples if config['filter']: filt_samples, self.bandpass_filt['zi'] = signal.lfilter( self.bandpass_filt['b'], self.bandpass_filt['a'], samples, axis=0, zi=self.bandpass_filt['zi']) # self._send_filtered_eeg(filt_samples, timestamp) self.filt_eeg_buffer.update(filt_samples) if config['hpfilter']: filt_samples, self.hp_filt['zi'] = signal.lfilter( self.hp_filt['b'], self.hp_filt['a'], filt_samples, axis=0, zi=self.hp_filt['zi']) self.hpfilt_eeg_buffer.update(filt_samples) if config['lpfilter']: smooth_eeg_samples, self.lp_filt['zi'] = signal.lfilter( self.lp_filt['b'], self.lp_filt['a'], filt_samples, axis=0, zi=self.lp_filt['zi']) if self.debug_outputs: self._send_output_vec(smooth_eeg_samples, timestamp, 'smooth_eeg') else: smooth_eeg_samples = filt_samples self.smooth_eeg_buffer.update(smooth_eeg_samples) if config['filter_bank']: filter_bank_samples = {} for name, filt_dict in self.filter_bank.items(): filter_bank_samples[name], self.filter_bank[name]['zi'] = \ signal.lfilter(filt_dict['b'], filt_dict['a'], filt_samples, axis=0, zi=self.filter_bank[name]['zi']) low_freq_chs = filter_bank_samples['delta'][0, [ 0, 2 ]] #+ filter_bank_samples['theta'][0, [0, 1]] window = self.smooth_eeg_buffer.extract(self.window_len) eegEarWindow = window[:, 3] #data from right ear Channel #eye movement computed from the difference between two frontal channels eyewindow = self.smooth_eeg_buffer.extract(200) eegFLWindow = eyewindow[:, 1] eegFRWindow = eyewindow[:, 2] # norm_diff_eyes = eegFLWindow[-1] - eegFRWindow[-1]*np.nanstd(eegFLWindow, axis=0)/np.nanstd(eegFRWindow, axis=0) # eyeH = np.reshape([np.square(norm_diff_eyes)], (1, 1)) #find blinks in the left eegEarWindow blinkVal = ut.blink_template_match(eegEarWindow) if (blinkVal > 100000 and self.blink == 0): self.blink = 50 self.blinkwait = 350 else: if (self.blinkwait > 0): self.blinkwait -= 1 if (self.blink > 0): self.blink -= 1 # LONGER-TERM CALM SCORE based on Saccadic Eye Movement eye_mov_percent = np.reshape( np.percentile(eegFLWindow - eegFRWindow, 90), (1, 1)) self.eye_mov_percent_buffer.update(eye_mov_percent) remap_eye_mov_percent = ut.sigmoid( self.eye_mov_percent_buffer.extract().mean(), 0.5, -10, 0) max_value = 1 incr_decr = remap_eye_mov_percent < 0.2 inc = self.increments_buffer.extract().mean() dpoints_per_second = 0.0005 if incr_decr: self.slow_calm_score += dpoints_per_second * inc # 1/max([max_value - self.slow_calm_score, 1]) else: self.slow_calm_score -= dpoints_per_second * inc * 4 #0.7 # (self.slow_calm_score)/1280 self.increments_buffer.update(np.reshape(incr_decr, (1, 1))) if self.slow_calm_score > max_value: self.slow_calm_score = max_value elif self.slow_calm_score < 0: self.slow_calm_score = 0 self.slow_calm_score_buffer.update( np.reshape(self.slow_calm_score, (1, 1))) # Send outputs at a reduced sampling rate if self.smooth_eeg_buffer.pts % 3 == 0: self._send_output_vec(smooth_eeg_samples, timestamp, 'muse/eeg') if (self.blink > 0): self._send_output(np.array([[1]]), timestamp, 'blink') else: self._send_output(np.array([[0]]), timestamp, 'blink') self._send_output(blinkVal / 300000, timestamp, 'blinkVal') self._send_output(remap_eye_mov_percent, timestamp, 'saccad') self._send_output( np.reshape(self.slow_calm_score_buffer.extract().mean(), (1, 1)), timestamp, 'calm') # slow_calm_score self._send_output(low_freq_chs / self.low_freq_chs_std + 0.5, timestamp, 'low_freq_chs') # process and send output at every step. usually about every 1/10s if self.eeg_buffer.pts > self.step: self.eeg_buffer.pts = 0 # Get filtered EEG window if config['lpfilter']: window = self.smooth_eeg_buffer.extract(self.window_len) else: window = self.eeg_buffer.extract(self.window_len) psd_raw_buffer = self.eeg_buffer.extract(self.window_len) # Get average PSD psd, f = ut.fft_continuous(psd_raw_buffer, n=int(self.fs), psd=True, log='psd', fs=self.fs, window='hamming') self.psd_buffer.update(np.expand_dims(psd, axis=0)) mean_psd = np.nanmean(self.psd_buffer.extract(), axis=0) # find variance of eegWindow for Bad Signal detact eegVar = np.nanvar(window, axis=0) self._send_output_vec(eegVar.reshape(1, self.n_channels), timestamp, 'hsi') if (self.sparseOutput != None): #send channel varience for signal quality indication at source Raspberry Pi #send(Address('10.0.0.14','1234'), "/hsi", eegVar[0],eegVar[1],eegVar[2],eegVar[3]) self._send_sparseOutput_vec(eegVar.reshape(1, self.n_channels), timestamp, 'hsi') # Get band powers and ratios bandPowers, bandNames = ut.compute_band_powers(mean_psd, f, relative=False) ratioPowers, ratioNames = ut.compute_band_ratios(bandPowers) if (self.firstWindowProc): self.band_powers = bandPowers self.band_names = bandNames self.ratio_powers = ratioPowers self.ratio_names = ratioNames self.scores = np.zeros((len(self.band_names), self.n_channels)) self.firstWindowProc = False if (eegVar.mean() < 300 and self.blinkwait == 0): #threshold for good data for i, (name, hist) in enumerate(self.hists.items()): self.band_powers = bandPowers self.ratio_powers = ratioPowers #send good data indicator based on mean eegWindow variance and blinkwait self._send_output(np.array([[1]]), timestamp, 'goodData') #good data else: self._send_output(np.array([[0]]), timestamp, 'goodData') #good data self._send_outputs(self.band_powers, timestamp, 'bands') self._send_outputs(self.ratio_powers, timestamp, 'ratios') mask = ((f >= 30) & (f < 50)) self.low_freq_chs_buffer.update(np.reshape(low_freq_chs, (1, -1))) self.low_freq_chs_std = self.low_freq_chs_buffer.extract().std( axis=0) emg_power = np.mean(mean_psd[mask, 0], axis=0) #HF power of right ear self._send_output(np.array([np.sqrt(emg_power) / 2]), timestamp, 'emg') def _process_acc(self, samples, timestamp): self._send_output_vec(samples, 0, 'muse/acc') self.acc_buffer.update(samples) window = self.acc_buffer.extract(self.acc_window_len) timestamps = np.linspace(0, 1 / 50 * self.acc_window_len, self.acc_window_len) new_fs = 250 timestamps_upsampled = np.arange(timestamps[0], timestamps[-1], 1 / new_fs) f = interpolate.interp1d(timestamps, window, kind='cubic', axis=0, fill_value=np.nan, assume_sorted=True) window_upsampled = f(timestamps_upsampled) for t in range(timestamps_upsampled.size - 5, timestamps_upsampled.size): if self.debug_outputs: self._send_output(window_upsampled[t], 0, 'upsamp') upsample = np.array(window_upsampled[t]).reshape(1, 3) filt_samples, self.filter0['zi'] = signal.lfilter( self.filter0['b'], self.filter0['a'], upsample, axis=0, zi=self.filter0['zi']) self.filt0_buffer.update(filt_samples) if self.debug_outputs: self._send_outputs(filt_samples, 0, 'filter0') filt_samples, self.filter1['zi'] = signal.lfilter( self.filter1['b'], self.filter1['a'], filt_samples, axis=0, zi=self.filter1['zi']) if self.debug_outputs: self._send_outputs(filt_samples, 0, 'filter1') filt_samples = np.sqrt(np.sum(filt_samples**2, axis=1)) if self.debug_outputs: self._send_output(filt_samples, 0, 'filter1L2') heart_samples, self.filter2['zi'] = signal.lfilter( self.filter2['b'], self.filter2['a'], filt_samples, axis=0, zi=self.filter2['zi']) if self.debug_outputs: self._send_output(heart_samples, 0, 'filter2') breathfilt_samples, self.filter3['zi'] = signal.lfilter( self.filter3['b'], self.filter3['a'], upsample, axis=0, zi=self.filter3['zi']) if self.debug_outputs: self._send_outputs(breathfilt_samples, 0, 'filter3') self.heart_buffer.update(heart_samples.reshape(1, 1)) heartbuf = self.heart_buffer.extract(150) heartbufMin = heartbuf.min() heartbufMax = heartbuf.max() heart = np.reshape( (heartbuf[-1] - heartbufMin) / (heartbufMax - heartbufMin), (1, 1)) self._send_output(heart, 0, 'heart') breathSmooth = breathfilt_samples[0, 2].reshape(1, ) if self.debug_outputs: self._send_output(breathSmooth, 0, 'breathRaw') breathSmooth, self.filter4['zi'] = signal.lfilter( self.filter4['b'], self.filter4['a'], breathSmooth, axis=0, zi=self.filter4['zi']) if self.debug_outputs: self._send_output(breathSmooth, 0, 'breathSmooth') breathNorm, self.filter5['zi'] = signal.lfilter(self.filter5['b'], self.filter5['a'], breathSmooth, axis=0, zi=self.filter5['zi']) if self.debug_outputs: self._send_output(breathNorm, 0, 'breathNorm') breathFast, self.filter6['zi'] = signal.lfilter(self.filter6['b'], self.filter6['a'], breathSmooth, axis=0, zi=self.filter6['zi']) if self.debug_outputs: self._send_output(breathFast, 0, 'breathFast') breathLow, self.filter7['zi'] = signal.lfilter(self.filter7['b'], self.filter7['a'], breathSmooth, axis=0, zi=self.filter7['zi']) if self.debug_outputs: self._send_output(breathLow, 0, 'breathLow') self.breath_buffer.update(breathLow.reshape(1, 1)) breathbuf = self.breath_buffer.extract(1000) breathbufMin = breathbuf.min() breathbufMax = breathbuf.max() breath = np.reshape( (breathbuf[-1] - breathbufMin) / (breathbufMax - breathbufMin), (1, 1)) self._send_output(breath, 0, 'breath') def _process_gyro(self, samples, timestamp): self._send_output_vec(samples, 0, 'muse/gyro') def _send_outputs(self, output, timestamp, name): """Send pipeline outputs through the LSL or OSC stream. Args: output (scalar): output of the pipeline timestamp (float): timestamp """ for out in self._output_threads: if isinstance(out, str): # LSL outlet self._outlet.push_sample([output], timestamp=timestamp) else: # OSC output stream if USE_LIBLO: for c in range(self.n_channels): new_output = [('f', x) for x in output[:, c]] message = Message('/{}{}'.format(name, c), *new_output) #send(out, Bundle(timestamp, message)) send(out, message) else: for c in range(self.n_channels): self._client.send_message('/{}{}'.format(name, c), output[:, c]) if self.verbose: print('Output: {}'.format(output)) def _send_output_vec(self, output, timestamp, name): """Send pipeline outputs through the LSL or OSC stream. Args: output (scalar): output of the pipeline timestamp (float): timestamp """ for out in self._output_threads: if isinstance(out, str): # LSL outlet self._outlet.push_sample([output], timestamp=timestamp) else: # OSC output stream if USE_LIBLO: new_output = [('f', x) for x in output[0, :]] message = Message('/{}'.format(name), *new_output) # send(out, Bundle(timestamp, message)) send(out, message) if self.verbose: print('Output: {}'.format(output)) def _send_sparseOutput_vec(self, output, timestamp, name): """Send pipeline outputs through the LSL or OSC stream. Args: output (scalar): output of the pipeline timestamp (float): timestamp """ for out in self._sparseOutput_threads: if isinstance(out, str): # LSL outlet self._outlet.push_sample([output], timestamp=timestamp) else: # OSC output stream if USE_LIBLO: new_output = [('f', x) for x in output[0, :]] message = Message('/{}'.format(name), *new_output) #send(out, Bundle(timestamp, message)) send(out, message) if self.verbose: print('sparseOutput: {}'.format(output)) def _send_output(self, output, timestamp, name): """Send pipeline outputs through the LSL or OSC stream. NOT PER CHANNEL Args: output (scalar): output of the pipeline timestamp (float): timestamp """ for out in self._output_threads: if isinstance(out, str): # LSL outlet raise NotImplementedError # self._outlet.push_sample([output], timestamp=timestamp) else: # OSC output stream if USE_LIBLO: if (np.array(output).size == 1): new_output = [('f', np.asscalar(output))] message = Message('/{}'.format(name), *new_output) else: new_output = [('f', x) for x in output[:]] message = Message('/{}'.format(name), *new_output) # send(out, Bundle(timestamp, message)) send(out, message) else: raise NotImplementedError # self._client.send_message(}{}'.format(name),output[:]) if self.verbose: print('Output: {}'.format(output)) def _send_sparseOutput(self, output, timestamp, name): for out in self._sparseOutput_threads: if isinstance(out, str): # LSL outlet raise NotImplementedError else: # OSC output stream if USE_LIBLO: if (np.array(output).size == 1): new_output = [('f', np.asscalar(output))] message = Message('/{}'.format(name), *new_output) else: new_output = [('f', x) for x in output[:]] message = Message('/{}'.format(name), *new_output) #send(out, Bundle(timestamp, message)) send(out, message) else: raise NotImplementedError if self.verbose: print('spareOutput: {}'.format(output)) def start(self): """Start receiving and processing EEG data. """ self.started = True if isinstance(self.incoming, str): # LSL inlet self.eeg_thread = Thread(target=self._update_eeg_lsl) self.eeg_thread.daemon = True self.eeg_thread.start() else: # OSC input stream if USE_LIBLO: self._osc_server.add_method('/muse/eeg', None, self._update_eeg_liblo_osc) self._osc_server.add_method('/muse/acc', None, self._update_acc_liblo_osc) self._osc_server.add_method('/muse/gyro', None, self._update_gyro_liblo_osc) self._osc_server.start() else: self._dispatcher.map('Person2/eeg', self._update_eeg_python_osc, 'EEG') self._osc_server = osc_server.ThreadingOSCUDPServer( ('127.0.0.1', self.incoming['port']), self._dispatcher) print('OSC server initialized at port {}.'.format( self.incoming['port'])) self._server_thread = Thread( target=self._osc_server.serve_forever) self._server_thread.start() def stop(self): """ """ self.started = False if isinstance(self.incoming, dict): if not USE_LIBLO: self._server_thread.shutdown()
def start(self): ServerThread.start(self) midiServer.run(midiServer.Call(self.processMIDI))
def __init__(self, parent, mode): ServerThread.__init__(self, 8087, mode) self.fParent = parent
def __init__( self, callback ): self.callback = callback ServerThread.__init__(self, 5001)
def __init__(self, host, mode): ServerThread.__init__(self, 8998 + int(random()*9000), mode) self.host = host
def __init__(self, muse_port, client_ip, client_port): ServerThread.__init__(self, muse_port) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.client_ip = client_ip self.client_port = client_port
def __init__(self, muse_port, began_at): # Configuring the Muse OSC client ServerThread.__init__(self, muse_port) self.began_at = began_at self.osc_paths = [{ 'address': "/muse/eeg", 'arguments': 4 }, { 'address': "/muse/eeg/quantization", 'arguments': 4 }, { 'address': "/muse/eeg/dropped_samples", 'arguments': 1 }, { 'address': "/muse/acc", 'arguments': 3 }, { 'address': "/muse/acc/dropped_samples", 'arguments': 1 }, { 'address': "/muse/batt", 'arguments': 4 }, { 'address': "/muse/drlref", 'arguments': 2 }, { 'address': "/muse/elements/low_freqs_absolute", 'arguments': 4 }, { 'address': "/muse/elements/delta_absolute", 'arguments': 4 }, { 'address': "/muse/elements/theta_absolute", 'arguments': 4 }, { 'address': "/muse/elements/alpha_absolute", 'arguments': 4 }, { 'address': "/muse/elements/beta_absolute", 'arguments': 4 }, { 'address': "/muse/elements/gamma_absolute", 'arguments': 4 }, { 'address': "/muse/elements/delta_relative", 'arguments': 4 }, { 'address': "/muse/elements/theta_relative", 'arguments': 4 }, { 'address': "/muse/elements/alpha_relative", 'arguments': 4 }, { 'address': "/muse/elements/beta_relative", 'arguments': 4 }, { 'address': "/muse/elements/gamma_relative", 'arguments': 4 }, { 'address': "/muse/elements/delta_session_score", 'arguments': 4 }, { 'address': "/muse/elements/theta_session_score", 'arguments': 4 }, { 'address': "/muse/elements/alpha_session_score", 'arguments': 4 }, { 'address': "/muse/elements/beta_session_score", 'arguments': 4 }, { 'address': "/muse/elements/gamma_session_score", 'arguments': 4 }, { 'address': "/muse/elements/touching_forehead", 'arguments': 1 }, { 'address': "/muse/elements/horseshoe", 'arguments': 4 }, { 'address': "/muse/elements/is_good", 'arguments': 4 }, { 'address': "/muse/elements/blink", 'arguments': 1 }, { 'address': "/muse/elements/jaw_clench", 'arguments': 1 }, { 'address': "/muse/elements/experimental/concentration", 'arguments': 1 }, { 'address': "/muse/elements/experimental/mellow", 'arguments': 1 }] self.paths = [path['address'] for path in self.osc_paths]