for phase_d in range(5): if phase_d in [phase_a, phase_b, phase_c]: continue for phase_e in range(5): if phase_e in [phase_a, phase_b, phase_c, phase_d]: continue amp_a.reset() amp_b.reset() amp_c.reset() amp_d.reset() amp_e.reset() amp_a.run([phase_a, 0]) amp_b.run([phase_b, amp_a.output()]) amp_c.run([phase_c, amp_b.output()]) amp_d.run([phase_d, amp_c.output()]) amp_e.run([phase_e, amp_d.output()]) if amp_e.output() > max_output: max_output = amp_e.output() print("Part 1:", max_output) max_output = 0 for phase_a in range(5, 10): for phase_b in range(5, 10): if phase_b in [phase_a]:
class FMOperator(Oscillator): """A class to represent an FM operator""" def __init__( self, freq=440.0, level=1.0, phase=0.0, feedback=0, wave_type='sine', fm_type='DX', gate=Gate([0.0]), key_in=None ): Oscillator.__init__(self, key_in) self.freq = freq #frequency self.mixer = Mixer() #mixer to add modulators' outputs #sound generator if fm_type == 'LinearFM': self.generator = LinearFMGenerator( freq, level, phase, wave_type, self.mixer, key_in ) elif fm_type == 'DX': self.generator = DXGenerator( freq, level, phase, wave_type, self.mixer, key_in ) self.eg = ADSR(input=gate) #envelope generator #amplifier self.amp = Amplifier(input=self.generator, mod=self.eg) def add_modulator(self, mod, level=1.0): """Adds a modulator""" self.mixer.add_input(mod, level) def set_key_in(self, key_in): """Sets key input""" self.key_in = key_in self.generator.set_key_in(key_in) def set_eg_params(self, *args, **kwargs): """Sets parameters of the envelope""" self.eg.set_params(*args, **kwargs) def set_gate(self, gate): """Sets the gate input""" self.eg.set_input(gate) def set_keyboard(self, keyboard): """sets key and gate input from a keybord""" self.set_key_in(keyboard.key) self.set_gate(keyboard.gate) def output(self, t, **kwargs): """Returns the value of operators signal in time t""" return self.amp.output(t, **kwargs) def draw(self, ax, time=None, cycles=1, **kwargs): """ Draws the shape of the operators output signal along with its modulators and modulators' modulators etc. If 'time' is not provided, the shape will be drawn for 'cycles' cycles of operators carrier generator """ #draw the operators output signal Oscillator.draw(self, ax, time, cycles, **kwargs) #draw modulators' output signals if time is None: time = cycles / np.float64(self.freq) try: kwargs['alpha'] *= 0.5 except KeyError: kwargs['alpha'] = 0.5 self.mixer.draw(ax, time, **kwargs)