Work-in-progress repo for ambisonics extensions for OM-SoX
Alexander Nguyen
25.01.25 a590ad25dc2fa20301f47b7b6c84e5f176e36db2
commit | author | age
92c40d 1 ;*********************************************************************
AN 2 ; OM-SoX, (c) 2011-2014 Marlon Schumacher (CIRMMT/McGill University) *
3 ;             http://sourceforge.net/projects/omsox/                 *
4 ;                                                                    *
5 ;  Multichannel Audio Manipulation and Functional Batch Processing.  *
6 ;        DSP based on SoX - (c) C.Bagwell and Contributors           *
7 ;                  http://sox.sourceforge.net/                       *
8 ;*********************************************************************
9 ;
10 ;This program is free software; you can redistribute it and/or
11 ;modify it under the terms of the GNU General Public License
12 ;as published by the Free Software Foundation; either version 2
13 ;of the License, or (at your option) any later version.
14 ;
15 ;See file LICENSE for further informations on licensing terms.
16 ;
17 ;This program is distributed in the hope that it will be useful,
18 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;GNU General Public License for more details.
21 ;
22 ;You should have received a copy of the GNU General Public License
23 ;along with this program; if not, write to the Free Software
24 ;Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,10 USA.
25 ;
26 ;Authors: M. Schumacher
27
28 (in-package :om)
29
30
31 ; %%%%%%%%%%%%%%% SOX-PROCESS %%%%%%%%%%%%%%%%%%%%
32 ;           Main Processing Function 
33
34 ;== method for pathname + string =======
35 (defmethod! sox-process ((sox-input pathname) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
36             :icon 08
37             :initvals '(nil "" "realtime" nil nil nil Off "break")
38             :menuins (list (list 2 *sox-output-options*)
39                            (list 3 *sox-file-formats*)
40                            (list 4 *sox-samplerates*)
41                            (list 5 *sox-bitdephts*)
42                            (list 6 '(("On" On) ("Off" Off)))
43                            (list 7 '(("break" break) ("repeat" repeat) ("cycle" cycle))))                       
44             :indoc '("Audio input to be processed [sound, path, string/pipe, sox-input]" 
45                      "Sox-effect to be applied to audio input [string]" "Output type (new file, replace file, pipe, or realtime) [string]. Also accepts directory, filename, filepath [path]"
46                      "Filetype of produced audio [string]" "Samplerate of produced audio [string]" "Bitdepth of produced audio [number]"
47                      "recursive (when 'on' applies processing recursively to audio) [symbol]" "Mode for batch-processing (break, repeat, cycle) [symbol]") 
48             :doc "Main audio processing function for OM-SoX. Takes audio provided in <sox-input> and processes it with sox-effects provided in <sox-effect>. 
49
50 <output> determines whether output is to be rendered into 
51 a) new file, 
52 b) replace the input file, 
53 c) create a sox-command as a pipe (for further processing), 
54 d) play back in realtime through audio device.
55
56 Alternatively, a filename, directory, or path can be provided which will write an audio file to the given destination. 
57 It is also posisble to connect a function/patch in lambda mode, which (if available) will receive the path of <sox-input> as first argument. 
58 This can be used to algorithmically name output files (e.g. in batch processes).
59
60 Optionally, the audio output <filetype> can be specified, as well as the <samplerate> and <bitdepth> (default: same as input). 
61 <recursive> is an experimental option allowing to apply a sox-effect recursively to audio input (e.g. trimming).
62 <batch-mode> determines the behaviour when processing lists of sox-inputs and sox-effects that differ in length.
63 "
64
65             (if (probe-file *sox-path*)
66
67                 (let ((outfile (create-path sox-input output filetype)))
68
69                   (setf str (format nil "~s ~a ~s" (namestring *sox-path*) *sox-options* (namestring sox-input)))
70
71                   (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*)) 
72                                   ((equal output "pipe") (sox-samplebits str bitdepth samplerate '-p))
73                                   (t (sox-samplebits str bitdepth samplerate outfile))))
74
75                   (setf str (string+ str sox-effect))
76                  
77                   ; optional removal of temp-files
78                   (if *delete-inter-file* 
79                       (let ((outstring (sox-out str sox-input output outfile recursive))) 
80                         (if (>= *om-version* 6.07)
81                             (om-run-process "cleantempfiles" #'sleep&clean 5)
82                           (om-run-process "cleantempfiles" sleep&clean 5))
83                         outstring)                   
84                     (sox-out str sox-input output outfile recursive)
85                     ))
86               (sox-not-found))
87             )
88
89
90 ;== method for sound + string =======
91 (defmethod! sox-process ((sox-input sound) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
92             (let ((gainval (sox-vol-convert (vol sox-input)))
93                   (panval (+ (tracknum sox-input) (* 0.01 (pan sox-input)))))
94               (if (equal (tracknum sox-input) 0)
95                   (sox-process (sound-path sox-input) (string+ sox-effect " gain " (makestring gainval)) 
96                                :output output :filetype filetype :samplerate samplerate :bitdepth bitdepth 
97                                :recursive recursive :batch-mode batch-mode)            
98                 (sox-process (make-instance 'sox-pan
99                                             :sound sox-input
100                                             :gains gainval
101                                             :panning panval
102                                             :numchannels nil)
103                              sox-effect 
104                              :output output :filetype filetype :samplerate samplerate :bitdepth bitdepth 
105                              :recursive recursive :batch-mode batch-mode)))
106             )
107
108
109 ;== method for string + string =======
110 ;-> allows for arbitrary 'manual' sox-effect or options or pipe inputs...
111 (defmethod! sox-process ((sox-input string) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
112             (if (probe-file *sox-path*)
113
114             ;catch exception ---------
115                 (if (and (pipe-p sox-input) (equal output "pipe") )
116                    (om-message-abort "Pipe output not possible with this type of input.") 
117             ; ------------------------
118
119                 (let ((outfile (create-path nil output filetype)))
120
121                   (setf str (format nil "~s ~a ~s" (namestring *sox-path*) *sox-options* sox-input))
122                   
123                   (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*)) 
124                                   ((equal output "pipe") (sox-samplebits str bitdepth samplerate '-p))
125                                   (t (sox-samplebits str bitdepth samplerate outfile))))
126
127                   (setf str (string+ str sox-effect))
128
129                   (if *delete-inter-file* 
130                       (let ((outstring (sox-out str sox-input output outfile recursive))) 
131                         (if (>= *om-version* 6.07)
132                             (om-run-process "cleantempfiles" #'sleep&clean 5)
133                           (om-run-process "cleantempfiles" sleep&clean 5))
134                         outstring)                   
135                     (sox-out str sox-input output outfile recursive)
136                     )))
137
138               (sox-not-found))
139               )
140
141 ; %%%%%%%% METHODS FOR SOX-INPUT CLASSES %%%%%%%%%%%%
142
143 ; === sox-merge =============
144
145 (defmethod! sox-process ((sox-input sox-merge) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
146             (if (probe-file *sox-path*)
147
148                 (progn
149                   ;(print (sound sox-input))
150                   (let* ((sound (if (> (length (gains sox-input)) (length (list! (sound sox-input)))) 
151                                         (repeat-n (first (list! (sound sox-input))) (length (gains sox-input)))
152                                       (sound sox-input))))                                     
153
154                   (when (and (find-if 'stringp sound) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
155
156                 (let ((outfile (create-path nil output filetype))) ;(create-path (first (soundfiles sox-input)) output filetype)
157
158                   (let* ((filenames (loop for soundfile in sound collect                              
159                                           (namestring soundfile))))          
160
161                     (setf str (format nil "~s ~a -M " (namestring *sox-path*) *sox-options* )) 
162                     
163                     (if (gains sox-input)
164                         (loop for filename in filenames do
165                               for gain in (db->lin (list! (gains sox-input))) do
166                               (setf str (string+ str (format nil " -v~d ~s  " gain filename))))
167                       (loop for filename in filenames do
168                             (setf str (string+ str (format nil " ~s" filename)))))
169  
170                     (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
171                                     ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p"))
172                                     (t (sox-samplebits str bitdepth samplerate outfile))))
173
174                     (setf str (string+ str sox-effect))
175
176                     (sox-out str sox-input output outfile recursive)))))
177
178               (sox-not-found))
179             )
180
791513 181 ; === sox-hoaencode =============
AN 182
183 (defmethod! sox-process ((sox-input sox-hoaencode) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
184     (if (probe-file *sox-path*)
185         (progn
a590ad 186             (when 
AN 187                 (not (listp (sound sox-input)))
188                 (om-message-abort "sound must be a list.")
189             )
190             (when 
191                 (not 
192                     (or 
193                         (subtypep (type-of (positions sox-input)) '3dc)
194                         (and (subtypep (type-of (positions sox-input)) 'list) (> (length (positions sox-input)) 0) (subtypep (type-of (first (positions sox-input))) 'list) (> (length (first (positions sox-input))) 1)))
195                 )
196                 (om-message-abort "positions must be of type 3dc, or a list of lists.")
197             )
791513 198             (let* 
AN 199                 (
200                     (sound 
a590ad 201                         (sound sox-input)
791513 202                     )
AN 203                     (positions-ae 
204                         (sox-hoaencode-auto-convert-positions (positions sox-input))
205                     )
206                 )
207                 (when 
208                     (and (find-if 'stringp sound) (equal output "pipe")) 
209                     (om-message-abort "Pipe output not possible with this type of input.")
210                 )
211                 (let* 
212                     (
213                         (filenames (loop for soundfile in sound collect (namestring soundfile)))
a590ad 214                         (outfile (create-path nil output filetype))
791513 215                     )
AN 216                     (setf str (format nil " ~s ~a" (namestring *sox-path*) *sox-options*))
217                     (if (= (length filenames) 1)
218                         ; case: one file 
219                         (let* 
220                             (
221                                 (filename (first filenames))
222                                 (position (first positions-ae))
223                             )
224                             (if (= (order sox-input) 0) 
225                                 ; case: order = 0
226                                 (setf str (string+ str (format nil " ~s" filename)))
227                                 ; case: order > 0
228                                 (progn
229                                     (setf str (string+ str " -M"))
230                                     (loop for gain in (sox-hoaencode-gains-up-to-order (order sox-input) (first position) (second position)) do
231                                         (setf str (string+ str (format nil " -v~d ~s" gain filename))))
232                                 )
233                             )
234                         )
235                         ; case: multiple files
236                         (if (= (order sox-input) 0)
237                             ; case: order = 0
238                             (progn 
239                                 (setf str (string+ str " -m"))
240                                 (loop for filename in filenames do
241                                     (setf str (string+ str (format nil " ~s" filename)))
242                                 )
243                             )
244                             ; case: order > 0
245                             (progn
246                                 (setf str (string+ str " -m"))
247                                 
248                                 ; Create components 
249                                 (setf component_idx 0)
250                                 (loop 
251                                     for filename in filenames
252                                     for position in positions-ae do
253                                     (progn  
254                                         (setf str_component (format nil " ~s ~a -M" (namestring *sox-path*) *sox-options*))
255                                         (loop 
256                                             for gain in (sox-hoaencode-gains-up-to-order (order sox-input) (first position) (second position)) do
257                                             (setf str_component (string+ str_component (format nil " -v~d ~s" gain filename)))
258                                         )
259                                         (setf outfile_tmp (format nil "~a~a-~d.~a" (directory-namestring outfile) (pathname-name outfile) component_idx (pathname-type outfile)))
260                                         (setf str_component (sox-samplebits str_component bitdepth samplerate outfile_tmp))
261                                         (sox-out str_component filename "new file" outfile_tmp nil)
262
263                                         (setf component_idx (1+ component_idx))
264
265                                         ; Add component path to <str>
266                                         (setf str (string+ str (format nil " ~s" outfile_tmp)))
267                                     )
268                                 )
269                             )
270                         )
271                     )
272                     (setf str 
273                         (cond 
a590ad 274                             ((equal output "realtime") 
AN 275                                 (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
276                             ((equal output "pipe") 
277                                 (sox-samplebits str bitdepth samplerate "-p"))
278                             (t 
279                                 (sox-samplebits str bitdepth samplerate outfile))
791513 280                         )
AN 281                     )
282                     (setf str (string+ str sox-effect))
283                     (print str)
284                     (sox-out str sox-input output outfile recursive)
285                 )
286             )
287         )
288         (sox-not-found)
289     )
290 )
291
92c40d 292 ; === sox-mix =============
AN 293
294 (defmethod! sox-process ((sox-input sox-mix) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
295             (if (probe-file *sox-path*)
296
297                  (progn
298                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe"))
299                     (om-message-abort "Pipe output not possible with this type of input."))
300
301                   (let ((outfile (create-path nil output filetype))) ;(create-path (first (soundfiles sox-input)) output filetype)
302
303                     (let* ((filenames 
304                             (loop for soundfile in (sound sox-input) collect ;I guess this throws the error when using pipes as inputs
305                                   (namestring soundfile))))
306
307                       (setf str (format nil "~s ~a -m " (namestring *sox-path*) *sox-options*))
308                                                           
309                       ;potentially not needed...
310                    ;(if (1/n sox-input) 
311                         
312                    ;     (loop for filename in filenames do
313                    ;           for gain in (om/ (db->lin (gains sox-input)) (length (gains sox-input))) do ;(db->lin (list! (gains sox-input))) -> but it's always going to be a list, no?
314                    ;           (setf str (string+ str (format nil " -v~d ~s " gain filename )))) ; is it not scaling when volumes are given? try out!
315
316                       (loop for filename in filenames do
317                               for gain in (db->lin (gains sox-input)) do ;(db->lin (list! (gains sox-input))) -> but it's always going to be a list, no?
318                               (setf str (string+ str (format nil " -v~d ~s " gain filename ))))
319                       ;)
320
321                     (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
322                                     ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p")) ; "-p is an alias for '-t sox -' " and it's better but I can't use as I need to provide the sox path again...
323                                     (t (sox-samplebits str bitdepth samplerate outfile))))
324
325                     (setf str (string+ str sox-effect))
326
327                     (sox-out str sox-input output outfile recursive))))
328
329               (sox-not-found))
330             )
331
332 ; === sox-multiply =============
333
334 (defmethod! sox-process ((sox-input sox-multiply) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
335             (if (probe-file *sox-path*)
336
337                  (progn
338                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe"))
339                     (om-message-abort "Pipe output not possible with this type of input."))
340
341                   (let ((outfile (create-path nil output filetype))) ;(create-path (first (soundfiles sox-input)) output filetype)
342
343                     (let* ((filenames 
344                             (loop for soundfile in (sound sox-input) collect ;I guess this throws the error when using pipes as inputs
345                                   (namestring soundfile))))
346
347                       (setf str (format nil "~s ~a -T " (namestring *sox-path*) *sox-options*))
348                                                           
349                       (loop for filename in filenames do
350                               for gain in (db->lin (gains sox-input)) do 
351                               (setf str (string+ str (format nil " -v~d ~s " gain filename )))) ; note that "-v" suppresses sox's in-built clipping protection
352                       ;)
353
354                     (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
355                                     ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p"))
356                                     (t (sox-samplebits str bitdepth samplerate outfile))))
357
358                     (setf str (string+ str sox-effect))
359
360                     (sox-out str sox-input output outfile recursive))))
361
362               (sox-not-found))
363             )
364
365
366 ; === sox-concatenate =============
367
368 (defmethod! sox-process ((sox-input sox-concatenate) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
369             (if (probe-file *sox-path*)
370
371                 (progn
372                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
373
374                   (let ((outfile (create-path nil output filetype))) ;(create-path (first (soundfiles sox-input)) output filetype)
375
376                     (let* ((filenames (loop for soundfile in (sound sox-input) collect
377                                             (namestring soundfile))))    
378                                
379                       (setf str (format nil "~s ~a " (namestring *sox-path*) *sox-options*))
380                                   
381                       (if (gains sox-input)
382                           (loop for filename in filenames do
383                                 for gain in (db->lin (list! (gains sox-input))) do
384                                 (setf str (string+ str (format nil " -v~d ~s  " gain filename ))))
385                         ;I think this case never happens
386                         (loop for filename in filenames do 
387                               (setf str (string+ str (format nil " ~s" filename)))))
388                       
389                       (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
390                                       ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p"))
391                                       (t (sox-samplebits str bitdepth samplerate outfile))))
392
393                       (setf str (string+ str sox-effect))
394                       (sox-out str sox-input output outfile recursive))))
395
396               (sox-not-found))
397             )
398
399 ; === sox-splice ============
400
401 (defmethod! sox-process ((sox-input sox-splice) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
402             (if (probe-file *sox-path*)
403              
404               (progn
405                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
406
407                   (let ((outfile (create-path nil output filetype)))
408
409                     (let* ((mysoundfiles (sound sox-input))
410                            (filenames (loop for soundfile in mysoundfiles collect (namestring soundfile)))
411                            (first-dur (sox-sound-duration (car (sound sox-input))))
412                            (begins (list! (splice-begin sox-input)))
413                            (ends (list! (splice-end sox-input)))
414                            (tols (list! (tolerance sox-input))))
415
416                       (sox-print (format nil "begins: ~a" begins))
417                       (sox-print (format nil "ends: ~a" ends))
418                       (sox-print (format nil "tols: ~a" tols))
419
420                       (setf str (format nil "~s ~a " (namestring *sox-path*) *sox-options*))
421                       
422                       (loop for filename in filenames do
423                             for gain in (db->lin (list! (gains sox-input))) do
424                             (setf str (string+ str (format nil " -v~d ~s " gain filename ))))
425
426                     (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
427                                     ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p"))
428                                     (t (sox-samplebits str bitdepth samplerate outfile))))
429
430                     (setf str 
431                           (cond ((eql (fade-type sox-input) 'linear)  (string+ str (format nil " splice -t " )))
432                                 ((eql (fade-type sox-input) 'half-cosine)  (string+ str (format nil " splice -h " )))
433                                 ((eql (fade-type sox-input) 'quarter-cosine)  (string+ str  (format nil " splice -q " )))
434                                 (t  (string+ str  (format nil " splice " )))))
435
436                     ;dbl-check the sum-calculation. Something's fishy!
437
438                     (loop for mysound in (sound sox-input) 
439                           for end in ends 
440                           counting end into ordinal
441                           summing (or (sox-sound-duration mysound) 0) into dur-sum
442                           do
443                           (progn (sox-print ordinal)
444                           (let ((i (- ordinal 1))
445                                 (current-sum (- dur-sum (or first-dur 0))))
446                             
447                             (sox-print (format nil "begins: ~d" (nth i begins)))
448                             (sox-print (format nil "ends: ~d" (nth i ends)))
449                             (sox-print (format nil "current-sum: ~d" current-sum))
450                             
451                           (cond ((and (nth i begins) (nth i tols))
452                                  (progn 
453                                    (sox-print "begin+tol") 
454                                    (setf str (string+ str (format nil "~d,~d,~d " (+ current-sum (nth i ends))  (* 0.5 (- (nth i ends) (nth i begins))) (nth i tols))))))
455                                 ((nth i begins)
456                                  (progn 
457                                    (print "begin") 
458                                    (setf str (string+ str (format nil "~d,~d " (+ current-sum (nth i ends)) (* 0.5 (- (nth i ends) (nth i begins))))))))
459                                 (t (progn 
460                                      (print "end")
461                                      (setf str (string+ str (format nil "~d " (+ current-sum (nth i ends))))))))
462                           )))
463
464                     (setf str (sox-commands sox-effect str))
465                     (sox-out str sox-input output outfile recursive)
466                     )))
467             (sox-not-found))
468             )
469
470
471 ; === sox-remix =============
472
473 (defmethod! sox-process ((sox-input sox-remix) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
474            ;(print (first (channels sox-input)))
475             (if (probe-file *sox-path*)
476
477                 (progn
478                   (when (and (stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
479
480                   (let* ((inpath (sound sox-input))
481                          (outfile (create-path inpath output filetype)) ;(create-path nil output filetype)
482                          (channel-matrix (list! (channel-matrix sox-input)))) ; is this still needed?
483                     
484                     (setf str (format nil "~s ~a ~s" (namestring *sox-path*) *sox-options* (namestring inpath)))
485                    ; (print (channels sox-input))
486                    ; (print (gains sox-input))
487
488                     (when bitdepth (setf str (string+ str (format nil " -b ~d" bitdepth)))) ;could I not use sox-samplebits here?
489                     (setf str (string+ str (format nil " ~s remix ~a" 
490                                                    (cond ((equal output "realtime")  *sox-audio-device*) ;should add a quiet mode here...
491                                                          ((equal output "pipe") "-p")
492                                                          (t (namestring outfile)))
493                                                    (sox-remixconc-no-norm (gain-matrix sox-input) (channel-matrix sox-input))
494                                                    )))
495
496                     (when samplerate (setf str (string+ str (format nil " rate ~d " samplerate))))
497                     (setf str (string+ str sox-effect))
498                     (sox-out str inpath output outfile recursive)))
499             (sox-not-found))
500             )
501
502 ; === sox-pan ====================
503
504 (defmethod! sox-process ((sox-input sox-pan) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
505            ;(print (first (channels sox-input)))
506             (sox-process (sox-pan->sox-remix (sound sox-input) (car (list! (gains sox-input))) (car (list! (panning sox-input))) (numchannels sox-input))
507                          sox-effect
508                          :output output
509                          :filetype filetype
510                          :samplerate samplerate
511                          :bitdepth bitdepth
512                          :recursive recursive
513                          :batch-mode batch-mode))
514
515
516 ; === sox-mix-console =============
517
518
519 ; Notes
520 ; I could make this sox-mix-console (non-consistent) a method where a list of effects will be applied to each sound in the mix-console
521 ;   OR: keep this as an extra function for the player
522 ; I can't use pipe-input to the mix-console as it makes sox-remix instances with pipe-outputs ->is a pipe in a pipe not possible?
523
524
525 (defmethod! sox-process ((sox-input sox-mix-console) (sox-effect t) &key output filetype samplerate bitdepth recursive batch-mode)
526             (if (probe-file *sox-path*)
527
528                 (progn
529                   ;(print "sox-process: sox-mix-console")
530                   (when (and (stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
531
532                   (let* ((inpath (sound sox-input))
533                          (outfile (create-path inpath output filetype)) ;(create-path nil output filetype)  ; Filetype MUST NOT be pipe (it can be only a sound)
534
535                          (list-of-pans (loop for path in (list! inpath)
536                                for gain in (gains sox-input)
537                                for pan in (panning sox-input) collect
538                                (make-instance 'sox-pan 
539                                               :sound path
540                                               :gains gain
541                                               :panning pan)))
542                          
543                          (list-of-pipes (loop for sox-pan in list-of-pans 
544                                               for i from 0 to (length list-of-pans) collect 
545                                               (sox-process sox-pan (or (nth i (list! sox-effect)) "") ;note that in this special case a list of effects is applied to each in-sound
546                                                            :output "pipe"
547                                                            :filetype filetype 
548                                                            :samplerate samplerate 
549                                                            :bitdepth bitdepth
550                                                            :recursive recursive 
551                                                            :batch-mode batch-mode)))
552                          )
553                     (if (> (length list-of-pipes) 1)
554                         (sox-process (make-instance 'sox-mix :sound list-of-pipes) ""
555                                      :output output
556                                      :filetype filetype 
557                                      :samplerate samplerate 
558                                      :bitdepth bitdepth
559                                      :recursive recursive 
560                                      :batch-mode batch-mode
561                                      )
562                       (sox-process list-of-pipes ""
563                                      :output output
564                                      :filetype filetype 
565                                      :samplerate samplerate 
566                                      :bitdepth bitdepth
567                                      :recursive recursive 
568                                      :batch-mode batch-mode
569                                      ))
570                       ))
571               (sox-not-found)
572               ))
573
574
575
576
577 ; === sox-split =============
578 ; special method with (sox-effect t) only for sox-split (returns a sequence of sox-remixes)
579
580 (defmethod! sox-process ((sox-input sox-split) (sox-effect t) &key output filetype samplerate bitdepth recursive batch-mode)
581             (if (probe-file *sox-path*)
582
583                 (progn
584                   (cond ((equal output "replace file") (om-message-abort "Replace output not possible with sox-split."))
585                         ((and (equal output "pipe") (stringp (sound sox-input))) (om-message-abort "Pipe output not possible with this type of input")))
586                   
587                   (let*
588                       ((thesoundfile (sound sox-input))
589                        (list-of-remixes (loop for channel in (channels sox-input)
590                                               for gain in (gains sox-input) collect
591                                               (make-instance 'sox-remix
592                                                              :sound thesoundfile
593                                                              :gain-matrix (list gain)
594                                                              :channel-matrix (list (list channel)))))
595                      ;here the 'realtime' and 'pipe' ??
596                        (list-of-outpaths  (loop for channel in (channels sox-input) collect
597                                                 (format nil "~a_omsox-ch~d" (pathname-name thesoundfile) channel))
598                                           ))
599                     
600                     (sox-process list-of-remixes sox-effect 
601                                  :output (or output list-of-outpaths)
602                                  :filetype filetype 
603                                  :samplerate samplerate 
604                                  :bitdepth bitdepth 
605                                  :recursive recursive 
606                                  :batch-mode batch-mode)))
607
608               (sox-not-found))
609             )
610
611
612
613
614
615 ; === sox-record =============
616
617 (defmethod! sox-process ((sox-input sox-record) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
618             (if (probe-file *sox-path*)
619
620                 (let ((outfile (create-path nil output filetype)))                         
621                   
622                   (setf str (format nil "~s ~a ~s" (namestring *sox-path*) *sox-options* *sox-audio-device*)) ;perhaps sox-recording vs playback-device?
623                   
624                   (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
625                                   ((equal output "pipe") (sox-samplebits (string+ str " -q ") bitdepth samplerate "-p"))
626                                   (t (sox-samplebits (string+ str " -q ") bitdepth samplerate outfile))))
627                   
628                   (setf str (string+ str (format nil " trim 0 ~d " (duration sox-input))))
629                   
630                   (cond ((and (channels sox-input) (gains sox-input))
631                          (let ((newstr (format nil " remix" )))
632                            (loop for channel in (channels sox-input)
633                                  for gain in (gains sox-input) do
634                                  (setf newstr (string+ newstr (format nil " ~av~d " channel (db->lin gain)))))                                                   
635                            (setf str (string+ str newstr))))
636                         
637                         ((gains sox-input)
638                          (let ((newstr (format nil " remix" )))
639                            (loop for item from 1 to (length (gains sox-input))
640                                  for gain in (gains sox-input) do
641                                  (setf newstr (string+ newstr (format nil " ~av~d " item (db->lin gain)))))
642                            (setf str (string+ str newstr))))
643                         
644                         ((channels sox-input)
645                          (let ((newstr (format nil " remix" )))
646                            (loop for channel in (channels sox-input) do
647                                  (setf newstr (string+ newstr (format nil " ~a" channel))))                                                   
648                            (setf str (string+ str newstr))))
649                         )
650
651                   (setf str (string+ str sox-effect))       
652
653                   (sox-out str sox-input output outfile recursive))
654                     
655               (sox-not-found))
656             )
657
658
659 ; === sox synth =========
660 #|
661 (defmethod! sox-process ((sox-input sox-synth) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
662             (if (probe-file *sox-path*)
663
664                 (let ((outfile (create-path nil output filetype)))
665                   (setf str (format nil "~s ~a -n" (namestring *sox-path*) *sox-options*))
666                   (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
667                                   ((equal output "pipe") (sox-samplebits str bitdepth samplerate '-p))
668                                   (t (sox-samplebits str bitdepth samplerate outfile))))
669                   (setf str (string+ str " synth" (sox-synth-format sox-input)))
670                   (setf str (string+ str sox-effect))
671                   (sox-out str sox-input output outfile recursive))
672               
673               (sox-not-found))
674             )          
675 |#
676 ; %%%%%%%%%%%% LIST METHODS %%%%%%%%%%%%%%%%%%%%%%
677
678 ; for string + list + list/string
679 (defmethod! sox-process ((sox-input t) (sox-effect list) &key output filetype samplerate bitdepth recursive batch-mode)
680             (if (and (listp output) (first output))
681                 (mapcar (lambda (thesox-effect thepath)
682                           (sox-process sox-input thesox-effect :output thepath :filetype filetype :samplerate samplerate 
683                                        :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-effect output)
684               (mapcar (lambda (thesox-effect)
685                         (sox-process sox-input thesox-effect :output output :filetype filetype :samplerate samplerate 
686                                      :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-effect)))
687
688 ; these functions below can be grouped together into multiple if/when statements for the different types of 'output'
689 ;--------------------------------------------------------------------------------------------------------------------
690 ; for list + string + string
691 (defmethod! sox-process ((sox-input list) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
692             (mapcar (lambda (file) 
693                       (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
694                                    :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input))
695
696 ; #### NEED TO MAKE AN EXAMPLE FOR THIS CASE // ALSO, THE LIST LIST FUNCTION method seems to be missing.. PROBABLY FOR AUTO_NAMING FILES (batch processing)
697 ; for list + string + function
698 (defmethod! sox-process ((sox-input list) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
699             (if (functionp output)
700                 ; these 2 cases seem to be identical 
701                 (mapcar (lambda (file) 
702                           (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
703                                        :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input)
704               (mapcar (lambda (file) 
705                         (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
706                                      :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input)))
707
708 ; for list + string + list 
709 (defmethod! sox-process ((sox-input list) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
710             (if (and (listp output) (first output))
711                 (mapcar (lambda (file thepath) 
712                           (sox-process file sox-effect :output thepath :filetype filetype :samplerate samplerate 
713                                        :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input output)
714               (mapcar (lambda (file) 
715                         (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
716                                      :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input)))
717
718 ; ---------------------------------------------------------------------------------------------------------------------
719
720 ; for list + list + string/list
721 (defmethod! sox-process ((sox-input list) (sox-effect list) &key output filetype samplerate bitdepth recursive batch-mode)
722             (let (
723                   (numsounds (length sox-input))
724                   (numsox-effect (length sox-effect)))
725              
726               (when (> numsounds numsox-effect)
727                 (progn
728                   (when (equal batch-mode 'cycle)
729                     (setf sox-effect
730                           (flat (group-list sox-effect (list numsounds) 'circular))))
731                  (when (equal batch-mode 'repeat)
732                     (setf sox-effect
733                           (flat (x-append sox-effect (repeat-n (last sox-effect) (- numsounds numsox-effect))))))))
734
735               (if (consp output)
736                   (mapcar (lambda (file thesox-effect thepaths)
737                             (sox-process file thesox-effect :output thepaths :filetype filetype :samplerate samplerate 
738                                          :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input sox-effect output)
739                 (mapcar (lambda (file thesox-effect)
740                             (sox-process file thesox-effect :output output :filetype filetype :samplerate samplerate 
741                                          :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input sox-effect))))
742
743
744 ; for anything else beep ------------------
745 (defmethod! sox-process ((sox-input t) (sox-effect t) &key output filetype samplerate bitdepth recursive batch-mode)
746     (om-beep-msg (format nil "!!! Wrong input type or command for sox-process: ~A with ~A" sox-input sox-effect)))
747
748