Skip to main content

simple_dsp/
simple_dsp.rs

1//! Simple DSP Plugin Example
2//! 
3//! This example demonstrates how to create a basic DSP (audio effect) plugin
4//! using the VirtualDJ Rust SDK. This is meant to be a template for developers
5//! who want to write audio effects in Rust.
6
7use virtualdj_plugin_sdk::{DspPlugin, PluginBase, PluginInfo, Result};
8
9/// A simple gain/volume control DSP plugin
10pub struct GainPlugin {
11    gain: f32,
12    sample_rate: i32,
13}
14
15impl GainPlugin {
16    pub fn new() -> Self {
17        GainPlugin {
18            gain: 1.0,
19            sample_rate: 44100,
20        }
21    }
22}
23
24impl PluginBase for GainPlugin {
25    fn on_load(&mut self) -> Result<()> {
26        println!("Gain plugin loaded!");
27        Ok(())
28    }
29
30    fn get_info(&self) -> PluginInfo {
31        PluginInfo {
32            name: "Simple Gain".to_string(),
33            author: "Your Name".to_string(),
34            description: "A simple volume gain effect".to_string(),
35            version: "1.0.0".to_string(),
36            flags: 0,
37        }
38    }
39
40    fn on_parameter(&mut self, id: i32) -> Result<()> {
41        println!("Parameter {} changed", id);
42        Ok(())
43    }
44}
45
46impl DspPlugin for GainPlugin {
47    fn on_start(&mut self) -> Result<()> {
48        println!("Gain plugin started");
49        Ok(())
50    }
51
52    fn on_stop(&mut self) -> Result<()> {
53        println!("Gain plugin stopped");
54        Ok(())
55    }
56
57    fn on_process_samples(&mut self, buffer: &mut [f32]) -> Result<()> {
58        // Apply gain to all samples (stereo buffer is interleaved)
59        for sample in buffer.iter_mut() {
60            *sample *= self.gain;
61            // Clamp to prevent clipping
62            if *sample > 1.0 {
63                *sample = 1.0;
64            } else if *sample < -1.0 {
65                *sample = -1.0;
66            }
67        }
68        Ok(())
69    }
70
71    fn sample_rate(&self) -> i32 {
72        self.sample_rate
73    }
74
75    fn song_bpm(&self) -> i32 {
76        120
77    }
78
79    fn song_pos_beats(&self) -> f64 {
80        0.0
81    }
82}
83
84fn main() {
85    println!("VirtualDJ Rust SDK - Simple Gain Plugin Example");
86    println!("This example demonstrates a basic DSP plugin structure.");
87    
88    let mut plugin = GainPlugin::new();
89    if let Ok(()) = plugin.on_load() {
90        println!("✓ Plugin loaded successfully");
91    }
92    
93    let info = plugin.get_info();
94    println!("\nPlugin Information:");
95    println!("  Name: {}", info.name);
96    println!("  Author: {}", info.author);
97    println!("  Description: {}", info.description);
98    println!("  Version: {}", info.version);
99    
100    println!("\nTo use this plugin in a real project:");
101    println!("1. Implement DspPlugin trait methods for your audio processing");
102    println!("2. Compile with: cargo build --release");
103    println!("3. Deploy the resulting DLL to VirtualDJ's plugin directory");
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn test_plugin_creation() {
112        let mut plugin = GainPlugin::new();
113        assert!(plugin.on_load().is_ok());
114        assert_eq!(plugin.gain, 1.0);
115    }
116
117    #[test]
118    fn test_gain_processing() {
119        let mut plugin = GainPlugin::new();
120        plugin.gain = 0.5;
121        
122        let mut samples = vec![1.0, -1.0, 0.5, -0.5, 2.0, -2.0];
123        assert!(plugin.on_process_samples(&mut samples).is_ok());
124        
125        // Check that samples were scaled
126        assert_eq!(samples[0], 0.5);
127        assert_eq!(samples[1], -0.5);
128        assert_eq!(samples[4], 1.0); // Clipped from 1.0
129        assert_eq!(samples[5], -1.0); // Clipped from -1.0
130    }
131}