def impromptu_compose(self, unit, offset): self.compose_thread_mutex.acquire() self.synth.reset() self.patterns = {} self.tonalities = {} self.instruments = {} self.inpromptu = Impromptu(unit, offset) while True: if self.get_status() == 'stop': break self.composition_mutex.acquire() local_need_compose = self.need_compose next_index = self.current_playing_index + 1 local_mood = self.current_mood self.composition_mutex.release() if local_need_compose == True: (pattern, tonality, instruments) = self.inpromptu.compose(local_mood) dynamic_offset = Generator.gen_dynamic_parameter(local_mood) self.patterns[next_index] = pattern self.tonalities[next_index] = tonality self.instruments[next_index] = instruments samples = self.synth.convert_pattern_to_samples( pattern, instruments, unit, dynamic_offset) self.composition_mutex.acquire() self.next_samples = samples self.need_compose = False self.composition_mutex.release() else: sleep(0.01) MIDI.write_patterns('demo.mid', self.patterns, unit, self.tonalities, self.instruments) self.compose_thread_mutex.release()
def generate_melody_parameter(self, mood): param_basic = { 'tempo': Generator.gen_tempo_parameter(mood), 'pitch': Generator.gen_pitch_parameter(mood, 'melody'), 'dynamic': Generator.gen_dynamic_parameter(mood), 'mode': Generator.gen_mode_parameter(self.tonality, mood), } param_effect = Generator.gen_effect_parameter(mood, 'melody') return dict(param_basic.items() + param_effect.items())