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