History

python/lib/aubio/cut.py: clean-up, split in functions

Paul Brossier committed 9 years ago

Commit b02d52f1 parent 9a529621 file · +41 −26Browse files

Showing the changes to python/lib (1 of 1 file). Show all

Unified · Split

python/lib/aubio/cut.py+41−26
@@ -7,7 +7,7 @@ · 6 unmodified lines
77 import sys
88 import argparse
99
10−def parse_args():
10+def aubio_cut_parser():
1111 usage = "usage: %s [options] -i soundfile" % sys.argv[0]
1212 usage += "\n help: %s -h" % sys.argv[0]
1313 parser = argparse.ArgumentParser()
@@ -132,28 +132,23 @@ def parse_args(): · 118 unmodified lines
132132 parser.add_argument("-q","--quiet",
133133 action="store_false", dest="verbose", default=True,
134134 help="be quiet")
135− args = parser.parse_args()
136− if not args.source_file and not args.source_file2:
137− sys.stderr.write("Error: no file name given\n")
138− parser.print_help()
139− sys.exit(1)
140− elif args.source_file2 is not None:
141− args.source_file = args.source_file2
142− return args
135+ return parser
143136
144−def main():
145− options = parse_args()
146137
138+def _cut_analyze(options):
147139 source_file = options.source_file
148140 hopsize = options.hopsize
149141 bufsize = options.bufsize
150142 samplerate = options.samplerate
151143 source_file = options.source_file
152144
145+ # analyze pass
153146 from aubio import onset, tempo, source
154147
155148 s = source(source_file, samplerate, hopsize)
156− if samplerate == 0: samplerate = s.get_samplerate()
149+ if samplerate == 0:
150+ samplerate = s.get_samplerate()
151+ options.samplerate = samplerate
157152
158153 if options.beat:
159154 o = tempo(options.onset_method, bufsize, hopsize, samplerate=samplerate)
@@ -170,7 +165,6 @@ def main(): · 10 unmodified lines
170165
171166 timestamps = []
172167 total_frames = 0
173− # analyze pass
174168 while True:
175169 samples, read = s()
176170 if o(samples):
@@ -179,15 +173,12 @@ def main(): · 2 unmodified lines
179173 total_frames += read
180174 if read < hopsize: break
181175 del s
182− # print some info
183− nstamps = len(timestamps)
184− duration = float (total_frames) / float(samplerate)
185− info = 'found %(nstamps)d timestamps in %(source_file)s' % locals()
186− info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
187− sys.stderr.write(info)
176+ return timestamps, total_frames
188177
178+def _cut_slice(options, timestamps):
189179 # cutting pass
190− if options.cut and nstamps > 0:
180+ nstamps = len(timestamps)
181+ if nstamps > 0:
191182 # generate output files
192183 from aubio.slicing import slice_source_at_stamps
193184 timestamps_end = None
@@ -202,12 +193,36 @@ def main(): · 8 unmodified lines
202193 if options.cut_until_nslices:
203194 timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]]
204195 timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1)
205− slice_source_at_stamps(source_file, timestamps, timestamps_end = timestamps_end,
196+ slice_source_at_stamps(options.source_file,
197+ timestamps, timestamps_end = timestamps_end,
206198 output_dir = options.output_directory,
207− samplerate = samplerate)
199+ samplerate = options.samplerate)
200+
201+def main():
202+ parser = aubio_cut_parser()
203+ options = parser.parse_args()
204+ if not options.source_file and not options.source_file2:
205+ sys.stderr.write("Error: no file name given\n")
206+ parser.print_help()
207+ sys.exit(1)
208+ elif options.source_file2 is not None:
209+ options.source_file = options.source_file2
210+
211+ # analysis
212+ timestamps, total_frames = _cut_analyze(options)
213+
214+ # print some info
215+ duration = float (total_frames) / float(options.samplerate)
216+ base_info = '%(source_file)s' % {'source_file': options.source_file}
217+ base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \
218+ {'duration': duration, 'samplerate': options.samplerate}
219+
220+ info = "found %d timestamps in " % len(timestamps)
221+ info += base_info
222+ sys.stderr.write(info)
208223
209− # print some info
210− duration = float (total_frames) / float(samplerate)
211− info = 'created %(nstamps)d slices from %(source_file)s' % locals()
212− info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
224+ if options.cut:
225+ _cut_slice(options, timestamps)
226+ info = "created %d slices from " % len(timestamps)
227+ info += base_info
213228 sys.stderr.write(info)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t