def test_input_types(self):
     # Check INPUT_TYPES doesn't have bulk input types
     for datatype, cls in INPUT_TYPES:
         self.assertFalse(is_bulk_type(cls))
         # Check there are no duplicate names
     names, __ = zip(*(INPUT_TYPES + BULK_INPUT_TYPES))
     self.assertEqual(len(names), len(set(names)), msg="Duplicate input type names")
Beispiel #2
0
 def test_input_types(self):
     # Check INPUT_TYPES doesn't have bulk input types
     for datatype, cls in INPUT_TYPES:
         self.assertFalse(is_bulk_type(cls))
     # Check there are no duplicate names
     names, __ = zip(*(INPUT_TYPES + BULK_INPUT_TYPES))
     self.assertEqual(len(names),
                      len(set(names)),
                      msg="Duplicate input type names")
Beispiel #3
0
def main():
    usage = "%prog [options] <model_name> <in-file>"
    description = "Loads a chord labeling model and uses it to assign chord "\
        "labels to the given MIDI file."
    parser = OptionParser(usage=usage, description=description)
    # File input options
    parser.add_option(
        "--filetype",
        "--ft",
        dest="filetype",
        action="store",
        help=
        "select the file type for the input file. Same filetypes as jazzparser",
        default='segmidi')
    parser.add_option(
        "--file-options",
        "--fopt",
        dest="file_options",
        action="store",
        help=
        "options for the input file. Type '--fopt help', using '--ft <type>' to select file type, for a list of available options."
    )
    # Labeling options
    parser.add_option(
        "--labeler-options",
        "--lopt",
        dest="labeler_options",
        action="append",
        help=
        "options for the labeler. Type '--lopt help' for a list of available options."
    )
    parser.add_option(
        "--no-key",
        "--nk",
        dest="no_key",
        action="store_true",
        help="merge together labels with the same key (same as --lopt nokey)")
    # Output options
    parser.add_option(
        "--single",
        "-1",
        dest="single",
        action="store_true",
        help=
        "show only one chord per time segment (same as --lopt n=1, but formats the output in a simpler way)"
    )
    parser.add_option(
        '-r',
        '--realize',
        dest="realize",
        action="store",
        help="realize the chord sequence as a midi file, overlaid on the input"
    )
    parser.add_option(
        '--chords-only',
        dest="chords_only",
        action="store_true",
        help=
        "only realize the chords: don't overlay on the input midi (only works with -r)"
    )
    options, arguments = parse_args_with_config(parser)

    if options.labeler_options is not None and "help" in options.labeler_options:
        print options_help_text(HPChordLabeler.LABELING_OPTIONS,
                                intro="Options for HP chord labeler")
        sys.exit(0)

    if len(arguments) < 2:
        print >>sys.stderr, "You must specify a model name and an input "\
            "(MIDI) data file as arguments"
        sys.exit(1)
    filename = os.path.abspath(arguments[1])
    model_name = arguments[0]

    # Process the labeler options
    lopt_dict = ModuleOption.process_option_string(options.labeler_options)
    if options.single:
        # No point in getting more than one label, since we only display one
        lopt_dict['n'] = 1
    if options.no_key:
        # Just set the nokey option
        lopt_dict['nokey'] = True

    # Check they're valid before doing anything else
    HPChordLabeler.process_labeling_options(lopt_dict)

    input_data = command_line_input(filename,
                                    filetype=options.filetype,
                                    options=options.file_options,
                                    allowed_types=['segmidi', 'bulk-segmidi'])
    bulk = not is_bulk_type(type(input_data))
    if bulk:
        input_data = [input_data]

    for i, data in enumerate(input_data):
        input_stream = data.stream
        print "Read midi data in %d segments" % len(data)

        # Load the model
        model = HPChordLabeler.load_model(model_name)
        # Perform labeling
        labels = model.label(data, options=lopt_dict)
        # Try labeling as it will be passed to the tagger
        labs = model.label_lattice(data, options=lopt_dict)

        if options.single:
            # Special output for single label output
            print ", ".join(["%s" % timelabs[0][0] for timelabs in labels])
        else:
            # Print out the labels for each timestep
            for time, timelabs in enumerate(labels):
                print "%d: %s" % (time, ", ".join([
                    "%s (%.2e)" % (label, prob) for (label, prob) in timelabs
                ]))

        if options.realize is not None:
            # Get the single best chord label for each time
            best_labels = [timelabs[0][0] for timelabs in labels]
            # Realize as a midi file
            print "Realizing output chord sequence"
            real = ChordSequenceRealizer(best_labels,
                                         model.chord_vocab,
                                         resolution=input_stream.resolution,
                                         chord_length=data.time_unit,
                                         text_events=True)
            if options.chords_only:
                # Don't overlay
                stream = real.generate(offset=data.tick_offset)
            else:
                stream = real.generate(overlay=input_stream,
                                       offset=data.tick_offset)

            if bulk:
                filename = "%s-%d" % (options.realize, i)
            else:
                filename = options.realize
            write_midifile(stream, filename)
Beispiel #4
0
def main():
    usage = "%prog [options] <model_name> <in-file>"
    description = "Loads a chord labeling model and uses it to assign chord "\
        "labels to the given MIDI file."
    parser = OptionParser(usage=usage, description=description)
    # File input options
    parser.add_option("--filetype", "--ft", dest="filetype", action="store", help="select the file type for the input file. Same filetypes as jazzparser", default='segmidi')
    parser.add_option("--file-options", "--fopt", dest="file_options", action="store", help="options for the input file. Type '--fopt help', using '--ft <type>' to select file type, for a list of available options.")
    # Labeling options
    parser.add_option("--labeler-options", "--lopt", dest="labeler_options", action="append", help="options for the labeler. Type '--lopt help' for a list of available options.")
    parser.add_option("--no-key", "--nk", dest="no_key", action="store_true", help="merge together labels with the same key (same as --lopt nokey)")
    # Output options
    parser.add_option("--single", "-1", dest="single", action="store_true", help="show only one chord per time segment (same as --lopt n=1, but formats the output in a simpler way)")
    parser.add_option('-r', '--realize', dest="realize", action="store", help="realize the chord sequence as a midi file, overlaid on the input")
    parser.add_option('--chords-only', dest="chords_only", action="store_true", help="only realize the chords: don't overlay on the input midi (only works with -r)")
    options, arguments = parse_args_with_config(parser)
    
    if options.labeler_options is not None and "help" in options.labeler_options:
        print options_help_text(HPChordLabeler.LABELING_OPTIONS, intro="Options for HP chord labeler")
        sys.exit(0)
        
    if len(arguments) < 2:
        print >>sys.stderr, "You must specify a model name and an input "\
            "(MIDI) data file as arguments"
        sys.exit(1)
    filename = os.path.abspath(arguments[1])
    model_name = arguments[0]
    
    # Process the labeler options
    lopt_dict = ModuleOption.process_option_string(options.labeler_options)
    if options.single:
        # No point in getting more than one label, since we only display one
        lopt_dict['n'] = 1
    if options.no_key:
        # Just set the nokey option
        lopt_dict['nokey'] = True
    
    # Check they're valid before doing anything else
    HPChordLabeler.process_labeling_options(lopt_dict)
    
    input_data = command_line_input(filename, 
                                    filetype=options.filetype, 
                                    options=options.file_options,
                                    allowed_types=['segmidi','bulk-segmidi'])
    bulk = not is_bulk_type(type(input_data))
    if bulk:
        input_data = [input_data]
        
    for i,data in enumerate(input_data):
        input_stream = data.stream
        print "Read midi data in %d segments" % len(data)
        
        # Load the model
        model = HPChordLabeler.load_model(model_name)
        # Perform labeling
        labels = model.label(data, options=lopt_dict)
        # Try labeling as it will be passed to the tagger
        labs = model.label_lattice(data, options=lopt_dict)
        
        if options.single:
            # Special output for single label output
            print ", ".join(["%s" % timelabs[0][0] for timelabs in labels])
        else:
            # Print out the labels for each timestep
            for time,timelabs in enumerate(labels):
                print "%d: %s" % (time, 
                    ", ".join(["%s (%.2e)" % (label,prob) for (label,prob) in timelabs]))
        
        if options.realize is not None:
            # Get the single best chord label for each time
            best_labels = [timelabs[0][0] for timelabs in labels]
            # Realize as a midi file
            print "Realizing output chord sequence"
            real = ChordSequenceRealizer(best_labels, 
                                         model.chord_vocab, 
                                         resolution=input_stream.resolution, 
                                         chord_length=data.time_unit,
                                         text_events=True)
            if options.chords_only:
                # Don't overlay
                stream = real.generate(offset=data.tick_offset)
            else:
                stream = real.generate(overlay=input_stream, offset=data.tick_offset)
                
            if bulk:
                filename = "%s-%d" % (options.realize, i)
            else:
                filename = options.realize
            write_midifile(stream, filename)
 def test_bulk_input_types(self):
     # Check BULK_INPUT_TYPES is only bulk types
     for datatype, cls in BULK_INPUT_TYPES:
         self.assertTrue(is_bulk_type(cls))
Beispiel #6
0
 if input_data is None:
     # No input file: process command line input
     input_string = " ".join(clinput)
     input_list = [input_string]
     name_getter = iter(["commandline"])
     # Take input from stdin if nothing else is given
     if len(input_string) == 0:
         stdinput = True
         # Use integers to identify each input
         name_getter = count()
         num_inputs = None
     else:
         num_inputs = 1
 else:
     # Input file was given
     if is_bulk_type(type(input_data)):
         # If this is a bulk filetype, we can just iterate over it
         input_list = input_data
         # Get the bulk input to supply names
         name_getter = iter(input_data.get_identifiers())
         num_inputs = len(input_data)
     else:
         # Otherwise, there's just one input
         input_list = [input_data]
         num_inputs = 1
         # Try getting a name for this
         if input_data.name is None:
             name = "unnamed"
         else:
             name = input_data.name
         name_getter = iter([name])
Beispiel #7
0
 if input_data is None:
     # No input file: process command line input
     input_string = " ".join(clinput)
     input_list = [input_string]
     name_getter = iter(["commandline"])
     # Take input from stdin if nothing else is given
     if len(input_string) == 0:
         stdinput = True
         # Use integers to identify each input
         name_getter = count()
         num_inputs = None
     else:
         num_inputs = 1
 else:
     # Input file was given
     if is_bulk_type(type(input_data)):
         # If this is a bulk filetype, we can just iterate over it
         input_list = input_data
         # Get the bulk input to supply names
         name_getter = iter(input_data.get_identifiers())
         num_inputs = len(input_data)
     else:
         # Otherwise, there's just one input
         input_list = [input_data]
         num_inputs = 1
         # Try getting a name for this
         if input_data.name is None:
             name = "unnamed"
         else:
             name = input_data.name
         name_getter = iter([name])
Beispiel #8
0
 def test_bulk_input_types(self):
     # Check BULK_INPUT_TYPES is only bulk types
     for datatype, cls in BULK_INPUT_TYPES:
         self.assertTrue(is_bulk_type(cls))