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