Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -0,0 +1,49 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | import sys | ||
| 4 | import aubio | ||
| 5 | |||
| 6 | if __name__ == '__main__': | ||
| 7 | if len(sys.argv) < 3: | ||
| 8 | print('usage: %s <inputfile> <outputfile> [transpose] [samplerate] [hop_size]' % sys.argv[0]) | ||
| 9 | sys.exit(1) | ||
| 10 | if len(sys.argv) > 3: transpose = float(sys.argv[3]) | ||
| 11 | else: transpose = 12. | ||
| 12 | if len(sys.argv) > 4: samplerate = int(sys.argv[4]) | ||
| 13 | else: samplerate = 0 | ||
| 14 | if len(sys.argv) > 5: hop_size = int(sys.argv[5]) | ||
| 15 | else: hop_size = 256 | ||
| 16 | |||
| 17 | source_read = aubio.source(sys.argv[1], samplerate, hop_size) | ||
| 18 | if samplerate == 0: samplerate = source_read.samplerate | ||
| 19 | sink_out = aubio.sink(sys.argv[2], samplerate) | ||
| 20 | |||
| 21 | pitchshifter = aubio.pitchshift("default", 1., hop_size, samplerate) | ||
| 22 | if transpose: pitchshifter.set_transpose(transpose) | ||
| 23 | |||
| 24 | total_frames, read = 0, hop_size | ||
| 25 | transpose_range = 23.9 | ||
| 26 | while read: | ||
| 27 | vec, read = source_read() | ||
| 28 | # transpose the samples | ||
| 29 | out = pitchshifter(vec) | ||
| 30 | # position in the file (between 0. and 1.) | ||
| 31 | percent_read = total_frames / float(source_read.duration) | ||
| 32 | # variable transpose rate (in semitones) | ||
| 33 | transpose = 2 * transpose_range * percent_read - transpose_range | ||
| 34 | # set transpose rate | ||
| 35 | pitchshifter.set_transpose(transpose) | ||
| 36 | # print the transposition | ||
| 37 | #print pitchshifter.get_transpose() | ||
| 38 | # write the output | ||
| 39 | sink_out(out, read) | ||
| 40 | total_frames += read | ||
| 41 | |||
| 42 | # end of file, print some results | ||
| 43 | outstr = "wrote %.2fs" % (total_frames / float(samplerate)) | ||
| 44 | outstr += " (%d frames in" % total_frames | ||
| 45 | outstr += " %d blocks" % (total_frames // source_read.hop_size) | ||
| 46 | outstr += " at %dHz)" % source_read.samplerate | ||
| 47 | outstr += " from " + source_read.uri | ||
| 48 | outstr += " to " + sink_out.uri | ||
| 49 | print(outstr) | ||