generated from hack-ink/rust-initializer
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
439 additions
and
18 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod audio; | ||
|
||
pub mod function; | ||
|
||
pub mod keyboard; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// std | ||
use std::{ | ||
fmt::{Debug, Formatter, Result as FmtResult}, | ||
io::Cursor, | ||
}; | ||
// crates.io | ||
use rodio::{ | ||
source::{Buffered, Source as _}, | ||
Decoder, OutputStream, OutputStreamHandle, Sink, | ||
}; | ||
// self | ||
use crate::prelude::*; | ||
|
||
type Source = Buffered<Decoder<Cursor<&'static [u8]>>>; | ||
|
||
pub struct Audio { | ||
pub notification: Source, | ||
sink: Sink, | ||
// Stream must be kept alive. | ||
_stream: (OutputStream, OutputStreamHandle), | ||
} | ||
impl Audio { | ||
pub fn new() -> Result<Self> { | ||
let sound_data = include_bytes!("../../asset/notification.mp3"); | ||
let cursor = Cursor::new(sound_data.as_ref()); | ||
let decoder = Decoder::new(cursor).map_err(RodioError::Decoder)?; | ||
let notification = decoder.buffered(); | ||
let _stream = OutputStream::try_default().map_err(RodioError::Stream)?; | ||
let sink = Sink::try_new(&_stream.1).map_err(RodioError::Play)?; | ||
|
||
Ok(Audio { notification, sink, _stream }) | ||
} | ||
|
||
pub fn play(&self, audio_src: Source) { | ||
self.sink.append(audio_src); | ||
self.sink.sleep_until_end(); | ||
} | ||
} | ||
impl Debug for Audio { | ||
fn fmt(&self, f: &mut Formatter) -> FmtResult { | ||
write!(f, "Audio(..)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// std | ||
use std::{ | ||
fmt::{Debug, Formatter, Result as FmtResult}, | ||
sync::mpsc::{self, Sender}, | ||
thread, | ||
}; | ||
// self | ||
use crate::{component::audio::Audio as A, prelude::*}; | ||
|
||
#[derive(Clone)] | ||
pub struct Audio(Sender<Effect>); | ||
impl Audio { | ||
pub fn new() -> Result<Self> { | ||
let (tx, rx) = mpsc::channel::<Effect>(); | ||
|
||
thread::spawn(move || { | ||
let audio = A::new().expect("audio must be created"); | ||
|
||
loop { | ||
match rx.recv().expect("receive must succeed") { | ||
Effect::Notification => audio.play(audio.notification.clone()), | ||
Effect::Abort => return, | ||
} | ||
} | ||
}); | ||
|
||
Ok(Self(tx)) | ||
} | ||
|
||
pub fn play_notification(&self) { | ||
self.0.send(Effect::Notification).expect("send must succeed"); | ||
} | ||
|
||
pub fn abort(&self) { | ||
self.0.send(Effect::Abort).expect("send must succeed"); | ||
} | ||
} | ||
impl Debug for Audio { | ||
fn fmt(&self, f: &mut Formatter) -> FmtResult { | ||
write!(f, "Audio(..)") | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Effect { | ||
Notification, | ||
Abort, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters