Skip to content

Commit

Permalink
Reorganize examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tdgne committed Aug 19, 2017
1 parent b363edd commit 5f4603e
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 83 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ Requests and contributions are welcome!
## Screenshot

```sh
cargo run --features "example-gui" --example waveform
# Demonstrates rendering using a single BinnedWaveformRenderer.
cargo run --features "example-gui" --example binned
```

```sh
# The same but by using a MultiWaveformRenderer, which is
# a combination of multiple BinnedWaveformRenderers.
cargo run --features "example-gui" --example binned
```

![examples/waveform.rs](https://user-images.githubusercontent.com/29127111/27250722-dd579ff6-5370-11e7-99c2-7dc3e7705c14.png)
Expand Down
82 changes: 0 additions & 82 deletions examples/benchmark.rs

This file was deleted.

File renamed without changes.
108 changes: 108 additions & 0 deletions examples/multi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// The GUI parts conditioned by the "example-gui" feature
// depend on the gtk crate, which is an interface to the
// native GTK libs.
// Although, the other parts which are not conditioned
// altogether demonstrate the basic use of this crate.

extern crate waveform;

#[cfg(feature = "example-gui")]
extern crate gtk;
#[cfg(feature = "example-gui")]
extern crate gdk_pixbuf;

use waveform::{
SampleSequence,
WaveformConfig,
Color,
MultiWaveformRenderer,
TimeRange,
};

#[cfg(feature = "example-gui")]
use gtk::{ContainerExt, Image, Inhibit, WidgetExt, Window, WindowExt, WindowType};
#[cfg(feature = "example-gui")]
use gdk_pixbuf::Pixbuf;

fn main() {
#[cfg(feature = "example-gui")]
{
if gtk::init().is_err() {
panic!("Failed to initialize gtk.");
}
}

#[cfg(feature = "example-gui")]
let window = Window::new(WindowType::Toplevel);
#[cfg(feature = "example-gui")]
{
window.set_title("A simple waveform renderer test");
window.set_default_size(800, 100);
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
}

// Generate samples to show.
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(),
);
}

// The renderer's config.
let config = WaveformConfig::new(
-1f64, // Minimum amplitude to show
1f64, // Maximum amplitude to show

// Foreground color
Color::RGBA {
r: 0,
g: 0,
b: 0,
a: 255,
},

// Background color
Color::RGBA {
r: 0,
g: 0,
b: 0,
a: 0,
}
).unwrap();

// Put a reference to the samples here along with its sample rate.
// We need to set a sample rate because it will be used
// when you specify the time range in seconds.
let ss = SampleSequence {
data: &samples[..],
sample_rate: 44100f64,
};

// Construct the renderer.
// The second argument is a `&Vec<usize>` containing the bin sizes.
// `MultiWaveformRenderer` will generate a `BinnedWaveformRenderer` for
// each bin size.
let mut wfg = MultiWaveformRenderer::new(&ss, &vec![10, 100, 1000], config).unwrap();

// Render!
// Each time `MultiWaveformRenderer` will choose the appropriate bin size.
// The largest bin size that is not larger than the average number of samples
// that a pixel contains.
let mut vec: Vec<u8> =
wfg.render_vec(TimeRange::Seconds(0.0f64, 1.0f64), (800, 100))
.unwrap();

#[cfg(feature = "example-gui")]
{
let pixbuf =
Pixbuf::new_from_vec(vec, 0, true, 8, 800, 100, 800 * 4);
let image = Image::new_from_pixbuf(Some(&pixbuf));
window.add(&image);
window.show_all();
gtk::main();
}
}

0 comments on commit 5f4603e

Please sign in to comment.