Skip to content

Commit

Permalink
Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tdgne committed Aug 18, 2017
1 parent 6a3f49d commit 35248bc
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 114 deletions.
22 changes: 11 additions & 11 deletions benches/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ fn gen_samples() -> Vec<f64> {
for t in 0..441000 {
// 10 seconds
samples.push(
((t as f64) / 100f64 * 2f64 * 3.1415f64).sin() *
((t as f64) / 10000f64 * 2f64 * 3.1415f64).sin(),
((t as f64) / 100f64 * 2f64 * 3.1415f64).sin() * ((t as f64) / 10000f64 * 2f64 * 3.1415f64).sin(),
);
}
samples
}

fn gen_config() -> WaveformConfig {
WaveformConfig::new(
-1f64, 1f64,
-1f64,
1f64,
Color::RGBA {
r: 0,
g: 0,
Expand All @@ -31,7 +31,7 @@ fn gen_config() -> WaveformConfig {
g: 0,
b: 0,
a: 0,
}
},
).unwrap()
}

Expand All @@ -46,13 +46,13 @@ fn bench_binned(b: &mut Bencher) {
let config = gen_config();

let wfr = BinnedWaveformRenderer::new(
&SampleSequence {
data: &samples[..],
sample_rate: 44100f64,
},
bin_size,
config.clone(),
).unwrap();
&SampleSequence {
data: &samples[..],
sample_rate: 44100f64,
},
bin_size,
config.clone(),
).unwrap();


b.iter(|| {
Expand Down
26 changes: 17 additions & 9 deletions examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ fn main() {
let ratio = 1.0f64; // The ratio of samples to render.
let width = 1000usize; // The width of the rendered image.
let bin_size = 10usize; // Bin size for BinnedWaveformRenderer
let multi_bin_sizes: Vec<usize> = vec![10,50,100,500,1000]; // Bin sizes for MultiWaveformRenderer
let multi_bin_sizes: Vec<usize> = vec![10, 50, 100, 500, 1000]; // Bin sizes for MultiWaveformRenderer

let mut samples: Vec<f64> = Vec::new();
for t in 0..441000 {
// 10 seconds
samples.push(
((t as f64) / 100f64 * 2f64 * 3.1415f64).sin() *
((t as f64) / 10000f64 * 2f64 * 3.1415f64).sin(),
((t as f64) / 100f64 * 2f64 * 3.1415f64).sin() * ((t as f64) / 10000f64 * 2f64 * 3.1415f64).sin(),
);
}

let config = WaveformConfig::new(
-1f64, 1f64,
-1f64,
1f64,
Color::RGBA {
r: 0,
g: 0,
Expand All @@ -31,7 +31,7 @@ fn main() {
g: 0,
b: 0,
a: 0,
}
},
).unwrap();

// Multilevel binned version
Expand All @@ -46,10 +46,14 @@ fn main() {
).unwrap();
let now = SystemTime::now();
for _ in 0..100 {
wfg.render_vec(TimeRange::Seconds(0f64, 10f64*ratio), (width, 100));
wfg.render_vec(TimeRange::Seconds(0f64, 10f64 * ratio), (width, 100));
}
if let Ok(elapsed) = now.elapsed() {
println!("Multi * 1000 times: {} secs + {} nsecs", elapsed.as_secs(), elapsed.subsec_nanos());
println!(
"Multi * 1000 times: {} secs + {} nsecs",
elapsed.as_secs(),
elapsed.subsec_nanos()
);
}
}

Expand All @@ -65,10 +69,14 @@ fn main() {
).unwrap();
let now = SystemTime::now();
for _ in 0..100 {
wfg.render_vec(TimeRange::Seconds(0f64, 10f64*ratio), (width, 100));
wfg.render_vec(TimeRange::Seconds(0f64, 10f64 * ratio), (width, 100));
}
if let Ok(elapsed) = now.elapsed() {
println!("Binned * 1000 times: {} secs + {} nsecs", elapsed.as_secs(), elapsed.subsec_nanos());
println!(
"Binned * 1000 times: {} secs + {} nsecs",
elapsed.as_secs(),
elapsed.subsec_nanos()
);
}
}
}
13 changes: 7 additions & 6 deletions examples/waveform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ extern crate gtk;
extern crate gdk_pixbuf;

use waveform::*;
use gtk::{Window, WindowType, WindowExt, WidgetExt, Inhibit, Image, ContainerExt};
use waveform::direct::*;
use gtk::{ContainerExt, Image, Inhibit, WidgetExt, Window, WindowExt, WindowType};
use gdk_pixbuf::Pixbuf;

fn main() {
Expand All @@ -23,13 +24,13 @@ fn main() {
let mut samples: Vec<f64> = Vec::new();
for t in 0..44100 {
samples.push(
((t as f64) / 100f64 * 2f64 * 3.1415f64).sin() *
((t as f64) / 10000f64 * 2f64 * 3.1415f64).sin(),
((t as f64) / 100f64 * 2f64 * 3.1415f64).sin() * ((t as f64) / 10000f64 * 2f64 * 3.1415f64).sin(),
);
}

let config = WaveformConfig::new(
-1f64, 1f64,
-1f64,
1f64,
Color::RGBA {
r: 0,
g: 0,
Expand All @@ -41,7 +42,7 @@ fn main() {
g: 0,
b: 0,
a: 0,
}
},
).unwrap();

let vec: Vec<u8>;
Expand All @@ -50,7 +51,7 @@ fn main() {
data: &samples[..],
sample_rate: 44100f64,
};
let mut wfg = BinnedWaveformRenderer::new(&ss, 10, config).unwrap();
let wfg = BinnedWaveformRenderer::new(&ss, 10, config).unwrap();
vec = wfg.render_vec(TimeRange::Seconds(0.0f64, 1.0f64), (800, 100))
.unwrap();
} else {
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 140
87 changes: 55 additions & 32 deletions src/binned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::cmp;
use error::InvalidSizeError;
use misc::*;

#[cfg(not(feature="rlibc"))]
#[cfg(not(feature = "rlibc"))]
use std::io::Write;

#[cfg(feature="rlibc")]
#[cfg(feature = "rlibc")]
use rlibc;


Expand All @@ -33,18 +33,14 @@ impl<T: Sample> BinnedWaveformRenderer<T> {
/// * `bin_size` - The size of the bins which the min / max values will be binned
/// into.
/// * `config` - See `WaveformConfig`.
pub fn new(
samples: &SampleSequence<T>,
bin_size: usize,
config: WaveformConfig,
) -> Result<BinnedWaveformRenderer<T>, Box<Error>> {
pub fn new(samples: &SampleSequence<T>, bin_size: usize, config: WaveformConfig) -> Result<BinnedWaveformRenderer<T>, Box<Error>> {
let mut data: Vec<MinMaxPair<T>> = Vec::new();
let nb_samples = samples.data.len();

if bin_size > nb_samples {
return Err(Box::new(
InvalidSizeError { var_name: "bin_size".to_string() },
));
return Err(Box::new(InvalidSizeError {
var_name: "bin_size".to_string(),
}));
}

let nb_bins = nb_samples / bin_size;
Expand Down Expand Up @@ -146,20 +142,46 @@ impl<T: Sample> BinnedWaveformRenderer<T> {
}

let scale = 1f64 / (self.config.amp_max - self.config.amp_min) * (h as f64);
let min_translated: usize = h - cmp::max(0, cmp::min(h, ((min.into() - self.config.amp_min) * scale).floor() as usize));
let max_translated: usize = h - cmp::max(0, cmp::min(h, ((max.into() - self.config.amp_min) * scale).floor() as usize));
let min_translated: usize = h -
cmp::max(
0,
cmp::min(
h,
((min.into() - self.config.amp_min) * scale).floor() as usize,
),
);
let max_translated: usize = h -
cmp::max(
0,
cmp::min(
h,
((max.into() - self.config.amp_min) * scale).floor() as usize,
),
);

// The following part intensively uses macros.
// See src/macros/*.rs for their defenitions.
match (self.config.get_background(), self.config.get_foreground()) {
(Color::RGBA{r:br, g:bg, b:bb, a:ba}, Color::RGBA{r:fr, g:fg, b:fb, a:fa})
=> {
let bg_colors: [u8; 4] = [br, bg, bb, ba];
let fg_colors: [u8; 4] = [fr, fg, fb, fa];

#[cfg(feature="rlibc")]
unsafe {
flipping_three_segment_for!{
(
Color::RGBA {
r: br,
g: bg,
b: bb,
a: ba,
},
Color::RGBA {
r: fr,
g: fg,
b: fb,
a: fa,
},
) => {
let bg_colors: [u8; 4] = [br, bg, bb, ba];
let fg_colors: [u8; 4] = [fr, fg, fb, fa];

#[cfg(feature = "rlibc")]
unsafe {
flipping_three_segment_for!{
for y in 0, max_translated, min_translated, h, {
rlibc::memcpy(
&mut pixel!(img[w, h, 4; x, y, 0]) as _,
Expand All @@ -173,27 +195,28 @@ impl<T: Sample> BinnedWaveformRenderer<T> {
)
}
}
}
}

#[cfg(not(feature="rlibc"))]
{
flipping_three_segment_for!{
#[cfg(not(feature = "rlibc"))]
{
flipping_three_segment_for!{
for y in 0, max_translated, min_translated, h, {
(&mut pixel!(img[w, h, 4; x, y, 0 => 4])).write(&bg_colors).unwrap(),
(&mut pixel!(img[w, h, 4; x, y, 0 => 4])).write(&fg_colors).unwrap()
(&mut pixel!(img[w, h, 4; x, y, 0 => 4]))
.write(&bg_colors).unwrap(),
(&mut pixel!(img[w, h, 4; x, y, 0 => 4]))
.write(&fg_colors).unwrap()
}
}
}
},
(Color::Scalar(ba), Color::Scalar(fa))
=> {
flipping_three_segment_for!{
}
}
(Color::Scalar(ba), Color::Scalar(fa)) => {
flipping_three_segment_for!{
for y in 0, max_translated, min_translated, h, {
pixel!(img[w, h; x, y]) = ba,
pixel!(img[w, h; x, y]) = fa
}
}
},
}
(_, _) => unreachable!(),
}
}
Expand Down
60 changes: 25 additions & 35 deletions src/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ impl DirectWaveformRenderer {
// Unlike BinnedWaveformRenderer, the minmax values corresponding to
// each horizontal pixel is calculated beforehand, for the same reasons
// discussed later in these comments.
let mut minmax = MinMaxPairSequence { data: Vec::with_capacity(w) };
let mut minmax = MinMaxPairSequence {
data: Vec::with_capacity(w),
};
for x in 0..w {
let mut min = samples[x * samples_per_pixel + 0];
let mut max = samples[x * samples_per_pixel + 0];
Expand Down Expand Up @@ -65,44 +67,32 @@ impl DirectWaveformRenderer {
b: fb,
a: fa,
},
) => {
for y in 0..h {
let y_translated = ((h - y) as f64) / (h as f64) *
(self.config.amp_max - self.config.amp_min) +
self.config.amp_min;
for x in 0..w {
if y_translated < minmax.data[x].min.into() ||
y_translated > minmax.data[x].max.into()
{
img[4 * (y * w + x) + 0] = br;
img[4 * (y * w + x) + 1] = bg;
img[4 * (y * w + x) + 2] = bb;
img[4 * (y * w + x) + 3] = ba;
} else {
img[4 * (y * w + x) + 0] = fr;
img[4 * (y * w + x) + 1] = fg;
img[4 * (y * w + x) + 2] = fb;
img[4 * (y * w + x) + 3] = fa;
}
) => for y in 0..h {
let y_translated = ((h - y) as f64) / (h as f64) * (self.config.amp_max - self.config.amp_min) + self.config.amp_min;
for x in 0..w {
if y_translated < minmax.data[x].min.into() || y_translated > minmax.data[x].max.into() {
img[4 * (y * w + x) + 0] = br;
img[4 * (y * w + x) + 1] = bg;
img[4 * (y * w + x) + 2] = bb;
img[4 * (y * w + x) + 3] = ba;
} else {
img[4 * (y * w + x) + 0] = fr;
img[4 * (y * w + x) + 1] = fg;
img[4 * (y * w + x) + 2] = fb;
img[4 * (y * w + x) + 3] = fa;
}
}
}
(Color::Scalar(ba), Color::Scalar(fa)) => {
for y in 0..h {
let y_translated = ((h - y) as f64) / (h as f64) *
(self.config.amp_max - self.config.amp_min) +
self.config.amp_min;
for x in 0..w {
if y_translated < minmax.data[x].min.into() ||
y_translated > minmax.data[x].max.into()
{
img[1 * (y * w + x) + 0] = ba;
} else {
img[1 * (y * w + x) + 0] = fa;
}
},
(Color::Scalar(ba), Color::Scalar(fa)) => for y in 0..h {
let y_translated = ((h - y) as f64) / (h as f64) * (self.config.amp_max - self.config.amp_min) + self.config.amp_min;
for x in 0..w {
if y_translated < minmax.data[x].min.into() || y_translated > minmax.data[x].max.into() {
img[1 * (y * w + x) + 0] = ba;
} else {
img[1 * (y * w + x) + 0] = fa;
}
}
}
},
_ => {
panic!("Color formats of background and foreground are inconsistent!");
}
Expand Down
5 changes: 4 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ pub struct InconsistentFormatError;

impl fmt::Display for InconsistentFormatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Color formats of background and foreground must be consistent.")
write!(
f,
"Color formats of background and foreground must be consistent."
)
}
}
impl Error for InconsistentFormatError {
Expand Down
Loading

0 comments on commit 35248bc

Please sign in to comment.