56 lines
1.8 KiB
Racket
56 lines
1.8 KiB
Racket
#lang racket/base
|
|
(require racket-audio/flac-decoder
|
|
racket-audio/audio-encoder
|
|
"log.rkt"
|
|
)
|
|
|
|
(provide determine-flac-khz-and-bits
|
|
convert-flac-to-rate)
|
|
|
|
|
|
(define (determine-flac-khz-and-bits path)
|
|
(with-handlers ((exn? (λ args
|
|
(error (format "Cannot process ~a" path)))))
|
|
(let* ((meta #f)
|
|
(flac-handle (flac-open path
|
|
(λ (info) (set! meta info))
|
|
(λ data #t)))
|
|
)
|
|
(flac-read-meta flac-handle)
|
|
(flac-stop flac-handle)
|
|
(if (eq? meta #f)
|
|
(values #f #f)
|
|
(values (hash-ref meta 'sample-rate)
|
|
(hash-ref meta 'audio-bits-per-sample))
|
|
)
|
|
)
|
|
)
|
|
)
|
|
|
|
|
|
(define (convert-flac-to-rate path rate)
|
|
(let* ((tmp-file (build-path (string-append (path->string path) ".tmp.flac")))
|
|
(encoder-settings (hash 'target-sample-rate rate
|
|
'compression-level 8))
|
|
)
|
|
(with-handlers ([exn:break? (λ (e)
|
|
(err-am "Ctrl-c used")
|
|
(when (file-exists? tmp-file)
|
|
(delete-file tmp-file))
|
|
(raise e))]
|
|
[exn? (λ (e)
|
|
(err-am (format "~a" e))
|
|
(when (file-exists? tmp-file)
|
|
(delete-file tmp-file))
|
|
#f)])
|
|
(let ((result (audio-encode path tmp-file
|
|
encoder-settings
|
|
#:encoder 'flac
|
|
#:copy-tags? #t
|
|
)))
|
|
(rename-file-or-directory tmp-file path #t)
|
|
#t)
|
|
)
|
|
)
|
|
)
|