示例#1
0
def main():
    usage = "%prog [options] <in-file>"
    description = "Play music using the Harmonical. This allows you to "\
        "play music specified precisely in the tonal space. By default, "\
        "plays back the input, but can also output to a file."
    parser = OptionParser(usage=usage, description=description)
    parser.add_option('-o', '--output', dest="outfile", action="store", help="output the result to a wave file instead of playing back.")
    parser.add_option('-m', '--midi', dest="midi", action="store_true", help="generate midi data, not audio. Depends on the input format supporting midi file generation.")
    options, arguments = parse_args_with_config(parser)
    
    filename = arguments[0]
    
    # Load up the input file
    infile = HarmonicalInputFile.from_file(filename)
    if options.midi:
        midi = infile.render_midi()
        
        if options.outfile is not None:
            # Output a midi file
            write_midifile(midi, options.outfile)
            print >>sys.stderr, "Saved midi data to %s" % options.outfile
        else:
            print >>sys.stderr, "Playing..."
            play_stream(midi, block=True)
    else:
        print >>sys.stderr, "Generating audio..."
        audio = infile.render()
        
        if options.outfile is not None:
            # Output to a file instead of playing
            save_wave_data(audio, options.outfile)
            print >>sys.stderr, "Saved data to %s" % options.outfile
        else:
            print >>sys.stderr, "Playing..."
            play_audio(audio, wait_for_end=True)
示例#2
0
def main():
    usage = "%prog [options] <results-file> [<result-number>=0]"
    parser = OptionParser(usage=usage)
    parser.add_option("-q", "--quiet", dest="quiet", action="store_true", help="only output the requested information, no meta-info.")
    parser.add_option("-p", "--print", dest="printout", action="store_true", help="output the result to stdout.")
    parser.add_option("--path", dest="path", action="store_true", help="display the fully-specified tonal space path.")
    parser.add_option("--play", dest="play", action="store_true", help="use the harmonical to play the root sequence of the result's semantics.")
    parser.add_option("--audio", dest="audio", action="store", help="use the harmonical to render the root sequence, as with --play, and store the result to a wave file.")
    options, arguments = parser.parse_args()
    
    # Just get the default formalism
    formalism = get_default_formalism()
    
    def _print(string=""):
        if not options.quiet:
            print >>sys.stderr, string
        
    if len(arguments) == 0:
        print >>sys.stderr, "Specify a file to read the results from"
        sys.exit(1)
    results = ParseResults.from_file(arguments[0])
    
    if len(arguments) > 1:
        res_num = int(arguments[1])
    else:
        res_num = 0
    prob,result = results.sorted_results[res_num]
    
    if options.printout:
        _print("Result:")
        # Just display the resulting category
        print result
        _print()
        
    if options.path:
        _print("Tonal space path:")
        # Compute the tonal path (coordinates) from the result
        path = formalism.semantics_to_coordinates(result.semantics)
        points,timings = zip(*path)
        print ", ".join(coordinates_to_roman_names(points))
        _print()
        
    if options.play or options.audio is not None:
        _print("Building pitch structure from result...")
        # Convert the semantics into a list of TS points
        path = formalism.semantics_to_coordinates(result.semantics)
        # Decide on chord types
        # For now, since we don't know the original chords, use dom7 
        #  for dom chords, maj for subdoms, and M7 for tonics
        fun_chords = {
            'T' : 'M7',
            'D' : '7',
            'S' : '',
        }
        functions = formalism.semantics_to_functions(result.semantics)
        chord_types = [(fun_chords[f],t) for (f,t) in functions]
        
        tones = path_to_tones(path, chord_types=chord_types, double_root=True)
        
        _print("Rendering audio samples...")
        samples = tones.render()
        if options.audio is not None:
            filename = os.path.abspath(options.audio)
            _print("Writing wave data to %s" % filename)
            save_wave_data(samples, filename)
        if options.play:
            _print("Playing...")
            play_audio(samples, wait_for_end=True)
        _print()
示例#3
0
def main():
    usage = "%prog [options] <results-file> [<result-number>=0]"
    parser = OptionParser(usage=usage)
    parser.add_option(
        "-q",
        "--quiet",
        dest="quiet",
        action="store_true",
        help="only output the requested information, no meta-info.")
    parser.add_option("-p",
                      "--print",
                      dest="printout",
                      action="store_true",
                      help="output the result to stdout.")
    parser.add_option("--path",
                      dest="path",
                      action="store_true",
                      help="display the fully-specified tonal space path.")
    parser.add_option(
        "--play",
        dest="play",
        action="store_true",
        help=
        "use the harmonical to play the root sequence of the result's semantics."
    )
    parser.add_option(
        "--audio",
        dest="audio",
        action="store",
        help=
        "use the harmonical to render the root sequence, as with --play, and store the result to a wave file."
    )
    options, arguments = parser.parse_args()

    # Just get the default formalism
    formalism = get_default_formalism()

    def _print(string=""):
        if not options.quiet:
            print >> sys.stderr, string

    if len(arguments) == 0:
        print >> sys.stderr, "Specify a file to read the results from"
        sys.exit(1)
    results = ParseResults.from_file(arguments[0])

    if len(arguments) > 1:
        res_num = int(arguments[1])
    else:
        res_num = 0
    prob, result = results.sorted_results[res_num]

    if options.printout:
        _print("Result:")
        # Just display the resulting category
        print result
        _print()

    if options.path:
        _print("Tonal space path:")
        # Compute the tonal path (coordinates) from the result
        path = formalism.semantics_to_coordinates(result.semantics)
        points, timings = zip(*path)
        print ", ".join(coordinates_to_roman_names(points))
        _print()

    if options.play or options.audio is not None:
        _print("Building pitch structure from result...")
        # Convert the semantics into a list of TS points
        path = formalism.semantics_to_coordinates(result.semantics)
        # Decide on chord types
        # For now, since we don't know the original chords, use dom7
        #  for dom chords, maj for subdoms, and M7 for tonics
        fun_chords = {
            'T': 'M7',
            'D': '7',
            'S': '',
        }
        functions = formalism.semantics_to_functions(result.semantics)
        chord_types = [(fun_chords[f], t) for (f, t) in functions]

        tones = path_to_tones(path, chord_types=chord_types, double_root=True)

        _print("Rendering audio samples...")
        samples = tones.render()
        if options.audio is not None:
            filename = os.path.abspath(options.audio)
            _print("Writing wave data to %s" % filename)
            save_wave_data(samples, filename)
        if options.play:
            _print("Playing...")
            play_audio(samples, wait_for_end=True)
        _print()