Skip to content

Commit 0d992d9

Browse files
Merge pull request #57 from mitchmindtree/signal
Change the `Signal` trait to return `Self::Frame` rather than `Option<Self::Item>`
2 parents 0e00a97 + d6680a0 commit 0d992d9

File tree

10 files changed

+711
-730
lines changed

10 files changed

+711
-730
lines changed

README.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -50,38 +50,39 @@ let bar: [u8; 2] = foo.map(Sample::to_sample);
5050
assert_eq!(bar, [128u8, 128]);
5151
```
5252

53-
Use the **Signal** trait for working with `Iterator`s that yield `Frame`s.
54-
To complement the `Iterator` trait, **Signal** provides methods for adding,
55-
scaling, offsetting, multiplying, clipping and generating frame iterators and
56-
more. Working with **Signal**s allows for easy, readable creation of rich and
57-
complex DSP graphs with a simple and familiar API.
53+
Use the **Signal** trait for working with infinite-iterator-like types that
54+
yield `Frame`s. **Signal** provides methods for adding, scaling, offsetting,
55+
multiplying, clipping and generating streams of `Frame`s. Working with
56+
**Signal**s allows for easy, readable creation of rich and complex DSP graphs
57+
with a simple and familiar API.
5858

5959
```rust
6060
// Clip to an amplitude of 0.9.
6161
let frames = [[1.2, 0.8], [-0.7, -1.4]];
62-
let clipped: Vec<_> = frames.iter().cloned().clip_amp(0.9).collect();
62+
let clipped: Vec<_> = signal::from_slice(&frames).clip_amp(0.9).take(2).collect();
6363
assert_eq!(clipped, vec![[0.9, 0.8], [-0.7, -0.9]]);
6464

6565
// Add `a` with `b` and yield the result.
6666
let a = [[0.2], [-0.6], [0.5]];
6767
let b = [[0.2], [0.1], [-0.8]];
68-
let a_signal = a.iter().cloned();
69-
let b_signal = b.iter().cloned();
70-
let added: Vec<[f32; 1]> = a_signal.add_amp(b_signal).collect();
68+
let a_signal = signal::from_slice(&a);
69+
let b_signal = signal::from_slice(&b);
70+
let added: Vec<[f32; 1]> = a_signal.add_amp(b_signal).take(3).collect();
7171
assert_eq!(added, vec![[0.4], [-0.5], [-0.3]]);
7272

7373
// Scale the playback rate by `0.5`.
7474
let foo = [[0.0], [1.0], [0.0], [-1.0]];
75-
let mut source = foo.iter().cloned();
76-
let interp = Linear::from_source(&mut source).unwrap();
77-
let frames: Vec<_> = source.scale_hz(interp, 0.5).collect();
75+
let mut source = signal::from_slice(&foo);
76+
let interp = Linear::from_source(&mut source);
77+
let frames: Vec<_> = source.scale_hz(interp, 0.5).take(8).collect();
7878
assert_eq!(&frames[..], &[[0.0], [0.5], [1.0], [0.5], [0.0], [-0.5], [-1.0], [-0.5]][..]);
7979
```
8080

8181
The **signal** module also provides a series of **Signal** source types,
8282
including:
8383

84-
- `FromInterleavedSamples`
84+
- `FromIterator`
85+
- `FromInterleavedSamplesIterator`
8586
- `Equilibrium` (silent signal)
8687
- `Phase`
8788
- `Sine`

examples/resample.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ extern crate sample;
44

55
use hound::{WavReader, WavSpec, WavWriter};
66
use sample::interpolate::{Sinc, Converter};
7-
use sample::Sample;
7+
use sample::{signal, Sample, Signal};
88

99
fn main() {
1010
let assets = find_folder::Search::ParentsThenKids(5, 5).for_folder("assets").unwrap();
1111
let mut reader = WavReader::open(assets.join("two_vowels.wav")).unwrap();
1212
let samples: Vec<[f64; 1]> = reader.samples::<i16>()
1313
.map(|s| [s.unwrap().to_sample()])
1414
.collect();
15+
let len = samples.len();
16+
let signal = signal::from_slice(&samples[..]);
1517

1618
let sample_rate = reader.spec().sample_rate as f64;
1719
let new_sample_rate = 10_000.0;
1820
let sinc = Sinc::zero_padded(50);
19-
let conv = Converter::from_hz_to_hz(samples.iter().cloned(), sinc, sample_rate, new_sample_rate);
21+
let conv = Converter::from_hz_to_hz(signal, sinc, sample_rate, new_sample_rate);
2022

2123
let spec = WavSpec {
2224
channels: 1,
@@ -26,7 +28,8 @@ fn main() {
2628
};
2729

2830
let mut writer = WavWriter::create(assets.join("two_vowels_10k.wav"), spec).unwrap();
29-
for f in conv {
31+
let len = (len as f64 * new_sample_rate / sample_rate) as usize;
32+
for f in conv.take(len) {
3033
writer.write_sample((f[0].to_sample::<i16>())).unwrap();
3134
}
3235
}

examples/synth.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate portaudio as pa;
22
extern crate sample;
33

4-
use sample::{Frame, Sample, Signal, ToFrameSliceMut};
5-
use sample::signal;
4+
use sample::{signal, Frame, Sample, Signal, ToFrameSliceMut};
65

76
const FRAMES_PER_BUFFER: u32 = 512;
87
const NUM_CHANNELS: i32 = 1;
@@ -17,13 +16,12 @@ fn run() -> Result<(), pa::Error> {
1716
// Create a signal chain to play back 1 second of each oscillator at A4.
1817
let hz = signal::rate(SAMPLE_RATE).const_hz(440.0);
1918
let one_sec = SAMPLE_RATE as usize;
20-
let mut signal = hz.clone().sine().take(one_sec)
19+
let mut waves = hz.clone().sine().take(one_sec)
2120
.chain(hz.clone().saw().take(one_sec))
2221
.chain(hz.clone().square().take(one_sec))
2322
.chain(hz.clone().noise_simplex().take(one_sec))
2423
.chain(signal::noise(0).take(one_sec))
25-
.map(|f| f.map(|s| s.to_sample::<f32>()))
26-
.scale_amp(0.2);
24+
.map(|f| f.map(|s| s.to_sample::<f32>() * 0.2));
2725

2826
// Initialise PortAudio.
2927
let pa = try!(pa::PortAudio::new());
@@ -35,7 +33,7 @@ fn run() -> Result<(), pa::Error> {
3533
let callback = move |pa::OutputStreamCallbackArgs { buffer, .. }| {
3634
let buffer: &mut [[f32; 1]] = buffer.to_frame_slice_mut().unwrap();
3735
for out_frame in buffer {
38-
match signal.next() {
36+
match waves.next() {
3937
Some(frame) => *out_frame = frame,
4038
None => return pa::Complete,
4139
}

examples/wav.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ fn frames(file_name: &'static str) -> Vec<wav::Frame> {
6363
let sample_file = assets.join(file_name);
6464
let mut reader = hound::WavReader::open(&sample_file).unwrap();
6565
let spec = reader.spec();
66+
let duration = reader.duration();
67+
let new_duration = (duration as f64 * (SAMPLE_RATE as f64 / spec.sample_rate as f64)) as usize;
6668
let samples = reader.samples().map(|s| s.unwrap());
67-
let mut signal = signal::from_interleaved_samples::<_, wav::Frame>(samples);
68-
let interp = Linear::from_source(&mut signal).unwrap();
69+
let mut signal = signal::from_interleaved_samples_iter::<_, wav::Frame>(samples);
70+
let interp = Linear::from_source(&mut signal);
6971
signal.from_hz_to_hz(interp, spec.sample_rate as f64, SAMPLE_RATE as f64)
72+
.take(new_duration)
7073
.collect()
7174
}

0 commit comments

Comments
 (0)