History

python/scripts/aubiocut: add slicing

Paul Brossier committed 13 years ago

Commit dee41641 parent 69440b81 file · +52 −18Browse files

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

Unified · Split

python/scripts/aubiocut+52−18
@@ -8,6 +8,7 @@ import sys · 7 unmodified lines
88 #from aubio.task import *
99
1010 usage = "usage: %s [options] -i soundfile" % sys.argv[0]
11+usage += "\nhelp: %s -h" % sys.argv[0]
1112
1213 def parse_args():
1314 from optparse import OptionParser
@@ -133,31 +134,64 @@ if __name__ == '__main__': · 119 unmodified lines
133134 o = onset(options.onset_method, bufsize, hopsize)
134135 o.set_threshold(options.threshold)
135136
136− slice_number = 0
137− #if options.cut:
138− this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate)
139−
140137 timestamps = []
141− block_read = 0
138+ total_frames = 0
139+ # analyze pass
142140 while True:
143141 samples, read = s()
144− #ring_buffer = hstack([ring_buffer, samples])
145− is_onset = o(samples)
146− if is_onset:
147− this_onset = (block_read - 4. + is_onset[0]) * hopsize
148− if options.verbose:
149− print "%.4f" % ( this_onset / samplerate )
142+ if o(samples):
143+ this_onset = o.get_last_onset()
144+ if options.verbose: print "%.4f" % o.get_last_onset_s()
150145 timestamps.append (this_onset)
151− del this_slice
152− slice_number += 1
153− this_slice = sink('/tmp/t-%02d.wav' % slice_number, samplerate)
154− this_slice(samples, read)
155− block_read += 1
146+ total_frames += read
156147 if read < hopsize: break
157148
158149 # print some info
159− duration = float ( block_read * hopsize + read ) / samplerate
160150 nstamps = len(timestamps)
151+ duration = float (total_frames) / float(samplerate)
161152 info = 'found %(nstamps)d timestamps in %(source_file)s' % locals()
162− info += ' (read %(duration).2fs at %(samplerate)dHz)\n' % locals()
153+ info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
163154 sys.stderr.write(info)
155+
156+ # cutting pass
157+ if options.cut and nstamps > 0:
158+ # generate output filenames
159+ import os
160+ source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
161+ def new_sink_name(source_base_name, timestamp):
162+ return source_base_name + '_%02.3f' % (timestamp) + '.wav'
163+ # reopen source file
164+ del s
165+ s = source(source_file, samplerate, hopsize)
166+ # create first sink at 0
167+ g = sink(new_sink_name(source_base_name, 0.), samplerate)
168+ total_frames = 0
169+ # get next region
170+ next_onset = int(timestamps.pop(0))
171+ while True:
172+ vec, read = s()
173+ remaining = next_onset - total_frames
174+ if remaining <= read:
175+ # write remaining samples from current region
176+ g(vec[0:remaining], remaining)
177+ # close this file
178+ del g
179+ # create a new file for the new region
180+ g = sink(new_sink_name(source_base_name, next_onset / float(samplerate)), samplerate)
181+ # write the remaining samples in the new file
182+ g(vec[remaining:read], read - remaining)
183+ #print "new slice", total_frames_written, "+", remaining, "=", start_of_next_region
184+ if len(timestamps):
185+ next_onset = int(timestamps.pop(0))
186+ else:
187+ next_onset = 1e120
188+ else:
189+ g(vec[0:read], read)
190+ total_frames += read
191+ if read < hopsize: break
192+
193+ # print some info
194+ duration = float (total_frames) / float(samplerate)
195+ info = 'created %(nstamps)d slices from %(source_file)s' % locals()
196+ info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals()
197+ 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