Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demos/home-automation/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ name = "home_automation_lib"
[dependencies]
slint = { path = "../../../api/rs/slint" }
chrono = "0.4"
kira = "0.10"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce build times, I suggest to include only the audio format needed.

Suggested change
kira = "0.10"
kira = { version = "0.10", default-features = false, features = ["wav"] }


[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
Expand Down
21 changes: 21 additions & 0 deletions demos/home-automation/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
// SPDX-License-Identifier: MIT

use chrono::{Datelike, Local, Timelike};
use kira::{
AudioManager, AudioManagerSettings, DefaultBackend,
sound::static_sound::StaticSoundData,
};
use std::io::Cursor;
use slint::{Timer, TimerMode};

#[cfg(target_arch = "wasm32")]
Expand All @@ -11,6 +16,10 @@ slint::slint! {
export { Api, AppWindow } from "../ui/demo.slint";
}

// https://sourceforge.net/projects/sox/
// $ sox -n dial-tick.wav synth 0.01 sine 1000 fade t 0 0.01 0.005 gain -1
const DIAL_TICK : &[u8] = include_bytes!("../ui/sounds/dial-tick.wav");

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn main() {
// This provides better error messages in debug mode.
Expand All @@ -21,6 +30,18 @@ pub fn main() {
let app = AppWindow::new().expect("AppWindow::new() failed");
let app_weak = app.as_weak();

let mut manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default()).unwrap();

let api = app.global::<Api>();
api.on_play_sound(move |sound| {
let sound = match sound {
SoundEffect::DialTick => DIAL_TICK,
};
let cursor = Cursor::new(sound);
let sound_data = StaticSoundData::from_cursor(cursor).unwrap();
let _sound_handle = manager.play(sound_data).unwrap();
});

let timer = Timer::default();
timer.start(TimerMode::Repeated, std::time::Duration::from_millis(1000), move || {
if let Some(app) = app_weak.upgrade() {
Expand Down
6 changes: 6 additions & 0 deletions demos/home-automation/ui/api.slint
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export struct WeatherData {
temperature: float,
}

export enum SoundEffect {
dial-tick,
}

export global Api {
in property <float> indoor-temperature: 22.0;
in property <float> outdoor-temperature: 24.0;
Expand Down Expand Up @@ -95,4 +99,6 @@ export global Api {
temperature: 23.0,
},
];

callback play-sound(SoundEffect);
}
4 changes: 4 additions & 0 deletions demos/home-automation/ui/components/dial/dial.slint
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: MIT
import { Palette } from "../../common.slint";
import { Api, SoundEffect } from "../../api.slint";

export global DialState {
out property <int> totalLights: 60;
Expand All @@ -19,6 +20,9 @@ export component Dial {
property <bool> moving: ta.firstTouch;
in-out property <angle> dialAngle: DialState.startAngle;
out property <int> volume: ((dialAngle - DialState.startAngle) / DialState.degreesFilledWithLights) * DialState.totalLights;
changed volume => {
Api.play-sound(SoundEffect.dial-tick);
}

width: 212px;
height: 213px;
Expand Down
Binary file added demos/home-automation/ui/sounds/dial-tick.wav
Binary file not shown.
Loading