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;
#[derive(Copy, Clone)]
pub enum Color {
Scalar(u8),
Vector3(u8, u8, u8),
Vector4(u8, u8, u8, u8),
}
#[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));
}
},
}
}
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
}
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),
}
}
}
#[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),
}
}
}
pub trait Sample: PartialOrd + Into<f64> + Copy + Zero {}
impl<T> Sample for T
where
T: PartialOrd + Into<f64> + Copy + Zero,
{
}
pub struct SampleSequence<'a, T: Sample + 'a> {
pub data: &'a [T],
pub sample_rate: f64,
}
#[derive(Copy, Clone)]
pub struct MinMaxPair<T: Sample> {
pub min: T,
pub max: T,
}
pub struct MinMaxPairSequence<T: Sample> {
pub data: Vec<MinMaxPair<T>>,
}