1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
use evdev::{Device, EventType, InputEvent, SoundType};
use std::thread::sleep;
use std::time::Duration;
pub const DEFAULT_DELAY: u64 = 100;
pub const DEFAULT_FREQ: u16 = 440;
pub const DEFAULT_LEN: u64 = 200;
pub const DEFAULT_REPEATS: u64 = 1;
const FILE: &str = "/dev/input/by-path/platform-pcspkr-event-spkr";
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Melody(Vec<Note>);
/// A sequence of notes
impl Melody {
#[must_use]
pub fn new(notes: &[Note]) -> Self {
Self(Vec::from(notes))
}
/// Plays the melody
///
/// # Examples
/// ```
/// use beep_evdev::Melody;
///
/// let melody: Melody = vec![
/// (659, 120).into(),
/// (622, 120).into(),
/// (659, 120).into(),
/// (622, 120).into(),
/// (659, 120).into(),
/// (94, 120).into(),
/// (587, 120).into(),
/// (523, 120).into(),
/// (440, 120).into(),
/// (262, 120).into(),
/// (330, 120).into(),
/// (440, 120).into(),
/// (494, 120).into(),
/// (330, 120).into(),
/// (415, 120).into(),
/// (494, 120).into(),
/// (523, 120).into(),
/// (330, 120).into(),
/// (659, 120).into(),
/// (622, 120).into(),
/// (659, 120).into(),
/// (622, 120).into(),
/// (659, 120).into(),
/// (494, 120).into(),
/// (587, 120).into(),
/// (523, 120).into(),
/// (440, 120).into(),
/// (262, 120).into(),
/// (330, 120).into(),
/// (440, 120).into(),
/// (494, 120).into(),
/// (330, 120).into(),
/// (523, 120).into(),
/// (494, 120).into(),
/// (440, 120).into(),
/// ]
/// .into();
/// melody.play().expect("could not play melody :-(");
/// ```
///
/// # Errors
/// Returns an [`std::io::Error`] on I/O errors
pub fn play(&self) -> Result<(), std::io::Error> {
for note in &self.0 {
if note.repeats > 0 {
beep(note.frequency)?;
sleep(Duration::from_millis(note.length));
beep(0)?;
}
for _ in 1..note.repeats {
sleep(Duration::from_millis(note.delay));
beep(note.frequency)?;
sleep(Duration::from_millis(note.length));
beep(0)?;
}
}
Ok(())
}
}
impl From<Vec<Note>> for Melody {
fn from(notes: Vec<Note>) -> Self {
Self(notes)
}
}
impl From<&[Note]> for Melody {
fn from(notes: &[Note]) -> Self {
Self::new(notes)
}
}
/// A note of a certain frequency and duration
/// May be repeated with a delay
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Note {
frequency: u16,
length: u64,
repeats: u64,
delay: u64,
}
impl Note {
/// Creates a new note
/// # Attributes
/// * frequency - The frequency in Hertz
/// * length - The length in milliseconds
/// * repeats - The amount of repeats
/// * delay - The delay in seconds when repeating
#[must_use]
pub const fn new(frequency: u16, length: u64, repeats: u64, delay: u64) -> Self {
Self {
frequency,
length,
repeats,
delay,
}
}
}
impl Default for Note {
fn default() -> Self {
Self::new(DEFAULT_FREQ, DEFAULT_LEN, DEFAULT_REPEATS, DEFAULT_DELAY)
}
}
impl From<(u16, u64)> for Note {
fn from((frequency, length): (u16, u64)) -> Self {
Self::new(frequency, length, DEFAULT_REPEATS, DEFAULT_DELAY)
}
}
/// Beeps the PC speaker at the given frequency
///
/// # Examples
/// ```
/// use beep_evdev::beep;
/// use std::{thread, time};
///
/// beep(440).expect("could not beep");
/// thread::sleep(time::Duration::from_millis(500));
/// beep(880).expect("could not beep");
/// thread::sleep(time::Duration::from_millis(500));
/// beep(0).expect("could not beep");
/// ```
///
/// # Errors
/// Returns [`std::io::Error`] on I/O errors
pub fn beep(hertz: u16) -> std::io::Result<()> {
Device::open(FILE)?.send_events(&[InputEvent::new(
EventType::SOUND,
SoundType::SND_TONE.0,
i32::from(hertz),
)])
}