示例#1
0
def main():
    usage = "%prog [options] <midi-file>"
    description = "Divides a midi file into chunks, with a given size and "\
        "offset, and print the chunks consecutively."
    parser = OptionParser(usage=usage, description=description)
    parser.add_option('-t', '--time-unit', dest="time_unit", action="store", type="float", help="size of chunks in crotchet beats (according to the midi file's resolution)", default=4)
    parser.add_option('-o', '--tick-offset', dest="tick_offset", action="store", type="int", help="offset of the first chunk in midi ticks", default=0)
    parser.add_option('--force-res', dest="force_res", action="store", type="int", help="force the midi file's resolution to be the given value, rather than using that read from the file")
    parser.add_option('-i', "--tick-times", dest="tick_times", action="store_true", help="show times as tick values, rather than proportions of the chunk")
    options, arguments = parse_args_with_config(parser)
    
    filename = arguments[0]
    
    # Load up the input midi file
    infile = read_midifile(filename, force_resolution=options.force_res)
    handler = MidiHandler(infile,
                          time_unit=options.time_unit,
                          tick_offset=options.tick_offset)
    slices = handler.get_slices()
    
    print "Printing %d-beat chunks with a %d-tick offset" % (options.time_unit, options.tick_offset)
    print "Total chunks: %d" % len(slices)
    print
    
    chunk_length = options.time_unit * infile.resolution
    
    for i,slc in enumerate(slices):
        strm = slc.to_event_stream()
        # Print the header for this chunk
        print "Chunk %d: %d-%d (%d events)" % \
                (i, slc.start, slc.end,len(strm.trackpool))
        print "".join(str(i).ljust(2) for i in range(11)), \
                "Time   ", "Vel", "Ch", "Tr"
        
        # Only show note-on events
        noteons = [ev for ev in sorted(strm.trackpool) \
                    if type(ev) == NoteOnEvent and ev.velocity > 0]
        # Sorted by time: within same tick, sort by pitch
        for k,grp in groupby(noteons):
            for ev in sorted(list(grp), key=lambda e:e.pitch):
                # Display all the information for this note
                octave = ev.pitch / 12
                name = constants.NOTE_NAMES[ev.pitch % 12].ljust(2)
                indent = "  " * octave
                fill = "  " * (10-octave)
                if options.tick_times:
                    time = str(ev.tick+slc.start).ljust(7)
                else:
                    time = ("%.1f%%" % (100.0 * ev.tick / chunk_length)).ljust(7)
                channel = str(ev.channel).ljust(2)
                track = str(ev.track).ljust(2)
                velocity = str(ev.velocity).ljust(3)
                
                print "%s%s%s %s %s %s %s" % \
                        (indent, name, fill, time, velocity, channel, track)
        print
示例#2
0
def main():
    usage = "%prog [options] <midi-file>"
    description = "Divides a midi file into chunks, with a given size and "\
        "offset, and plays "\
        "the chunks consecutively, with a gap between each."
    parser = OptionParser(usage=usage, description=description)
    parser.add_option('-t', '--time-unit', dest="time_unit", action="store", type="float", help="size of chunks in crotchet beats (according to the midi file's resolution)", default=4)
    parser.add_option('-o', '--tick-offset', dest="tick_offset", action="store", type="int", help="offset of the first chunk in midi ticks", default=0)
    parser.add_option('-g', '--gap', dest="gap", action="store", type="float", help="time to wait between playing each chunk in seconds (potentially float). It will take some time to load the chunk and the sequencer usually pauses before reporting it's done: this is not included in this value", default=0.0)
    parser.add_option('-p', '--print', dest="print_events", action="store_true", help="print out all events for each chunk")
    parser.add_option('--pno', '--print-note-ons', dest="print_note_ons", action="store_true", help="print only note-on events")
    parser.add_option('--force-res', dest="force_res", action="store", type="int", help="force the midi file's resolution to be the given value, rather than using that read from the file")
    parser.add_option('-s', '--start', dest="start", action="store", type="int", help="chunk number to start at", default=0)
    options, arguments = parse_args_with_config(parser)
    
    filename = arguments[0]
    
    # Load up the input midi file
    infile = read_midifile(filename, force_resolution=options.force_res)
    handler = MidiHandler(infile,
                          time_unit=options.time_unit,
                          tick_offset=options.tick_offset)
    slices = handler.get_slices()
    
    # Start at the requested chunk
    slices = slices[options.start:]
    
    print "Playing %d-beat chunks with a %d-tick offset" % (options.time_unit, options.tick_offset)
    if options.start > 0:
        print "Start from chunk %d" % options.start
    print "Total chunks: %d" % len(slices)
    print "Ctrl+C to exit"
    print
    
    try:
        for i,slc in enumerate(slices):
            strm = slc.to_event_stream(cancel_playing=True)
            print "Playing chunk %d: %d-%d (%d events)" % (i, slc.start, slc.end,len(strm.trackpool))
            if options.print_events:
                print "\n".join("  %s" % ev for ev in sorted(strm.trackpool))
            elif options.print_note_ons:
                print "\n".join("  %s" % ev for ev in sorted(strm.trackpool) \
                                                    if type(ev) is NoteOnEvent)
            
            play_stream(strm, block=True)
            if options.gap > 0.0:
                print "  Waiting %s seconds..." % options.gap
                time.sleep(options.gap)
    except KeyboardInterrupt:
        print "Exiting"
示例#3
0
def main():
    usage = "%prog [options] <midi-file>"
    description = "Divides a midi file into chunks, with a given size and "\
        "offset, and print the chunks consecutively."
    parser = OptionParser(usage=usage, description=description)
    parser.add_option(
        '-t',
        '--time-unit',
        dest="time_unit",
        action="store",
        type="float",
        help=
        "size of chunks in crotchet beats (according to the midi file's resolution)",
        default=4)
    parser.add_option('-o',
                      '--tick-offset',
                      dest="tick_offset",
                      action="store",
                      type="int",
                      help="offset of the first chunk in midi ticks",
                      default=0)
    parser.add_option(
        '--force-res',
        dest="force_res",
        action="store",
        type="int",
        help=
        "force the midi file's resolution to be the given value, rather than using that read from the file"
    )
    parser.add_option(
        '-i',
        "--tick-times",
        dest="tick_times",
        action="store_true",
        help="show times as tick values, rather than proportions of the chunk")
    options, arguments = parse_args_with_config(parser)

    filename = arguments[0]

    # Load up the input midi file
    infile = read_midifile(filename, force_resolution=options.force_res)
    handler = MidiHandler(infile,
                          time_unit=options.time_unit,
                          tick_offset=options.tick_offset)
    slices = handler.get_slices()

    print "Printing %d-beat chunks with a %d-tick offset" % (
        options.time_unit, options.tick_offset)
    print "Total chunks: %d" % len(slices)
    print

    chunk_length = options.time_unit * infile.resolution

    for i, slc in enumerate(slices):
        strm = slc.to_event_stream()
        # Print the header for this chunk
        print "Chunk %d: %d-%d (%d events)" % \
                (i, slc.start, slc.end,len(strm.trackpool))
        print "".join(str(i).ljust(2) for i in range(11)), \
                "Time   ", "Vel", "Ch", "Tr"

        # Only show note-on events
        noteons = [ev for ev in sorted(strm.trackpool) \
                    if type(ev) == NoteOnEvent and ev.velocity > 0]
        # Sorted by time: within same tick, sort by pitch
        for k, grp in groupby(noteons):
            for ev in sorted(list(grp), key=lambda e: e.pitch):
                # Display all the information for this note
                octave = ev.pitch / 12
                name = constants.NOTE_NAMES[ev.pitch % 12].ljust(2)
                indent = "  " * octave
                fill = "  " * (10 - octave)
                if options.tick_times:
                    time = str(ev.tick + slc.start).ljust(7)
                else:
                    time = ("%.1f%%" %
                            (100.0 * ev.tick / chunk_length)).ljust(7)
                channel = str(ev.channel).ljust(2)
                track = str(ev.track).ljust(2)
                velocity = str(ev.velocity).ljust(3)

                print "%s%s%s %s %s %s %s" % \
                        (indent, name, fill, time, velocity, channel, track)
        print
示例#4
0
def main():
    usage = "%prog [options] <midi-file>"
    description = "Divides a midi file into chunks, with a given size and "\
        "offset, and plays "\
        "the chunks consecutively, with a gap between each."
    parser = OptionParser(usage=usage, description=description)
    parser.add_option(
        '-t',
        '--time-unit',
        dest="time_unit",
        action="store",
        type="float",
        help=
        "size of chunks in crotchet beats (according to the midi file's resolution)",
        default=4)
    parser.add_option('-o',
                      '--tick-offset',
                      dest="tick_offset",
                      action="store",
                      type="int",
                      help="offset of the first chunk in midi ticks",
                      default=0)
    parser.add_option(
        '-g',
        '--gap',
        dest="gap",
        action="store",
        type="float",
        help=
        "time to wait between playing each chunk in seconds (potentially float). It will take some time to load the chunk and the sequencer usually pauses before reporting it's done: this is not included in this value",
        default=0.0)
    parser.add_option('-p',
                      '--print',
                      dest="print_events",
                      action="store_true",
                      help="print out all events for each chunk")
    parser.add_option('--pno',
                      '--print-note-ons',
                      dest="print_note_ons",
                      action="store_true",
                      help="print only note-on events")
    parser.add_option(
        '--force-res',
        dest="force_res",
        action="store",
        type="int",
        help=
        "force the midi file's resolution to be the given value, rather than using that read from the file"
    )
    parser.add_option('-s',
                      '--start',
                      dest="start",
                      action="store",
                      type="int",
                      help="chunk number to start at",
                      default=0)
    options, arguments = parse_args_with_config(parser)

    filename = arguments[0]

    # Load up the input midi file
    infile = read_midifile(filename, force_resolution=options.force_res)
    handler = MidiHandler(infile,
                          time_unit=options.time_unit,
                          tick_offset=options.tick_offset)
    slices = handler.get_slices()

    # Start at the requested chunk
    slices = slices[options.start:]

    print "Playing %d-beat chunks with a %d-tick offset" % (
        options.time_unit, options.tick_offset)
    if options.start > 0:
        print "Start from chunk %d" % options.start
    print "Total chunks: %d" % len(slices)
    print "Ctrl+C to exit"
    print

    try:
        for i, slc in enumerate(slices):
            strm = slc.to_event_stream(cancel_playing=True)
            print "Playing chunk %d: %d-%d (%d events)" % (
                i, slc.start, slc.end, len(strm.trackpool))
            if options.print_events:
                print "\n".join("  %s" % ev for ev in sorted(strm.trackpool))
            elif options.print_note_ons:
                print "\n".join("  %s" % ev for ev in sorted(strm.trackpool) \
                                                    if type(ev) is NoteOnEvent)

            play_stream(strm, block=True)
            if options.gap > 0.0:
                print "  Waiting %s seconds..." % options.gap
                time.sleep(options.gap)
    except KeyboardInterrupt:
        print "Exiting"