beep_evdev/
note.rs

1use std::time::Duration;
2
3use crate::{DEFAULT_DELAY, DEFAULT_FREQ, DEFAULT_LEN, DEFAULT_REPEATS};
4
5/// A note of a certain frequency and duration
6/// that may be repeated with a delay.
7#[derive(Clone, Debug, Eq, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub struct Note {
10    frequency: u16,
11    length: Duration,
12    repeats: u16,
13    delay: Duration,
14}
15
16impl Note {
17    /// Create a new note.
18    #[must_use]
19    pub const fn new(frequency: u16, length: Duration, repeats: u16, delay: Duration) -> Self {
20        Self {
21            frequency,
22            length,
23            repeats,
24            delay,
25        }
26    }
27
28    /// Returns the frequency in Hertz.
29    #[must_use]
30    pub const fn frequency(&self) -> u16 {
31        self.frequency
32    }
33
34    /// Returns the length.
35    #[must_use]
36    pub const fn length(&self) -> Duration {
37        self.length
38    }
39
40    /// Returns the amount of repeats.
41    #[must_use]
42    pub const fn repeats(&self) -> u16 {
43        self.repeats
44    }
45
46    /// Returns the delay.
47    #[must_use]
48    pub const fn delay(&self) -> Duration {
49        self.delay
50    }
51}
52
53impl Default for Note {
54    fn default() -> Self {
55        Self::new(DEFAULT_FREQ, DEFAULT_LEN, DEFAULT_REPEATS, DEFAULT_DELAY)
56    }
57}
58
59impl From<(u16, u64)> for Note {
60    /// Creates a note from a `(frequency, length)` tuple with default repeats and delay.
61    fn from((frequency, length): (u16, u64)) -> Self {
62        Self::from((frequency, Duration::from_millis(length)))
63    }
64}
65
66impl From<(u16, Duration)> for Note {
67    /// Creates a note from a `(frequency, length)` tuple with default repeats and delay.
68    fn from((frequency, length): (u16, Duration)) -> Self {
69        Self::new(frequency, length, DEFAULT_REPEATS, DEFAULT_DELAY)
70    }
71}