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
use std::path::PathBuf;
use std::fs::File;
use std::io::BufWriter;
use crate::Channels;
pub fn visualize(samples: &[i16], channels: Channels, directory: &str, filename: &str) {
let image_width = 5000;
let image_height = 1000;
if channels.is_stereo() {
assert_eq!(0, samples.len() % 2, "If stereo is provided, the length of the audio data must be even!");
let (left, right) = channels.stereo_interleavement().to_channel_data(samples);
visualize(&left, Channels::Mono, directory, &format!("left_{}", filename));
visualize(&right, Channels::Mono, directory, &format!("right_{}", filename));
return;
}
let width_per_sample = image_width as f64 / samples.len() as f64;
let height_per_max_amplitude = image_height as f64 / 2_f64 / i16::max_value() as f64;
let mut image = vec![vec![(255,255,255); image_width]; image_height];
for (sample_index, sample_value) in samples.into_iter().enumerate() {
let x = (sample_index as f64 * width_per_sample) as usize;
let mut y = ((image_height/2) as f64 + *sample_value as f64 * height_per_max_amplitude) as usize;
if y == image_height {
y = y - 1;
}
image[y][x] = (0,0,0);
}
let mut path = PathBuf::new();
path.push(directory);
path.push(filename);
let file = File::create(path).unwrap();
let ref mut w = BufWriter::new(file);
let mut encoder = png::Encoder::new(w, image_width as u32, image_height as u32);
encoder.set_color(png::ColorType::RGB);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().unwrap();
let rgb_data = image.into_iter()
.flat_map(|row| row.into_iter())
.flat_map(|(r, g, b)| vec![r, g, b].into_iter())
.map(|v| v as u8)
.collect::<Vec<u8>>();
writer.write_image_data(&rgb_data).unwrap();
}
#[cfg(test)]
mod tests {
use super::*;
use minimp3::{Decoder as Mp3Decoder, Frame as Mp3Frame, Error as Mp3Error};
use crate::test::{TEST_SAMPLES_DIR, TEST_OUT_DIR};
use crate::ChannelInterleavement;
#[test]
fn test_no_out_of_bounds_panic() {
let audio_data = vec![i16::max_value(), i16::min_value()];
visualize(
&audio_data,
Channels::Mono,
TEST_OUT_DIR,
"sample_1_waveform-test-out-of-bounds-check.png"
);
}
#[test]
fn test_visualize_png_output() {
let mut path = PathBuf::new();
path.push(TEST_SAMPLES_DIR);
path.push("sample_1.mp3");
let mut decoder = Mp3Decoder::new(File::open(path).unwrap());
let mut lrlr_mp3_samples = vec![];
loop {
match decoder.next_frame() {
Ok(Mp3Frame { data: samples_of_frame, .. }) => {
for sample in samples_of_frame {
lrlr_mp3_samples.push(sample);
}
}
Err(Mp3Error::Eof) => break,
Err(e) => panic!("{:?}", e),
}
}
visualize(
&lrlr_mp3_samples,
Channels::Stereo(ChannelInterleavement::LRLR),
TEST_OUT_DIR,
"sample_1_waveform.png"
);
}
}