Work-in-progress repo for ambisonics extensions for OM-SoX
Marlon Schumacher
9 days ago b36c86db50ccd3ff51725fabf59c6e5723b7b2aa
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)
197ce0 184
MS 185 ; would be more consistent with OO programming to have a function sox-hoaencode->sox-merge 
186 ; (consider how to deal with multiple sounds to be hoaencoded and mixed later on... sox-mix might be better done in an explicit way rather than via the class directly. 
187 ; in that case a 3DC would have to be used by using points (point-pairs) in a loop 
188
791513 189     (if (probe-file *sox-path*)
AN 190         (progn
b36c86 191           (when 
MS 192               (not 
193                (or 
194                 (subtypep (type-of (positions sox-input)) '3dc)
195                 (and (subtypep (type-of (positions sox-input)) 'list) 
196                      (> (length (positions sox-input)) 0) 
197                      (subtypep (type-of (first (positions sox-input))) 'list) 
198                      (> (length (first (positions sox-input))) 1))
199                 )
200                )
201             (om-message-abort "positions must be of type 3dc, or a list of lists.")
a590ad 202             )
b36c86 203           (let* ((sound (list! (sound sox-input)))
MS 204                  (positions-ae (sox-hoaencode-auto-convert-positions (positions sox-input))))
205
206             (when (and (find-if 'stringp sound) (equal output "pipe")) 
207               (om-message-abort "Pipe output not possible with this type of input."))
208             
209             (let* ((filenames (loop for soundfile in sound collect (namestring soundfile)))
210                    (outfile (create-path nil output filetype)))
211               
212               (setf str (format nil " ~s ~a" (namestring *sox-path*) *sox-options*))
213               (if (= (length filenames) 1)
791513 214                         ; case: one file 
b36c86 215                   (let* ((filename (first filenames))
MS 216                          (position (first positions-ae)))
217                     (if (= (order sox-input) 0) 
218                      ; case: order = 0
219                         (setf str (string+ str (format nil " ~s" filename)))
220                      ; case: order > 0
221                       (progn
222                         (setf str (string+ str " -M"))
223                         (loop for gain in (sox-hoaencode-gains-up-to-order (order sox-input) (first position) (second position)) 
224                               do (setf str (string+ str (format nil " -v~d ~s" gain filename))))
791513 225                         )
b36c86 226                       )
MS 227                     )
228                       ; case: multiple files
229                 (if (= (order sox-input) 0)
230                  ; case: order = 0
231                     (progn 
232                       (setf str (string+ str " -m")) ; this is the mixer. e.g. sox-mix
233                                 (loop for filename in filenames 
234                                       do (setf str (string+ str (format nil " ~s" filename))))
791513 235                                 )
b36c86 236                  ; case: order > 0
MS 237                   (progn
238                     (setf str (string+ str " -m"))
239                     
791513 240                                 ; Create components 
b36c86 241                     (setf component_idx 0)
MS 242                     (loop 
243                        for filename in filenames
244                        for position in positions-ae 
245                        do
197ce0 246
MS 247                                                                    ; in Object-Oriented Programming we would call-next-method i.e. set a sox-merge input class with the corresponding params. 
248                                                                    ; e.g. (make-instance 'sox-merge ... (more redundancy and compactness, less error-prone) ... possible?
249                                                                    ; for multiple sound inputs if possible we would then call a sox-mix to mix the sox merge pipes. everything
250
b36c86 251                          (progn  
MS 252                            (setf str_component (format nil " ~s ~a -M" (namestring *sox-path*) *sox-options*))
253                            (loop for gain in (sox-hoaencode-gains-up-to-order (order sox-input) (first position) (second position)) 
254                                  do (setf str_component (string+ str_component (format nil " -v~d ~s" gain filename))))
791513 255
b36c86 256                            (setf outfile_tmp (format nil "~a~a-~d.~a" (directory-namestring outfile) (pathname-name outfile) component_idx (pathname-type outfile)))
MS 257                            (setf str_component (sox-samplebits str_component bitdepth samplerate outfile_tmp))
258                            (sox-out str_component filename "new file" outfile_tmp nil)
259                            
260                            (setf component_idx (1+ component_idx))
791513 261
AN 262                                         ; Add component path to <str>
b36c86 263                            (setf str (string+ str (format nil " ~s" outfile_tmp)))
MS 264                            )
265                        )
791513 266                     )
b36c86 267                   )
MS 268                 )
269               (setf str 
270                     (cond 
271                      ((equal output "realtime") 
272                       (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
273                      ((equal output "pipe") 
274                       (sox-samplebits str bitdepth samplerate "-p"))
275                      (t 
276                       (sox-samplebits str bitdepth samplerate outfile))
277                      )
791513 278                     )
b36c86 279               (setf str (string+ str sox-effect))
MS 280               (print str)
281               (sox-out str sox-input output outfile recursive)
197ce0 282
MS 283                        ;optional removal of temp file
284                   ;    (add-tmp-file outfile_tmp)
285                   ;    (when *delete-inter-file* (clean-tmp-files))
286
b36c86 287               )
791513 288             )
b36c86 289           )
MS 290       (sox-not-found)
291       )
791513 292     )
AN 293
92c40d 294 ; === sox-mix =============
AN 295
296 (defmethod! sox-process ((sox-input sox-mix) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
297             (if (probe-file *sox-path*)
298
299                  (progn
300                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe"))
301                     (om-message-abort "Pipe output not possible with this type of input."))
302
303                   (let ((outfile (create-path nil output filetype))) ;(create-path (first (soundfiles sox-input)) output filetype)
304
305                     (let* ((filenames 
306                             (loop for soundfile in (sound sox-input) collect ;I guess this throws the error when using pipes as inputs
307                                   (namestring soundfile))))
308
309                       (setf str (format nil "~s ~a -m " (namestring *sox-path*) *sox-options*))
310                                                           
311                       ;potentially not needed...
312                    ;(if (1/n sox-input) 
313                         
314                    ;     (loop for filename in filenames do
315                    ;           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?
316                    ;           (setf str (string+ str (format nil " -v~d ~s " gain filename )))) ; is it not scaling when volumes are given? try out!
317
318                       (loop for filename in filenames do
319                               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?
320                               (setf str (string+ str (format nil " -v~d ~s " gain filename ))))
321                       ;)
322
323                     (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
324                                     ((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...
325                                     (t (sox-samplebits str bitdepth samplerate outfile))))
326
327                     (setf str (string+ str sox-effect))
328
329                     (sox-out str sox-input output outfile recursive))))
330
331               (sox-not-found))
332             )
333
334 ; === sox-multiply =============
335
336 (defmethod! sox-process ((sox-input sox-multiply) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
337             (if (probe-file *sox-path*)
338
339                  (progn
340                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe"))
341                     (om-message-abort "Pipe output not possible with this type of input."))
342
343                   (let ((outfile (create-path nil output filetype))) ;(create-path (first (soundfiles sox-input)) output filetype)
344
345                     (let* ((filenames 
346                             (loop for soundfile in (sound sox-input) collect ;I guess this throws the error when using pipes as inputs
347                                   (namestring soundfile))))
348
349                       (setf str (format nil "~s ~a -T " (namestring *sox-path*) *sox-options*))
350                                                           
351                       (loop for filename in filenames do
352                               for gain in (db->lin (gains sox-input)) do 
353                               (setf str (string+ str (format nil " -v~d ~s " gain filename )))) ; note that "-v" suppresses sox's in-built clipping protection
354                       ;)
355
356                     (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
357                                     ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p"))
358                                     (t (sox-samplebits str bitdepth samplerate outfile))))
359
360                     (setf str (string+ str sox-effect))
361
362                     (sox-out str sox-input output outfile recursive))))
363
364               (sox-not-found))
365             )
366
367
368 ; === sox-concatenate =============
369
370 (defmethod! sox-process ((sox-input sox-concatenate) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
371             (if (probe-file *sox-path*)
372
373                 (progn
374                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
375
376                   (let ((outfile (create-path nil output filetype))) ;(create-path (first (soundfiles sox-input)) output filetype)
377
378                     (let* ((filenames (loop for soundfile in (sound sox-input) collect
379                                             (namestring soundfile))))    
380                                
381                       (setf str (format nil "~s ~a " (namestring *sox-path*) *sox-options*))
382                                   
383                       (if (gains sox-input)
384                           (loop for filename in filenames do
385                                 for gain in (db->lin (list! (gains sox-input))) do
386                                 (setf str (string+ str (format nil " -v~d ~s  " gain filename ))))
387                         ;I think this case never happens
388                         (loop for filename in filenames do 
389                               (setf str (string+ str (format nil " ~s" filename)))))
390                       
391                       (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
392                                       ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p"))
393                                       (t (sox-samplebits str bitdepth samplerate outfile))))
394
395                       (setf str (string+ str sox-effect))
396                       (sox-out str sox-input output outfile recursive))))
397
398               (sox-not-found))
399             )
400
401 ; === sox-splice ============
402
403 (defmethod! sox-process ((sox-input sox-splice) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
404             (if (probe-file *sox-path*)
405              
406               (progn
407                   (when (and (find-if 'stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
408
409                   (let ((outfile (create-path nil output filetype)))
410
411                     (let* ((mysoundfiles (sound sox-input))
412                            (filenames (loop for soundfile in mysoundfiles collect (namestring soundfile)))
413                            (first-dur (sox-sound-duration (car (sound sox-input))))
414                            (begins (list! (splice-begin sox-input)))
415                            (ends (list! (splice-end sox-input)))
416                            (tols (list! (tolerance sox-input))))
417
418                       (sox-print (format nil "begins: ~a" begins))
419                       (sox-print (format nil "ends: ~a" ends))
420                       (sox-print (format nil "tols: ~a" tols))
421
422                       (setf str (format nil "~s ~a " (namestring *sox-path*) *sox-options*))
423                       
424                       (loop for filename in filenames do
425                             for gain in (db->lin (list! (gains sox-input))) do
426                             (setf str (string+ str (format nil " -v~d ~s " gain filename ))))
427
428                     (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
429                                     ((equal output "pipe") (sox-samplebits str bitdepth samplerate "-p"))
430                                     (t (sox-samplebits str bitdepth samplerate outfile))))
431
432                     (setf str 
433                           (cond ((eql (fade-type sox-input) 'linear)  (string+ str (format nil " splice -t " )))
434                                 ((eql (fade-type sox-input) 'half-cosine)  (string+ str (format nil " splice -h " )))
435                                 ((eql (fade-type sox-input) 'quarter-cosine)  (string+ str  (format nil " splice -q " )))
436                                 (t  (string+ str  (format nil " splice " )))))
437
438                     ;dbl-check the sum-calculation. Something's fishy!
439
440                     (loop for mysound in (sound sox-input) 
441                           for end in ends 
442                           counting end into ordinal
443                           summing (or (sox-sound-duration mysound) 0) into dur-sum
444                           do
445                           (progn (sox-print ordinal)
446                           (let ((i (- ordinal 1))
447                                 (current-sum (- dur-sum (or first-dur 0))))
448                             
449                             (sox-print (format nil "begins: ~d" (nth i begins)))
450                             (sox-print (format nil "ends: ~d" (nth i ends)))
451                             (sox-print (format nil "current-sum: ~d" current-sum))
452                             
453                           (cond ((and (nth i begins) (nth i tols))
454                                  (progn 
455                                    (sox-print "begin+tol") 
456                                    (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))))))
457                                 ((nth i begins)
458                                  (progn 
459                                    (print "begin") 
460                                    (setf str (string+ str (format nil "~d,~d " (+ current-sum (nth i ends)) (* 0.5 (- (nth i ends) (nth i begins))))))))
461                                 (t (progn 
462                                      (print "end")
463                                      (setf str (string+ str (format nil "~d " (+ current-sum (nth i ends))))))))
464                           )))
465
466                     (setf str (sox-commands sox-effect str))
467                     (sox-out str sox-input output outfile recursive)
468                     )))
469             (sox-not-found))
470             )
471
472
473 ; === sox-remix =============
474
475 (defmethod! sox-process ((sox-input sox-remix) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
476            ;(print (first (channels sox-input)))
477             (if (probe-file *sox-path*)
478
479                 (progn
480                   (when (and (stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
481
482                   (let* ((inpath (sound sox-input))
483                          (outfile (create-path inpath output filetype)) ;(create-path nil output filetype)
484                          (channel-matrix (list! (channel-matrix sox-input)))) ; is this still needed?
485                     
486                     (setf str (format nil "~s ~a ~s" (namestring *sox-path*) *sox-options* (namestring inpath)))
487                    ; (print (channels sox-input))
488                    ; (print (gains sox-input))
489
490                     (when bitdepth (setf str (string+ str (format nil " -b ~d" bitdepth)))) ;could I not use sox-samplebits here?
491                     (setf str (string+ str (format nil " ~s remix ~a" 
492                                                    (cond ((equal output "realtime")  *sox-audio-device*) ;should add a quiet mode here...
493                                                          ((equal output "pipe") "-p")
494                                                          (t (namestring outfile)))
495                                                    (sox-remixconc-no-norm (gain-matrix sox-input) (channel-matrix sox-input))
496                                                    )))
497
498                     (when samplerate (setf str (string+ str (format nil " rate ~d " samplerate))))
499                     (setf str (string+ str sox-effect))
500                     (sox-out str inpath output outfile recursive)))
501             (sox-not-found))
502             )
503
504 ; === sox-pan ====================
505
506 (defmethod! sox-process ((sox-input sox-pan) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
507            ;(print (first (channels sox-input)))
508             (sox-process (sox-pan->sox-remix (sound sox-input) (car (list! (gains sox-input))) (car (list! (panning sox-input))) (numchannels sox-input))
509                          sox-effect
510                          :output output
511                          :filetype filetype
512                          :samplerate samplerate
513                          :bitdepth bitdepth
514                          :recursive recursive
515                          :batch-mode batch-mode))
516
517
518 ; === sox-mix-console =============
519
520
197ce0 521 ; Notes on polymorphism
92c40d 522 ; 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
AN 523 ;   OR: keep this as an extra function for the player
524 ; 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?
525
526
527 (defmethod! sox-process ((sox-input sox-mix-console) (sox-effect t) &key output filetype samplerate bitdepth recursive batch-mode)
528             (if (probe-file *sox-path*)
529
530                 (progn
531                   ;(print "sox-process: sox-mix-console")
532                   (when (and (stringp (sound sox-input)) (equal output "pipe")) (om-message-abort "Pipe output not possible with this type of input."))
533
534                   (let* ((inpath (sound sox-input))
535                          (outfile (create-path inpath output filetype)) ;(create-path nil output filetype)  ; Filetype MUST NOT be pipe (it can be only a sound)
536
537                          (list-of-pans (loop for path in (list! inpath)
538                                for gain in (gains sox-input)
539                                for pan in (panning sox-input) collect
540                                (make-instance 'sox-pan 
541                                               :sound path
542                                               :gains gain
543                                               :panning pan)))
544                          
545                          (list-of-pipes (loop for sox-pan in list-of-pans 
546                                               for i from 0 to (length list-of-pans) collect 
547                                               (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
548                                                            :output "pipe"
549                                                            :filetype filetype 
550                                                            :samplerate samplerate 
551                                                            :bitdepth bitdepth
552                                                            :recursive recursive 
553                                                            :batch-mode batch-mode)))
554                          )
555                     (if (> (length list-of-pipes) 1)
556                         (sox-process (make-instance 'sox-mix :sound list-of-pipes) ""
557                                      :output output
558                                      :filetype filetype 
559                                      :samplerate samplerate 
560                                      :bitdepth bitdepth
561                                      :recursive recursive 
562                                      :batch-mode batch-mode
563                                      )
564                       (sox-process list-of-pipes ""
565                                      :output output
566                                      :filetype filetype 
567                                      :samplerate samplerate 
568                                      :bitdepth bitdepth
569                                      :recursive recursive 
570                                      :batch-mode batch-mode
571                                      ))
572                       ))
573               (sox-not-found)
574               ))
575
576
577
578
579 ; === sox-split =============
580 ; special method with (sox-effect t) only for sox-split (returns a sequence of sox-remixes)
581
582 (defmethod! sox-process ((sox-input sox-split) (sox-effect t) &key output filetype samplerate bitdepth recursive batch-mode)
583             (if (probe-file *sox-path*)
584
585                 (progn
586                   (cond ((equal output "replace file") (om-message-abort "Replace output not possible with sox-split."))
587                         ((and (equal output "pipe") (stringp (sound sox-input))) (om-message-abort "Pipe output not possible with this type of input")))
588                   
589                   (let*
590                       ((thesoundfile (sound sox-input))
591                        (list-of-remixes (loop for channel in (channels sox-input)
592                                               for gain in (gains sox-input) collect
593                                               (make-instance 'sox-remix
594                                                              :sound thesoundfile
595                                                              :gain-matrix (list gain)
596                                                              :channel-matrix (list (list channel)))))
597                      ;here the 'realtime' and 'pipe' ??
598                        (list-of-outpaths  (loop for channel in (channels sox-input) collect
599                                                 (format nil "~a_omsox-ch~d" (pathname-name thesoundfile) channel))
600                                           ))
601                     
602                     (sox-process list-of-remixes sox-effect 
603                                  :output (or output list-of-outpaths)
604                                  :filetype filetype 
605                                  :samplerate samplerate 
606                                  :bitdepth bitdepth 
607                                  :recursive recursive 
608                                  :batch-mode batch-mode)))
609
610               (sox-not-found))
611             )
612
613
614
615
616
617 ; === sox-record =============
618
619 (defmethod! sox-process ((sox-input sox-record) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
620             (if (probe-file *sox-path*)
621
622                 (let ((outfile (create-path nil output filetype)))                         
623                   
624                   (setf str (format nil "~s ~a ~s" (namestring *sox-path*) *sox-options* *sox-audio-device*)) ;perhaps sox-recording vs playback-device?
625                   
626                   (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
627                                   ((equal output "pipe") (sox-samplebits (string+ str " -q ") bitdepth samplerate "-p"))
628                                   (t (sox-samplebits (string+ str " -q ") bitdepth samplerate outfile))))
629                   
630                   (setf str (string+ str (format nil " trim 0 ~d " (duration sox-input))))
631                   
632                   (cond ((and (channels sox-input) (gains sox-input))
633                          (let ((newstr (format nil " remix" )))
634                            (loop for channel in (channels sox-input)
635                                  for gain in (gains sox-input) do
636                                  (setf newstr (string+ newstr (format nil " ~av~d " channel (db->lin gain)))))                                                   
637                            (setf str (string+ str newstr))))
638                         
639                         ((gains sox-input)
640                          (let ((newstr (format nil " remix" )))
641                            (loop for item from 1 to (length (gains sox-input))
642                                  for gain in (gains sox-input) do
643                                  (setf newstr (string+ newstr (format nil " ~av~d " item (db->lin gain)))))
644                            (setf str (string+ str newstr))))
645                         
646                         ((channels sox-input)
647                          (let ((newstr (format nil " remix" )))
648                            (loop for channel in (channels sox-input) do
649                                  (setf newstr (string+ newstr (format nil " ~a" channel))))                                                   
650                            (setf str (string+ str newstr))))
651                         )
652
653                   (setf str (string+ str sox-effect))       
654
655                   (sox-out str sox-input output outfile recursive))
656                     
657               (sox-not-found))
658             )
659
660
661 ; === sox synth =========
662 #|
663 (defmethod! sox-process ((sox-input sox-synth) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
664             (if (probe-file *sox-path*)
665
666                 (let ((outfile (create-path nil output filetype)))
667                   (setf str (format nil "~s ~a -n" (namestring *sox-path*) *sox-options*))
668                   (setf str (cond ((equal output "realtime") (sox-samplebits (string+ str " -q ") bitdepth samplerate *sox-audio-device*))
669                                   ((equal output "pipe") (sox-samplebits str bitdepth samplerate '-p))
670                                   (t (sox-samplebits str bitdepth samplerate outfile))))
671                   (setf str (string+ str " synth" (sox-synth-format sox-input)))
672                   (setf str (string+ str sox-effect))
673                   (sox-out str sox-input output outfile recursive))
674               
675               (sox-not-found))
676             )          
677 |#
678 ; %%%%%%%%%%%% LIST METHODS %%%%%%%%%%%%%%%%%%%%%%
679
680 ; for string + list + list/string
681 (defmethod! sox-process ((sox-input t) (sox-effect list) &key output filetype samplerate bitdepth recursive batch-mode)
682             (if (and (listp output) (first output))
683                 (mapcar (lambda (thesox-effect thepath)
684                           (sox-process sox-input thesox-effect :output thepath :filetype filetype :samplerate samplerate 
685                                        :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-effect output)
686               (mapcar (lambda (thesox-effect)
687                         (sox-process sox-input thesox-effect :output output :filetype filetype :samplerate samplerate 
688                                      :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-effect)))
689
690 ; these functions below can be grouped together into multiple if/when statements for the different types of 'output'
691 ;--------------------------------------------------------------------------------------------------------------------
692 ; for list + string + string
693 (defmethod! sox-process ((sox-input list) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
694             (mapcar (lambda (file) 
695                       (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
696                                    :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input))
697
698 ; #### 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)
699 ; for list + string + function
700 (defmethod! sox-process ((sox-input list) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
701             (if (functionp output)
702                 ; these 2 cases seem to be identical 
703                 (mapcar (lambda (file) 
704                           (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
705                                        :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input)
706               (mapcar (lambda (file) 
707                         (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
708                                      :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input)))
709
710 ; for list + string + list 
711 (defmethod! sox-process ((sox-input list) (sox-effect string) &key output filetype samplerate bitdepth recursive batch-mode)
712             (if (and (listp output) (first output))
713                 (mapcar (lambda (file thepath) 
714                           (sox-process file sox-effect :output thepath :filetype filetype :samplerate samplerate 
715                                        :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input output)
716               (mapcar (lambda (file) 
717                         (sox-process file sox-effect :output output :filetype filetype :samplerate samplerate 
718                                      :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input)))
719
720 ; ---------------------------------------------------------------------------------------------------------------------
721
722 ; for list + list + string/list
723 (defmethod! sox-process ((sox-input list) (sox-effect list) &key output filetype samplerate bitdepth recursive batch-mode)
724             (let (
725                   (numsounds (length sox-input))
726                   (numsox-effect (length sox-effect)))
727              
728               (when (> numsounds numsox-effect)
729                 (progn
730                   (when (equal batch-mode 'cycle)
731                     (setf sox-effect
732                           (flat (group-list sox-effect (list numsounds) 'circular))))
733                  (when (equal batch-mode 'repeat)
734                     (setf sox-effect
735                           (flat (x-append sox-effect (repeat-n (last sox-effect) (- numsounds numsox-effect))))))))
736
737               (if (consp output)
738                   (mapcar (lambda (file thesox-effect thepaths)
739                             (sox-process file thesox-effect :output thepaths :filetype filetype :samplerate samplerate 
740                                          :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input sox-effect output)
741                 (mapcar (lambda (file thesox-effect)
742                             (sox-process file thesox-effect :output output :filetype filetype :samplerate samplerate 
743                                          :bitdepth bitdepth :recursive recursive :batch-mode batch-mode)) sox-input sox-effect))))
744
745
746 ; for anything else beep ------------------
747 (defmethod! sox-process ((sox-input t) (sox-effect t) &key output filetype samplerate bitdepth recursive batch-mode)
748     (om-beep-msg (format nil "!!! Wrong input type or command for sox-process: ~A with ~A" sox-input sox-effect)))
749
750