|
| 1 | +use ltc::LTCDecoder; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use mizer_audio_nodes::{AudioContext, Signal}; |
| 4 | +use mizer_clock::Timecode; |
| 5 | +use mizer_node::*; |
| 6 | + |
| 7 | +const AUDIO_INPUT: &str = "LTC"; |
| 8 | +const TIMECODE_OUTPUT: &str = "Clock"; |
| 9 | + |
| 10 | +const FPS_SETTING: &str = "FPS"; |
| 11 | + |
| 12 | +const SAMPLE_RATE: u32 = 44_100; |
| 13 | + |
| 14 | +#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] |
| 15 | +pub struct LtcDecoderNode { |
| 16 | + pub fps: f64, |
| 17 | +} |
| 18 | + |
| 19 | +impl Default for LtcDecoderNode { |
| 20 | + fn default() -> Self { |
| 21 | + Self { |
| 22 | + fps: 30.0, |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl ConfigurableNode for LtcDecoderNode { |
| 28 | + fn settings(&self, _injector: &Injector) -> Vec<NodeSetting> { |
| 29 | + vec![ |
| 30 | + setting!(FPS_SETTING, self.fps), |
| 31 | + ] |
| 32 | + } |
| 33 | + |
| 34 | + fn update_setting(&mut self, setting: NodeSetting) -> anyhow::Result<()> { |
| 35 | + update!(float setting, FPS_SETTING, self.fps); |
| 36 | + |
| 37 | + update_fallback!(setting) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl PipelineNode for LtcDecoderNode { |
| 42 | + fn details(&self) -> NodeDetails { |
| 43 | + NodeDetails { |
| 44 | + node_type_name: "LTC Decoder".into(), |
| 45 | + preview_type: PreviewType::Timecode, |
| 46 | + category: NodeCategory::Standard, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + fn display_name(&self, _injector: &Injector) -> String { |
| 51 | + format!("LTC Decoder ({} FPS)", self.fps) |
| 52 | + } |
| 53 | + |
| 54 | + fn list_ports(&self, _injector: &Injector) -> Vec<(PortId, PortMetadata)> { |
| 55 | + vec![ |
| 56 | + input_port!(AUDIO_INPUT, PortType::Multi), |
| 57 | + output_port!(TIMECODE_OUTPUT, PortType::Clock), |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + fn node_type(&self) -> NodeType { |
| 62 | + NodeType::LtcDecoder |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl ProcessingNode for LtcDecoderNode { |
| 67 | + type State = LTCDecoder; |
| 68 | + |
| 69 | + fn process(&self, context: &impl NodeContext, state: &mut Self::State) -> anyhow::Result<()> { |
| 70 | + let Some(mut signal) = context.input_signal(AUDIO_INPUT) else { |
| 71 | + return Ok(()); |
| 72 | + }; |
| 73 | + |
| 74 | + let frames = context.transfer_size_per_channel(); |
| 75 | + |
| 76 | + let frames = (0..frames).map(|_| { |
| 77 | + let [mono, _] = signal.next(); |
| 78 | + |
| 79 | + mono as f32 |
| 80 | + }).collect::<Vec<_>>(); |
| 81 | + |
| 82 | + |
| 83 | + let mut timecode_frame = None; |
| 84 | + if state.write_samples(&frames) { |
| 85 | + for frame in state { |
| 86 | + timecode_frame = Some(frame); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if let Some(frame) = timecode_frame { |
| 91 | + let timecode = Timecode { |
| 92 | + hours: frame.hour as u64, |
| 93 | + minutes: frame.minute as u64, |
| 94 | + seconds: frame.second as u64, |
| 95 | + frames: frame.frame as u64, |
| 96 | + negative: false |
| 97 | + }; |
| 98 | + context.write_timecode_preview(timecode); |
| 99 | + let timestamp = timecode.to_duration(context.fps()); |
| 100 | + context.write_port(TIMECODE_OUTPUT, timestamp); |
| 101 | + } |
| 102 | + |
| 103 | + Ok(()) |
| 104 | + } |
| 105 | + |
| 106 | + fn create_state(&self) -> Self::State { |
| 107 | + LTCDecoder::new(SAMPLE_RATE as f32 / self.fps as f32, Default::default()) |
| 108 | + } |
| 109 | +} |
0 commit comments