From: will Date: Wed, 16 Oct 2024 23:34:45 +0000 (+0100) Subject: Fixed normaization bug X-Git-Url: https://git.ozva.co.uk/?a=commitdiff_plain;h=bbbe5fb3cd7244dac90771587075410adeabcc0e;p=rust_fft Fixed normaization bug - added looping over audio file - begun adding audio output stream --- diff --git a/Cargo.lock b/Cargo.lock index f20bd0a..4e4b4f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,6 +66,28 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "alsa" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed7572b7ba83a31e20d1b48970ee402d2e3e0537dcfe0a3ff4d6eb7508617d43" +dependencies = [ + "alsa-sys", + "bitflags 2.6.0", + "cfg-if", + "libc", +] + +[[package]] +name = "alsa-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8fee663d06c4e303404ef5f40488a53e062f89ba8bfed81f42325aafad1527" +dependencies = [ + "libc", + "pkg-config", +] + [[package]] name = "android-activity" version = "0.4.3" @@ -78,9 +100,9 @@ dependencies = [ "jni-sys", "libc", "log", - "ndk", + "ndk 0.7.0", "ndk-context", - "ndk-sys", + "ndk-sys 0.4.1+23.1.7779620", "num_enum 0.6.1", ] @@ -143,25 +165,20 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.65.1" +version = "0.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfdf7b466f9a4903edc73f95d6d2bcd5baf8ae620638762244d3f60143643cc5" +checksum = "f49d8fed880d473ea71efb9bf597651e77201bdd4893efe54c9e5d65ae04ce6f" dependencies = [ - "bitflags 1.3.2", + "bitflags 2.6.0", "cexpr", "clang-sys", - "lazy_static", - "lazycell", - "log", - "peeking_take_while", - "prettyplease", + "itertools", "proc-macro2", "quote", "regex", "rustc-hash", "shlex", "syn 2.0.77", - "which", ] [[package]] @@ -229,10 +246,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" [[package]] -name = "byteorder" -version = "1.5.0" +name = "bytes" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" [[package]] name = "calloop" @@ -259,6 +276,12 @@ dependencies = [ "shlex", ] +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + [[package]] name = "cexpr" version = "0.6.0" @@ -307,6 +330,16 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf43edc576402991846b093a7ca18a3477e0ef9c588cde84964b5d3e43016642" +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -347,6 +380,49 @@ dependencies = [ "libc", ] +[[package]] +name = "coreaudio-rs" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "321077172d79c662f64f5071a03120748d5bb652f5231570141be24cfcd2bace" +dependencies = [ + "bitflags 1.3.2", + "core-foundation-sys", + "coreaudio-sys", +] + +[[package]] +name = "coreaudio-sys" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ce857aa0b77d77287acc1ac3e37a05a8c95a2af3647d23b15f263bdaeb7562b" +dependencies = [ + "bindgen", +] + +[[package]] +name = "cpal" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "873dab07c8f743075e57f524c583985fbaf745602acbe916a01539364369a779" +dependencies = [ + "alsa", + "core-foundation-sys", + "coreaudio-rs", + "dasp_sample", + "jni", + "js-sys", + "libc", + "mach2", + "ndk 0.8.0", + "ndk-context", + "oboe", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "windows 0.54.0", +] + [[package]] name = "crc32fast" version = "1.4.2" @@ -367,6 +443,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "dasp_sample" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" + [[package]] name = "dispatch" version = "0.2.0" @@ -400,22 +482,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" -[[package]] -name = "errno" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "fastrand" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" - [[package]] name = "fdeflate" version = "0.3.4" @@ -560,17 +626,6 @@ dependencies = [ "slab", ] -[[package]] -name = "getrandom" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" -dependencies = [ - "cfg-if", - "libc", - "wasi", -] - [[package]] name = "gimli" version = "0.31.0" @@ -601,16 +656,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "gnuplot" -version = "0.0.43" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce70b22788c02857a6436b96744f9030dd89e17c8482ddf674523c0a1f8ab6c" -dependencies = [ - "byteorder", - "tempfile", -] - [[package]] name = "gpu-alloc" version = "0.6.0" @@ -640,7 +685,7 @@ dependencies = [ "log", "thiserror", "winapi", - "windows", + "windows 0.44.0", ] [[package]] @@ -700,15 +745,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" -[[package]] -name = "home" -version = "0.5.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" -dependencies = [ - "windows-sys 0.52.0", -] - [[package]] name = "hound" version = "3.5.1" @@ -747,6 +783,31 @@ dependencies = [ "web-sys", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "jni" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" +dependencies = [ + "cesu8", + "cfg-if", + "combine", + "jni-sys", + "log", + "thiserror", + "walkdir", + "windows-sys 0.45.0", +] + [[package]] name = "jni-sys" version = "0.3.0" @@ -788,12 +849,6 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" -[[package]] -name = "lazycell" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" - [[package]] name = "libc" version = "0.2.158" @@ -831,12 +886,6 @@ dependencies = [ "redox_syscall 0.4.1", ] -[[package]] -name = "linux-raw-sys" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" - [[package]] name = "lock_api" version = "0.4.12" @@ -853,6 +902,15 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "mach2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709" +dependencies = [ + "libc", +] + [[package]] name = "malloc_buf" version = "0.0.6" @@ -967,12 +1025,26 @@ checksum = "451422b7e4718271c8b5b3aadf5adedba43dc76312454b387e98fae0fc951aa0" dependencies = [ "bitflags 1.3.2", "jni-sys", - "ndk-sys", + "ndk-sys 0.4.1+23.1.7779620", "num_enum 0.5.11", "raw-window-handle", "thiserror", ] +[[package]] +name = "ndk" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2076a31b7010b17a38c01907c45b945e8f11495ee4dd588309718901b1f7a5b7" +dependencies = [ + "bitflags 2.6.0", + "jni-sys", + "log", + "ndk-sys 0.5.0+25.2.9519653", + "num_enum 0.7.3", + "thiserror", +] + [[package]] name = "ndk-context" version = "0.1.1" @@ -988,6 +1060,15 @@ dependencies = [ "jni-sys", ] +[[package]] +name = "ndk-sys" +version = "0.5.0+25.2.9519653" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c196769dd60fd4f363e11d948139556a344e79d451aeb2fa2fd040738ef7691" +dependencies = [ + "jni-sys", +] + [[package]] name = "nix" version = "0.24.3" @@ -1032,6 +1113,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "num-integer" version = "0.1.46" @@ -1068,6 +1160,15 @@ dependencies = [ "num_enum_derive 0.6.1", ] +[[package]] +name = "num_enum" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179" +dependencies = [ + "num_enum_derive 0.7.3", +] + [[package]] name = "num_enum_derive" version = "0.5.11" @@ -1092,6 +1193,18 @@ dependencies = [ "syn 2.0.77", ] +[[package]] +name = "num_enum_derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "objc" version = "0.2.7" @@ -1146,6 +1259,29 @@ dependencies = [ "memchr", ] +[[package]] +name = "oboe" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8b61bebd49e5d43f5f8cc7ee2891c16e0f41ec7954d36bcb6c14c5e0de867fb" +dependencies = [ + "jni", + "ndk 0.8.0", + "ndk-context", + "num-derive", + "num-traits", + "oboe-sys", +] + +[[package]] +name = "oboe-sys" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8bb09a4a2b1d668170cfe0a7d5bc103f8999fb316c98099b6a9939c9f2e79d" +dependencies = [ + "cc", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -1199,12 +1335,6 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" -[[package]] -name = "peeking_take_while" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" - [[package]] name = "percent-encoding" version = "2.3.1" @@ -1252,25 +1382,6 @@ dependencies = [ "miniz_oxide 0.7.4", ] -[[package]] -name = "ppv-lite86" -version = "0.2.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" -dependencies = [ - "zerocopy", -] - -[[package]] -name = "prettyplease" -version = "0.2.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba" -dependencies = [ - "proc-macro2", - "syn 2.0.77", -] - [[package]] name = "primal-check" version = "0.3.4" @@ -1314,36 +1425,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rand" -version = "0.8.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" -dependencies = [ - "libc", - "rand_chacha", - "rand_core", -] - -[[package]] -name = "rand_chacha" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" -dependencies = [ - "ppv-lite86", - "rand_core", -] - -[[package]] -name = "rand_core" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -dependencies = [ - "getrandom", -] - [[package]] name = "range-alloc" version = "0.1.3" @@ -1422,12 +1503,10 @@ checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" name = "rust_fft" version = "0.1.0" dependencies = [ - "gnuplot", + "cpal", "hound", - "rand", "rustfft", "show-image", - "v4l", ] [[package]] @@ -1467,16 +1546,12 @@ dependencies = [ ] [[package]] -name = "rustix" -version = "0.38.37" +name = "same-file" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ - "bitflags 2.6.0", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.52.0", + "winapi-util", ] [[package]] @@ -1641,19 +1716,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tempfile" -version = "3.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" -dependencies = [ - "cfg-if", - "fastrand", - "once_cell", - "rustix", - "windows-sys 0.59.0", -] - [[package]] name = "termcolor" version = "1.4.1" @@ -1759,26 +1821,6 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "229730647fbc343e3a80e463c1db7f78f3855d3f3739bee0dda773c9a037c90a" -[[package]] -name = "v4l" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8fbfea44a46799d62c55323f3c55d06df722fbe577851d848d328a1041c3403" -dependencies = [ - "bitflags 1.3.2", - "libc", - "v4l2-sys-mit", -] - -[[package]] -name = "v4l2-sys-mit" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6779878362b9bacadc7893eac76abe69612e8837ef746573c4a5239daf11990b" -dependencies = [ - "bindgen", -] - [[package]] name = "vec_map" version = "0.8.2" @@ -1791,6 +1833,16 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -2046,18 +2098,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "which" -version = "4.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" -dependencies = [ - "either", - "home", - "once_cell", - "rustix", -] - [[package]] name = "widestring" version = "1.1.0" @@ -2104,6 +2144,35 @@ dependencies = [ "windows-targets 0.42.2", ] +[[package]] +name = "windows" +version = "0.54.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9252e5725dbed82865af151df558e754e4a3c2c30818359eb17465f1346a1b49" +dependencies = [ + "windows-core", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.54.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12661b9c89351d684a50a8a643ce5f608e20243b9fb84687800163429f161d65" +dependencies = [ + "windows-result", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.45.0" @@ -2122,15 +2191,6 @@ dependencies = [ "windows-targets 0.48.5", ] -[[package]] -name = "windows-sys" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" -dependencies = [ - "windows-targets 0.52.6", -] - [[package]] name = "windows-sys" version = "0.59.0" @@ -2334,7 +2394,7 @@ dependencies = [ "libc", "log", "mio", - "ndk", + "ndk 0.7.0", "objc2", "once_cell", "orbclient", @@ -2391,7 +2451,6 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ - "byteorder", "zerocopy-derive", ] diff --git a/Cargo.toml b/Cargo.toml index d199849..42c75ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,5 @@ edition = "2021" [dependencies] rustfft = "6.2.0" show-image = "0.14.0" -v4l = "0.14.0" -rand = "0.8.4" -gnuplot = "0.0.43" hound = "3.5.1" +cpal = "0.15.3" diff --git a/src/main.rs b/src/main.rs index 6c1f379..18efd08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,8 @@ use rustfft::{Fft, FftDirection}; use rustfft::num_complex::Complex; use show_image::{ImageView, ImageInfo, create_window}; use hound; -use gnuplot::{Figure, Caption, Color}; +use cpal::{Sample, StreamConfig, BufferSize, SampleRate}; +use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; const WINDOW_SIZE: usize = 128; const CHUNK_SIZE: usize = 72; @@ -21,9 +22,6 @@ const VOLUME_MAX: f32 = 100.0; // 60 - 65 const VOLUME_MIN: f32 = -40.0; const VOLUME_REL: f32 = VOLUME_MAX - VOLUME_MIN; -// sqrt(3) / 2 -const SQRT3_2: f32 = 0.86602540378443; - struct ImageArray { data: [u8; SPECTOGRAM_AREA * 3], chunks: usize @@ -41,8 +39,6 @@ impl ImageArray { for i in 0..self.chunks { let (r, theta): (f32, f32) = buffer[i].to_polar(); - if i == 100 {println!("theta: {theta}");} - let amplitude = 20f32 * r.log10(); let amplitude = ((amplitude - VOLUME_MIN) / (VOLUME_REL / AMPLITUDE_REL)) + AMPLITUDE_MIN; @@ -54,11 +50,6 @@ impl ImageArray { let s = angle / 255f32; let v = amplitude / 255f32; - let s1 = s * 255f32; - let v1 = v * 255f32; - - if i == 100 {println!("h: {hue} s:{s1} v:{v1}");} - let c = s * v; let m = v - c; let x = c * (1f32 - (d.rem_euclid(2f32) - 1f32).abs()); @@ -84,10 +75,6 @@ impl ImageArray { let g = self.data[i*3+1] as f32; let b = self.data[i*3+2] as f32; - let alpha = r - (g / 2f32) - (b / 2f32); - let beta = SQRT3_2 * (g - b); - - // let h = beta.atan2(alpha).to_degrees() + 180f32; let v = r.max(g).max(b); let c = (v - r.min(g).min(b)) * 255f32; let s = if v == 0f32 { 0f32 } else { c / v }; @@ -96,150 +83,76 @@ impl ImageArray { let amplitude = 10f32.powf(amplitude / 20f32); - if i == 100 {println!("alpha: {alpha}, beta: {beta} chroma: {c}");} - if i == 100 {println!("s: {s}, v: {v}");} - let angle = (s - ANGLE_MIN) / (ANGLE_REL / 360f32) - 180f32; let angle = angle.to_radians(); - if i == 100 {println!("theta: {angle}");} - buffer[i] = Complex::from_polar(amplitude, angle); } } } -fn _test () -> Result<(), Box> { - let forward_transform = Radix4::::new(WINDOW_SIZE, FftDirection::Forward); - let inverse_transform = Radix4::::new(WINDOW_SIZE, FftDirection::Inverse); - let scratch_space = forward_transform.get_inplace_scratch_len(); - - let mut buffer = vec![Complex{re: 0f32, im: 0f32}; SPECTOGRAM_AREA]; - let mut origional = vec![Complex{re: 0f32, im: 0f32}; SPECTOGRAM_AREA]; - let mut scratch = vec![Complex{re: 0f32, im: 0f32}; scratch_space]; - - for i in 0..SPECTOGRAM_AREA { - let rand1 = rand::random::(); - let rand2 = rand::random::(); - buffer[i] = Complex{re: rand1, im: rand2}; - origional[i] = Complex{re: rand1, im: rand2}; - } - - forward_transform.process_with_scratch(&mut buffer, &mut scratch); - inverse_transform.process_with_scratch(&mut buffer, &mut scratch); - - let mut real_diff = [0f32; SPECTOGRAM_AREA]; - let mut imag_diff = [0f32; SPECTOGRAM_AREA]; - - for i in 0..SPECTOGRAM_AREA { - real_diff[i] = origional[i].re - buffer[i].re; - imag_diff[i] = origional[i].im - buffer[i].im; - } - - let mut fg = Figure::new(); - fg.axes2d() - .lines( - &real_diff, - &imag_diff, - &[Caption("Difference"), Color("blue")] - ); - fg.show().unwrap(); - - Ok(()) -} - #[show_image::main] fn main () -> Result<(), Box> { - // register the fft transformers + let forward_transform = Radix4::::new(WINDOW_SIZE, FftDirection::Forward); let inverse_transform = Radix4::::new(WINDOW_SIZE, FftDirection::Inverse); - let scratch_space = forward_transform.get_inplace_scratch_len(); - - // register the buffer and scratch space - let mut buffer = vec![Complex{re: 0f32, im: 0f32}; SPECTOGRAM_AREA]; - let mut scratch = vec![Complex{re: 0f32, im: 0f32}; scratch_space]; + let scratch_size = forward_transform.get_inplace_scratch_len(); - let mut reader = hound::WavReader::open("/home/will/Downloads/Adducci - Around the Horn.wav").unwrap(); - for (i, sample) in reader.samples::().enumerate() { - if i >= SPECTOGRAM_AREA {break;} - let value = match sample { - Ok(t) => t, - Err(_) => 0i16 - }; - buffer[i] = Complex{re: value as f32, im: 0f32}; - } - - // register the image let mut image_array = ImageArray::new(); + let mut buffer = vec![Complex{re: 0f32, im: 0f32}; SPECTOGRAM_AREA]; + let mut scratch = vec![Complex{re: 0f32, im: 0f32}; scratch_size]; - // get the time, real and imag components of the fft buffer - let mut origional_real = [0f32; SPECTOGRAM_AREA]; - let mut origional_imag = [0f32; SPECTOGRAM_AREA]; - for i in 0..SPECTOGRAM_AREA { - origional_real[i] = buffer[i].re; - origional_imag[i] = buffer[i].im; - } - - forward_transform.process_with_scratch(&mut buffer, &mut scratch); + let window = create_window("image", Default::default())?; - let mut fft_real = [0f32; SPECTOGRAM_AREA]; - let mut fft_imag = [0f32; SPECTOGRAM_AREA]; - for i in 0..SPECTOGRAM_AREA { - fft_real[i] = buffer[i].re; - fft_imag[i] = buffer[i].im; - } + let host = cpal::default_host(); + let device = host.default_output_device().expect("no output device available"); + let error_function = |err| eprintln!("an error occurred on the output audio stream: {}", err); - //image_array.from_buffer(&buffer); + let stream = device.build_output_stream( + &StreamConfig{ + channels: 1, + sample_rate: SampleRate{0: 22_050}, + buffer_size: BufferSize::Default + }, + get_samples, error_function, None + ).unwrap(); - //image_array.to_buffer(&mut buffer); + let mut get_samples = (data: &mut [T], _: &cpal::OutputCallbackInfo)|| { - let mut transformed_real = [0f32; SPECTOGRAM_AREA]; - let mut transformed_imag = [0f32; SPECTOGRAM_AREA]; - for i in 0..SPECTOGRAM_AREA { - transformed_real[i] = buffer[i].re; - transformed_imag[i] = buffer[i].im; + return; } - inverse_transform.process_with_scratch(&mut buffer, &mut scratch); - - // get the time, real and imag components of the fft buffer - let mut processed_real = [0f32; SPECTOGRAM_AREA]; - let mut processed_imag = [0f32; SPECTOGRAM_AREA]; - for i in 0..SPECTOGRAM_AREA { - processed_real[i] = buffer[i].re; - processed_imag[i] = buffer[i].im; + let mut reader = hound::WavReader::open("/home/will/Downloads/Adducci - Around the Horn.wav").unwrap(); + let mut i = 0; + for sample in reader.samples::() { + if i == SPECTOGRAM_AREA { + forward_transform.process_with_scratch(&mut buffer, &mut scratch); + for x in buffer.iter_mut() { + *x *= 1f32 / WINDOW_SIZE as f32; + } + image_array.from_buffer(&buffer); + + image_array.to_buffer(&mut buffer); + inverse_transform.process_with_scratch(&mut buffer, &mut scratch); + + let image = ImageView::new(ImageInfo::rgb8(WINDOW_SIZE as u32, CHUNK_SIZE as u32), &image_array.data); + window.set_image ("image", image)?; + + i = 0; + } + let value = match sample { + Ok(t) => t, + Err(_) => 0i16 + }; + buffer[i] = Complex{re: value as f32, im: 0f32}; + i += 1; } - let image = ImageView::new(ImageInfo::rgb8(WINDOW_SIZE as u32, CHUNK_SIZE as u32), &image_array.data); // Create a window with default options and display the image. - let window = create_window("image", Default::default())?; - window.set_image("image-001", image)?; - - let mut fg = Figure::new(); - fg.axes2d() - .lines( - &fft_real, - &fft_imag, - &[Caption("FFT"), Color("red")] - ) - .lines( - &transformed_real, - &transformed_imag, - &[Caption("Transfomed"), Color("green")] - ) - .lines( - &processed_real, - &processed_imag, - &[Caption("Processed"), Color("yellow")] - ) - .lines( - &origional_real, - &origional_imag, - &[Caption("Origional"), Color("blue")] - ); - fg.show().unwrap(); + + loop {} Ok(())