def _validate_flags(): """Returns True if flag values are valid or prints error and returns False.""" if FLAGS.list_ports: print "Input ports: '%s'" % ( "', '".join(midi_hub.get_available_input_ports())) print "Ouput ports: '%s'" % ( "', '".join(midi_hub.get_available_output_ports())) return False if FLAGS.bundle_files is None: print '--bundle_files must be specified.' return False if (FLAGS.end_call_control_number, FLAGS.phrase_bars).count(None) != 1: print('Exactly one of --end_call_control_number or --phrase_bars should be ' 'specified.') return False if (len(FLAGS.bundle_files.split(',')) > 1 and FLAGS.generator_select_control_number is None): print('If specifiying multiple bundle files (generators), ' '--generator_select_control_number must be specified.') return False return True
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not _validate_flags(): return # Load generators. generators = [] for bundle_file in FLAGS.bundle_files.split(','): generators.append(_load_generator_from_bundle_file(bundle_file)) if generators[-1] is None: return # Initialize MidiHub. if FLAGS.input_port not in midi_hub.get_available_input_ports(): print "Opening '%s' as a virtual MIDI port for input." % FLAGS.input_port if FLAGS.output_port not in midi_hub.get_available_output_ports(): print "Opening '%s' as a virtual MIDI port for output." % FLAGS.output_port hub = midi_hub.MidiHub(FLAGS.input_port, FLAGS.output_port, midi_hub.TextureType.MONOPHONIC) start_call_signal = ( None if FLAGS.start_call_control_number is None else midi_hub.MidiSignal(control=FLAGS.start_call_control_number, value=0)) end_call_signal = ( None if FLAGS.end_call_control_number is None else midi_hub.MidiSignal(control=FLAGS.end_call_control_number, value=0)) interaction = midi_interaction.CallAndResponseMidiInteraction( hub, generators, FLAGS.qpm, generator_select_control_number=FLAGS.generator_select_control_number, phrase_bars=FLAGS.phrase_bars, start_call_signal=start_call_signal, end_call_signal=end_call_signal, temperature_control_number=FLAGS.temperature_control_number) _print_instructions() interaction.start() try: while True: time.sleep(1) except KeyboardInterrupt: interaction.stop() print 'Interaction stopped.'
def _validate_flags(): """Returns True if flag values are valid or prints error and returns False.""" if FLAGS.list_ports: print "Input ports: '%s'" % ("', '".join( midi_hub.get_available_input_ports())) print "Ouput ports: '%s'" % ("', '".join( midi_hub.get_available_output_ports())) return False if FLAGS.bundle_files is None: print '--bundle_files must be specified.' return False if (len(FLAGS.bundle_files.split(',')) > 1 and FLAGS.generator_select_control_number is None): tf.logging.warning( 'You have specified multiple bundle files (generators), without ' 'setting `--generator_select_control_number`. You will only be able to ' 'use the first generator (%s).', FLAGS.bundle_files[0]) return True
def _validate_flags(): """Returns True if flag values are valid or prints error and returns False.""" if FLAGS.list_ports: print("Input ports: '%s'" % ( "', '".join(midi_hub.get_available_input_ports()))) print("Ouput ports: '%s'" % ( "', '".join(midi_hub.get_available_output_ports()))) return False if FLAGS.bundle_files is None: print('--bundle_files must be specified.') return False if (len(FLAGS.bundle_files.split(',')) > 1 and FLAGS.generator_select_control_number is None): tf.logging.warning( 'You have specified multiple bundle files (generators), without ' 'setting `--generator_select_control_number`. You will only be able to ' 'use the first generator (%s).', FLAGS.bundle_files[0]) return True
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not _validate_flags(): return # Load generators. generators = [] for bundle_file in FLAGS.bundle_files.split(','): generators.append(_load_generator_from_bundle_file(bundle_file)) if generators[-1] is None: return # Initialize MidiHub. if FLAGS.input_port not in midi_hub.get_available_input_ports(): print "Opening '%s' as a virtual MIDI port for input." % FLAGS.input_port if FLAGS.output_port not in midi_hub.get_available_output_ports(): print "Opening '%s' as a virtual MIDI port for output." % FLAGS.output_port hub = midi_hub.MidiHub(FLAGS.input_port, FLAGS.output_port, midi_hub.TextureType.MONOPHONIC, passthrough=FLAGS.passthrough, playback_channel=FLAGS.playback_channel, playback_offset=FLAGS.playback_offset) if FLAGS.clock_control_number is None: # Set the tick duration to be a single bar, assuming a 4/4 time signature. clock_signal = None tick_duration = 4 * (60. / FLAGS.qpm) else: clock_signal = midi_hub.MidiSignal(control=FLAGS.clock_control_number, value=127) tick_duration = None end_call_signal = (None if FLAGS.end_call_control_number is None else midi_hub.MidiSignal( control=FLAGS.end_call_control_number, value=127)) panic_signal = (None if FLAGS.panic_control_number is None else midi_hub.MidiSignal(control=FLAGS.panic_control_number, value=127)) mutate_signal = (None if FLAGS.mutate_control_number is None else midi_hub.MidiSignal(control=FLAGS.mutate_control_number, value=127)) interaction = midi_interaction.CallAndResponseMidiInteraction( hub, generators, FLAGS.qpm, FLAGS.generator_select_control_number, clock_signal=clock_signal, tick_duration=tick_duration, end_call_signal=end_call_signal, panic_signal=panic_signal, mutate_signal=mutate_signal, allow_overlap=FLAGS.allow_overlap, enable_metronome=FLAGS.enable_metronome, min_listen_ticks_control_number=FLAGS.min_listen_ticks_control_number, max_listen_ticks_control_number=FLAGS.max_listen_ticks_control_number, response_ticks_control_number=FLAGS.response_ticks_control_number, tempo_control_number=FLAGS.tempo_control_number, temperature_control_number=FLAGS.temperature_control_number, loop_control_number=FLAGS.loop_control_number, state_control_number=FLAGS.state_control_number) _print_instructions() interaction.start() try: while True: time.sleep(1) except KeyboardInterrupt: interaction.stop() print 'Interaction stopped.'
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not _validate_flags(): return # Load generators. generators = [] for bundle_file in FLAGS.bundle_files.split(','): generators.append(_load_generator_from_bundle_file(bundle_file)) if generators[-1] is None: return # Initialize MidiHub. if FLAGS.input_port not in midi_hub.get_available_input_ports(): print "Opening '%s' as a virtual MIDI port for input." % FLAGS.input_port if FLAGS.output_port not in midi_hub.get_available_output_ports(): print "Opening '%s' as a virtual MIDI port for output." % FLAGS.output_port hub = midi_hub.MidiHub(FLAGS.input_port, FLAGS.output_port, midi_hub.TextureType.MONOPHONIC, passthrough=FLAGS.passthrough, playback_channel=FLAGS.playback_channel, playback_offset=FLAGS.playback_offset) if FLAGS.clock_control_number is None: # Set the tick duration to be a single bar, assuming a 4/4 time signature. clock_signal = None tick_duration = 4 * (60. / FLAGS.qpm) else: clock_signal = midi_hub.MidiSignal( control=FLAGS.clock_control_number, value=127) tick_duration = None end_call_signal = ( None if FLAGS.end_call_control_number is None else midi_hub.MidiSignal(control=FLAGS.end_call_control_number, value=127)) panic_signal = ( None if FLAGS.panic_control_number is None else midi_hub.MidiSignal(control=FLAGS.panic_control_number, value=127)) mutate_signal = ( None if FLAGS.mutate_control_number is None else midi_hub.MidiSignal(control=FLAGS.mutate_control_number, value=127)) interaction = midi_interaction.CallAndResponseMidiInteraction( hub, generators, FLAGS.qpm, FLAGS.generator_select_control_number, clock_signal=clock_signal, tick_duration=tick_duration, end_call_signal=end_call_signal, panic_signal=panic_signal, mutate_signal=mutate_signal, allow_overlap=FLAGS.allow_overlap, enable_metronome=FLAGS.enable_metronome, min_listen_ticks_control_number=FLAGS.min_listen_ticks_control_number, max_listen_ticks_control_number=FLAGS.max_listen_ticks_control_number, response_ticks_control_number=FLAGS.response_ticks_control_number, tempo_control_number=FLAGS.tempo_control_number, temperature_control_number=FLAGS.temperature_control_number, loop_control_number=FLAGS.loop_control_number, state_control_number=FLAGS.state_control_number) _print_instructions() interaction.start() try: while True: time.sleep(1) except KeyboardInterrupt: interaction.stop() print 'Interaction stopped.'
def main(unused_argv): if FLAGS.list_ports: print "Input ports: '%s'" % ( "', '".join(midi_hub.get_available_input_ports())) print "Ouput ports: '%s'" % ( "', '".join(midi_hub.get_available_output_ports())) return if FLAGS.bundle_file is None: print '--bundle_file must be specified.' return if (FLAGS.end_call_control_number, FLAGS.phrase_bars).count(None) != 1: print('Exactly one of --end_call_control_number or --phrase_bars should be ' 'specified.') return try: bundle = magenta.music.sequence_generator_bundle.read_bundle_file( FLAGS.bundle_file) except magenta.music.sequence_generator_bundle.GeneratorBundleParseException: print 'Failed to parse bundle file: %s' % FLAGS.bundle_file return generator_id = bundle.generator_details.id if generator_id not in _GENERATOR_FACTORY_MAP: print "Unrecognized SequenceGenerator ID '%s' in bundle file: %s" % ( generator_id, FLAGS.bundle_file) return generator = _GENERATOR_FACTORY_MAP[generator_id].create_generator( checkpoint=None, bundle=bundle) generator.initialize() print "Loaded '%s' generator bundle from file '%s'." % ( bundle.generator_details.id, FLAGS.bundle_file) if FLAGS.input_port not in midi_hub.get_available_input_ports(): print "Opening '%s' as a virtual MIDI port for input." % FLAGS.input_port if FLAGS.output_port not in midi_hub.get_available_output_ports(): print "Opening '%s' as a virtual MIDI port for output." % FLAGS.output_port hub = midi_hub.MidiHub(FLAGS.input_port, FLAGS.output_port, midi_hub.TextureType.MONOPHONIC) end_call_signal = (None if FLAGS.end_call_control_number is None else midi_hub.MidiSignal(control=FLAGS.end_call_control_number, value=0)) interaction = midi_interaction.CallAndResponseMidiInteraction( hub, FLAGS.qpm, generator, phrase_bars=FLAGS.phrase_bars, end_call_signal=end_call_signal) print '' print 'Instructions:' print 'Play when you hear the metronome ticking.' if FLAGS.phrase_bars is not None: print ('After %d bars (4 beats), Magenta will play its response.' % FLAGS.phrase_bars) print ('Once the response completes, the metronome will tick and you can ' 'play again.') else: print ('When you want to end the call phrase, signal control number %d ' 'with value 0' % FLAGS.end_call_control_number) print ('At the end of the current bar (4 beats), Magenta will play its ' 'response.') print ('Once the response completes, the metronome will tick and you can ' 'play again.') print '' print 'To end the interaction, press CTRL-C.' interaction.start() try: while True: time.sleep(1) except KeyboardInterrupt: interaction.stop() print 'Interaction stopped.'
def main(unused_argv): if FLAGS.list_ports: print "Input ports: '%s'" % ("', '".join( midi_hub.get_available_input_ports())) print "Ouput ports: '%s'" % ("', '".join( midi_hub.get_available_output_ports())) return if FLAGS.bundle_file is None: print '--bundle_file must be specified.' return if (FLAGS.end_call_control_number, FLAGS.phrase_bars).count(None) != 1: print( 'Exactly one of --end_call_control_number or --phrase_bars should be ' 'specified.') return try: bundle = magenta.music.sequence_generator_bundle.read_bundle_file( FLAGS.bundle_file) except magenta.music.sequence_generator_bundle.GeneratorBundleParseException: print 'Failed to parse bundle file: %s' % FLAGS.bundle_file return generator_id = bundle.generator_details.id if generator_id not in _GENERATOR_FACTORY_MAP: print "Unrecognized SequenceGenerator ID '%s' in bundle file: %s" % ( generator_id, FLAGS.bundle_file) return generator = _GENERATOR_FACTORY_MAP[generator_id].create_generator( checkpoint=None, bundle=bundle) generator.initialize() print "Loaded '%s' generator bundle from file '%s'." % ( bundle.generator_details.id, FLAGS.bundle_file) if FLAGS.input_port not in midi_hub.get_available_input_ports(): print "Opening '%s' as a virtual MIDI port for input." % FLAGS.input_port if FLAGS.output_port not in midi_hub.get_available_output_ports(): print "Opening '%s' as a virtual MIDI port for output." % FLAGS.output_port hub = midi_hub.MidiHub(FLAGS.input_port, FLAGS.output_port, midi_hub.TextureType.MONOPHONIC) end_call_signal = (None if FLAGS.end_call_control_number is None else midi_hub.MidiSignal( control=FLAGS.end_call_control_number, value=0)) interaction = midi_interaction.CallAndResponseMidiInteraction( hub, FLAGS.qpm, generator, phrase_bars=FLAGS.phrase_bars, end_call_signal=end_call_signal) print '' print 'Instructions:' print 'Play when you hear the metronome ticking.' if FLAGS.phrase_bars is not None: print('After %d bars (4 beats), Magenta will play its response.' % FLAGS.phrase_bars) print( 'Once the response completes, the metronome will tick and you can ' 'play again.') else: print( 'When you want to end the call phrase, signal control number %d ' 'with value 0' % FLAGS.end_call_control_number) print( 'At the end of the current bar (4 beats), Magenta will play its ' 'response.') print( 'Once the response completes, the metronome will tick and you can ' 'play again.') print '' print 'To end the interaction, press CTRL-C.' interaction.start() try: while True: time.sleep(1) except KeyboardInterrupt: interaction.stop() print 'Interaction stopped.'
def app(unused_argv): tf.logging.debug("Starting app") # Start action server action_server = ActionServer() action_server.start() # Init midi ports, keep direct references to output_ports for # direct sending without the hub player if platform.system() == "Windows": input_ports = [ port for port in midi_hub.get_available_input_ports() if MIDI_INPUT_PORT in port ] output_ports = [ port for port in midi_hub.get_available_output_ports() if MIDI_OUTPUT_PORT in port ] if len(input_ports) is not 1 or len(output_ports) is not 1: raise Exception(f"Need exactly 1 midi input ({input_ports}) " f"matching {MIDI_INPUT_PORT}" f"and 1 midi output port ({output_ports}) " f"matching {MIDI_OUTPUT_PORT}," f"you can use LoopMIDI for that") else: input_ports = [MIDI_INPUT_PORT] output_ports = [MIDI_OUTPUT_PORT] hub = midi_hub.MidiHub(input_ports, output_ports, None) output_port = hub._outport.ports[0] # Panic to stop all current messages (note off everywhere) [output_port.send(message) for message in mido.ports.panic_messages()] # Synchronise event for all the loopers, controlled by the metronome bar_start_event = threading.Event() # Common stuff qpm = 80 timing = Timing(qpm) loopers = [] try: # Init and start the loopers, they block on the event drum_looper = SequenceLooper("drums", bar_start_event, action_server, hub, "drum_kit_rnn", "drum_kit", timing, midi_channel=9, bar_per_loop=2) melody_looper = SequenceLooper("melody", bar_start_event, action_server, hub, "attention_rnn", "attention_rnn", timing, midi_channel=0, bar_per_loop=4) loopers.append(drum_looper) loopers.append(melody_looper) [looper.start() for looper in loopers] tf.logging.debug("Loopers started " + str([("drum_looper", drum_looper), ("melody_looper", melody_looper)])) # Start metronome (wait to make sure everything is started) time.sleep(1) metronome = Metronome(bar_start_event, timing) loopers.append(metronome) metronome.start() tf.logging.debug("Metronome started " + str([("metronome", metronome)])) # Wait for the loopers [looper.join() for looper in loopers] except KeyboardInterrupt: print("SIGINT received, stopping action server, loopers and stuff") action_server.stop() [looper.stop() for looper in loopers] return 1 return 0