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
use zero::Zero;
use error::InconsistentFormatError;
use std::error::Error;

/// Color specifiers.
#[derive(Copy, Clone)]
pub enum Color {
    /// A format with only one value per pixel, or gray scale in other words.
    Scalar(u8),
    /// 3-dimensional format (e.g. RGB format).
    Vector3(u8, u8, u8),
    /// 4-dimensional format (e.g. RGBA format).
    Vector4(u8, u8, u8, u8),
}

/// Configurations for image generators.
///
/// It contains the following information:
///
///  * Range of the amplitudes to be rendered
///  * Foreground and background `Color`s to be used
#[derive(Copy, Clone)]
pub struct WaveformConfig {
    pub amp_min: f64,
    pub amp_max: f64,
    foreground: Color,
    background: Color,
}

impl WaveformConfig {
    fn check_color_consistency(c1: Color, c2: Color) -> Result<(), Box<InconsistentFormatError>> {
        let mut c1_is_scalar = false;
        let mut c2_is_scalar = false;
        match c1 {
            Color::Scalar(_) => {
                if let Color::Scalar(_) = c2 {
                    return Ok(());
                }else{
                    return Err(Box::new(InconsistentFormatError));
                }
            },
            Color::Vector3(..) => {
                if let Color::Vector3(..) = c2 {
                    return Ok(());
                }else{
                    return Err(Box::new(InconsistentFormatError));
                }
            },
            Color::Vector4(..) => {
                if let Color::Vector4(..) = c2 {
                    return Ok(());
                }else{
                    return Err(Box::new(InconsistentFormatError));
                }
            },
        }
    }

    /// The constructor.
    ///
    /// # Arguments
    /// * `amp_min` - Minimum value of amplitude to be rendered
    /// * `amp_max` - Maximum value of amplitude to be rendered
    /// * `foreground` - Foreground `Color` of the image, format must be consistent with background.
    /// * `background` - Background `Color` of the image, format must be consistent with foreground.
    pub fn new(amp_min: f64, amp_max: f64, foreground: Color, background: Color) -> Result<Self, Box<Error>> {
        match Self::check_color_consistency(background, foreground) {
            Err(e) => return Err(e),
            _ => (),
        }

        Ok(Self {
            amp_min,
            amp_max,
            background,
            foreground,
        })
    }

    pub fn get_background(&self) -> Color {
        self.background
    }
    pub fn get_foreground(&self) -> Color {
        self.foreground
    }

    /// Sets `Color`s.
    ///
    /// # Arguments
    /// * `foreground` - Foreground `Color` of the image, format must be consistent with background.
    /// * `background` - Background `Color` of the image, format must be consistent with foreground.
    pub fn set_colors(&mut self, background: Color, foreground: Color) -> Result<(), Box<Error>> {
        match Self::check_color_consistency(background, foreground) {
            Err(e) => return Err(e),
            _ => (),
        }

        self.background = background;
        self.foreground = foreground;

        Ok(())
    }
}

impl Default for WaveformConfig {
    fn default() -> Self {
        Self {
            amp_min: -1f64,
            amp_max: 1f64,
            foreground: Color::Scalar(255),
            background: Color::Scalar(0),
        }
    }
}

/// Time range specifiers used to determine which part of the wave to plot.
#[derive(Copy, Clone)]
pub enum TimeRange {
    Seconds(f64, f64),
    Samples(usize, usize),
}

impl TimeRange {
    pub fn to_sample_tuple(&self, sample_rate: f64) -> (usize, usize) {
        match self {
            &TimeRange::Seconds(b, e) => (
                (b * sample_rate) as usize,
                (e * sample_rate) as usize,
            ),
            &TimeRange::Samples(b, e) => (b, e),
        }
    }
}

/// A sample.
pub trait Sample: PartialOrd + Into<f64> + Copy + Zero {}
impl<T> Sample for T
where
    T: PartialOrd + Into<f64> + Copy + Zero,
{
}

/// A reference to a `slice` of `Sample`s
/// (which describe a wave) combined with its sample rate.
pub struct SampleSequence<'a, T: Sample + 'a> {
    pub data: &'a [T],
    pub sample_rate: f64,
}

/// A pair of a minimum and maximum amplitude values for internal use.
#[derive(Copy, Clone)]
pub struct MinMaxPair<T: Sample> {
    pub min: T,
    pub max: T,
}

pub struct MinMaxPairSequence<T: Sample> {
    pub data: Vec<MinMaxPair<T>>,
}