diff --git a/src/flac/decoder.scm b/src/flac/decoder.scm index 4d8be19..6feea44 100644 --- a/src/flac/decoder.scm +++ b/src/flac/decoder.scm @@ -122,19 +122,18 @@ (let ((rice-parameter (flac-read-uint param-bits))) (if (< rice-parameter escape-param) (let ((count (if (= 0 partition) (- partition-samples predictor-order) partition-samples))) - (format #t "samples ~a\n" count) (residual-loop (+ sample count) (+ 1 partition) (cons rice-parameter parameters) (cons 0 raw-bits) (list-ec (: c count) (flac-read-rice-sint rice-parameter)))) - (let ((num-bits ((flac-read-sint 5)))) + (let ((num-bits (flac-read-sint 5))) (residual-loop sample (+ 1 partition) (cons rice-parameter parameters) (cons num-bits raw-bits) - (let ((order (if (= 0 partition) predictor-order 0))) - (if (= 0 raw-bits) + (let ((order (- partition-samples (if (= 0 partition) predictor-order 0)))) + (if (= 0 num-bits) (list-ec (: o order) 0) (list-ec (: o order) (flac-read-sint num-bits))))))))))))) @@ -144,7 +143,13 @@ (let-values (((entropy-coding-method residual) (read-residual-partiioned-rice blocksize predictor-order))) (%make-subframe-fixed entropy-coding-method predictor-order warmup residual)))) -(define (read-subframe-lpc) #f) +(define (read-subframe-lpc lpc-order blocksize sample-depth) + (let* ((warmup (list-ec (: o lpc-order) (flac-read-sint sample-depth))) + (precision (+ 1 (flac-read-uint 4))) + (shift (flac-read-sint 5)) + (coefs (list-ec (: o lpc-order) (flac-read-sint precision)))) + (let-values (((entropy-coding-method residual) (read-residual-partiioned-rice blocksize lpc-order))) + (%make-subframe-lpc entropy-coding-method lpc-order precision shift coefs warmup residual)))) ;;; 000000 constant ;;; 000001 verbatim @@ -159,7 +164,7 @@ [(= raw #b000000) (values #f 'constant)] [(= raw #b000001) (values #f 'verbatim)] [(between? raw #b001000 #b001100) (values (bit-extract raw 0 3) 'fixed)] - [(between? raw #b100000 #b111111) (values (bit-extract raw 0 6) 'lpc)] + [(between? raw #b100000 #b111111) (values (+ 1 (bit-extract raw 0 5)) 'lpc)] (else (values #f #f))))) (define (read-subframe-header) @@ -178,14 +183,13 @@ (frame-header-channel-assignment frame-header) channel)] [blocksize (frame-header-blocksize frame-header)]) - (format #t "sf: ~a\n" subframe-header) (%make-subframe subframe-header (match (subframe-header-subframe-type subframe-header) ('constant (read-subframe-constant blocksize sample-depth wasted-bits)) ('verbatim (read-subframe-verbatim blocksize sample-depth wasted-bits)) ('fixed (read-subframe-fixed predictor-order blocksize sample-depth)) - ('lpx (read-subframe-lpc)))))) + ('lpc (read-subframe-lpc predictor-order blocksize sample-depth)))))) (define (read-subframes stream-info frame-header) (let* ([channels (stream-info-channels stream-info)] diff --git a/src/flac/format.scm b/src/flac/format.scm index fc16a3a..d04ae06 100644 --- a/src/flac/format.scm +++ b/src/flac/format.scm @@ -36,6 +36,8 @@ %make-subframe-verbatim subframe-verbatim-data + %make-subframe-lpc + %make-rice-partition partitioned-rice-order partitioned-rice-contents @@ -118,11 +120,30 @@ make-metadata-stream-info metadata-stream-info? (header subframe-header) (data subframe-data)) +(define-record-type + (%make-subframe-lpc entropy-coding-method order qlp-coefficient-precision quantization-level qlp-coefficients warmup residual) + subframe-lpc? + (entropy-coding-method subframe-lpc-entropy-coding-method) + (order subframe-lpc-order) + (qlp-coefficient-precision subframe-lpc-qlp-coefficient-precision) + (quantization-level subframe-lpc-quantization-level) + (qlp-coefficients subframe-lpc-qlp-coefficients) + (warmup subframe-lpc-warmup) + (residual subframe-lpc-residual)) + (define-record-type (%make-subframe-verbatim value) subframe-verbatim? (value subframe-verbatim-value)) +(define-record-type + (%make-subframe-fixed entropy-coding-method predictor-order warmup residual) + subframe-fixed? + (entropy-coding-method subframe-fixed-entropy-coding-method) + (predictor-order subframe-fix-predictor-order) + (warmup subframe-fixed-warmup) + (residual subframe-fixed-residual)) + (define-record-type (%make-subframe-constant value) subframe-constant? @@ -147,13 +168,6 @@ make-metadata-stream-info metadata-stream-info? (type entropy-coding-method-type) (data entropy-coding-method-data)) -(define-record-type - (%make-subframe-fixed entropy-coding-method predictor-order warmup residual) - subframe-fixed? - (entropy-coding-method subframe-fixed-entropy-coding-method) - (predictor-order subframe-fix-predictor-order) - (warmup subframe-fixed-warmup) - (residual subframe-fixed-residual)) (define-record-type (make-frame-header blocking-strategy blocksize sample-rate channel-assignment bits-per-sample frame/sample-number crc) diff --git a/src/flac/tests.scm b/src/flac/tests.scm index d89e8d7..775db29 100644 --- a/src/flac/tests.scm +++ b/src/flac/tests.scm @@ -9,6 +9,13 @@ #:use-module (srfi srfi-64)) +(define example-1 + #vu8(#x66 #x4c #x61 #x43 #x80 #x00 #x00 #x22 #x10 #x00 #x10 #x00 + #x00 #x00 #x0f #x00 #x00 #x0f #x0a #xc4 #x42 #xf0 #x00 #x00 + #x00 #x01 #x3e #x84 #xb4 #x18 #x07 #xdc #x69 #x03 #x07 #x58 + #x6a #x3d #xad #x1a #x2e #x0f #xff #xf8 #x69 #x18 #x00 #x00 + #xbf #x03 #x58 #xfd #x03 #x12 #x8b #xaa #x9a)) + (define example-2 #vu8(#x66 #x4c #x61 #x43 #x00 #x00 #x00 #x22 #x00 #x10 #x00 #x10 #x00 #x00 #x17 #x00 #x00 #x44 #x0a #xc4 #x42 #xf0 #x00 #x00 @@ -30,18 +37,18 @@ #xff #xf8 #x69 #x18 #x01 #x02 #xa4 #x02 #xc3 #x82 #xc4 #x0b #xc1 #x4a #x03 #xee #x48 #xdd #x03 #xb6 #x7c #x13 #x30)) -(define example-1 - #vu8(#x66 #x4c #x61 #x43 #x80 #x00 #x00 #x22 #x10 #x00 #x10 #x00 - #x00 #x00 #x0f #x00 #x00 #x0f #x0a #xc4 #x42 #xf0 #x00 #x00 - #x00 #x01 #x3e #x84 #xb4 #x18 #x07 #xdc #x69 #x03 #x07 #x58 - #x6a #x3d #xad #x1a #x2e #x0f #xff #xf8 #x69 #x18 #x00 #x00 - #xbf #x03 #x58 #xfd #x03 #x12 #x8b #xaa #x9a)) +(define example-3 + #vu8(#x66 #x4c #x61 #x43 #x80 #x00 #x00 #x22 #x10 #x00 #x10 #x00 #x00 + #x00 #x1f #x00 #x00 #x1f #x07 #xd0 #x00 #x70 #x00 #x00 #x00 + #x18 #xf8 #xf9 #xe3 #x96 #xf5 #xcb #xcf #xc6 #xdc #x80 #x7f + #x99 #x77 #x90 #x6b #x32 #xff #xf8 #x68 #x02 #x00 #x17 #xe9 + #x44 #x00 #x4f #x6f #x31 #x3d #x10 #x47 #xd2 #x27 #xcb #x6d + #x09 #x08 #x31 #x45 #x2b #xdc #x28 #x22 #x22 #x80 #x57 #xa3)) ; (test-begin "RFC Examples") ; ; - (with-tests "RFC Examples" (test-group "Example 1"