def __init__(self, automode=True, timing_type="relative", original_tempo=False): self.time = 0.0 self.timeline = SequencedList() self.timescale = 1.0 self.midi_queues = {} self.tempo = 120.0 self.pretime = 0.1 # in seconds self.original_tempo = original_tempo self.triggers = dict() # list of triggering mode of every players self.timing_type = timing_type
def reset(self, players=None): if players == None: self.timeline = SequencedList() else: if type(players) != list: players = [players] for i in range(0, len(self.timeline)): t, c = self.timeline[i] if c[0] in players: del self.timeline[i] i -= 1 elif c[0] == "server" and c[2] in players: del self.timeline[i] i -= 1
def __init__(self, dates=[], states=[], \ label_type = Events.AbstractLabel, contents_type=Events.AbstractContents, event_type=Events.AbstractEvent): SequencedList.__init__(self, dates, states) self.label_type = label_type self.contents_type = contents_type self.event_type = event_type if type(label_type)==str or type(contents_type)==str or type(event_type)==str: if type(label_type)==str: self.label_type = getattr(Events, label_type) if type(contents_type)==str: self.contents_type = getattr(Events, contents_type) if type(event_type)==str: self.event_type = getattr(Events, event_type) self.infos = dict() self.available = True
def get_activity(self, date=None): if date == None: date = int(self.date) if self.available: self.available = 0 ztmp = np.array(self.zeta) vtmp = np.array(self.value) ttmp = deepcopy(self.transform) self.available = 1 ztmp += date - self.date vtmp *= np.exp(-np.divide(date - self.date, self.tau_mem_decay)) ztmp, vtmp, ttmp = self.clean_up(ztmp, vtmp, ttmp) return SequencedList(list(ztmp), list(zip(vtmp, ttmp))) else: return np.array([]), np.array([]), np.arra
def merge(self, pattern, memory_space=None, scheduler=None): #print '' #print '------BEGINNING MERGE-------' if len(pattern) == 0 or memory_space == None: return deepcopy(pattern) merged_pattern = SequencedList() states_list = [] current_index = -1 for i in range(len(pattern)): #print 'looop ',i #print 'current_index : ', current_index z, (v, t) = pattern[i] #print 'pattern at ', i, ' : ', z, v, t state, distance = self.memory_space.get_events(z) state, distance = state[0], distance[0] #print 'current state and distance : ', state, distance if current_index == -1: #print 'init loop' za, (va, ta) = pattern[i] merged_pattern.append(float(za), (float(va), deepcopy(ta))) states_list.append(state.index) current_index += 1 #print 'merged pattern : ', merged_pattern continue if state == None: #print 'no state....' continue if state.index == states_list[current_index]: #print 'conflicting states found' if t == merged_pattern[current_index][1][1]: #print "same transformations found" za, (va, ta) = merged_pattern[current_index] #print 'previous merged_pattern state : '; za, ta, va za = (za * va + z * v) / (v + va) va = v + va #print 'updating to ', za, ' and value ', va, 'and trasform ', ta merged_pattern[current_index] = za, (va, ta) #print 'new merged patter at', current_index, ' : ', merged_pattern[current_index] else: #print "different transformations" za, (va, ta) = merged_pattern[current_index] #print 'current merged pattern at ',current_index, ' : ', merged_pattern[current_index] za = (za * va + z * v) / (v + va) va = v + va #print 'before conversion', ta if type(ta) != list: ta = [ta] #print 'after conversion', ta cop = deepcopy(pattern[i][1][1]) #print 'original pattern : ', pattern[i][1][1] #print 'copy of original pattern : ', pattern[i][1][1] ta = ta + [cop] #print 'after mutation : ', ta #print 'updating to ', za, ' with transofrom ', va, ' and ta ', ta merged_pattern[current_index] = za, (va, ta) #print 'current merged pattern at ',current_index, ' : ', merged_pattern[current_index] else: #print 'different states' za, (va, ta) = pattern[i] merged_pattern.append(float(za), (float(va), deepcopy(ta))) states_list.append(state.index) #print 'appending ', state.index, ' to state list' current_index += 1 #print 'current merged pattern at ',current_index, ' : ', merged_pattern[current_index] return merged_pattern
def append(self, date, *args): event = self.build_event(*args) SequencedList.append(self, date, event)
class SomaxScheduler(object): def __init__(self, automode=True, timing_type="relative", original_tempo=False): self.time = 0.0 self.timeline = SequencedList() self.timescale = 1.0 self.midi_queues = {} self.tempo = 120.0 self.pretime = 0.1 # in seconds self.original_tempo = original_tempo self.triggers = dict() # list of triggering mode of every players self.timing_type = timing_type ###################################################### ###### TIMING METHODS # starting the scheduler def start(self, time): self.reset() self.time = time - self.get_pretime() # stopping the scheduler def stop(self): self.reset() # setting time of the scheduler ; returns events to handle def set_time(self, time): self.time = time events = self.pop_events(time) return events # time accesors for players and server def get_time(self): return self.time ###################################################### ###### WRITING METHODS # writing general data in the scheduler queue def write(self, player, time, *args): # writes events in scheduler self.timeline.insert(time, tuple([player]) + args) # writing an event object in the queue def write_event(self, time, player, event, automode=True): if not event: return factor = self.tempo if self.timing_type == "relative" else self.timescale content_object = event.get_contents() contents = content_object.get_contents(self.timing_type, factor) trig_mode = self.triggers[player] midiCheck = False if trig_mode == "automatic": next_time = time + content_object.get_state_length( self.timing_type, factor) self.write('server', next_time - self.get_pretime(), "ask_for_event", player, next_time) midiCheck = True tempos = [] if self.original_tempo: self.set_tempo(content_object.get_tempo()) if trig_mode == "reactive" and player in self.midi_queues: elts = self.midi_queues[player].new_slice(trig_mode) for elt in elts: self.write( player, time + content_object.get_state_length(self.timing_type, factor), elt['content']) for elt in contents: if elt['content'][0] == 'midi': if not player in self.midi_queues: self.midi_queues[player] = MIDIQueue() elt = self.midi_queues[player].process_midi_event( elt, trig_mode) if elt != None: time_elt = time + elt['time'][0] tempos.append(float(elt['time'][2])) self.write(player, time_elt, elt['content']) else: time_elt = time + elt['time'][0] event_tempo = elt['time'][2] self.write(player, time_elt, elt['content']) '''if self.originalTempo and tempo!=None: self.write(3, time, "set_tempo "+str(tempo))''' if midiCheck == True: elts = self.midi_queues[player].new_slice(trig_mode) for elt in elts: self.write( player, time + content_object.get_state_length(self.timing_type, factor), elt['content']) # if self.triggers[player]=="automatic": # print "writing event at [2]", time, "next planned event : ", content_object.get_state_length(self.timing_type, factor) def pop_events(self, time): def opgklm(x): if x[0] == 'internal': self.process_internal_event(x[1:]) return False else: return True toplay, self.timeline = self.timeline.truncate(time) events_to_outlet = toplay.get_events_list() return filter(opgklm, events_to_outlet) def process_internal_event(self, event): if event[0] == "ask_for_event": self.ask_for_event(self.time + self.pre_time) if event[0] == "print": if len(event) > 2: print event[2:] # external methods def set_original_tempo(self, original_tempo): self.original_tempo = original_tempo def set_tempo(self, tempo): self.tempo = tempo def set_timescale(self, timescale): self.timescale = timescale def reset(self, players=None): if players == None: self.timeline = SequencedList() else: if type(players) != list: players = [players] for i in range(0, len(self.timeline)): t, c = self.timeline[i] if c[0] in players: del self.timeline[i] i -= 1 elif c[0] == "server" and c[2] in players: del self.timeline[i] i -= 1 def get_pretime(self, timing_type=None): timing_type = self.timing_type if timing_type == None else timing_type if timing_type == 'relative': # print round(self.pretime*(self.tempo/60)*10)/10 return round(self.pretime * (self.tempo / 60) * 10) / 10 else: return self.pretime