Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Oscillator node example to exercise different oscillator types #163

Merged
merged 7 commits into from Nov 9, 2018
Prev

Formatted files from previous commit using rustfmt

  • Loading branch information
Avanthikaa committed Nov 9, 2018
commit 5d8cdfc029daf69122c64f8bfcceaec6f80916a4
@@ -37,7 +37,6 @@ impl Default for OscillatorNodeOptions {
}
}


#[derive(AudioScheduledSourceNode, AudioNodeCommon)]
pub(crate) struct OscillatorNode {
channel_info: ChannelInfo,
@@ -70,8 +69,6 @@ impl OscillatorNode {
pub fn update_parameters(&mut self, info: &BlockInfo, tick: Tick) -> bool {
self.frequency.update(info, tick)
}


}

impl AudioNodeEngine for OscillatorNode {
@@ -89,7 +86,7 @@ impl AudioNodeEngine for OscillatorNode {
ShouldPlay::No => {
return inputs;
}
ShouldPlay::Between(start, end) => (start, end)
ShouldPlay::Between(start, end) => (start, end),
};

{
@@ -110,7 +107,7 @@ impl AudioNodeEngine for OscillatorNode {
while let Some(mut frame) = iter.next() {
let tick = frame.tick();
if tick < start_at {
continue
continue;
} else if tick > stop_at {
break;
}
@@ -126,38 +123,35 @@ impl AudioNodeEngine for OscillatorNode {

OscillatorType::Square => {
if self.phase >= PI && self.phase < two_pi {
value = vol * 1.0;
}
else if self.phase > 0.0 && self.phase < PI {
value = vol * (-1.0);
}
value = vol * 1.0;
} else if self.phase > 0.0 && self.phase < PI {
value = vol * (-1.0);
}
}

OscillatorType::Sawtooth => {
value = vol * ((self.phase as f64) / (PI)) as f32;
}

OscillatorType::Triangle => {
if self.phase >= 0. && self.phase < PI/2.{
if self.phase >= 0. && self.phase < PI / 2. {
value = vol * 2.0 * ((self.phase as f64) / (PI)) as f32;
}
else if self.phase >= PI/2. && self.phase < PI {
value = vol * ( 1. - ( ( (self.phase as f64) - (PI/2.)) * (2./PI) ) as f32 );
}
else if self.phase >= PI && self.phase < (3.* PI/2.) {
value = vol * -1. * ( 1. - ( ( (self.phase as f64) - (PI/2.)) * (2./PI) ) as f32 );
}
else if self.phase >= 3.*PI/2. && self.phase < 2.*PI {
} else if self.phase >= PI / 2. && self.phase < PI {
value =
vol * (1. - (((self.phase as f64) - (PI / 2.)) * (2. / PI)) as f32);
} else if self.phase >= PI && self.phase < (3. * PI / 2.) {
value = vol
* -1.
* (1. - (((self.phase as f64) - (PI / 2.)) * (2. / PI)) as f32);
} else if self.phase >= 3. * PI / 2. && self.phase < 2. * PI {
value = vol * (-2.0) * ((self.phase as f64) / (PI)) as f32;
}
}

OscillatorType::Custom => {

}
OscillatorType::Custom => {}
}

frame.mutate_with(|sample, _| *sample = value);
frame.mutate_with(|sample, _| *sample = value);

self.phase += step;
if self.phase >= two_pi {
@@ -2,11 +2,11 @@ extern crate servo_media;

use servo_media::audio::node::{AudioNodeInit, AudioNodeMessage, AudioScheduledSourceNodeMessage};
use servo_media::audio::oscillator_node::OscillatorNodeOptions;
use servo_media::audio::oscillator_node::OscillatorType::Triangle;
use servo_media::audio::oscillator_node::OscillatorType::Sawtooth;
use servo_media::audio::oscillator_node::OscillatorType::Triangle;
//use servo_media::audio::oscillator_node::OscillatorType::Sine;
use servo_media::audio::oscillator_node::OscillatorType::Square;
use servo_media::audio::oscillator_node::OscillatorType::Custom;
use servo_media::audio::oscillator_node::OscillatorType::Square;
use servo_media::ServoMedia;
use std::sync::Arc;
use std::{thread, time};
@@ -58,7 +58,6 @@ fn run_example(servo_media: Arc<ServoMedia>) {
let _ = context.close();
thread::sleep(time::Duration::from_millis(1000));


options.oscillator_type = Triangle;
let context = servo_media.create_audio_context(Default::default());
let dest = context.dest_node();
@@ -76,7 +75,6 @@ fn run_example(servo_media: Arc<ServoMedia>) {
let _ = context.close();
thread::sleep(time::Duration::from_millis(1000));


options.oscillator_type = Custom;
let context = servo_media.create_audio_context(Default::default());
let dest = context.dest_node();
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.