From f65fbb42c4bdb48ebade6fb5164031939c188ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Goran=20Meki=C4=87?= Date: Fri, 27 Nov 2026 02:19:13 +0100 Subject: [PATCH] Optimize dependencies --- Cargo.lock | 541 ++++++++++++++--------------------- Cargo.toml | 18 +- README.md | 3 +- src/audio_codec.rs | 696 +-------------------------------------------- src/io.rs | 2 +- 5 files changed, 217 insertions(+), 1043 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b3ca42c..0a1ce9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,15 +24,27 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + [[package]] name = "ahash" -version = "0.8.12" +version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +checksum = "54dae3d1d7b8956f3506c4e999a74e77f932a59153ccf38ab8cf96db74bb9dc0" dependencies = [ "cfg-if", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", + "serde", "version_check", "zerocopy", ] @@ -129,9 +141,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.104" +version = "1.0.105" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +checksum = "2cb931c189b85683d69a5cfad41d4d418671e1ac413dd209f3f2fc31e3cfd902" [[package]] name = "arbitrary" @@ -468,6 +480,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" +[[package]] +name = "block-padding" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8894febbff9f758034a5b8e12d87918f56dfc64a8e1fe757d65e29041538d93" +dependencies = [ + "generic-array", +] + [[package]] name = "block2" version = "0.5.1" @@ -539,9 +560,9 @@ checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" [[package]] name = "bytes" -version = "1.12.1" +version = "1.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" +checksum = "776dfebd015e2d8732c684a2c71003674ccb49457b539b9e82151ba38e96dd7c" [[package]] name = "calloop" @@ -593,11 +614,20 @@ dependencies = [ "wayland-client", ] +[[package]] +name = "cbc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" +dependencies = [ + "cipher", +] + [[package]] name = "cc" -version = "1.11.2" +version = "1.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ef9e3e8154a45c7a95cff4c1299702106ad94062d7bb85fdc5d3a5afd91d45" +checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69" dependencies = [ "find-msvc-tools", "jobserver", @@ -630,6 +660,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clipboard-win" version = "5.4.3" @@ -805,11 +845,20 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" -version = "1.5.5" +version = "1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b929dd2a135771f40afabd1073f85eed59c800b7c61901e5c254270497dad64" +checksum = "fd9ae8fcb3ea71b0b26338b4ffb13006bb175de8fd07921089a906ce7e5e6945" dependencies = [ "cfg-if", ] @@ -833,6 +882,15 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-queue" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03e8bd762f7479489c70ed6c768ddca99d7296857de437a68dcb2a94365b3fae" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.23" @@ -858,6 +916,16 @@ dependencies = [ "wgpu", ] +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "ctor" version = "0.10.1" @@ -867,6 +935,15 @@ dependencies = [ "dtor", ] +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "cursor-icon" version = "1.2.0" @@ -1057,12 +1134,6 @@ dependencies = [ "zune-inflate", ] -[[package]] -name = "extended" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af9673d8203fcb076b19dfd17e38b3d4ae9f44959416ea532ce72415a6020365" - [[package]] name = "fastrand" version = "2.5.0" @@ -1086,9 +1157,9 @@ dependencies = [ [[package]] name = "fearless_simd" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "629c9d6b6f30b5604089e0980f4d924a013aacb1654a6cdf5d3d852224f68651" +checksum = "6a7107516a8be9a4ffe92495f3dbfe04348be0ad18f17c67a1d846511f99e599" [[package]] name = "fearless_simd_macros" @@ -1305,6 +1376,16 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "gethostname" version = "1.1.0" @@ -1334,8 +1415,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi 6.0.0", + "wasm-bindgen", ] [[package]] @@ -1492,9 +1575,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.17.1" +version = "0.17.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" +checksum = "735eaa21a6a5ffb2e84362faaeb90c05ba8e19797ca30428478cb6ea3a4921bc" [[package]] name = "heck" @@ -1608,11 +1691,10 @@ dependencies = [ [[package]] name = "iced_drop" -version = "0.2.57" +version = "0.2.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5459a21e5793a18bc40756f2872590bbfd2ba5ab4e528658e4b6ffcbc5c9b8bf" +checksum = "eaf898bb059c076ca03c26cd4e70ef52303e9321a60062f7d12dd14a30859038" dependencies = [ - "iced", "iced_core", "iced_runtime", "iced_widget", @@ -1833,7 +1915,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" dependencies = [ "equivalent", - "hashbrown 0.17.1", + "hashbrown 0.17.2", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "block-padding", + "generic-array", ] [[package]] @@ -1864,27 +1956,25 @@ checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" [[package]] name = "jack" -version = "0.13.5" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7811b07bcac5dafabf814ab52c4b0ca9b7948aa1e279f572f03aa6544d47d27" +checksum = "f3ce606a8f7242b40873816bc2af3605a17161700e13a4c666261ca178ae35d4" dependencies = [ "bitflags 2.13.2", "jack-sys", - "lazy_static", "libc", "log", ] [[package]] name = "jack-sys" -version = "0.5.1" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6013b7619b95a22b576dfb43296faa4ecbe40abbdb97dfd22ead520775fc86ab" +checksum = "2358f2a05f9b19a51d1b518d1596ffc92e645276b1927803d343d22fe9e9ff1e" dependencies = [ - "bitflags 1.3.2", - "lazy_static", + "bitflags 2.13.2", "libc", - "libloading 0.7.4", + "libloading 0.9.1", "log", "pkg-config", ] @@ -2004,12 +2094,6 @@ dependencies = [ "smallvec", ] -[[package]] -name = "lazy_static" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" - [[package]] name = "lebe" version = "0.5.3" @@ -2034,19 +2118,19 @@ dependencies = [ [[package]] name = "libloading" -version = "0.7.4" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" dependencies = [ "cfg-if", - "winapi", + "windows-link", ] [[package]] name = "libloading" -version = "0.8.9" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +checksum = "a1832accd5a4ca2795ace4d05d15c4039be15b62d3b950eff99746a990e3cc10" dependencies = [ "cfg-if", "windows-link", @@ -2099,9 +2183,9 @@ checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" [[package]] name = "litrs" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4744e383959f0db86ede514b809b1c53251889093803c05267acc7d4e7030d70" +checksum = "02b614978e47ce529faee66579f2b2fe19c94aa96d910a6a9a8043b2b02d8175" [[package]] name = "lock_api" @@ -2205,30 +2289,24 @@ dependencies = [ [[package]] name = "maolan-editor" -version = "0.1.6" +version = "0.1.7" dependencies = [ "alsa", "maolan-engine", "maolan-widgets", - "oxideav-basic 0.0.12", - "oxideav-core", - "oxideav-flac 0.0.13", - "oxideav-mp3", - "oxideav-ogg", "rfd", "rubato", "serde", "serde_json", - "symphonia", "tokio", "toml", ] [[package]] name = "maolan-engine" -version = "0.4.24" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14ddcdb24bb33c8fd128b3ee45a164b2735cadc1dda715fc0fb995dc89637068" +checksum = "e4d719514d35d0c442ddf44fda37e074c594821d4e2d56c52523fe5794c805cb" dependencies = [ "alsa", "arc-swap", @@ -2236,6 +2314,7 @@ dependencies = [ "audioadapter-buffers", "basedrop", "block", + "crossbeam-queue", "fearless_simd", "fearless_simd_macros", "jack", @@ -2245,17 +2324,19 @@ dependencies = [ "nix", "num_cpus", "nvtree", - "oxideav-basic 0.0.11", + "oxideav-aac", + "oxideav-basic", "oxideav-core", - "oxideav-flac 0.0.12", + "oxideav-flac", "oxideav-mp3", + "oxideav-mp4", "oxideav-ogg", + "oxideav-vorbis", "rayon", "rtrb", "rubato", "serde", "serde_json", - "symphonia", "timestretch", "tokio", "toml", @@ -2276,9 +2357,9 @@ dependencies = [ [[package]] name = "maolan-widgets" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b93e91ffe56a67ea22d8b5851439545a6398919802b635793ccd673cdb2bf360" +checksum = "ab8f1fa0d6a52285842226e8578631157471a485a003b8c73aeca3533f2eb2c0" dependencies = [ "bytemuck", "iced", @@ -2287,7 +2368,6 @@ dependencies = [ "iced_fonts", "iced_wgpu", "serde", - "symphonia", ] [[package]] @@ -2497,9 +2577,9 @@ checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" [[package]] name = "nix" -version = "0.31.4" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bc81ce8148d3b4f3274da811f11cdd824ae661ee4ef732cdb229825cf5be7f7" +checksum = "77730ed34f01f4990cb2ea67fec715ecebc6fab9a6396cf83c5529b13f581bdc" dependencies = [ "bitflags 2.13.2", "cfg-if", @@ -3078,10 +3158,10 @@ dependencies = [ ] [[package]] -name = "oxideav-basic" -version = "0.0.11" +name = "oxideav-aac" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7969c668656223b44c2950e4d736fd870d70d3880bab6e5d27c92c3226fcbdc" +checksum = "35453ffa827080f322dba5d2233879f008627d4ffb07c496c0c6fe3190b35048" dependencies = [ "oxideav-core", ] @@ -3106,16 +3186,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "oxideav-flac" -version = "0.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4cec9a2ebb660ce0b46e148f0339d1a529c7660cc167f13aaef7784f06b1220" -dependencies = [ - "oxideav-core", - "oxideav-id3", -] - [[package]] name = "oxideav-flac" version = "0.0.13" @@ -3138,13 +3208,25 @@ dependencies = [ [[package]] name = "oxideav-mp3" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8583bbbe119a19c0b88ee5b0f76c90493395350878e7c770ce6402287b164477" +checksum = "32cac68f81027f049d176ec97dfe79738c0fe3f4ad89096b6b75abbbca982d98" dependencies = [ "oxideav-core", ] +[[package]] +name = "oxideav-mp4" +version = "0.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7766473a510b620ddcd87ea772d2b8193c566d9069799bd362ea695bc9562bd" +dependencies = [ + "aes", + "cbc", + "ctr", + "oxideav-core", +] + [[package]] name = "oxideav-ogg" version = "0.1.9" @@ -3154,6 +3236,16 @@ dependencies = [ "oxideav-core", ] +[[package]] +name = "oxideav-vorbis" +version = "0.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b786a2c716843ab2a4509c03ce7836173384a59831e1c893b5492ac611d872b" +dependencies = [ + "oxideav-core", + "oxideav-ogg", +] + [[package]] name = "parking" version = "2.2.1" @@ -3203,29 +3295,29 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" [[package]] name = "pin-project" -version = "1.1.13" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924" +checksum = "a6c2123f4708bbd422630a6dae7c03f678b1ce1f1c99e4c4b3beac69c8174cc9" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.13" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b" +checksum = "358938fd6df944c1e44bb328b60fb275f13c804aac47ba26a1165d45a3f87756" dependencies = [ "proc-macro2", "quote", - "syn 2.0.119", + "syn 3.0.10", ] [[package]] name = "pin-project-lite" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" +checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752" [[package]] name = "piper" @@ -3279,9 +3371,9 @@ dependencies = [ [[package]] name = "pollster" -version = "1.0.1" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc6355899e1c9462875b6757c79f3caa011a1fdae12bbb1a2e72dd1f234f8336" +checksum = "7e9b9cf600136f5b976fbc5727d58c26e1ecab8ee23088d44ef1c7af793550ab" [[package]] name = "portable-atomic" @@ -3460,9 +3552,9 @@ checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" [[package]] name = "rangemap" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a611d15b50743feb4c76b7d03edcb0e64f399c26961e4efe6975bc398be6aa3d" +checksum = "f2d43a8c8c74056248124068fde0df374d1ddf9039e747d190f01c36faeaab08" [[package]] name = "rav1e" @@ -3598,12 +3690,6 @@ dependencies = [ "bitflags 2.13.2", ] -[[package]] -name = "regex-lite" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cab834c73d247e67f4fae452806d17d3c7501756d98c8808d7c9c7aa7d18f973" - [[package]] name = "renderdoc-sys" version = "1.1.0" @@ -3882,12 +3968,9 @@ dependencies = [ [[package]] name = "simd_helpers" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" -dependencies = [ - "quote", -] +checksum = "f48ea32978b7ad365bd1ecce83827746d5d50dc006a8c85112b7acc3c716e3e7" [[package]] name = "simdutf8" @@ -3932,9 +4015,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.16.5" +version = "1.16.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46" +checksum = "3244542535d5de959d90ec55d1b5ff0fd2bb7b1310223907d5f11932847f7b59" [[package]] name = "smithay-client-toolkit" @@ -4099,192 +4182,6 @@ dependencies = [ "zeno", ] -[[package]] -name = "symphonia" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7edef6a96b696d4e0cab5ee9ebb7ca155ed95f30a6b45bbb8b97d2727f02424" -dependencies = [ - "lazy_static", - "symphonia-bundle-flac", - "symphonia-bundle-mp3", - "symphonia-codec-aac", - "symphonia-codec-adpcm", - "symphonia-codec-alac", - "symphonia-codec-pcm", - "symphonia-codec-vorbis", - "symphonia-core", - "symphonia-format-isomp4", - "symphonia-format-mkv", - "symphonia-format-ogg", - "symphonia-format-riff", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-bundle-flac" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6405d5c34ff6f8f7ca08a4101efe66d21e180f7322ef9359900a0e55698a7892" -dependencies = [ - "log", - "symphonia-common", - "symphonia-core", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-bundle-mp3" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98ea5ffc8716bff677dfb3b01b420c7b758de901a72b8c330bf2040ab74b4add" -dependencies = [ - "lazy_static", - "log", - "symphonia-core", -] - -[[package]] -name = "symphonia-codec-aac" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5bf8e39552d34a3c4c98333370e62f48c92456d2e814f273b5c3ad7c4a5f45c" -dependencies = [ - "lazy_static", - "log", - "symphonia-common", - "symphonia-core", -] - -[[package]] -name = "symphonia-codec-adpcm" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "445932ecb0c59362fde9c082dd63a75380650c5077fdb8b80b8cac850442a74c" -dependencies = [ - "log", - "symphonia-core", -] - -[[package]] -name = "symphonia-codec-alac" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "920a78f96f3cf62932d0c497959c0cc2100ef36b03c7ca1417b260f432a482d8" -dependencies = [ - "log", - "symphonia-common", - "symphonia-core", -] - -[[package]] -name = "symphonia-codec-pcm" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e04ba75686acbe43542fdd374571195f0530c0b7785ca25cc6840e9c6c4b6eea" -dependencies = [ - "log", - "symphonia-core", -] - -[[package]] -name = "symphonia-codec-vorbis" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73d90b4fcf796137cc683c538282804ff9629f8ad9dbfd881fcbba331ac4e986" -dependencies = [ - "log", - "symphonia-common", - "symphonia-core", -] - -[[package]] -name = "symphonia-common" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acc3fcc18ec9b8cdd48614e259c4cf0d27b71d41e5d9b120b42c5adab12d7c4" -dependencies = [ - "log", - "symphonia-core", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-core" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01c412864d599d4750d0c3d684d7e093ec05e5309681ef5252cc1096a437f6e0" -dependencies = [ - "bitflags 2.13.2", - "bytemuck", - "lazy_static", - "log", - "num-complex", - "rustfft", - "smallvec", -] - -[[package]] -name = "symphonia-format-isomp4" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e681a70e1870d34e02abf1dbc51e4267c3f1827801474e8870be8c689fc4dc3" -dependencies = [ - "log", - "symphonia-common", - "symphonia-core", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-format-mkv" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d015c5c0558864665894b3f4cbd95e10abb01b9c868e751c72670f326a56360e" -dependencies = [ - "lazy_static", - "log", - "symphonia-common", - "symphonia-core", -] - -[[package]] -name = "symphonia-format-ogg" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b5495e7f7e3c7035328d82b6d6e377eef289bb0c4105bdeb557fc93a833f994" -dependencies = [ - "log", - "symphonia-common", - "symphonia-core", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-format-riff" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ff70929083a8c1a5f6cd7c904b6071c7914ad04739b510c2f7239dfc9b7dabe" -dependencies = [ - "extended", - "log", - "symphonia-core", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-metadata" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83713a97705d77bdef7cdbc0768fd6e5a54e4cd7e48d60a806ae85639e2c87c6" -dependencies = [ - "lazy_static", - "log", - "regex-lite", - "smallvec", - "symphonia-core", -] - [[package]] name = "syn" version = "2.0.119" @@ -4400,9 +4297,9 @@ dependencies = [ [[package]] name = "timestretch" -version = "0.16.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad484075af6caf97e09f3f869bed42c99ff4fea501f6df6ad65d5bb8c7186808" +checksum = "673a71577e4d5d7975f884d1a4722ac80d287de3ab0c3129ede7db36bde7496f" dependencies = [ "timestretch-core", ] @@ -4415,9 +4312,9 @@ checksum = "ffbbc9a0a366ab1e33dbb5edc124d0aa2f7a29f9cf05e16718490c460e50aae5" [[package]] name = "timestretch-core" -version = "0.16.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa8cb1cae3fd3788a7a9e704dd079d7dc56b00a8b2d0688e9be94130ebcbe1aa" +checksum = "01ee7084b5f066fff156d695a0fdc9d3b714b743f332e02814acb6645af88414" dependencies = [ "rustfft", "timestretch-accelerate", @@ -4497,9 +4394,9 @@ dependencies = [ [[package]] name = "toml" -version = "1.2.1+spec-1.1.0" +version = "1.2.3+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d5755c51863cdac711f1956baf8eb9588e060755f7d3c4c979e70ea1e1f32cd7" +checksum = "26b1f26e9485c1fa43ee89fe76e9f3fb9e2e52bb227f8594b4d687496b83d073" dependencies = [ "indexmap", "serde_core", @@ -4521,9 +4418,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.25.19+spec-1.1.0" +version = "0.25.21+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd43688c4858d7264ae1cf9a9f874a67c3feb74a883b1cd7bebd6a4ea8307218" +checksum = "88497dab46de5bf343ec66b05c251e48c6213f3e9170c70a70a823f7abe886a4" dependencies = [ "indexmap", "toml_datetime", @@ -4533,9 +4430,9 @@ dependencies = [ [[package]] name = "toml_parser" -version = "1.1.5+spec-1.1.0" +version = "1.1.7+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +checksum = "0bb28d6e049a9766c3bd91a8326e6179f5b132dec87906bb97018ef7a2f3431a" dependencies = [ "winnow", ] @@ -4597,6 +4494,12 @@ dependencies = [ "core_maths", ] +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + [[package]] name = "uds_windows" version = "1.2.1" @@ -4686,9 +4589,9 @@ dependencies = [ [[package]] name = "walkdir" -version = "2.5.0" +version = "2.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +checksum = "b42e295b7452c4604982280d8586001fcad9b8b201cac67bca9771e7a7475403" dependencies = [ "same-file", "winapi-util", @@ -5090,22 +4993,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - [[package]] name = "winapi-util" version = "0.1.11" @@ -5115,12 +5002,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - [[package]] name = "window_clipboard" version = "0.5.1" @@ -5620,9 +5501,9 @@ dependencies = [ [[package]] name = "zcheapstr" -version = "1.2.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "939f186d9ecebe044315a5cb6d6f78568bbc21152adc954d5a0c2f58bdbc9920" +checksum = "2da34e7164b75d803c3197b86e6cf61b874106fbd472c5082575a8bc5fbd0907" dependencies = [ "serde", ] @@ -5635,18 +5516,18 @@ checksum = "6df3dc4292935e51816d896edcd52aa30bc297907c26167fec31e2b0c6a32524" [[package]] name = "zerocopy" -version = "0.8.63" +version = "0.8.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +checksum = "cba8a86aee14fc8cc6c208af60854b3d4ab7185a9a369a12c648faa2716ff42a" dependencies = [ "zerocopy-derive", ] [[package]] name = "zerocopy-derive" -version = "0.8.63" +version = "0.8.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +checksum = "f89bca3b96ceb3cfcb4572790402f977442270d252b9a8504ebc258760c55f94" dependencies = [ "proc-macro2", "quote", @@ -5655,9 +5536,9 @@ dependencies = [ [[package]] name = "zlib-rs" -version = "0.6.8" +version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" +checksum = "aa76918995d776d9dd34d371030be228d021137449771493d56e29f77e4148ac" [[package]] name = "zmij" diff --git a/Cargo.toml b/Cargo.toml index bc7f74a..2e9d054 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "maolan-editor" -version = "0.1.6" +version = "0.1.7" edition = "2024" description = "Simple audio editor for Maolan audio files" license = "BSD-2-Clause" @@ -22,24 +22,12 @@ standalone = [ "dep:maolan-engine", "maolan-engine?/audio", "maolan-engine?/plugins", - "dep:oxideav-basic", - "dep:oxideav-core", - "dep:oxideav-flac", - "dep:oxideav-mp3", - "dep:oxideav-ogg", - "dep:symphonia", ] embedded = [] [dependencies] -maolan-engine = { version = "0.4.24", optional = true } -oxideav-core = { version = "0.1.43", optional = true } -oxideav-basic = { version = "0.0.12", optional = true } -oxideav-flac = { version = "0.0.13", optional = true } -oxideav-mp3 = { version = "0.1.3", optional = true } -oxideav-ogg = { version = "0.1.9", optional = true } -symphonia = { version = "0.6.1", features = ["aac", "alac", "flac", "isomp4", "mp3", "ogg", "vorbis", "wav"], optional = true } -maolan-widgets = "0.1.5" +maolan-engine = { version = "0.4.27", optional = true } +maolan-widgets = "0.1.6" rfd = "0.18.0" serde = { version = "1.0", features = ["derive"] } rubato = "5.1.0" diff --git a/README.md b/README.md index e8595d5..c1a2455 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,7 @@ does not create a reopenable editing project. ## Audio I/O -Opening uses Maolan's Symphonia-backed decoder. Saving uses the shared -OxideAV-backed encoders: +Opening and saving use Maolan's shared OxideAV-backed audio codecs: - WAV (32-bit float for ordinary save; configurable bit depth for marker export) - FLAC and Ogg FLAC diff --git a/src/audio_codec.rs b/src/audio_codec.rs index 2b8a6d1..acbf22a 100644 --- a/src/audio_codec.rs +++ b/src/audio_codec.rs @@ -1,695 +1 @@ -use std::io::{self, Write}; -use std::path::Path; -use symphonia::core::codecs::CodecParameters as SymphoniaCodecParameters; -use symphonia::core::codecs::audio::{AudioCodecParameters, AudioDecoderOptions}; -use symphonia::core::errors::Error as SymphoniaError; -use symphonia::core::formats::probe::Hint; -use symphonia::core::formats::{FormatOptions, FormatReader, TrackType}; -use symphonia::core::io::MediaSourceStream; -use symphonia::core::meta::MetadataOptions; - -use oxideav_core::{ - AudioFrame, CodecId, CodecParameters, Frame, MediaType, Packet, RuntimeContext, SampleFormat, - StreamInfo, TimeBase, -}; - -/// Export format selector for [`encode_audio_to_file`]. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum AudioEncodeFormat { - /// Microsoft RIFF/WAVE, integer or float PCM. - Wav(WavBitDepth), - /// Native FLAC (`*.flac`). The `u16` is the desired bit depth - /// (16, 24 or 32). - Flac(u16), - /// Ogg-encapsulated FLAC (`*.ogg`). The `u16` is the desired bit - /// depth (16, 24 or 32). - OggFlac(u16), - /// MPEG-1/2/2.5 Layer III (`*.mp3`). Uses a sensible CBR bitrate - /// chosen from the standard Layer III ladder based on sample rate - /// and channel count. - Mp3, -} - -/// WAV PCM bit-depth / sample-format choices. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum WavBitDepth { - Int16, - Int24, - Int32, - Float32, -} - -/// Dither mode applied when quantising floating-point samples to an -/// integer target format. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub enum AudioDither { - #[default] - None, - Rectangular, - Triangular, -} - -/// Decode an audio file to interleaved `f32` samples. -/// -/// The format is auto-detected by Symphonia for `.wav`, `.flac`, `.mp3`, -/// `.ogg`/`.vorbis`, `.m4a`/`.aac`/`.alac`, and friends. -/// Returns `(samples, channels, sample_rate)`. -/// All decode paths emit samples in the range `[-1.0, 1.0]`. -pub fn decode_audio_to_f32_interleaved_sync(path: &Path) -> io::Result<(Vec, usize, u32)> { - decode_with_symphonia(path) -} - -/// Decode a WAV file preferentially, falling back to the general decoder. -/// -/// This used to short-circuit `.wav` inputs to a dedicated path. Symphonia -/// handles WAV natively, so this now simply delegates to the unified decoder. -pub fn decode_audio_to_f32_interleaved_preferring_wav( - path: &Path, -) -> io::Result<(Vec, usize, u32)> { - decode_audio_to_f32_interleaved_sync(path) -} - -// --------------------------------------------------------------------------- -// Symphonia decode (WAV, FLAC, MP3, Vorbis, AAC, ALAC, ...) -// --------------------------------------------------------------------------- - -fn decode_with_symphonia(path: &Path) -> io::Result<(Vec, usize, u32)> { - let file = std::fs::File::open(path) - .map_err(|e| io::Error::other(format!("Failed to open '{}': {e}", path.display())))?; - let mss = MediaSourceStream::new(Box::new(file), Default::default()); - - let mut hint = Hint::new(); - if let Some(ext) = path.extension().and_then(|e| e.to_str()) { - hint.with_extension(ext); - } - - let format_opts = FormatOptions::default(); - let metadata_opts = MetadataOptions::default(); - let decoder_opts = AudioDecoderOptions::default(); - - let mut format: Box = symphonia::default::get_probe() - .probe(&hint, mss, format_opts, metadata_opts) - .map_err(|e| { - io::Error::other(format!( - "Symphonia failed to probe format for '{}': {e}", - path.display() - )) - })?; - - let track = format - .default_track(TrackType::Audio) - .or_else(|| format.tracks().first()) - .ok_or_else(|| { - io::Error::other(format!("No usable audio track in '{}'", path.display())) - })?; - - let codec_params: &AudioCodecParameters = track - .codec_params - .as_ref() - .and_then(SymphoniaCodecParameters::audio) - .ok_or_else(|| { - io::Error::other(format!("No usable audio track in '{}'", path.display())) - })?; - - let channels = codec_params - .channels - .as_ref() - .map(|c| c.count()) - .unwrap_or(1); - let sample_rate = codec_params.sample_rate.unwrap_or(48_000); - let track_id = track.id; - - let mut decoder = symphonia::default::get_codecs() - .make_audio_decoder(codec_params, &decoder_opts) - .map_err(|e| { - io::Error::other(format!( - "Symphonia failed to create decoder for '{}': {e}", - path.display() - )) - })?; - - let mut samples = Vec::new(); - - loop { - let packet = match format.next_packet() { - Ok(Some(packet)) => packet, - Ok(None) => break, - Err(SymphoniaError::IoError(e)) if e.kind() == std::io::ErrorKind::UnexpectedEof => { - break; - } - Err(e) => { - return Err(io::Error::other(format!( - "Symphonia read error for '{}': {e}", - path.display() - ))); - } - }; - - if packet.track_id != track_id { - continue; - } - - let decoded = decoder.decode(&packet).map_err(|e| { - io::Error::other(format!( - "Symphonia decode error for '{}': {e}", - path.display() - )) - })?; - - let mut packet_samples = Vec::new(); - decoded.copy_to_vec_interleaved(&mut packet_samples); - samples.extend_from_slice(&packet_samples); - } - - if samples.is_empty() { - return Err(io::Error::other(format!( - "No samples decoded from '{}'", - path.display() - ))); - } - - Ok((samples, channels, sample_rate)) -} - -// --------------------------------------------------------------------------- -// Encode entry point -// --------------------------------------------------------------------------- - -/// Encode interleaved `f32` samples to a file using OxideAV. -/// -/// `samples` must be interleaved (`ch0 ch1 ... chN ...`). -/// `channels` is clamped to at least 1. `sample_rate` must be non-zero. -/// Integer formats are quantised from the `[-1.0, 1.0]` float range; for -/// WAV and FLAC the requested bit depth is honoured, while MP3 always -/// uses 16-bit PCM internally. -pub fn encode_audio_to_file( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, - format: AudioEncodeFormat, - dither: AudioDither, -) -> io::Result<()> { - let channels = channels.max(1); - if sample_rate == 0 { - return Err(io::Error::other("encode: sample_rate must be > 0")); - } - if channels > 8 { - return Err(io::Error::other(format!( - "encode: channel count {channels} exceeds the supported maximum of 8" - ))); - } - if !samples.len().is_multiple_of(channels) { - return Err(io::Error::other( - "encode: sample slice length is not a multiple of channels", - )); - } - - match format { - AudioEncodeFormat::Wav(depth) => { - encode_wav(path, samples, channels, sample_rate, depth, dither) - } - AudioEncodeFormat::Flac(bits) => { - encode_flac_to_file(path, samples, channels, sample_rate, bits, dither) - } - AudioEncodeFormat::OggFlac(bits) => { - encode_ogg_flac(path, samples, channels, sample_rate, bits, dither) - } - AudioEncodeFormat::Mp3 => encode_mp3(path, samples, channels, sample_rate, dither), - } -} - -/// Backwards-compatible WAV writer: 32-bit float PCM. -pub fn write_wav_f32( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, -) -> io::Result<()> { - encode_audio_to_file( - path, - samples, - channels, - sample_rate, - AudioEncodeFormat::Wav(WavBitDepth::Float32), - AudioDither::None, - ) -} - -/// Backwards-compatible native FLAC writer. -pub fn write_flac( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, - bits_per_sample: u16, -) -> io::Result<()> { - encode_audio_to_file( - path, - samples, - channels, - sample_rate, - AudioEncodeFormat::Flac(bits_per_sample), - AudioDither::None, - ) -} - -// --------------------------------------------------------------------------- -// WAV -// --------------------------------------------------------------------------- - -fn encode_wav( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, - depth: WavBitDepth, - dither: AudioDither, -) -> io::Result<()> { - let (codec_id, sample_format) = match depth { - WavBitDepth::Int16 => ("pcm_s16le", SampleFormat::S16), - WavBitDepth::Int24 => ("pcm_s24le", SampleFormat::S24), - WavBitDepth::Int32 => ("pcm_s32le", SampleFormat::S32), - WavBitDepth::Float32 => ("pcm_f32le", SampleFormat::F32), - }; - let bytes = pack_interleaved_samples(samples, sample_format, dither)?; - - let mut ctx = RuntimeContext::new(); - oxideav_basic::register(&mut ctx); - - let stream = audio_stream_info(codec_id, channels, sample_rate, sample_format, None); - let file = std::fs::File::create(path)?; - let output: Box = Box::new(file); - let mut mux = ctx - .containers - .open_muxer("wav", output, std::slice::from_ref(&stream)) - .map_err(oxideav_err_to_io)?; - mux.write_header().map_err(oxideav_err_to_io)?; - let packet = Packet::new(0, TimeBase::new(1, sample_rate as i64), bytes); - mux.write_packet(&packet).map_err(oxideav_err_to_io)?; - mux.write_trailer().map_err(oxideav_err_to_io)?; - Ok(()) -} - -// --------------------------------------------------------------------------- -// FLAC (native and Ogg) -// --------------------------------------------------------------------------- - -fn encode_flac_to_file( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, - bits_per_sample: u16, - dither: AudioDither, -) -> io::Result<()> { - let (packets, output_params) = - encode_flac_packets(samples, channels, sample_rate, bits_per_sample, dither)?; - - let mut ctx = RuntimeContext::new(); - oxideav_flac::register(&mut ctx); - - let stream = StreamInfo { - index: 0, - time_base: TimeBase::new(1, sample_rate as i64), - duration: None, - start_time: Some(0), - params: output_params, - }; - let file = std::fs::File::create(path)?; - let output: Box = Box::new(file); - let mut mux = ctx - .containers - .open_muxer("flac", output, std::slice::from_ref(&stream)) - .map_err(oxideav_err_to_io)?; - mux.write_header().map_err(oxideav_err_to_io)?; - for pkt in &packets { - mux.write_packet(pkt).map_err(oxideav_err_to_io)?; - } - mux.write_trailer().map_err(oxideav_err_to_io)?; - Ok(()) -} - -/// Returns the encoded FLAC frame packets and the finalised output -/// parameters (including the STREAMINFO extradata). -fn encode_flac_packets( - samples: &[f32], - channels: usize, - sample_rate: u32, - bits_per_sample: u16, - dither: AudioDither, -) -> io::Result<(Vec, CodecParameters)> { - let sample_format = flac_sample_format(bits_per_sample)?; - let bytes = pack_interleaved_samples(samples, sample_format, dither)?; - - let mut ctx = RuntimeContext::new(); - oxideav_flac::register(&mut ctx); - - let params = audio_codec_params("flac", channels, sample_rate, sample_format, None); - let mut enc = ctx - .codecs - .first_encoder(¶ms) - .map_err(oxideav_err_to_io)?; - - let frame = AudioFrame { - samples: (samples.len() / channels) as u32, - pts: Some(0), - data: vec![bytes], - }; - enc.send_frame(&Frame::Audio(frame)) - .map_err(oxideav_err_to_io)?; - enc.flush().map_err(oxideav_err_to_io)?; - - let mut packets = Vec::new(); - loop { - match enc.receive_packet() { - Ok(p) => packets.push(p), - Err(oxideav_core::Error::NeedMore) | Err(oxideav_core::Error::Eof) => break, - Err(e) => return Err(oxideav_err_to_io(e)), - } - } - - Ok((packets, enc.output_params().clone())) -} - -fn encode_ogg_flac( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, - bits_per_sample: u16, - dither: AudioDither, -) -> io::Result<()> { - let (packets, output_params) = - encode_flac_packets(samples, channels, sample_rate, bits_per_sample, dither)?; - - // Build the FLAC-in-Ogg mapping header packet: - // 0x7F "FLAC" major minor header_packets_be "fLaC" - let mut mapping = Vec::with_capacity(13); - mapping.push(0x7F); - mapping.extend_from_slice(b"FLAC"); - mapping.push(0x01); // mapping major version - mapping.push(0x00); // mapping minor version - // One header packet follows the mapping header: the STREAMINFO block. - mapping.extend_from_slice(&1u16.to_be_bytes()); - mapping.extend_from_slice(b"fLaC"); - - let streaminfo = output_params.extradata; - - let mut writer = oxideav_ogg::framing::PageWriter::new(0).with_page_target(4096); - writer.push_packet(&mapping, 0); - writer.flush_page(); - writer.push_packet(&streaminfo, 0); - writer.flush_page(); - - for pkt in &packets { - let granule = pkt - .pts - .map(|pts| pts + pkt.duration.unwrap_or(0)) - .unwrap_or(0); - writer.push_packet(&pkt.data, granule); - } - - std::fs::write(path, writer.finish())?; - Ok(()) -} - -fn flac_sample_format(bits_per_sample: u16) -> io::Result { - match bits_per_sample { - 8 => Ok(SampleFormat::U8), - 16 => Ok(SampleFormat::S16), - 24 => Ok(SampleFormat::S24), - 32 => Ok(SampleFormat::S32), - _ => Err(io::Error::other(format!( - "FLAC bit depth {bits_per_sample} not supported (use 8, 16, 24 or 32)" - ))), - } -} - -// --------------------------------------------------------------------------- -// MP3 -// --------------------------------------------------------------------------- - -fn encode_mp3( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, - dither: AudioDither, -) -> io::Result<()> { - if channels > 2 { - return Err(io::Error::other( - "MP3 encode: only mono and stereo are supported", - )); - } - let bitrate = mp3_default_bitrate(sample_rate, channels); - let bytes = pack_interleaved_samples(samples, SampleFormat::S16, dither)?; - - let mut ctx = RuntimeContext::new(); - oxideav_mp3::register(&mut ctx); - - let params = audio_codec_params( - "mp3", - channels, - sample_rate, - SampleFormat::S16, - Some(bitrate as u64), - ); - let mut enc = ctx - .codecs - .first_encoder(¶ms) - .map_err(oxideav_err_to_io)?; - - let frame = AudioFrame { - samples: (samples.len() / channels) as u32, - pts: Some(0), - data: vec![bytes], - }; - enc.send_frame(&Frame::Audio(frame)) - .map_err(oxideav_err_to_io)?; - enc.flush().map_err(oxideav_err_to_io)?; - - let mut file = std::fs::File::create(path)?; - loop { - match enc.receive_packet() { - Ok(pkt) => file.write_all(&pkt.data)?, - Err(oxideav_core::Error::NeedMore) | Err(oxideav_core::Error::Eof) => break, - Err(e) => return Err(oxideav_err_to_io(e)), - } - } - Ok(()) -} - -fn mp3_default_bitrate(sample_rate: u32, channels: usize) -> u32 { - // MPEG-1 (32/44.1/48 kHz) ladder - if sample_rate >= 32_000 { - if channels >= 2 { 192_000 } else { 128_000 } - } else if sample_rate >= 16_000 { - // MPEG-2 LSF (16/22.05/24 kHz) ladder - if channels >= 2 { 96_000 } else { 64_000 } - } else { - // MPEG-2.5 (8/11.025/12 kHz) ladder - if channels >= 2 { 48_000 } else { 32_000 } - } -} - -// --------------------------------------------------------------------------- -// Helpers -// --------------------------------------------------------------------------- - -fn audio_codec_params( - codec_id: &str, - channels: usize, - sample_rate: u32, - sample_format: SampleFormat, - bit_rate: Option, -) -> CodecParameters { - let mut params = CodecParameters::audio(CodecId::new(codec_id)); - params.media_type = MediaType::Audio; - params.channels = Some(channels as u16); - params.sample_rate = Some(sample_rate); - params.sample_format = Some(sample_format); - if let Some(br) = bit_rate { - params.bit_rate = Some(br); - } - params -} - -fn audio_stream_info( - codec_id: &str, - channels: usize, - sample_rate: u32, - sample_format: SampleFormat, - bit_rate: Option, -) -> StreamInfo { - let params = audio_codec_params(codec_id, channels, sample_rate, sample_format, bit_rate); - StreamInfo { - index: 0, - time_base: TimeBase::new(1, sample_rate as i64), - duration: None, - start_time: Some(0), - params, - } -} - -fn pack_interleaved_samples( - samples: &[f32], - format: SampleFormat, - dither: AudioDither, -) -> io::Result> { - let bytes_per_sample = format.bytes_per_sample(); - let mut out = Vec::with_capacity(samples.len().saturating_mul(bytes_per_sample)); - let mut rng = DitherRng::new(0x1234_5678_9abc_defe); - - for &sample in samples { - let s = sample.clamp(-1.0, 1.0); - match format { - SampleFormat::U8 => { - let v = ((s + 1.0) * 127.5 + dither_offset(&mut rng, dither)).round() as u8; - out.push(v); - } - SampleFormat::S16 => { - let scale = i16::MAX as f32; - let q = quantize_with_dither(s, scale, &mut rng, dither) - .round() - .clamp(i16::MIN as f32, i16::MAX as f32) as i16; - out.extend_from_slice(&q.to_le_bytes()); - } - SampleFormat::S24 => { - let scale = 8_388_607.0; - let q = quantize_with_dither(s, scale, &mut rng, dither) - .round() - .clamp(-8_388_608.0, 8_388_607.0) as i32; - let b = q.to_le_bytes(); - out.extend_from_slice(&b[..3]); - } - SampleFormat::S32 => { - let scale = i32::MAX as f32; - let q = quantize_with_dither(s, scale, &mut rng, dither) - .round() - .clamp(i32::MIN as f32, i32::MAX as f32) as i32; - out.extend_from_slice(&q.to_le_bytes()); - } - SampleFormat::F32 => { - out.extend_from_slice(&s.to_le_bytes()); - } - _ => { - return Err(io::Error::other(format!( - "unsupported sample format {format:?}" - ))); - } - } - } - Ok(out) -} - -fn quantize_with_dither(sample: f32, scale: f32, rng: &mut DitherRng, dither: AudioDither) -> f32 { - let d = dither_offset(rng, dither); - (sample + d / scale).clamp(-1.0, 1.0) * scale -} - -fn dither_offset(rng: &mut DitherRng, dither: AudioDither) -> f32 { - match dither { - AudioDither::None => 0.0, - AudioDither::Rectangular => rng.uniform_half(), - AudioDither::Triangular => rng.uniform_half() + rng.uniform_half(), - } -} - -fn oxideav_err_to_io(e: oxideav_core::Error) -> io::Error { - io::Error::other(format!("OxideAV error: {e}")) -} - -/// Tiny deterministic PRNG used for export dither. -struct DitherRng { - state: u64, -} - -impl DitherRng { - fn new(seed: u64) -> Self { - Self { state: seed.max(1) } - } - - fn next_u64(&mut self) -> u64 { - // xorshift64* - self.state ^= self.state >> 12; - self.state ^= self.state << 25; - self.state ^= self.state >> 27; - self.state.wrapping_mul(0x2545_f491_4f6c_dd1d) - } - - /// Uniform random value in [-0.5, 0.5). - fn uniform_half(&mut self) -> f32 { - let u = self.next_u64() >> 32; - (u as f32 / 4_294_967_296.0) - 0.5 - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn decode_stereo_wav_returns_interleaved_samples() { - let path = - std::env::temp_dir().join(format!("maolan_stereo_decode_{}.wav", std::process::id())); - write_test_wav_f32( - &path, - &[ - 0.10, 0.60, // - 0.20, 0.70, // - 0.30, 0.80, // - 0.40, 0.90, - ], - 2, - 48_000, - ) - .expect("write test wav"); - - let (samples, channels, sample_rate) = - decode_audio_to_f32_interleaved_sync(&path).expect("decode test wav"); - let _ = std::fs::remove_file(&path); - - assert_eq!(channels, 2); - assert_eq!(sample_rate, 48_000); - assert_eq!(samples.len(), 8); - for (actual, expected) in samples - .iter() - .zip([0.10, 0.60, 0.20, 0.70, 0.30, 0.80, 0.40, 0.90]) - { - assert!((actual - expected).abs() < 1.0e-6); - } - } - - fn write_test_wav_f32( - path: &Path, - samples: &[f32], - channels: usize, - sample_rate: u32, - ) -> io::Result<()> { - let bytes_per_sample = 4usize; - let block_align = (channels * bytes_per_sample) as u16; - let byte_rate = sample_rate * u32::from(block_align); - let data_size = samples.len() * bytes_per_sample; - let riff_size = 36 + data_size as u32; - - let mut file = std::fs::File::create(path)?; - file.write_all(b"RIFF")?; - file.write_all(&riff_size.to_le_bytes())?; - file.write_all(b"WAVE")?; - file.write_all(b"fmt ")?; - file.write_all(&16u32.to_le_bytes())?; - file.write_all(&3u16.to_le_bytes())?; - file.write_all(&(channels as u16).to_le_bytes())?; - file.write_all(&sample_rate.to_le_bytes())?; - file.write_all(&byte_rate.to_le_bytes())?; - file.write_all(&block_align.to_le_bytes())?; - file.write_all(&32u16.to_le_bytes())?; - file.write_all(b"data")?; - file.write_all(&(data_size as u32).to_le_bytes())?; - for sample in samples { - file.write_all(&sample.to_le_bytes())?; - } - Ok(()) - } -} +pub use maolan_engine::audio_codec::*; diff --git a/src/io.rs b/src/io.rs index 219260d..3ecb5e7 100644 --- a/src/io.rs +++ b/src/io.rs @@ -244,7 +244,7 @@ pub(crate) async fn open_audio_dialog() -> Option { rfd::FileDialog::new() .add_filter( "Audio", - &["wav", "flac", "mp3", "ogg", "vorbis", "m4a", "aac", "alac"], + &["wav", "flac", "mp3", "ogg", "vorbis", "m4a", "aac"], ) .pick_file() }