trying to overcome gapless problems with mpg123 (very small tick left) and facilitate output to wav
This commit is contained in:
+48
-32
@@ -28,37 +28,52 @@
|
||||
;; Functions to do the good stuff
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
(define mp3-handles '())
|
||||
|
||||
(register-finalizer mp3-handles
|
||||
(λ (handles)
|
||||
(for-each (λ (handle)
|
||||
(handle 'delete)) handles)))
|
||||
|
||||
(define (mp3-valid? mp3-file)
|
||||
#t)
|
||||
|
||||
(define audio-type 'mp3)
|
||||
|
||||
(define last-rate 44200) ; An assumption for if we've got nothing
|
||||
(define last-channels 2) ; An assumption for if we've got nothing
|
||||
(define last-bits 16) ; An assumption for if we've got nothing
|
||||
|
||||
(define (correct-format-hash h)
|
||||
(let ((rate (hash-ref h 'sample-rate #f)))
|
||||
(when (eq? rate #f)
|
||||
(hash-set! h 'sample-rate last-rate)))
|
||||
(let ((channels (hash-ref h 'channels #f)))
|
||||
(when (eq? channels #f)
|
||||
(hash-set! h 'channels last-channels)))
|
||||
(let ((bits (hash-ref h 'bits-per-sample #f)))
|
||||
(when (eq? bits #f)
|
||||
(hash-set! h 'bits-per-sample last-bits)))
|
||||
(let ((total-samples (hash-ref h 'total-samples #f)))
|
||||
(when (eq? total-samples #f)
|
||||
(hash-set! h 'total-samples 0)
|
||||
(hash-set! h 'duration 0)))
|
||||
)
|
||||
|
||||
(define (report-format handle current-pcm-pos)
|
||||
(dbg-sound "Reporting format at pcm-pos: ~a" current-pcm-pos)
|
||||
(let ((h (mp3-handle-format handle)))
|
||||
(set! last-rate (hash-ref h 'sample-rate))
|
||||
(set! last-channels (hash-ref h 'channels))
|
||||
(set! last-bits (hash-ref h 'bits-per-sample)))
|
||||
((mp3-handle-cb-info handle) (mp3-handle-format handle)))
|
||||
|
||||
(define (give-audio handle info pos buffer size)
|
||||
(let ((h (mp3-handle-format handle)))
|
||||
(correct-format-hash h)
|
||||
(hash-set! h 'sample pos)
|
||||
(hash-set! h 'current-time (exact->inexact (/ pos (hash-ref h 'sample-rate))))
|
||||
(let ((sample-rate (hash-ref h 'sample-rate last-rate)))
|
||||
(hash-set! h 'current-time (exact->inexact (/ pos sample-rate))))
|
||||
((mp3-handle-cb-audio handle) h buffer size)))
|
||||
|
||||
(define (mp3-open mp3-file* cb-stream-info cb-audio)
|
||||
(let ((mp3-file (if (path? mp3-file*) (path->string mp3-file*) mp3-file*)))
|
||||
(if (file-exists? mp3-file)
|
||||
(let ((handler (if (null? mp3-handles)
|
||||
(let ((h (mpg123-ffi-decoder-handler)))
|
||||
(h 'new)
|
||||
h)
|
||||
(let ((h (car mp3-handles)))
|
||||
(set! mp3-handles (cdr mp3-handles))
|
||||
h))))
|
||||
(let ((handler (mpg123-ffi-decoder-handler)))
|
||||
(handler 'new)
|
||||
(handler 'init mp3-file)
|
||||
(let ((h (make-mp3-handle handler
|
||||
cb-stream-info
|
||||
@@ -66,26 +81,25 @@
|
||||
#f
|
||||
#f
|
||||
#f
|
||||
#f
|
||||
(make-hash)
|
||||
)))
|
||||
(handler 'format
|
||||
(λ (rate channels sample-bits sample-bytes pcm-length)
|
||||
(let ((f (make-hash)))
|
||||
(hash-set! f 'duration (exact->inexact (/ pcm-length rate)))
|
||||
(hash-set! f 'sample-rate rate)
|
||||
(hash-set! f 'channels channels)
|
||||
(hash-set! f 'bits-per-sample sample-bits)
|
||||
(hash-set! f 'bytes-per-sample sample-bytes)
|
||||
(hash-set! f 'total-samples pcm-length)
|
||||
(set-mp3-handle-format! h f)
|
||||
)
|
||||
)
|
||||
)
|
||||
(report-format h 0)
|
||||
h))
|
||||
#f)))
|
||||
|
||||
|
||||
(define (handle-format handle pcm-pos rate channels sample-bits sample-bytes pcm-length)
|
||||
(let ((f (make-hash)))
|
||||
(hash-set! f 'duration (exact->inexact (/ pcm-length rate)))
|
||||
(hash-set! f 'sample-rate rate)
|
||||
(hash-set! f 'channels channels)
|
||||
(hash-set! f 'bits-per-sample sample-bits)
|
||||
(hash-set! f 'bytes-per-sample sample-bytes)
|
||||
(hash-set! f 'total-samples pcm-length)
|
||||
(set-mp3-handle-format! handle f)
|
||||
)
|
||||
(report-format handle pcm-pos)
|
||||
)
|
||||
|
||||
(define (mp3-read handle)
|
||||
(let* ((ffi-handler (mp3-handle-if handle))
|
||||
(cb-info (mp3-handle-cb-info handle))
|
||||
@@ -110,18 +124,20 @@
|
||||
(set-mp3-handle-stop! handle #t)
|
||||
(give-audio handle info pos buffer size)
|
||||
))
|
||||
(λ (pcm-pos rate channels sample-bits sample-bytes pcm-length)
|
||||
(handle-format handle pcm-pos rate channels sample-bits sample-bytes pcm-length))
|
||||
)
|
||||
(loop)
|
||||
)
|
||||
))
|
||||
(ffi-handler 'close)
|
||||
(set! mp3-handles (cons ffi-handler mp3-handles))
|
||||
(ffi-handler 'delete)
|
||||
)
|
||||
)
|
||||
|
||||
(define (mp3-seek handle percentage)
|
||||
(let ((fmt (mp3-handle-format handle)))
|
||||
(let ((total-samples (hash-ref fmt 'total-samples)))
|
||||
(let ((total-samples (hash-ref fmt 'total-samples 0)))
|
||||
(unless (or
|
||||
(eq? total-samples #f)
|
||||
(= total-samples -1))
|
||||
|
||||
Reference in New Issue
Block a user