Exemplo n.º 1
0
 def _load_model(name):
     # Load the model from its file
     return HPChordLabeler.load_model(name)
Exemplo n.º 2
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)
Exemplo n.º 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)
Exemplo n.º 4
0
 
 # Work out how many results to print out
 if options.print_results == -1:
     print_up_to = None
 else:
     print_up_to = options.print_results
     
 
 # Process the labeler options
 lopt_dict = ModuleOption.process_option_string(options.labeler_options)
 # No point in getting more than one label, since we'll only use one
 lopt_dict['viterbi'] = True
 lopt_dict['nokey'] = True
 # Load the chord labeling model
 model_name = arguments[1]
 model = HPChordLabeler.load_model(model_name)
 
 ranks = []
 num_ranked = 0
 for midi_file in input_data:
     # Skip any inputs that don't have a gold sequence associated with them
     # We won't know what the correct answer is
     if options.gold_only and midi_file.gold is None:
         continue
     
     print "######################"
     print "Processing %s" % midi_file.name
     
     # Try to get a correct answer from the PR file
     if midi_file.gold is None:
         print "No correct answer specified in input file"