Skip to main content

devalang_wasm/
lib.rs

1#![allow(dead_code)]
2#![allow(clippy::module_inception)]
3
4pub mod engine;
5pub mod language;
6pub mod shared;
7pub mod utils;
8
9// Plugin development SDK (available with "plugin" feature)
10#[cfg(feature = "plugin")]
11pub mod plugin {
12    //! Plugin development SDK for Devalang
13    //!
14    //! This module provides types, macros, and utilities for writing WASM plugins.
15    //! Enable the "plugin" feature to use this module.
16
17    pub use crate::engine::plugin::bindings::*;
18
19    // Re-export macros from crate root (they are exported there due to #[macro_export])
20    pub use crate::{export_plugin, export_plugin_with_state, simple_oscillator_plugin};
21}
22
23// CLI-specific modules (requires terminal, file system, etc.)
24#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
25pub mod platform;
26
27#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
28pub mod services;
29
30#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
31pub mod tools;
32
33#[cfg(all(feature = "cli", not(target_arch = "wasm32")))]
34pub mod workspace;
35
36// WebAssembly bindings (only compiled for wasm32 target with "wasm" feature)
37#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
38pub mod web;
39
40#[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
41pub mod web {
42    //! Minimal web registry stubs for plugin builds
43
44    pub mod registry {
45        pub mod samples {
46            pub fn get_sample(_name: &str) -> Option<Vec<f32>> {
47                None
48            }
49        }
50        pub mod debug {
51            pub fn log(_msg: &str) {}
52            pub fn is_debug_errors_enabled() -> bool {
53                false
54            }
55            pub fn push_parse_error_from_parts(
56                _message: String,
57                _line: usize,
58                _column: usize,
59                _error_type: String,
60            ) {
61            }
62        }
63        pub mod banks {
64            use std::collections::HashMap;
65            pub static REGISTERED_BANKS: once_cell::sync::Lazy<
66                std::sync::Mutex<HashMap<String, String>>,
67            > = once_cell::sync::Lazy::new(|| std::sync::Mutex::new(HashMap::new()));
68        }
69        pub mod playhead {
70            #[derive(Default)]
71            pub struct PlayheadEvent {
72                pub kind: String,
73                pub event_type: String,
74                pub midi: Vec<u8>,
75                pub time: f32,
76                pub velocity: f32,
77                pub synth_id: String,
78                pub pitch: u8,
79                pub sample: Option<String>,
80            }
81            pub fn push_event(_event: PlayheadEvent) {}
82        }
83    }
84}
85
86#[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
87pub mod web_sys {
88    //! Minimal web_sys stubs for plugin builds
89    pub mod console {
90        pub fn log_1(_msg: &str) {}
91        pub fn warn_1(_msg: &str) {}
92    }
93}
94
95#[cfg(not(target_arch = "wasm32"))]
96pub mod web {
97    pub mod registry {
98        pub mod samples {
99            pub fn get_sample(_name: &str) -> Option<Vec<f32>> {
100                None
101            }
102        }
103        pub mod debug {
104            pub fn log(_msg: &str) {}
105            pub fn is_debug_errors_enabled() -> bool {
106                false
107            }
108            pub fn push_parse_error_from_parts(
109                _message: String,
110                _line: usize,
111                _column: usize,
112                _error_type: String,
113            ) {
114            }
115        }
116        pub mod banks {
117            pub struct BankEntry {
118                pub full_name: String,
119                pub alias: String,
120            }
121            use std::cell::RefCell;
122            thread_local! {
123                pub static REGISTERED_BANKS: RefCell<Vec<BankEntry>> = RefCell::new(Vec::new());
124            }
125        }
126        pub mod playhead {
127            pub fn push_event<T>(_event: T) {}
128        }
129    }
130}
131
132#[cfg(all(target_arch = "wasm32", not(any(feature = "wasm", feature = "cli"))))]
133pub mod midly {
134    //! Minimal midly stubs for plugin builds
135    pub struct Header;
136    pub enum Format {
137        SingleTrack,
138    }
139    pub enum Timing {
140        Metrical(u16),
141    }
142
143    #[derive(Debug, Clone)]
144    pub struct TrackEvent {
145        pub delta: u28,
146        pub kind: TrackEventKind,
147    }
148
149    #[derive(Debug, Clone)]
150    pub struct u28(pub u32);
151    impl From<u32> for u28 {
152        fn from(v: u32) -> Self {
153            u28(v)
154        }
155    }
156
157    #[derive(Debug, Clone)]
158    pub struct u24(pub u32);
159    impl From<u32> for u24 {
160        fn from(v: u32) -> Self {
161            u24(v)
162        }
163    }
164
165    #[derive(Debug, Clone)]
166    pub struct u7(pub u8);
167    impl From<u8> for u7 {
168        fn from(v: u8) -> Self {
169            u7(v)
170        }
171    }
172
173    #[derive(Debug, Clone)]
174    pub struct u4(pub u8);
175    impl From<u8> for u4 {
176        fn from(v: u8) -> Self {
177            u4(v)
178        }
179    }
180
181    #[derive(Debug, Clone)]
182    pub enum MetaMessage {
183        Tempo(u24),
184        EndOfTrack,
185    }
186
187    #[derive(Debug, Clone)]
188    pub enum TrackEventKind {
189        Meta(MetaMessage),
190        Midi { channel: u4, message: MidiMessage },
191    }
192
193    #[derive(Debug, Clone)]
194    pub enum MidiMessage {
195        NoteOn { key: u7, vel: u7 },
196        NoteOff { key: u7, vel: u7 },
197    }
198
199    pub struct Track(Vec<TrackEvent>);
200    impl From<Vec<TrackEvent>> for Track {
201        fn from(events: Vec<TrackEvent>) -> Self {
202            Track(events)
203        }
204    }
205
206    pub struct Smf {
207        pub tracks: Vec<Track>,
208    }
209
210    impl Header {
211        pub fn new(_format: Format, _timing: Timing) -> Self {
212            Header
213        }
214    }
215
216    impl Smf {
217        pub fn new(_header: Header) -> Self {
218            Smf { tracks: Vec::new() }
219        }
220        pub fn write<W: std::io::Write>(&self, _writer: &mut W) -> std::io::Result<()> {
221            Ok(())
222        }
223    }
224}