No places.

This commit is contained in:
2026-06-10 08:31:47 +02:00
parent 736638f2a4
commit 930903fb42
7 changed files with 77 additions and 63 deletions
+30 -33
View File
@@ -1,9 +1,7 @@
#lang racket/base
(require racket/file
racket/path
racket/place
"util.rkt")
racket/path)
(provide convert-flac-to-target-in-place)
@@ -12,35 +10,34 @@
(define name-str (path->string name))
(build-path base (format ".~a.tmp-~a.flac" name-str (current-inexact-milliseconds))))
(define (settings->alist max-sample-rate compression-level)
(list (cons 'target-sample-rate max-sample-rate)
(cons 'compression-level compression-level)))
(define (settings->hash max-sample-rate compression-level)
(make-immutable-hash
(list (cons 'target-sample-rate max-sample-rate)
(cons 'compression-level compression-level))))
(define (convert-flac-to-target-in-place input-path max-sample-rate compression-level)
(define (delete-file/quiet path)
(with-handlers ([exn:fail? (lambda (_) #f)])
(when (file-exists? path) (delete-file path))))
(define (convert-flac-to-target-in-place input-path max-sample-rate compression-level
#:progress-callback [progress-callback #f])
(define tmp-path (temp-output-path input-path))
(define worker
(place ch
(define msg (place-channel-get ch))
(define in-file (list-ref msg 0))
(define out-file (list-ref msg 1))
(define settings (list-ref msg 2))
(with-handlers ([exn:fail?
(lambda (e)
(place-channel-put ch (list 'error (exn-message e))))])
(define audio-encode (dynamic-require 'racket-audio/audio-encoder 'audio-encode))
(define result (audio-encode in-file out-file (make-immutable-hash settings)
#:encoder 'flac
#:copy-tags? #t))
(place-channel-put ch (list 'ok result)))))
(place-channel-put worker (list (path->string input-path)
(path->string tmp-path)
(settings->alist max-sample-rate compression-level)))
(define response (place-channel-get worker))
(cond [(and (pair? response) (eq? (car response) 'ok))
(rename-file-or-directory tmp-path input-path #t)
(cadr response)]
[else
(when (file-exists? tmp-path) (delete-file tmp-path))
(error 'convert-flac-to-target-in-place "conversion failed for ~a: ~a"
input-path
(if (and (pair? response) (pair? (cdr response))) (cadr response) response))]))
(define audio-encode (dynamic-require 'racket-audio/audio-encoder 'audio-encode))
(with-handlers ([exn:break?
(lambda (e)
(delete-file/quiet tmp-path)
(raise e))]
[exn:fail?
(lambda (e)
(delete-file/quiet tmp-path)
(error 'convert-flac-to-target-in-place
"conversion failed for ~a: ~a" input-path (exn-message e)))])
(define result
(audio-encode (path->string input-path)
(path->string tmp-path)
(settings->hash max-sample-rate compression-level)
#:encoder 'flac
#:copy-tags? #t
#:progress-callback progress-callback))
(rename-file-or-directory tmp-path input-path #t)
result))
+29 -24
View File
@@ -3,7 +3,6 @@
(require racket/file
racket/list
racket/path
racket/place
racket/string
"util.rkt")
@@ -84,28 +83,34 @@
(tags-save! tags)))
#:mode 'read-write))
(define (convert-flac-to-opus input-path output-path kbps)
(define (delete-file/quiet path)
(with-handlers ([exn:fail? (lambda (_) #f)])
(when (file-exists? path) (delete-file path))))
(define (convert-flac-to-opus input-path output-path kbps
#:progress-callback [progress-callback #f])
(define tmp-path (temp-output-path output-path))
(ensure-parent-directory! tmp-path)
(define worker
(place ch
(define msg (place-channel-get ch))
(define in-file (list-ref msg 0))
(define out-file (list-ref msg 1))
(define kbps (list-ref msg 2))
(with-handlers ([exn:fail? (lambda (e) (place-channel-put ch (list 'error (exn-message e))))])
(define-values (settings properties picture) (source-tags->settings in-file kbps))
(define audio-encode (dynamic-require 'racket-audio/audio-encoder 'audio-encode))
(define result (audio-encode in-file out-file settings #:encoder 'opus #:copy-tags? #f))
(copy-all-tag-properties! out-file properties picture)
(place-channel-put ch (list 'ok result)))))
(place-channel-put worker (list (path->string input-path) (path->string tmp-path) kbps))
(define response (place-channel-get worker))
(cond [(and (pair? response) (eq? (car response) 'ok))
(ensure-parent-directory! output-path)
(rename-file-or-directory tmp-path output-path #t)
(cadr response)]
[else
(when (file-exists? tmp-path) (delete-file tmp-path))
(error 'convert-flac-to-opus "conversion failed for ~a: ~a" input-path
(if (and (pair? response) (pair? (cdr response))) (cadr response) response))]))
(with-handlers ([exn:break?
(lambda (e)
(delete-file/quiet tmp-path)
(raise e))]
[exn:fail?
(lambda (e)
(delete-file/quiet tmp-path)
(error 'convert-flac-to-opus
"conversion failed for ~a: ~a" input-path (exn-message e)))])
(define-values (settings properties picture)
(source-tags->settings (path->string input-path) kbps))
(define audio-encode (dynamic-require 'racket-audio/audio-encoder 'audio-encode))
(define result
(audio-encode (path->string input-path)
(path->string tmp-path)
settings
#:encoder 'opus
#:copy-tags? #f
#:progress-callback progress-callback))
(copy-all-tag-properties! (path->string tmp-path) properties picture)
(ensure-parent-directory! output-path)
(rename-file-or-directory tmp-path output-path #t)
result))
+2 -1
View File
@@ -36,4 +36,5 @@
[else acc]))))
(define (find-flac-files base-dir)
(sort-paths (filter flac-path? (find-regular-files base-dir))))
(sort-paths (filter (lambda (p) (and (flac-path? p) (not (manager-temp-path? p))))
(find-regular-files base-dir))))
+11
View File
@@ -17,6 +17,7 @@
filesystem-path
flac-path?
opus-path?
manager-temp-path?
directory-file-paths
replace-path-extension
ensure-parent-directory!
@@ -112,6 +113,16 @@
(define (file-exists?/quiet p)
(with-handlers ([exn:fail? (lambda (_) #f)]) (file-exists? p)))
(define (path-file-name-string p)
(let-values ([(base name dir?) (split-path p)])
(and (path? name) (path->string name))))
(define (manager-temp-path? p)
(define name (path-file-name-string p))
(and name
(string-prefix? name ".")
(regexp-match? #rx"\\.tmp-[0-9.]+\\.(flac|opus)$" name)))
(define (flac-path? p)
(and (file-exists?/quiet p) (extension-ci=? p ".flac")))