python/scripts/aubiocut: use slice_source_at_stamps
authorPaul Brossier <piem@piem.org>
Sun, 12 Jan 2014 02:09:25 +0000 (22:09 -0400)
committerPaul Brossier <piem@piem.org>
Sun, 12 Jan 2014 02:09:25 +0000 (22:09 -0400)
python/scripts/aubiocut

index 54724e1..1c1cdbb 100755 (executable)
@@ -166,43 +166,9 @@ if __name__ == '__main__':
     # cutting pass
     if options.cut and nstamps > 0:
         # generate output filenames
-        import os
-        source_base_name, source_ext = os.path.splitext(os.path.basename(source_file))
-        if options.output_directory != None:
-            if not os.path.isdir(options.output_directory):
-                os.makedirs(options.output_directory)
-            source_base_name = os.path.join(options.output_directory, source_base_name)
-        def new_sink_name(source_base_name, timestamp):
-            return source_base_name + '_%02.3f' % (timestamp) + '.wav'
-        # reopen source file
-        s = source(source_file, samplerate, hopsize)
-        if samplerate == 0: samplerate = s.get_samplerate()
-        # create first sink at 0
-        g = sink(new_sink_name(source_base_name, 0.), samplerate)
-        total_frames = 0
-        # get next region
-        next_stamp = int(timestamps.pop(0))
-        while True:
-            vec, read = s()
-            remaining = next_stamp - total_frames
-            if remaining <= read:
-                # write remaining samples from current region
-                g(vec[0:remaining], remaining)
-                # close this file
-                del g
-                # create a new file for the new region
-                g = sink(new_sink_name(source_base_name, next_stamp / float(samplerate)), samplerate)
-                # write the remaining samples in the new file
-                g(vec[remaining:read], read - remaining)
-                #print "new slice", total_frames_written, "+", remaining, "=", start_of_next_region
-                if len(timestamps):
-                    next_stamp = int(timestamps.pop(0))
-                else:
-                    next_stamp = 1e120
-            else:
-                g(vec[0:read], read)
-            total_frames += read
-            if read < hopsize: break
+        from aubio.slicing import slice_source_at_stamps
+        slice_source_at_stamps(source_file, timestamps,
+                output_dir = options.output_directory)
 
         # print some info
         duration = float (total_frames) / float(samplerate)