[py] move cut_slice from cut to cmd to avoid cyclic import
[aubio.git] / python / lib / aubio / cmd.py
index 2ae9f18..05780ea 100644 (file)
@@ -11,6 +11,7 @@ readable code examples, check out the `python/demos` folder."""
 
 import sys
 import argparse
+import warnings
 import aubio
 
 def aubio_parser():
@@ -242,13 +243,13 @@ class AubioArgumentParser(argparse.ArgumentParser):
         self.add_argument("-o", "--output", type = str,
                 metavar = "<outputdir>",
                 action="store", dest="output_directory", default=None,
-                help="specify path where slices of the original file should'
-                ' be created")
+                help="specify path where slices of the original file should"
+                " be created")
         self.add_argument("--cut-until-nsamples", type = int,
                 metavar = "<samples>",
                 action = "store", dest = "cut_until_nsamples", default = None,
-                help="how many extra samples should be added at the end of'
-                ' each slice")
+                help="how many extra samples should be added at the end of"
+                " each slice")
         self.add_argument("--cut-every-nslices", type = int,
                 metavar = "<samples>",
                 action = "store", dest = "cut_every_nslices", default = None,
@@ -256,8 +257,8 @@ class AubioArgumentParser(argparse.ArgumentParser):
         self.add_argument("--cut-until-nslices", type = int,
                 metavar = "<slices>",
                 action = "store", dest = "cut_until_nslices", default = None,
-                help="how many extra slices should be added at the end of'
-                ' each slice")
+                help="how many extra slices should be added at the end of"
+                " each slice")
         self.add_argument("--create-first",
                 action = "store_true", dest = "create_first", default = False,
                 help="always include first slice")
@@ -507,7 +508,6 @@ class process_cut(process_onset):
         return ret
 
     def flush(self, frames_read, samplerate):
-        from aubio.cut import _cut_slice
         _cut_slice(self.options, self.slices)
         duration = float(frames_read) / float(samplerate)
         base_info = '%(source_file)s' % \
@@ -518,6 +518,32 @@ class process_cut(process_onset):
         info += base_info
         sys.stderr.write(info)
 
+def _cut_slice(options, timestamps):
+    # cutting pass
+    nstamps = len(timestamps)
+    if nstamps > 0:
+        # generate output files
+        timestamps_end = None
+        if options.cut_every_nslices:
+            timestamps = timestamps[::options.cut_every_nslices]
+            nstamps = len(timestamps)
+        if options.cut_until_nslices and options.cut_until_nsamples:
+            msg = "using cut_until_nslices, but cut_until_nsamples is set"
+            warnings.warn(msg)
+        if options.cut_until_nsamples:
+            lag = options.cut_until_nsamples
+            timestamps_end = [t + lag for t in timestamps[1:]]
+            timestamps_end += [1e120]
+        if options.cut_until_nslices:
+            slice_lag = options.cut_until_nslices
+            timestamps_end = [t for t in timestamps[1 + slice_lag:]]
+            timestamps_end += [1e120] * (options.cut_until_nslices + 1)
+        aubio.slice_source_at_stamps(options.source_uri,
+                timestamps, timestamps_end = timestamps_end,
+                output_dir = options.output_directory,
+                samplerate = options.samplerate,
+                create_first = options.create_first)
+
 def main():
     parser = aubio_parser()
     if sys.version_info[0] != 3: