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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use evdev::{Device, EventType, InputEvent, SoundType};
use std::thread::sleep;
use std::time::Duration;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub const DEFAULT_DELAY: Duration = Duration::from_millis(100);
pub const DEFAULT_FREQ: u16 = 440;
pub const DEFAULT_LEN: Duration = Duration::from_millis(200);
pub const DEFAULT_REPEATS: u16 = 1;
const FILE: &str = "/dev/input/by-path/platform-pcspkr-event-spkr";

/// A sequence of notes
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Melody(Vec<Note>);

impl Melody {
    #[must_use]
    pub fn new(notes: Vec<Note>) -> Self {
        Self(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 {
            note.play()?;
        }

        Ok(())
    }
}

impl Default for Melody {
    fn default() -> Self {
        Self::new(vec![Note::default()])
    }
}

impl From<Vec<Note>> for Melody {
    fn from(notes: Vec<Note>) -> Self {
        Self::new(notes)
    }
}

impl From<&[Note]> for Melody {
    fn from(notes: &[Note]) -> Self {
        Self::new(Vec::from(notes))
    }
}

/// A note of a certain frequency and duration
/// that may be repeated with a delay
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Note {
    frequency: u16,
    length: Duration,
    repeats: u16,
    delay: Duration,
}

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: Duration, repeats: u16, delay: Duration) -> Self {
        Self {
            frequency,
            length,
            repeats,
            delay,
        }
    }

    /// Plays the note
    ///
    /// # Examples
    /// ```
    /// use beep_evdev::Note;
    ///
    /// Note::default().play().expect("could not play melody :-(");
    /// ```
    ///
    /// # Errors
    /// Returns an [`std::io::Error`] on I/O errors
    pub fn play(&self) -> Result<(), std::io::Error> {
        if self.repeats > 0 {
            beep(self.frequency)?;
            sleep(self.length);
            beep(0)?;
        }

        for _ in 1..self.repeats {
            sleep(self.delay);
            beep(self.frequency)?;
            sleep(self.length);
            beep(0)?;
        }

        Ok(())
    }
}

impl Default for Note {
    fn default() -> Self {
        Self::new(DEFAULT_FREQ, DEFAULT_LEN, DEFAULT_REPEATS, DEFAULT_DELAY)
    }
}

impl From<(u16, u64)> for Note {
    /// Creates a note from a `(frequency, length)` tuple with default repeats and delay
    fn from((frequency, length): (u16, u64)) -> Self {
        Self::from((frequency, Duration::from_millis(length)))
    }
}

impl From<(u16, Duration)> for Note {
    /// Creates a note from a `(frequency, length)` tuple with default repeats and delay
    fn from((frequency, length): (u16, Duration)) -> 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),
    )])
}