Skip to content

Commit 1686a04

Browse files
authored
Merge pull request #326 from uklotzde/feature/worklet-example
onmessage: Either consume and handle or drop incoming messages
2 parents 15491a2 + 5dac157 commit 1686a04

File tree

5 files changed

+93
-55
lines changed

5 files changed

+93
-55
lines changed

examples/worklet.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,17 @@ impl AudioProcessor for WhiteNoiseProcessor {
143143

144144
fn onmessage(&mut self, msg: Box<dyn std::any::Any + Send + 'static>) {
145145
// handle incoming signals requesting for change of color
146-
if let Some(&color) = msg.downcast_ref::<NoiseColor>() {
147-
self.color = color;
148-
return;
149-
}
146+
let msg = match msg.downcast::<NoiseColor>() {
147+
Ok(color) => {
148+
self.color = *color;
149+
return;
150+
}
151+
Err(msg) => msg,
152+
};
150153

151-
// ignore other incoming messages
152-
log::warn!("WhiteNoiseProcessor: Ignoring incoming message");
154+
// ...add more message handlers here...
155+
156+
log::warn!("WhiteNoiseProcessor: Dropping incoming message {msg:?}");
153157
}
154158
}
155159

src/node/audio_buffer_source.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ struct AudioBufferSourceRenderer {
341341
ended_triggered: bool,
342342
}
343343

344+
impl AudioBufferSourceRenderer {
345+
fn handle_control_message(&mut self, control: Control) {
346+
match control {
347+
Control::StartWithOffsetAndDuration(start, offset, duration) => {
348+
self.start_time = start;
349+
self.offset = offset;
350+
self.duration = duration;
351+
}
352+
Control::Stop(v) => self.stop_time = v,
353+
Control::Loop(v) => self.controller.set_loop(v),
354+
Control::LoopStart(v) => self.controller.set_loop_start(v),
355+
Control::LoopEnd(v) => self.controller.set_loop_end(v),
356+
}
357+
}
358+
}
359+
344360
impl AudioProcessor for AudioBufferSourceRenderer {
345361
fn process(
346362
&mut self,
@@ -701,26 +717,23 @@ impl AudioProcessor for AudioBufferSourceRenderer {
701717
}
702718

703719
fn onmessage(&mut self, msg: Box<dyn std::any::Any + Send + 'static>) {
704-
if let Some(&control) = msg.downcast_ref::<Control>() {
705-
match control {
706-
Control::StartWithOffsetAndDuration(start, offset, duration) => {
707-
self.start_time = start;
708-
self.offset = offset;
709-
self.duration = duration;
710-
}
711-
Control::Stop(v) => self.stop_time = v,
712-
Control::Loop(v) => self.controller.set_loop(v),
713-
Control::LoopStart(v) => self.controller.set_loop_start(v),
714-
Control::LoopEnd(v) => self.controller.set_loop_end(v),
720+
let msg = match msg.downcast::<Control>() {
721+
Ok(control) => {
722+
self.handle_control_message(*control);
723+
return;
715724
}
716-
}
725+
Err(msg) => msg,
726+
};
717727

718-
if let Ok(buffer) = msg.downcast::<AudioBuffer>() {
719-
self.buffer = Some(*buffer);
720-
return;
721-
}
728+
let msg = match msg.downcast::<AudioBuffer>() {
729+
Ok(buffer) => {
730+
self.buffer = Some(*buffer);
731+
return;
732+
}
733+
Err(msg) => msg,
734+
};
722735

723-
log::warn!("AudioBufferSourceRenderer: Ignoring incoming message");
736+
log::warn!("AudioBufferSourceRenderer: Dropping incoming message {msg:?}");
724737
}
725738
}
726739

src/node/constant_source.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,18 @@ impl AudioProcessor for ConstantSourceRenderer {
203203
}
204204

205205
fn onmessage(&mut self, msg: Box<dyn std::any::Any + Send + 'static>) {
206-
if let Ok(schedule) = msg.downcast::<Schedule>() {
207-
match *schedule {
208-
Schedule::Start(v) => self.start_time = v,
209-
Schedule::Stop(v) => self.stop_time = v,
206+
let msg = match msg.downcast::<Schedule>() {
207+
Ok(schedule) => {
208+
match *schedule {
209+
Schedule::Start(v) => self.start_time = v,
210+
Schedule::Stop(v) => self.stop_time = v,
211+
}
212+
return;
210213
}
211-
}
214+
Err(msg) => msg,
215+
};
212216

213-
log::warn!("ConstantSourceRenderer: Ignoring incoming message");
217+
log::warn!("ConstantSourceRenderer: Dropping incoming message {msg:?}");
214218
}
215219
}
216220

src/node/oscillator.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -432,27 +432,36 @@ impl AudioProcessor for OscillatorRenderer {
432432
}
433433

434434
fn onmessage(&mut self, msg: Box<dyn std::any::Any + Send + 'static>) {
435-
if let Some(&type_) = msg.downcast_ref::<OscillatorType>() {
436-
self.shared_type.store(type_ as u32, Ordering::Release);
437-
self.type_ = type_;
438-
return;
439-
}
435+
let msg = match msg.downcast::<OscillatorType>() {
436+
Ok(type_) => {
437+
self.shared_type.store(*type_ as u32, Ordering::Release);
438+
self.type_ = *type_;
439+
return;
440+
}
441+
Err(msg) => msg,
442+
};
440443

441-
if let Some(&schedule) = msg.downcast_ref::<Schedule>() {
442-
match schedule {
443-
Schedule::Start(v) => self.start_time = v,
444-
Schedule::Stop(v) => self.stop_time = v,
444+
let msg = match msg.downcast::<Schedule>() {
445+
Ok(schedule) => {
446+
match *schedule {
447+
Schedule::Start(v) => self.start_time = v,
448+
Schedule::Stop(v) => self.stop_time = v,
449+
}
450+
return;
445451
}
446-
return;
447-
}
452+
Err(msg) => msg,
453+
};
448454

449-
if let Ok(periodic_wave) = msg.downcast::<PeriodicWave>() {
450-
self.periodic_wave = Some(*periodic_wave);
451-
self.type_ = OscillatorType::Custom; // shared type is already updated by control
452-
return;
453-
}
455+
let msg = match msg.downcast::<PeriodicWave>() {
456+
Ok(periodic_wave) => {
457+
self.periodic_wave = Some(*periodic_wave);
458+
self.type_ = OscillatorType::Custom; // shared type is already updated by control
459+
return;
460+
}
461+
Err(msg) => msg,
462+
};
454463

455-
log::warn!("OscillatorRenderer: Ignoring incoming message");
464+
log::warn!("OscillatorRenderer: Dropping incoming message {msg:?}");
456465
}
457466
}
458467

src/param.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -642,16 +642,24 @@ impl AudioProcessor for AudioParamProcessor {
642642
}
643643

644644
fn onmessage(&mut self, msg: Box<dyn std::any::Any + Send + 'static>) {
645-
if let Some(&automation_rate) = msg.downcast_ref::<AutomationRate>() {
646-
self.automation_rate = automation_rate;
647-
self.shared_parts.store_automation_rate(automation_rate);
648-
return;
649-
}
645+
let msg = match msg.downcast::<AutomationRate>() {
646+
Ok(automation_rate) => {
647+
self.automation_rate = *automation_rate;
648+
self.shared_parts.store_automation_rate(*automation_rate);
649+
return;
650+
}
651+
Err(msg) => msg,
652+
};
650653

651-
match msg.downcast::<AudioParamEvent>() {
652-
Ok(event) => self.handle_incoming_event(*event),
653-
_ => log::warn!("AudioParamProcessor: Ignoring incoming message"),
654-
}
654+
let msg = match msg.downcast::<AudioParamEvent>() {
655+
Ok(event) => {
656+
self.handle_incoming_event(*event);
657+
return;
658+
}
659+
Err(msg) => msg,
660+
};
661+
662+
log::warn!("AudioParamProcessor: Dropping incoming message {msg:?}");
655663
}
656664
}
657665

0 commit comments

Comments
 (0)