-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudio.tin
643 lines (574 loc) · 29 KB
/
audio.tin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defnet audio-cb (self)
(opt (audio-low self)) )
(defnet audio-low (self)
(deflocal audio-info q vbox dlg)
(truep (cfg-exists (key-map <_kv "av1">
<_kv "av2">
(fullpath->name <_kv "path1">)
(fullpath->name <_kv "path2">) )))
(set audio-info (assoc))
(set <_kv "audio-info"> audio-info)
(set q (queue))
(set vbox (iup-vbox
(audio-setup-open 1 q)
(audio-setup-label 1)
(iup-horizontal-separator)
(audio-setup-open 2 q)
(audio-setup-label 2)
(iup-horizontal-separator)
(audio-setup-bottom q) ))
(iup-set-str-attribute vbox "MARGIN" "4x4")
(iup-set-int vbox "GAP" 0)
(set dlg (iup-dialog vbox))
(set <audio-info "dlg"> dlg)
(iup-set-dlg-parent self dlg)
(iup-set-str-attribute dlg "TITLE" $"Audio tracks")
(iup-set-bool dlg "DIALOGFRAME" true)
(iup-set-callback dlg "K_ANY" (netptr audio-kb-cb))
(iup-popup dlg (cmacro IUP_CENTERPARENT) (cmacro IUP_CENTERPARENT))
(close dlg)
(iup-close-queue q)
(assoc-clr _kv "audio-info") )
(defun audio-setup-open (which q) net audio-setup-open)
(defnet audio-setup-open (which q @wid)
(deflocal audio-info but text spin)
(set audio-info <_kv "audio-info">)
(set but (iup-button-icon (+ $"select audio" ' ' which)
(if (= which 1)
(netptr audio-open1-cb)
(netptr audio-open2-cb) )
(pix-load-static "16x16/document-open-5.png")
q ))
(set text (iup-text))
(set <audio-info (+ "text" which)> text)
(iup-set-bool text "READONLY" true)
(iup-set-int text "VISIBLECOLUMNS" 30)
(set spin (iup-text-spin -3000 3000 (audio-setup-open-delay which)))
(set <audio-info (+ "sb-delay" which)> spin)
(iup-set-active spin false)
(set @wid (iup-hbox but text (iup-label $"delay (ms): ") spin))
(iup-set-int @wid "GAP" 3)
(iup-set-str-attribute @wid "ALIGNMENT" "ACENTER") )
(defun audio-setup-label (which) net audio-setup-label)
(defnet audio-setup-label (which @wid)
(deflocal label)
(set label (iup-label))
(set <_kv "audio-info" (+ "label" which)> label)
(iup-set-bool label "EXPAND" true)
;(iup-set-str-attribute label "ALIGNMENT" "ARIGHT")
(iup-set-str-attribute label "ALIGNMENT" "ACENTER")
(set @wid (iup-hbox label))
(iup-set-str-attribute @wid "ALIGNMENT" "ACENTER") )
(defun audio-setup-bottom (q) net audio-setup-bottom)
(defnet audio-setup-bottom (q @wid)
(deflocal audio-info l1 l2 btn-save btn-ok i)
(set audio-info <_kv "audio-info">)
(set l1 (iup-list))
(set <audio-info "combo1"> l1)
(iup-set-bool l1 "DROPDOWN" true)
(iup-set-bool l1 "EDITBOX" true)
(iup-set-str-attribute l1 "MASK" "(/d+/.?/d*|/./d+)")
(for i in (framerates) do
(iup-set-str-attribute l1 (+ (for-pos) 1) (approx3 i)) )
(iup-set-str-attribute l1 "VALUE" (approx3 <(framerates) 0>))
(set l2 (iup-list))
(set <audio-info "combo2"> l2)
(iup-set-bool l2 "DROPDOWN" true)
(iup-set-str-attribute l2 1 "video 1 + audio 2")
(iup-set-str-attribute l2 2 "video 2 + audio 1")
(iup-set-int l2 "VALUE" (if (>= (width <_kv "av1">) (width <_kv "av2">)) 1 2))
(set btn-save (iup-button-icon $"save track as..." (netptr audio-save-as-cb) (pix-load-static "16x16/document-save-5.png") q))
(set <audio-info "btn-save"> btn-save)
(iup-set-active btn-save false)
(set btn-ok (iup-button-icon-ok (netptr iup-cancel-cb) q))
(set <audio-info "btn-ok"> btn-ok)
(set @wid (iup-hbox l1 l2 btn-save (iup-fill) btn-ok))
(iup-set-int @wid "GAP" 3)
(iup-set-str-attribute @wid "ALIGNMENT" "ACENTER") )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defnet audio-kb-cb (dlg c)
(case c of
(cmacro K_ESC) (iup-exit-loop)
(cmacro K_F1) (audio-open1-cb dlg)
(cmacro K_F2) (audio-open2-cb dlg)
default (fail) ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun audio-setup-open-delay (which) 0)
(defun audio-setup-open-delay- (which) net audio-setup-open-delay)
(defnet audio-setup-open-delay (which @t)
(deflocal av aidx i)
(alt (seq (set av (av-avformat-open-input <_kv (+ "path" which)>))
(<> av undef)
(for i in 0 .. (- (av-nb-streams av) 1) do
(if (= (av-codec-type av i) (cmacro AVMEDIA_TYPE_AUDIO))
then (set aidx i) )
until (integerp aidx) )
(if (not (integerp aidx))
then (close av)
(fail) )
(set @t (rint (* (- (* (av-first-ts av aidx) (av-time-base av aidx))
(* (av-first-ts av) (av-time-base av (av-video-stream-idx av))) )
1000 )))
(close av) )
(set @t 0) ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defnet audio-open1-cb (self)
(opt (audio-open-low self 1)) )
(defnet audio-open2-cb (self)
(opt (audio-open-low self 2)) )
(defnet audio-open-low (self which)
(deflocal audio-info duration path f th i j)
(set path (cfg-get "audio-path"))
(if (not (stringp path))
then (set path (cfg-get "path")) )
(set path (iup-choose-file-open self (+ $"Select audio" ' ' which) path undef true))
(stringp path)
(set f (fopenro path))
(<> f undef)
(cfg-set "audio-path" path)
(set audio-info <_kv "audio-info">)
(set th (thread-create (netptr audio-open-th) (thread-self) f which audio-info))
(iup-progress th self "Parsing audio..." false true false true false)
(receive duration from th)
(thread-join th)
(close f)
(if (= duration undef)
then (sound-iup-error60 self (+ path ": " $"unrecognized audio format"))
(fail) )
(iup-set-str-attribute <audio-info (+ "label" which)> "TITLE" <duration 1>)
(if (<> <duration 2> undef)
then (iup-warning60 self <duration 2>) )
(set duration <duration 0>)
(iup-set-str-attribute <audio-info (+ "text" which)> "VALUE" (fullpath->name path))
(set j (/ (av-approximated-number-of-frames <_kv (+ "av" which)>) duration))
(for i in (framerates) rev do
(if (>= (/ (min i j) (max i j)) 0.9991)
then (iup-set-str-attribute <audio-info "combo1"> "VALUE" (approx3 i)) ))
(set <audio-info (+ "duration" which)> duration)
(set <audio-info (+ "path" which)> path)
(iup-set-active <audio-info (+ "sb-delay" which)> (in <audio-info (+ "codec" which)> [ "WAV" "SOX" ]))
(iup-set-active <audio-info "btn-save"> (stringp <audio-info (+ "path" (- 3 which))>)) )
(defnet audio-open-th (th f which audio-info)
(deflocal duration)
(alt (audio-open-wav f which audio-info duration)
(audio-open-sox f which audio-info duration)
(audio-open-compressed th f which audio-info duration)
(success) )
(send "q" to th)
(send duration to th) )
(defnet audio-open-wav (f which audio-info @duration)
(deflocal ch freq smplsize bps offset smpl)
(fsetpos 0 f)
(= (freadstr f 4) "RIFF")
(skip (freaduint-le f 32))
(= (freadstr f 8) "WAVEfmt ")
(set offset (freaduint-le f 32))
(>= offset 16)
(skip (freaduint-le f 16))
(set ch (freaduint-le f 16))
(set freq (freaduint-le f 32))
(skip (freaduint-le f 32))
(set smplsize (freaduint-le f 16))
(set bps (freaduint-le f 16))
(fsetpos (+ offset 20) f)
(while (<> (freadstr f 4) "data") do
(fsetpos (+ (fpos f) (freaduint-le f 32)) f) )
(skip (freaduint-le f 32))
(set offset (fpos f))
(set smpl (div (- (length f) offset) smplsize))
(> smpl 0)
(set @duration (/ smpl freq))
(set @duration (list @duration
(+ "WAVE " ch "ch " freq "Hz " bps "bps " (date-s2hhmmss @duration))
undef ))
(set <audio-info (+ "codec" which)> "WAV")
(set <audio-info (+ "mode" which)> ch)
(set <audio-info (+ "frequency" which)> freq)
(set <audio-info (+ "smplsize" which)> smplsize)
(set <audio-info (+ "bps" which)> bps)
(set <audio-info (+ "smpl" which)> smpl)
(set <audio-info (+ "ts" which)> offset) )
(defnet audio-open-sox (f which audio-info @duration)
(deflocal ch freq smplsize bps offset smpl comment-size)
(fsetpos 0 f)
(= (freadstr f 4) ".SoX")
(set offset (freaduint-le f 32))
(skip (freaduint-le f 64))
(set freq (floor (freadfloat-le f 64)))
(set ch (freaduint-le f 32))
(set comment-size (freaduint-le f 32))
(<= comment-size (- 0xFFFFFFFF (+ 4 8 8 4 4) 4))
(> freq 0)
(< freq (maxint))
(> ch 0)
(< ch (maxint))
(set smplsize (* 4 ch))
(set bps 32)
(inc offset 4)
(set smpl (div (- (length f) offset) smplsize))
(> smpl 0)
(set @duration (/ smpl freq))
(set @duration (list @duration
(+ "SoX " ch "ch " freq "Hz " bps "bps " (date-s2hhmmss @duration))
undef ))
(set <audio-info (+ "codec" which)> "SOX")
(set <audio-info (+ "mode" which)> ch)
(set <audio-info (+ "frequency" which)> freq)
(set <audio-info (+ "smplsize" which)> smplsize)
(set <audio-info (+ "bps" which)> bps)
(set <audio-info (+ "smpl" which)> smpl)
(set <audio-info (+ "ts" which)> offset) )
(defnet audio-open-compressed (th f which audio-info @duration)
(deflocal dlg aud a n i)
(send "dlg" to th)
(receive dlg from th)
(fsetpos 0 f)
(set aud (aud-create f))
(set n 0)
(set i (* (length f) 2))
(opt* (not (iup-progress-aborted dlg))
(aud-parse-step aud)
(inc n)
(if (= (% n 4000) 1)
then (sendbuf 100 (/ (fpos f) i) to th) ))
(close aud)
(not (iup-progress-aborted dlg))
(fsetpos 0 f)
(set aud (aud-create f))
(set a (array (+ n 1) 2))
(set <a 0 0> 0)
(set <a 0 1> 0)
(for i in 1 .. n do
(if (iup-progress-aborted dlg)
then (close aud)
(fail) )
(aud-parse-step aud)
(set <a i 0> (aud-duration aud))
(set <a i 1> (- (fpos f) (aud-buf-act aud)))
(if (= (% i 4000) 1)
then (sendbuf 100 (+ (/ i n 2) 0.5) to th) ))
(if (or (= (aud-codec aud) undef) (< n 5)
(< (/ (- <a (- n 1) 1> (aud-initial-skip aud) (aud-internal-skip aud))
(length f) )
0.9 ))
then (close aud)
(fail) )
(set @duration (aud-duration aud))
(set @duration (list @duration
(+ (aud-codec aud) ' '
(aud-mode aud) ' '
(aud-frequency aud) "Hz "
(date-s2hhmmss @duration) )
(if (or (> (aud-initial-skip aud) 0) (> (aud-internal-skip aud) 0))
$"It seems like the audio track contains "\
"extraneous data. Please fix it."
undef )))
(set <audio-info (+ "codec" which)> (aud-codec aud))
(set <audio-info (+ "mode" which)> (aud-mode aud))
(set <audio-info (+ "frequency" which)> (aud-frequency aud))
(assoc-clr audio-info (+ "smplsize" which))
(assoc-clr audio-info (+ "bps" which))
(assoc-clr audio-info (+ "smpl" which))
(set <audio-info (+ "ts" which)> a)
(close aud) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defnet audio-save-as-cb (self)
(opt (audio-save-as-low self)) )
(defnet audio-save-as-low (self)
(deflocal audio-info dlg fps which path g)
(set audio-info <_kv "audio-info">)
(set dlg <audio-info "dlg">)
(set fps (str->num (iup-get-str-attribute <audio-info "combo1"> "VALUE")))
(if (or (not (rationalp fps)) (<= fps 0))
then (iup-warning60 dlg $"Please, select a valid frame rate")
(fail) )
(case fps of
23.976 (set fps (/ 24000 1001))
29.97 (set fps (/ 30000 1001)) )
(set which (iup-get-int <audio-info "combo2"> "VALUE"))
(if (or (<> <audio-info "codec1"> <audio-info "codec2">)
(<> <audio-info "mode1"> <audio-info "mode2">)
(<> <audio-info "frequency1"> <audio-info "frequency2">)
(<> <audio-info "smplsize1"> <audio-info "smplsize2">)
(<> <audio-info "bps1"> <audio-info "bps2">) )
then (iup-warning60 dlg $"Audio tracks are not compatible")
(fail) )
(audio-save-as-check-duration audio-info dlg fps)
(set path (iup-choose-file-save dlg
(+ $"Save audio track" ' ' (- 3 which) ' ' $"for video" ' ' which ' ' $"as...")
(cfg-get "audio-path")
(+ $"to-mux" "." (utf8-tolower <audio-info "codec1">))
true ))
(stringp path)
(set g (fcreate path))
(if (= g undef)
then (sound-iup-error60 dlg (+ $"Creation of file `" path $"' failed."))
(fail) )
(alt (audio-save-as-ok-low audio-info dlg fps which g)
(seq (close g)
(remove path)
(sound-iup-error60 dlg $"I/O error.")
(fail) ))
(close g)
(sound-iup-info60 dlg (+ $"Audio track successfully saved as `" path "'.")) )
(defnet audio-save-as-check-duration (audio-info dlg fps)
(deflocal v1 v2 a1 a2 in1 in2)
(set v1 (/ (av-approximated-number-of-frames <_kv "av1">) fps))
(set a1 <audio-info "duration1">)
(set in1 (< (/ (min a1 v1) (max a1 v1)) 0.98))
(set v2 (/ (av-approximated-number-of-frames <_kv "av2">) fps))
(set a2 <audio-info "duration2">)
(set in2 (< (/ (min a2 v2) (max a2 v2)) 0.98))
(if (or in1 in2)
then (iup-confirm60 dlg (+ $"It seems like stream lengths are inconsistent:" nl
(if in1 (+ $"duration of video" " 1: " (date-s2hhmmss v1) nl
$"duration of audio" " 1: " (date-s2hhmmss a1) nl ) "" )
(if in2 (+ $"duration of video" " 2: " (date-s2hhmmss v2) nl
$"duration of audio" " 2: " (date-s2hhmmss a2) nl ) "" )
$"Do you want to continue?" ))))
(defnet audio-save-as-ok-low (audio-info dlg fps which g)
(deflocal path f1 f2)
(set path <audio-info "path1">)
(set f1 (fopenro path))
(if (= f1 undef)
then (sound-iup-error60 dlg (+ "Cannot open `" path "'."))
(fail) )
(set path <audio-info "path2">)
(set f2 (fopenro path))
(if (= f2 undef)
then (close f1)
(sound-iup-error60 dlg (+ "Cannot open `" path "'."))
(fail) )
(alt (iup-progress
(thread-create (netptr audio-save-as-ok-th) (thread-self) audio-info fps which g f1 f2
(iup-get-int <audio-info "sb-delay1"> "VALUE")
(iup-get-int <audio-info "sb-delay2"> "VALUE") )
dlg "Saving audio track..." false true false true true )
(seq (close f1 f2)
(fail) ))
(close f1 f2) )
(defnet audio-save-as-ok-th (th audio-info fps which g f1 f2 delay1 delay2)
(deflocal buf)
(set buf (raw 65536))
(alt (seq (audio-save-as-ok-th-low th audio-info fps which g f1 f2 buf delay1 delay2)
(send "q" to th) )
(send "a" to th) )
(close buf) )
(defnet audio-save-as-ok-th-low (th audio-info fps which g f1 f2 buf delay1 delay2)
(deflocal dlg f a1 a2 a smpl1 smpl2 smpl delay map p q n ts last-ts err pf freq smplsize i j)
(send "dlg" to th)
(receive dlg from th)
(set i (cfg-get (key-map <_kv "av1">
<_kv "av2">
(fullpath->name <_kv "path1">)
(fullpath->name <_kv "path2">) )))
(set map (map-create <i 0> <i 1>))
(for i in <i 2> do
(set p <i 0>)
(set q <i 1>)
(set n <i 2>)
(map-bind-low (car map) p q n)
(map-bind-low (cdr map) q p n) )
(set a1 <audio-info "ts1">)
(set a2 <audio-info "ts2">)
(set freq <audio-info "frequency1">)
(set smplsize <audio-info "smplsize1">)
(set smpl1 <audio-info "smpl1">)
(set smpl2 <audio-info "smpl2">)
(set delay1 (rint (* (/ delay1 1000) freq)))
(set delay2 (rint (* (/ delay2 1000) freq)))
(if (= which 1)
then (set map (car map))
else (set map (cdr map))
(set i f1)
(set f1 f2)
(set f2 i)
(set i a1)
(set a1 a2)
(set a2 i)
(set i smpl1)
(set smpl1 smpl2)
(set smpl2 i)
(set i delay1)
(set delay1 delay2)
(set delay2 i) )
(set last-ts (/ (+ <map -1 1> 1) fps))
(set ts 0)
(set err 0)
(if (in <audio-info "codec1"> [ "WAV" "SOX"])
then (filecopy-basic f1 g a1 buf)
(for i in (audio-segment map which) do
(not (iup-progress-aborted dlg))
(set p (/ (+ <i 1> 1) fps))
(set n (- p ts))
(if (> n 0)
then (set q <i 2>)
(if (= q undef)
then (set q <i 0>)
(set f f1)
(set a a1)
(set smpl smpl1)
(set delay delay1)
else (set f f2)
(set a a2)
(set smpl smpl2)
(set delay delay2) )
(if (= <i 0> 1)
then (dec q) )
(set q (rint (* (- (/ q fps) err) freq)))
(set pf (- (min (+ q (min (rint (* n freq)) (- smpl1 (* ts freq)))) smpl) q))
(if (> pf 0)
then (sendbuf 100 (/ ts last-ts) to th)
(dec q delay)
(inc ts (/ pf freq))
; devo copiare esattamente pf campioni
(if (< q 0)
then (set j (min pf -q))
(fsetpos a f)
(filecopy-basic f g (* j smplsize) buf)
(dec pf j)
(set q 0) )
(while (> pf 0) do
(set j (min pf (- smpl q)))
(fsetpos (+ a (* q smplsize)) f)
(filecopy-basic f g (* j smplsize) buf)
(dec pf j) )))
(set err (- p ts)) )
(set p (* ts freq))
(if (< p smpl1)
then (fsetpos (+ a1 (* p smplsize)) f1)
(filecopy-basic f1 g (* (- smpl1 p) smplsize) buf) )
else (for i in (audio-segment map which) do
(not (iup-progress-aborted dlg))
(set p (/ (+ <i 1> 1) fps))
(set n (- p ts))
(if (> n 0)
then (sendbuf 100 (/ ts last-ts) to th)
(set q <i 2>)
(if (= q undef)
then (set q <i 0>)
(set f f1)
(set a a1)
else (set f f2)
(set a a2) )
(if (= <i 0> 1)
then (dec q) )
(audio-save-as-ok-search (- (/ q fps) err) a q)
(set pf <q 1>)
(fsetpos pf f)
(set q <q 0>)
(audio-save-as-ok-search (+ q n) a n)
(filecopy-basic f g (- <n 1> pf) buf)
(set ts (+ ts <n 0> -q)) )
(set err (- p ts)) )))
(defnet audio-save-as-ok-search (t a @x)
(deflocal p q i)
(set p 0)
(set q (- (length a) 1))
(while (> (- q p) 1) do
(set i (div (+ p q) 2))
(if (>= t <a i 0>)
then (set p i)
else (set q i) ))
(if (> q 0)
then (if (<= (abs (- t <a (- q 1) 0>))
(abs (- t <a q 0>)) )
then (dec q) ))
(set @x <a q>) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun audio-segment-max () 2000)
(defun audio-segment (map which) net audio-segment)
(defnet audio-segment (map which @a)
(deflocal prm i p q n real-p real-n a)
(set prm <_kv "prm">)
(set @a (array 0))
(for i in map do
(set p <i 0>)
(set q <i 1>)
(set n <i 2>)
(repeat (if (= which 1)
then (set real-p p)
(if (integerp n)
then (if (= prm undef)
then (set real-n n)
else (set real-n <prm n>) )
else (set real-n n) )
else (if (= prm undef)
then (set real-p p)
else (set real-p <prm p>) )
(set real-n n) )
(alt (seq (= <@a -1 1> (- real-p 1))
(if (integerp real-n)
then (integerp <@a -1 2>)
(= (- real-n <@a -1 2>) (- real-p <@a -1 0>))
else (not (integerp <@a -1 2>)) )
(< (- <@a -1 1> <@a -1 0> -1) (audio-segment-max))
(inc <@a (- (length @a) 1) 1>) )
(seq (set a (array 3))
(set <a 0> real-p)
(set <a 1> real-p)
(set <a 2> real-n)
(array-append @a a) ))
until (= p q)
(inc p)
(if (integerp n)
then (inc n) )))
(sort @a (funptr less0)) )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;