class Expression(Block): args = List(Input) input = Input() def init(self, func, *args): # HACK: should not directly reference lupa here import lupa if lupa.lua_type(args) == 'table': args = args.values() print ('Expression: ', func, args) self.func = func self.args = list(args) self.output = Signal() # Workaround for list event bug self.input = self.args[0] def _input_changed(self): self.output.copy_traits(self.input, ['label', 'color']) def process(self): args = [chan.last for chan in self.args] self.output.append([self.func(*args)]) self.output.process()
class PulseAnalyzer(Block): input = Input() def init(self, input): self.input = input self.output = Signal() self.bpm = Signal() self.gradient = Signal() self.pulse = Signal() self.last_beat = -1 self.timestamp = 0 def _input_changed(self): self.output.copy_traits(self.input, ['label', 'color']) def process(self): avg = np.average(self.output.buffer) max = np.max(self.output.buffer) self.timestamp += 1 self.gradient.append([self.input.buffer[-1] - self.input.buffer[-2]]) buf = np.power(self.gradient.buffer[-50:], 2) s = np.sum(np.hanning(50)[:50] * buf) self.output.append([s]) for i, sample in enumerate(self.output.new): new = 1.0 if sample > max * 0.7 else 0.0 self.pulse.append([new]) if new > self.pulse.buffer[-2]: diff = self.timestamp - self.last_beat bpm = 1.0 / (diff / 250.) * 60 self.bpm.append([bpm]) self.bpm.process() print('beat event', diff, bpm, self.timestamp, self.last_beat) self.last_beat = self.timestamp self.output.process() self.gradient.process()
class PulseAnalyzer(Block): input = Input() def init(self, input): self.input = input self.output = Signal() self.bpm = Signal() self.gradient = Signal() self.pulse = Signal() self.last_beat = -1 self.timestamp = 0 def _input_changed(self): self.output.copy_traits(self.input, ["label", "color"]) def process(self): avg = np.average(self.output.buffer) max = np.max(self.output.buffer) self.timestamp += 1 self.gradient.append([self.input.buffer[-1] - self.input.buffer[-2]]) buf = np.power(self.gradient.buffer[-50:], 2) s = np.sum(np.hanning(50)[:50] * buf) self.output.append([s]) for i, sample in enumerate(self.output.new): new = 1.0 if sample > max * 0.7 else 0.0 self.pulse.append([new]) if new > self.pulse.buffer[-2]: diff = self.timestamp - self.last_beat bpm = 1.0 / (diff / 250.0) * 60 self.bpm.append([bpm]) self.bpm.process() print("beat event", diff, bpm, self.timestamp, self.last_beat) self.last_beat = self.timestamp self.output.process() self.gradient.process()
class RMS(Block): input = Input() avg_size = Int(42) def init(self, input): self.output = Signal() self.input = input def _input_changed(self): self.output.copy_traits(self.input, ['label', 'color']) def process(self): buf = np.array(self.input.buffer[-self.avg_size:]) rms = sum(buf ** 2) / len(buf) avg = np.sqrt(rms) self.output.append([avg]) self.output.process()