From 99100be148664b477d32508090e1298253401d99 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Tue, 28 Sep 2021 19:54:05 +0200 Subject: [PATCH 1/5] trying to play sound on F12 --- Cargo.lock | 404 +++++++++++++++++- editor/Cargo.toml | 1 + editor/src/editor/keyboard_input.rs | 11 +- editor/src/editor/main.rs | 14 +- editor/src/editor/mvc/app_model.rs | 28 +- editor/src/editor/mvc/app_update.rs | 12 +- editor/src/editor/mvc/ed_update.rs | 30 +- .../editor/resources/sounds/bell_sound.mp3 | Bin 0 -> 7345 bytes 8 files changed, 477 insertions(+), 23 deletions(-) create mode 100644 editor/src/editor/resources/sounds/bell_sound.mp3 diff --git a/Cargo.lock b/Cargo.lock index e0b4825c91..fe4df731ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,28 @@ dependencies = [ "memchr", ] +[[package]] +name = "alsa" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75c4da790adcb2ce5e758c064b4f3ec17a30349f9961d3e5e6c9688b052a9e18" +dependencies = [ + "alsa-sys", + "bitflags", + "libc", + "nix 0.20.0", +] + +[[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 = "andrew" version = "0.3.1" @@ -186,6 +208,25 @@ dependencies = [ "serde", ] +[[package]] +name = "bindgen" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2da379dbebc0b76ef63ca68d8fc6e71c0f13e59432e0987e508c1820e6ab5239" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "peeking_take_while", + "proc-macro2 1.0.29", + "quote 1.0.9", + "regex", + "rustc-hash", + "shlex", +] + [[package]] name = "bit-set" version = "0.5.2" @@ -308,6 +349,12 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + [[package]] name = "calloop" version = "0.6.5" @@ -336,6 +383,21 @@ dependencies = [ "jobserver", ] +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + +[[package]] +name = "cexpr" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27" +dependencies = [ + "nom 5.1.2", +] + [[package]] name = "cfg-if" version = "0.1.10" @@ -364,6 +426,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "clang-sys" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10612c0ec0e0a1ff0e97980647cb058a6e7aedb913d01d009c406b8b7d0b26ee" +dependencies = [ + "glob", + "libc", + "libloading 0.7.0", +] + [[package]] name = "clap" version = "2.33.3" @@ -404,6 +477,12 @@ dependencies = [ "syn 1.0.76", ] +[[package]] +name = "claxon" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bfbf56724aa9eca8afa4fcfadeb479e722935bb2a0900c2d37e0cc477af0688" + [[package]] name = "cli_utils" version = "0.1.0" @@ -493,6 +572,16 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "combine" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a909e4d93292cd8e9c42e189f61681eff9d67b6541f96b8a1a737f23737bd001" +dependencies = [ + "bytes 1.1.0", + "memchr", +] + [[package]] name = "confy" version = "0.4.0" @@ -625,6 +714,50 @@ dependencies = [ "objc", ] +[[package]] +name = "coreaudio-rs" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11894b20ebfe1ff903cbdc52259693389eea03b94918a2def2c30c3bf227ad88" +dependencies = [ + "bitflags", + "coreaudio-sys", +] + +[[package]] +name = "coreaudio-sys" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b7e3347be6a09b46aba228d6608386739fb70beff4f61e07422da87b0bb31fa" +dependencies = [ + "bindgen", +] + +[[package]] +name = "cpal" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98f45f0a21f617cd2c788889ef710b63f075c949259593ea09c826f1e47a2418" +dependencies = [ + "alsa", + "core-foundation-sys 0.8.2", + "coreaudio-rs", + "jni", + "js-sys", + "lazy_static", + "libc", + "mach", + "ndk 0.3.0", + "ndk-glue 0.3.0", + "nix 0.20.0", + "oboe", + "parking_lot", + "stdweb", + "thiserror", + "web-sys", + "winapi 0.3.9", +] + [[package]] name = "cpufeatures" version = "0.2.1" @@ -1587,6 +1720,12 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" +[[package]] +name = "glob" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" + [[package]] name = "glow" version = "0.9.0" @@ -1720,6 +1859,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hound" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549" + [[package]] name = "humantime" version = "2.1.0" @@ -1948,6 +2093,20 @@ version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" +[[package]] +name = "jni" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6df18c2e3db7e453d3c6ac5b3e9d5182664d28788126d39b91f2d1e22b017ec" +dependencies = [ + "cesu8", + "combine", + "jni-sys", + "log", + "thiserror", + "walkdir", +] + [[package]] name = "jni-sys" version = "0.3.0" @@ -2016,6 +2175,17 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3576a87f2ba00f6f106fdfcd16db1d698d648a26ad8e0573cad8537c3c362d2a" +[[package]] +name = "lewton" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "777b48df9aaab155475a83a7df3070395ea1ac6902f5cd062b8f2b028075c030" +dependencies = [ + "byteorder", + "ogg", + "tinyvec", +] + [[package]] name = "libc" version = "0.2.101" @@ -2219,6 +2389,26 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c835948974f68e0bd58636fc6c5b1fbff7b297e3046f11b3b3c18bbac012c6d" +[[package]] +name = "minimp3" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985438f75febf74c392071a975a29641b420dd84431135a6e6db721de4b74372" +dependencies = [ + "minimp3-sys", + "slice-deque", + "thiserror", +] + +[[package]] +name = "minimp3-sys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e21c73734c69dc95696c9ed8926a2b393171d98b3f5f5935686a26a487ab9b90" +dependencies = [ + "cc", +] + [[package]] name = "miniz_oxide" version = "0.4.4" @@ -2313,7 +2503,32 @@ checksum = "5eb167c1febed0a496639034d0c76b3b74263636045db5489eee52143c246e73" dependencies = [ "jni-sys", "ndk-sys", - "num_enum", + "num_enum 0.4.3", + "thiserror", +] + +[[package]] +name = "ndk" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8794322172319b972f528bf90c6b467be0079f1fa82780ffb431088e741a73ab" +dependencies = [ + "jni-sys", + "ndk-sys", + "num_enum 0.5.4", + "thiserror", +] + +[[package]] +name = "ndk" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d64d6af06fde0e527b1ba5c7b79a6cc89cfc46325b0b2887dffe8f70197e0c3c" +dependencies = [ + "bitflags", + "jni-sys", + "ndk-sys", + "num_enum 0.5.4", "thiserror", ] @@ -2326,7 +2541,35 @@ dependencies = [ "lazy_static", "libc", "log", - "ndk", + "ndk 0.2.1", + "ndk-macro", + "ndk-sys", +] + +[[package]] +name = "ndk-glue" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5caf0c24d51ac1c905c27d4eda4fa0635bbe0de596b8f79235e0b17a4d29385" +dependencies = [ + "lazy_static", + "libc", + "log", + "ndk 0.3.0", + "ndk-macro", + "ndk-sys", +] + +[[package]] +name = "ndk-glue" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e9e94628f24e7a3cb5b96a2dc5683acd9230bf11991c2a1677b87695138420" +dependencies = [ + "lazy_static", + "libc", + "log", + "ndk 0.4.0", "ndk-macro", "ndk-sys", ] @@ -2338,7 +2581,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05d1c6307dc424d0f65b9b06e94f88248e6305726b14729fd67a5e47b2dc481d" dependencies = [ "darling 0.10.2", - "proc-macro-crate", + "proc-macro-crate 0.1.5", "proc-macro2 1.0.29", "quote 1.0.9", "syn 1.0.76", @@ -2398,6 +2641,16 @@ dependencies = [ "libc", ] +[[package]] +name = "nom" +version = "5.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" +dependencies = [ + "memchr", + "version_check", +] + [[package]] name = "nom" version = "7.0.0" @@ -2415,6 +2668,17 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fa586da3e43cc7df44aae0e21ed2e743218b876de3f38035683d30bd8a3828e" +[[package]] +name = "num-derive" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" +dependencies = [ + "proc-macro2 1.0.29", + "quote 1.0.9", + "syn 1.0.76", +] + [[package]] name = "num-traits" version = "0.2.14" @@ -2441,7 +2705,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca565a7df06f3d4b485494f25ba05da1435950f4dc263440eda7a6fa9b8e36e4" dependencies = [ "derivative", - "num_enum_derive", + "num_enum_derive 0.4.3", +] + +[[package]] +name = "num_enum" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9bd055fb730c4f8f4f57d45d35cd6b3f0980535b056dc7ff119cee6a66ed6f" +dependencies = [ + "derivative", + "num_enum_derive 0.5.4", ] [[package]] @@ -2450,7 +2724,19 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffa5a33ddddfee04c0283a7653987d634e880347e96b5b2ed64de07efb59db9d" dependencies = [ - "proc-macro-crate", + "proc-macro-crate 0.1.5", + "proc-macro2 1.0.29", + "quote 1.0.9", + "syn 1.0.76", +] + +[[package]] +name = "num_enum_derive" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "486ea01961c4a818096de679a8b740b26d9033146ac5291b1c98557658f8cdd9" +dependencies = [ + "proc-macro-crate 1.1.0", "proc-macro2 1.0.29", "quote 1.0.9", "syn 1.0.76", @@ -2529,6 +2815,38 @@ dependencies = [ "memchr", ] +[[package]] +name = "oboe" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e15e22bc67e047fe342a32ecba55f555e3be6166b04dd157cd0f803dfa9f48e1" +dependencies = [ + "jni", + "ndk 0.4.0", + "ndk-glue 0.4.0", + "num-derive", + "num-traits", + "oboe-sys", +] + +[[package]] +name = "oboe-sys" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338142ae5ab0aaedc8275aa8f67f460e43ae0fca76a695a742d56da0a269eadc" +dependencies = [ + "cc", +] + +[[package]] +name = "ogg" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6951b4e8bf21c8193da321bcce9c9dd2e13c858fe078bf9054a288b419ae5d6e" +dependencies = [ + "byteorder", +] + [[package]] name = "once_cell" version = "1.8.0" @@ -2657,6 +2975,12 @@ dependencies = [ "winapi 0.3.9", ] +[[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.1.0" @@ -2842,6 +3166,16 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-crate" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebace6889caf889b4d3f76becee12e90353f2b8c7d875534a71e5742f8f6f83" +dependencies = [ + "thiserror", + "toml", +] + [[package]] name = "proc-macro-error" version = "0.4.12" @@ -3603,6 +3937,7 @@ dependencies = [ "roc_solve", "roc_types", "roc_unify", + "rodio", "ropey", "serde", "snafu", @@ -3944,6 +4279,19 @@ dependencies = [ "roc_types", ] +[[package]] +name = "rodio" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d98f5e557b61525057e2bc142c8cd7f0e70d75dc32852309bec440e6e046bf9" +dependencies = [ + "claxon", + "cpal", + "hound", + "lewton", + "minimp3", +] + [[package]] name = "ropey" version = "1.3.1" @@ -4204,6 +4552,12 @@ dependencies = [ "opaque-debug 0.3.0", ] +[[package]] +name = "shlex" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" + [[package]] name = "siphasher" version = "0.3.7" @@ -4236,6 +4590,17 @@ version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c307a32c1c5c437f38c7fd45d753050587732ba8628319fbdf12a7e289ccc590" +[[package]] +name = "slice-deque" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31ef6ee280cdefba6d2d0b4b78a84a1c1a3f3a4cec98c2d4231c8bc225de0f25" +dependencies = [ + "libc", + "mach", + "winapi 0.3.9", +] + [[package]] name = "slotmap" version = "0.4.3" @@ -4349,6 +4714,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "stdweb" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef5430c8e36b713e13b48a9f709cc21e046723fe44ce34587b73a830203b533e" + [[package]] name = "storage-map" version = "0.3.0" @@ -4602,13 +4973,28 @@ dependencies = [ "serde_json", ] +[[package]] +name = "tinyvec" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83b2a3d4d9091d0abd7eba4dc2710b1718583bd4d8992e2190720ea38f391f7" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + [[package]] name = "tokio" version = "0.2.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6703a273949a90131b290be1fe7b039d0fc884aa1935860dfcbe056f28cd8092" dependencies = [ - "bytes", + "bytes 0.5.6", "fnv", "memchr", "num_cpus", @@ -5395,8 +5781,8 @@ dependencies = [ "log", "mio", "mio-extras", - "ndk", - "ndk-glue", + "ndk 0.2.1", + "ndk-glue 0.2.1", "ndk-sys", "objc", "parking_lot", @@ -5473,7 +5859,7 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "463705a63313cd4301184381c5e8042f0a7e9b4bb63653f216311d4ae74690b7" dependencies = [ - "nom", + "nom 7.0.0", ] [[package]] diff --git a/editor/Cargo.toml b/editor/Cargo.toml index f9207b2e38..c7d140ba3d 100644 --- a/editor/Cargo.toml +++ b/editor/Cargo.toml @@ -54,6 +54,7 @@ nonempty = "0.6.0" tempfile = "3.2.0" uuid = { version = "0.8", features = ["v4"] } fs_extra = "1.2.0" +rodio = "0.14.0" [dependencies.bytemuck] version = "1.4" diff --git a/editor/src/editor/keyboard_input.rs b/editor/src/editor/keyboard_input.rs index 95538347a3..d781cc5c97 100644 --- a/editor/src/editor/keyboard_input.rs +++ b/editor/src/editor/keyboard_input.rs @@ -1,9 +1,13 @@ +use std::fs::File; +use std::io::BufReader; + use crate::editor::ed_error::EdResult; use crate::editor::mvc::app_model::AppModel; use crate::editor::mvc::app_update::{ handle_copy, handle_cut, handle_paste, pass_keydown_to_focused, }; use crate::window::keyboard_input::from_winit; +use rodio::{Decoder, OutputStream, Source}; use winit::event::VirtualKeyCode::*; use winit::event::{ElementState, ModifiersState, VirtualKeyCode}; @@ -13,6 +17,7 @@ pub fn handle_keydown( modifiers_winit: ModifiersState, app_model: &mut AppModel, ) -> EdResult<()> { + if let ElementState::Released = elem_state { return Ok(()); } @@ -43,11 +48,7 @@ pub fn handle_keydown( } } - A | S | R | Home | End => pass_keydown_to_focused(&modifiers, virtual_keycode, app_model)?, - - F11 => pass_keydown_to_focused(&modifiers, virtual_keycode, app_model)?, - - _ => (), + _ => pass_keydown_to_focused(&modifiers, virtual_keycode, app_model)?, } Ok(()) diff --git a/editor/src/editor/main.rs b/editor/src/editor/main.rs index 9ab70d32ba..f1be6b8ec5 100644 --- a/editor/src/editor/main.rs +++ b/editor/src/editor/main.rs @@ -31,9 +31,10 @@ use roc_load; use roc_load::file::LoadedModule; use roc_module::symbol::IdentIds; use roc_types::subs::VarStore; +use rodio::{Decoder, OutputStream, Source}; use std::collections::HashSet; use std::fs::{self, File}; -use std::io::Write; +use std::io::{BufReader, Write}; use std::{error::Error, io, path::Path}; use wgpu::{CommandEncoder, LoadOp, RenderPass, TextureView}; use wgpu_glyph::GlyphBrush; @@ -62,6 +63,15 @@ pub fn launch(project_dir_path_opt: Option<&Path>) -> io::Result<()> { fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box> { env_logger::init(); + //let (_, stream_handle) = OutputStream::try_default().unwrap(); + /* + // Load a sound from a file, using a path relative to Cargo.toml + let file = BufReader::new(File::open("./editor/src/editor/resources/sounds/bell_sound.mp3").unwrap()); + // Decode that sound file into a source + let source = Decoder::new(file).unwrap(); + // Play the sound directly on the device + stream_handle.play_raw(source.convert_samples()).unwrap(); + */ // Open window and create a surface let mut event_loop = winit::event_loop::EventLoop::new(); @@ -173,7 +183,7 @@ fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box = None; - let mut app_model = AppModel::init(ed_model_opt); + let mut app_model = AppModel::init(ed_model_opt, None); let mut keyboard_modifiers = ModifiersState::empty(); diff --git a/editor/src/editor/mvc/app_model.rs b/editor/src/editor/mvc/app_model.rs index b537e7a34f..1029602db9 100644 --- a/editor/src/editor/mvc/app_model.rs +++ b/editor/src/editor/mvc/app_model.rs @@ -7,19 +7,31 @@ use crate::editor::ed_error::{ EdResult, }; use copypasta::{ClipboardContext, ClipboardProvider}; -use std::fmt; +use rodio::{Decoder, OutputStream, OutputStreamHandle}; +use std::{fmt, fs::File, io::BufReader}; -#[derive(Debug)] pub struct AppModel<'a> { pub ed_model_opt: Option>, pub clipboard_opt: Option, + pub sound_output_stream_opt: Option, + pub sound_file_name_opt: Option<&'static str> } impl<'a> AppModel<'a> { - pub fn init(ed_model_opt: Option>) -> AppModel { + pub fn init(ed_model_opt: Option>, sound_output_stream_opt: Option) -> AppModel { + + /*let (sound_output_stream_opt, sound_file_name_opt) = + if let Some((sound_output_stream, sound_file_name)) = setup_sound() { + (Some(sound_output_stream), Some(sound_file_name)) + } else { + (None, None) + };*/ + AppModel { ed_model_opt, clipboard_opt: AppModel::init_clipboard_opt(), + sound_output_stream_opt, + sound_file_name_opt: None } } @@ -98,6 +110,16 @@ pub fn get_clipboard_txt(clipboard_opt: &mut Option) -> EdResult Option<(OutputStreamHandle, &'static str)> { + // TODO no unwrap + //let (_, stream_handle) = OutputStream::try_default().unwrap(); + // Load a sound from a file, using a path relative to Cargo.toml + let file_path_str = "./editor/src/editor/resources/sounds/bell_sound.mp3"; + + //Some((stream_handle, file_path_str)) + None +} + impl fmt::Debug for Clipboard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // showing the clipboard would require a mut ref which is not possible diff --git a/editor/src/editor/mvc/app_update.rs b/editor/src/editor/mvc/app_update.rs index 1b6ef6bd60..8036a3583d 100644 --- a/editor/src/editor/mvc/app_update.rs +++ b/editor/src/editor/mvc/app_update.rs @@ -1,7 +1,11 @@ +use std::fs::File; +use std::io::BufReader; + use super::app_model::AppModel; use super::ed_update; use crate::window::keyboard_input::Modifiers; use crate::{editor::ed_error::EdResult, window::keyboard_input::from_winit}; +use rodio::{Decoder, OutputStream, Source}; use winit::event::{ModifiersState, VirtualKeyCode}; pub fn handle_copy(app_model: &mut AppModel) -> EdResult<()> { @@ -39,9 +43,15 @@ pub fn pass_keydown_to_focused( virtual_keycode: VirtualKeyCode, app_model: &mut AppModel, ) -> EdResult<()> { + if let Some(ref mut ed_model) = app_model.ed_model_opt { if ed_model.has_focus { - ed_model.ed_handle_key_down(modifiers, virtual_keycode)?; + ed_model.ed_handle_key_down( + modifiers, + virtual_keycode, + app_model.sound_output_stream_opt.as_ref(), + app_model.sound_file_name_opt, + )?; } } diff --git a/editor/src/editor/mvc/ed_update.rs b/editor/src/editor/mvc/ed_update.rs index 96a9f61d33..f4c8f9509c 100644 --- a/editor/src/editor/mvc/ed_update.rs +++ b/editor/src/editor/mvc/ed_update.rs @@ -1,7 +1,10 @@ #![allow(dead_code)] +use std::fs::File; +use std::io::BufReader; use std::process::Command; use std::process::Stdio; +use std::time::Duration; use crate::editor::code_lines::CodeLines; use crate::editor::ed_error::from_ui_res; @@ -59,6 +62,12 @@ use roc_region::all::Region; use roc_types::solved_types::Solved; use roc_types::subs::{Subs, Variable}; use roc_types::{pretty_print::content_to_string, subs::VarStore}; +use rodio::Decoder; +use rodio::OutputStream; +use rodio::OutputStreamHandle; +use rodio::Sink; +use rodio::Source; +use rodio::source::SineWave; use snafu::OptionExt; use winit::event::VirtualKeyCode; use VirtualKeyCode::*; @@ -520,7 +529,10 @@ impl<'a> EdModel<'a> { &mut self, modifiers: &Modifiers, virtual_keycode: VirtualKeyCode, + sound_output_stream_opt: Option<&OutputStreamHandle>, + sound_file_name_opt: Option<&str>, ) -> EdResult<()> { + match virtual_keycode { Left => self.move_caret_left(modifiers)?, Up => { @@ -556,6 +568,18 @@ impl<'a> EdModel<'a> { self.show_debug_view = !self.show_debug_view; self.dirty = true; } + F12 => { + let (_stream, stream_handle) = OutputStream::try_default().unwrap(); + // Load a sound from a file, using a path relative to Cargo.toml + let file = BufReader::new(File::open("./editor/src/editor/resources/sounds/bell_sound.mp3").unwrap()); + // Decode that sound file into a source + let source = Decoder::new(file).unwrap(); + // Play the sound directly on the device + stream_handle.play_raw(source.convert_samples()).unwrap(); + dbg!("played sound"); + std::thread::sleep(std::time::Duration::from_secs(1)); + + } _ => (), } @@ -2646,7 +2670,7 @@ pub mod test_ed_update { } for _ in 0..repeats { - ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up)?; + ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, None, None)?; } let mut post_lines = ui_res_to_res(ed_model_to_dsl(&ed_model))?; @@ -3223,7 +3247,7 @@ pub mod test_ed_update { )?; for _ in 0..repeats { - ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up)?; + ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, None, None)?; } move_caret_fun(&mut ed_model, &no_mods())?; @@ -3387,7 +3411,7 @@ pub mod test_ed_update { )?; for _ in 0..repeats { - ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up)?; + ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, None, None)?; } handle_new_char(&'\u{8}', &mut ed_model)?; // \u{8} is the char for backspace on linux diff --git a/editor/src/editor/resources/sounds/bell_sound.mp3 b/editor/src/editor/resources/sounds/bell_sound.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..c18b13085876c7ab70338f28833ed651c701a51e GIT binary patch literal 7345 zcmdscc{J30|M%xRJ7&gUFqSmN5>ZAGTE`larqESN4Oz=Fxpbw~ckDzVH0=~crKm(y z#*)2-kh-dCv?xjTvVWiNbkqI2|9H-G?th;1oacRx;T&V;^?ohy&-*iw$rNw!f2b`1 z!G7pdWYI4T0Mv_shK7crp`n?XnKfF@&d%=c?!IV+goGS8Z~#UtK0ZDzE$uW~+1c6o z`T2!t6&DwmmzP(fRa;xz+}zxPR!2w2n>TOXqt)Hr{q5VgLA1uk#t;NS-;3*Hx!!i^ zVq-QNcYh9{DN`FKzaBth2@cu^71Oz2{D1o$nS724gvr%=9Yhw90H83l0Ho&Nux|WO zq&)y8QE2Bu&$!UN&cLH6?Wb2yRsi7X-5)Wy|LFJc?^$SlN9$id^dnP#hjiDk*t_=V z43~z3fM`vldFkr;NZgQEtf@!BPKX3a@aicNLp=T|lLr8_G#M`j;07Q;yrnoPCK)eh zI-l)H%QF_x1esG!1dt8+64M9T-NzQ2)Ukx3LPitA6^B<*planuiZ@-$>Je~^+T|ku z`DUk$tjjmB%c(#HU+()F>v1lyqhK+DjQP4z^wYeBhFZulhh|>;u@{-*Q;mJRA*%B+ zi%_49c`d0tLIHvZKgtHbL?#_P3jW%yk=A&Tmxo7vGS-VeAn#~}&gu#_o}IqMrl#(j zwLMe9Z1(i7L68w&8P!@0yfXqpW-^OSmoW7CDcpqB{LZ6XbV*riXd!yc_X9kR5+G9qS!@jqBoYRo z&=6#ak&OLCYm$H`O#qxi##%*=_x23v*}YnVNlZPF%w@|1OqC^x0CCin_bM&plQIx- zdpUw=?oGh}kP&J9GJmZ-hruCUA!ajq9n!Vx@4Z7Y*1FnR{=5_@i?@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 Date: Tue, 28 Sep 2021 20:07:27 +0200 Subject: [PATCH 2/5] can now play multiple sounds in rapid succession --- editor/src/editor/mvc/ed_update.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/editor/src/editor/mvc/ed_update.rs b/editor/src/editor/mvc/ed_update.rs index f4c8f9509c..fb20cacca6 100644 --- a/editor/src/editor/mvc/ed_update.rs +++ b/editor/src/editor/mvc/ed_update.rs @@ -569,16 +569,29 @@ impl<'a> EdModel<'a> { self.dirty = true; } F12 => { - let (_stream, stream_handle) = OutputStream::try_default().unwrap(); + /*let (_stream, stream_handle) = OutputStream::try_default().unwrap(); // Load a sound from a file, using a path relative to Cargo.toml let file = BufReader::new(File::open("./editor/src/editor/resources/sounds/bell_sound.mp3").unwrap()); // Decode that sound file into a source let source = Decoder::new(file).unwrap(); // Play the sound directly on the device stream_handle.play_raw(source.convert_samples()).unwrap(); - dbg!("played sound"); - std::thread::sleep(std::time::Duration::from_secs(1)); - + dbg!("played sound");*/ + // TODO use thread pool for efficiency + std::thread::spawn(|| { + let (_stream, stream_handle) = OutputStream::try_default().unwrap(); + let sink = Sink::try_new(&stream_handle).unwrap(); + + // Add a dummy source of the sake of the example. + let file = BufReader::new(File::open("./editor/src/editor/resources/sounds/bell_sound.mp3").unwrap()); + // Decode that sound file into a source + let source = Decoder::new(file).unwrap(); + sink.append(source); + + // The sound plays in a separate thread. This call will block the current thread until the sink + // has finished playing all its queued sounds. + sink.sleep_until_end(); + }); } _ => (), } From 082890a8cbc8e51aa1e63f94b61d0dadf8d3ded4 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Wed, 29 Sep 2021 13:48:53 +0200 Subject: [PATCH 3/5] sound on F12 with threadpool --- Cargo.lock | 10 +++++++ editor/Cargo.toml | 1 + editor/src/editor/keyboard_input.rs | 3 -- editor/src/editor/main.rs | 5 ++-- editor/src/editor/mod.rs | 1 + editor/src/editor/mvc/app_model.rs | 29 ++++--------------- editor/src/editor/mvc/app_update.rs | 6 +--- editor/src/editor/mvc/ed_update.rs | 45 ++++++----------------------- editor/src/editor/sound.rs | 42 +++++++++++++++++++++++++++ 9 files changed, 71 insertions(+), 71 deletions(-) create mode 100644 editor/src/editor/sound.rs diff --git a/Cargo.lock b/Cargo.lock index fe4df731ed..f0ad233d5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3942,6 +3942,7 @@ dependencies = [ "serde", "snafu", "tempfile", + "threadpool", "uuid", "ven_graph", "wgpu", @@ -4947,6 +4948,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "threadpool" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +dependencies = [ + "num_cpus", +] + [[package]] name = "thunderdome" version = "0.4.1" diff --git a/editor/Cargo.toml b/editor/Cargo.toml index c7d140ba3d..15078a6894 100644 --- a/editor/Cargo.toml +++ b/editor/Cargo.toml @@ -55,6 +55,7 @@ tempfile = "3.2.0" uuid = { version = "0.8", features = ["v4"] } fs_extra = "1.2.0" rodio = "0.14.0" +threadpool = "1.8.1" [dependencies.bytemuck] version = "1.4" diff --git a/editor/src/editor/keyboard_input.rs b/editor/src/editor/keyboard_input.rs index d781cc5c97..0d62f7d2e8 100644 --- a/editor/src/editor/keyboard_input.rs +++ b/editor/src/editor/keyboard_input.rs @@ -1,5 +1,3 @@ -use std::fs::File; -use std::io::BufReader; use crate::editor::ed_error::EdResult; use crate::editor::mvc::app_model::AppModel; @@ -7,7 +5,6 @@ use crate::editor::mvc::app_update::{ handle_copy, handle_cut, handle_paste, pass_keydown_to_focused, }; use crate::window::keyboard_input::from_winit; -use rodio::{Decoder, OutputStream, Source}; use winit::event::VirtualKeyCode::*; use winit::event::{ElementState, ModifiersState, VirtualKeyCode}; diff --git a/editor/src/editor/main.rs b/editor/src/editor/main.rs index f1be6b8ec5..8d6fef4fa7 100644 --- a/editor/src/editor/main.rs +++ b/editor/src/editor/main.rs @@ -31,10 +31,9 @@ use roc_load; use roc_load::file::LoadedModule; use roc_module::symbol::IdentIds; use roc_types::subs::VarStore; -use rodio::{Decoder, OutputStream, Source}; use std::collections::HashSet; use std::fs::{self, File}; -use std::io::{BufReader, Write}; +use std::io::{Write}; use std::{error::Error, io, path::Path}; use wgpu::{CommandEncoder, LoadOp, RenderPass, TextureView}; use wgpu_glyph::GlyphBrush; @@ -183,7 +182,7 @@ fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box = None; - let mut app_model = AppModel::init(ed_model_opt, None); + let mut app_model = AppModel::init(ed_model_opt); let mut keyboard_modifiers = ModifiersState::empty(); diff --git a/editor/src/editor/mod.rs b/editor/src/editor/mod.rs index 89efb3a318..a428731aed 100644 --- a/editor/src/editor/mod.rs +++ b/editor/src/editor/mod.rs @@ -14,3 +14,4 @@ mod style; mod syntax_highlight; mod theme; mod util; +mod sound; diff --git a/editor/src/editor/mvc/app_model.rs b/editor/src/editor/mvc/app_model.rs index 1029602db9..7220d10aaf 100644 --- a/editor/src/editor/mvc/app_model.rs +++ b/editor/src/editor/mvc/app_model.rs @@ -7,31 +7,22 @@ use crate::editor::ed_error::{ EdResult, }; use copypasta::{ClipboardContext, ClipboardProvider}; -use rodio::{Decoder, OutputStream, OutputStreamHandle}; -use std::{fmt, fs::File, io::BufReader}; +use threadpool::ThreadPool; +use std::{fmt}; pub struct AppModel<'a> { pub ed_model_opt: Option>, pub clipboard_opt: Option, - pub sound_output_stream_opt: Option, - pub sound_file_name_opt: Option<&'static str> + pub sound_thread_pool: ThreadPool, // thread is blocked while sound is played, hence the threadpool } impl<'a> AppModel<'a> { - pub fn init(ed_model_opt: Option>, sound_output_stream_opt: Option) -> AppModel { - - /*let (sound_output_stream_opt, sound_file_name_opt) = - if let Some((sound_output_stream, sound_file_name)) = setup_sound() { - (Some(sound_output_stream), Some(sound_file_name)) - } else { - (None, None) - };*/ + pub fn init(ed_model_opt: Option>) -> AppModel { AppModel { ed_model_opt, clipboard_opt: AppModel::init_clipboard_opt(), - sound_output_stream_opt, - sound_file_name_opt: None + sound_thread_pool: ThreadPool::new(7), // can play up to 7 sounds simultaneously } } @@ -110,16 +101,6 @@ pub fn get_clipboard_txt(clipboard_opt: &mut Option) -> EdResult Option<(OutputStreamHandle, &'static str)> { - // TODO no unwrap - //let (_, stream_handle) = OutputStream::try_default().unwrap(); - // Load a sound from a file, using a path relative to Cargo.toml - let file_path_str = "./editor/src/editor/resources/sounds/bell_sound.mp3"; - - //Some((stream_handle, file_path_str)) - None -} - impl fmt::Debug for Clipboard { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // showing the clipboard would require a mut ref which is not possible diff --git a/editor/src/editor/mvc/app_update.rs b/editor/src/editor/mvc/app_update.rs index 8036a3583d..c56bd0bcdd 100644 --- a/editor/src/editor/mvc/app_update.rs +++ b/editor/src/editor/mvc/app_update.rs @@ -1,11 +1,8 @@ -use std::fs::File; -use std::io::BufReader; use super::app_model::AppModel; use super::ed_update; use crate::window::keyboard_input::Modifiers; use crate::{editor::ed_error::EdResult, window::keyboard_input::from_winit}; -use rodio::{Decoder, OutputStream, Source}; use winit::event::{ModifiersState, VirtualKeyCode}; pub fn handle_copy(app_model: &mut AppModel) -> EdResult<()> { @@ -49,8 +46,7 @@ pub fn pass_keydown_to_focused( ed_model.ed_handle_key_down( modifiers, virtual_keycode, - app_model.sound_output_stream_opt.as_ref(), - app_model.sound_file_name_opt, + &mut app_model.sound_thread_pool, )?; } } diff --git a/editor/src/editor/mvc/ed_update.rs b/editor/src/editor/mvc/ed_update.rs index fb20cacca6..12ebdda6a8 100644 --- a/editor/src/editor/mvc/ed_update.rs +++ b/editor/src/editor/mvc/ed_update.rs @@ -1,10 +1,7 @@ #![allow(dead_code)] -use std::fs::File; -use std::io::BufReader; use std::process::Command; use std::process::Stdio; -use std::time::Duration; use crate::editor::code_lines::CodeLines; use crate::editor::ed_error::from_ui_res; @@ -32,6 +29,7 @@ use crate::editor::mvc::string_update::update_string; use crate::editor::mvc::tld_value_update::{start_new_tld_value, update_tld_val_name}; use crate::editor::slow_pool::MarkNodeId; use crate::editor::slow_pool::SlowPool; +use crate::editor::sound::play_sound; use crate::editor::syntax_highlight::HighlightStyle; use crate::lang::ast::Def2; use crate::lang::ast::DefId; @@ -62,13 +60,8 @@ use roc_region::all::Region; use roc_types::solved_types::Solved; use roc_types::subs::{Subs, Variable}; use roc_types::{pretty_print::content_to_string, subs::VarStore}; -use rodio::Decoder; -use rodio::OutputStream; -use rodio::OutputStreamHandle; -use rodio::Sink; -use rodio::Source; -use rodio::source::SineWave; use snafu::OptionExt; +use threadpool::ThreadPool; use winit::event::VirtualKeyCode; use VirtualKeyCode::*; @@ -529,8 +522,7 @@ impl<'a> EdModel<'a> { &mut self, modifiers: &Modifiers, virtual_keycode: VirtualKeyCode, - sound_output_stream_opt: Option<&OutputStreamHandle>, - sound_file_name_opt: Option<&str>, + sound_thread_pool: &mut ThreadPool, ) -> EdResult<()> { match virtual_keycode { @@ -569,28 +561,8 @@ impl<'a> EdModel<'a> { self.dirty = true; } F12 => { - /*let (_stream, stream_handle) = OutputStream::try_default().unwrap(); - // Load a sound from a file, using a path relative to Cargo.toml - let file = BufReader::new(File::open("./editor/src/editor/resources/sounds/bell_sound.mp3").unwrap()); - // Decode that sound file into a source - let source = Decoder::new(file).unwrap(); - // Play the sound directly on the device - stream_handle.play_raw(source.convert_samples()).unwrap(); - dbg!("played sound");*/ - // TODO use thread pool for efficiency - std::thread::spawn(|| { - let (_stream, stream_handle) = OutputStream::try_default().unwrap(); - let sink = Sink::try_new(&stream_handle).unwrap(); - - // Add a dummy source of the sake of the example. - let file = BufReader::new(File::open("./editor/src/editor/resources/sounds/bell_sound.mp3").unwrap()); - // Decode that sound file into a source - let source = Decoder::new(file).unwrap(); - sink.append(source); - - // The sound plays in a separate thread. This call will block the current thread until the sink - // has finished playing all its queued sounds. - sink.sleep_until_end(); + sound_thread_pool.execute(move || { + play_sound("./editor/src/editor/resources/sounds/bell_sound.mp3"); }); } _ => (), @@ -1274,6 +1246,7 @@ pub mod test_ed_update { use crate::window::keyboard_input::Modifiers; use bumpalo::Bump; use roc_module::symbol::ModuleIds; + use threadpool::ThreadPool; use winit::event::VirtualKeyCode::*; fn ed_res_to_res(ed_res: EdResult) -> Result { @@ -2683,7 +2656,7 @@ pub mod test_ed_update { } for _ in 0..repeats { - ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, None, None)?; + ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, &mut ThreadPool::new(1))?; } let mut post_lines = ui_res_to_res(ed_model_to_dsl(&ed_model))?; @@ -3260,7 +3233,7 @@ pub mod test_ed_update { )?; for _ in 0..repeats { - ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, None, None)?; + ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, &mut ThreadPool::new(1))?; } move_caret_fun(&mut ed_model, &no_mods())?; @@ -3424,7 +3397,7 @@ pub mod test_ed_update { )?; for _ in 0..repeats { - ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, None, None)?; + ed_model.ed_handle_key_down(&ctrl_cmd_shift(), Up, &mut ThreadPool::new(1))?; } handle_new_char(&'\u{8}', &mut ed_model)?; // \u{8} is the char for backspace on linux diff --git a/editor/src/editor/sound.rs b/editor/src/editor/sound.rs new file mode 100644 index 0000000000..96b5d91ab5 --- /dev/null +++ b/editor/src/editor/sound.rs @@ -0,0 +1,42 @@ + +use std::fs::File; +use std::io::BufReader; +use rodio::{Decoder, OutputStream, Sink}; + +pub(crate) fn play_sound(sound_path_str: &str) { + let out_stream_res = OutputStream::try_default(); + + match out_stream_res { + Ok((_, out_stream_handle)) => { + match Sink::try_new(&out_stream_handle) { + Ok(sink) => { + match File::open(sound_path_str){ + Ok(file) => { + let reader = BufReader::new(file); + + match Decoder::new(reader) { + Ok(decoder) => { + + sink.append(decoder); + sink.sleep_until_end(); + }, + Err(e) => { + println!("Failed to create Decoder from BufReader from sound file at {}. Error message: {:?}", sound_path_str, e); + } + } + }, + Err(e) => { + println!("Failed to open sound file at {}. Error message: {}", sound_path_str, e); + } + } + }, + Err(e) => { + println!("Failed to create Sink to play sound. Error message: {:?}", e); + } + } + }, + Err(e) => { + println!("Failed to create OutputStream to play sound. Error message: {:?}", e); + } + } +} \ No newline at end of file From 9c2b334be734736d41efcbdb8c83a234e3c14fc9 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Wed, 29 Sep 2021 15:02:23 +0200 Subject: [PATCH 4/5] fmt, added libasound2-dev to Earthfile --- Earthfile | 1 + editor/src/editor/keyboard_input.rs | 2 -- editor/src/editor/main.rs | 4 +-- editor/src/editor/mod.rs | 2 +- editor/src/editor/mvc/app_model.rs | 3 +- editor/src/editor/mvc/app_update.rs | 2 -- editor/src/editor/mvc/ed_update.rs | 1 - editor/src/editor/sound.rs | 53 +++++++++++++++-------------- 8 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Earthfile b/Earthfile index f2a31d2fc7..032d17bdf0 100644 --- a/Earthfile +++ b/Earthfile @@ -8,6 +8,7 @@ install-other-libs: FROM +prep-debian RUN apt -y install wget git RUN apt -y install libxcb-shape0-dev libxcb-xfixes0-dev # for editor clipboard + RUN apt -y install libasound2-dev # for editor sounds RUN apt -y install libunwind-dev pkg-config libx11-dev zlib1g-dev install-zig-llvm-valgrind-clippy-rustfmt: diff --git a/editor/src/editor/keyboard_input.rs b/editor/src/editor/keyboard_input.rs index 0d62f7d2e8..f1dad9a2af 100644 --- a/editor/src/editor/keyboard_input.rs +++ b/editor/src/editor/keyboard_input.rs @@ -1,4 +1,3 @@ - use crate::editor::ed_error::EdResult; use crate::editor::mvc::app_model::AppModel; use crate::editor::mvc::app_update::{ @@ -14,7 +13,6 @@ pub fn handle_keydown( modifiers_winit: ModifiersState, app_model: &mut AppModel, ) -> EdResult<()> { - if let ElementState::Released = elem_state { return Ok(()); } diff --git a/editor/src/editor/main.rs b/editor/src/editor/main.rs index 8d6fef4fa7..eeec775167 100644 --- a/editor/src/editor/main.rs +++ b/editor/src/editor/main.rs @@ -33,7 +33,7 @@ use roc_module::symbol::IdentIds; use roc_types::subs::VarStore; use std::collections::HashSet; use std::fs::{self, File}; -use std::io::{Write}; +use std::io::Write; use std::{error::Error, io, path::Path}; use wgpu::{CommandEncoder, LoadOp, RenderPass, TextureView}; use wgpu_glyph::GlyphBrush; @@ -63,7 +63,7 @@ fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box { pub ed_model_opt: Option>, @@ -18,7 +18,6 @@ pub struct AppModel<'a> { impl<'a> AppModel<'a> { pub fn init(ed_model_opt: Option>) -> AppModel { - AppModel { ed_model_opt, clipboard_opt: AppModel::init_clipboard_opt(), diff --git a/editor/src/editor/mvc/app_update.rs b/editor/src/editor/mvc/app_update.rs index c56bd0bcdd..57430f9390 100644 --- a/editor/src/editor/mvc/app_update.rs +++ b/editor/src/editor/mvc/app_update.rs @@ -1,4 +1,3 @@ - use super::app_model::AppModel; use super::ed_update; use crate::window::keyboard_input::Modifiers; @@ -40,7 +39,6 @@ pub fn pass_keydown_to_focused( virtual_keycode: VirtualKeyCode, app_model: &mut AppModel, ) -> EdResult<()> { - if let Some(ref mut ed_model) = app_model.ed_model_opt { if ed_model.has_focus { ed_model.ed_handle_key_down( diff --git a/editor/src/editor/mvc/ed_update.rs b/editor/src/editor/mvc/ed_update.rs index 12ebdda6a8..fb2267a5c3 100644 --- a/editor/src/editor/mvc/ed_update.rs +++ b/editor/src/editor/mvc/ed_update.rs @@ -524,7 +524,6 @@ impl<'a> EdModel<'a> { virtual_keycode: VirtualKeyCode, sound_thread_pool: &mut ThreadPool, ) -> EdResult<()> { - match virtual_keycode { Left => self.move_caret_left(modifiers)?, Up => { diff --git a/editor/src/editor/sound.rs b/editor/src/editor/sound.rs index 96b5d91ab5..76a9994de6 100644 --- a/editor/src/editor/sound.rs +++ b/editor/src/editor/sound.rs @@ -1,42 +1,45 @@ - +use rodio::{Decoder, OutputStream, Sink}; use std::fs::File; use std::io::BufReader; -use rodio::{Decoder, OutputStream, Sink}; pub(crate) fn play_sound(sound_path_str: &str) { let out_stream_res = OutputStream::try_default(); match out_stream_res { - Ok((_, out_stream_handle)) => { - match Sink::try_new(&out_stream_handle) { - Ok(sink) => { - match File::open(sound_path_str){ - Ok(file) => { - let reader = BufReader::new(file); + Ok((_, out_stream_handle)) => match Sink::try_new(&out_stream_handle) { + Ok(sink) => match File::open(sound_path_str) { + Ok(file) => { + let reader = BufReader::new(file); - match Decoder::new(reader) { - Ok(decoder) => { - - sink.append(decoder); - sink.sleep_until_end(); - }, - Err(e) => { - println!("Failed to create Decoder from BufReader from sound file at {}. Error message: {:?}", sound_path_str, e); - } - } - }, + match Decoder::new(reader) { + Ok(decoder) => { + sink.append(decoder); + sink.sleep_until_end(); + } Err(e) => { - println!("Failed to open sound file at {}. Error message: {}", sound_path_str, e); + println!("Failed to create Decoder from BufReader from sound file at {}. Error message: {:?}", sound_path_str, e); } } - }, - Err(e) => { - println!("Failed to create Sink to play sound. Error message: {:?}", e); } + Err(e) => { + println!( + "Failed to open sound file at {}. Error message: {}", + sound_path_str, e + ); + } + }, + Err(e) => { + println!( + "Failed to create Sink to play sound. Error message: {:?}", + e + ); } }, Err(e) => { - println!("Failed to create OutputStream to play sound. Error message: {:?}", e); + println!( + "Failed to create OutputStream to play sound. Error message: {:?}", + e + ); } } -} \ No newline at end of file +} From 68512d5712a8adb0c40171c4bec0656b86c10ab5 Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Wed, 6 Oct 2021 16:52:08 +0200 Subject: [PATCH 5/5] removed old comment --- editor/src/editor/main.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/editor/src/editor/main.rs b/editor/src/editor/main.rs index 02afb87bfe..a3a091508a 100644 --- a/editor/src/editor/main.rs +++ b/editor/src/editor/main.rs @@ -62,15 +62,6 @@ pub fn launch(project_dir_path_opt: Option<&Path>) -> io::Result<()> { fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box> { env_logger::init(); - //let (_, stream_handle) = OutputStream::try_default().unwrap(); - /* - // Load a sound from a file, using a path relative to Cargo.toml - let file = BufReader::new(File::open("./editor/src/editor/resources/sounds/bell_sound.mp3").unwrap()); - // Decode that sound file into a source - let source = Decoder::new(file).unwrap(); - // Play the sound directly on the device - stream_handle.play_raw(source.convert_samples()).unwrap(); - */ // Open window and create a surface let mut event_loop = winit::event_loop::EventLoop::new();