1#![allow(unsafe_code)]
34
35pub mod core;
36
37use std::sync::Arc;
38
39use wasm_bindgen::prelude::*;
40
41#[wasm_bindgen]
43pub struct Compiled {
44 diagnostics: String,
45 song: Option<pikopiko::Song>,
46}
47
48#[wasm_bindgen]
49impl Compiled {
50 #[wasm_bindgen(getter)]
52 pub fn diagnostics(&self) -> String {
53 self.diagnostics.clone()
54 }
55
56 #[wasm_bindgen(getter)]
58 pub fn ok(&self) -> bool {
59 self.song.is_some()
60 }
61
62 #[wasm_bindgen(js_name = takeSong)]
64 pub fn take_song(&mut self) -> Option<Song> {
65 self.song.take().map(|s| Song(Arc::new(s)))
66 }
67}
68
69#[wasm_bindgen]
72pub fn compile(src: &str, options: &str) -> Result<Compiled, JsError> {
73 let options = core::parse_options(options).map_err(|e| JsError::new(&e))?;
74 let c = core::compile(src, &options).map_err(|e| JsError::new(&e))?;
75 Ok(Compiled {
76 diagnostics: c.diagnostics_json,
77 song: c.song,
78 })
79}
80
81#[wasm_bindgen]
83pub struct Song(Arc<pikopiko::Song>);
84
85#[wasm_bindgen]
86impl Song {
87 #[wasm_bindgen(js_name = createPlayer)]
89 pub fn create_player(&self, sample_rate: u32) -> Player {
90 Player(core::CorePlayer::new(self.0.clone(), sample_rate))
91 }
92
93 pub fn info(&self) -> String {
95 core::info_json(&self.0)
96 }
97
98 #[wasm_bindgen(js_name = encodeBinary)]
100 pub fn encode_binary(&self, lang: &str) -> Result<Vec<u8>, JsError> {
101 core::encode_binary(&self.0, lang).map_err(|e| JsError::new(&e))
102 }
103}
104
105#[wasm_bindgen]
107pub struct Player(core::CorePlayer);
108
109#[wasm_bindgen]
110impl Player {
111 pub fn render(&mut self, left: &mut [f32], right: &mut [f32]) {
113 self.0.render(left, right);
114 }
115
116 pub fn command(&mut self, name: &str, a: f64, b: f64) -> bool {
119 self.0.command(name, a, b)
120 }
121
122 pub fn notify(&mut self, names: &str) -> bool {
124 self.0.notify(names)
125 }
126
127 #[wasm_bindgen(js_name = pollNotice)]
129 pub fn poll_notice(&mut self) -> Option<String> {
130 self.0.poll_notice()
131 }
132
133 #[wasm_bindgen(getter)]
135 pub fn position(&self) -> f64 {
136 self.0.position() as f64
137 }
138
139 #[wasm_bindgen(getter, js_name = isFinished)]
141 pub fn is_finished(&self) -> bool {
142 self.0.is_finished()
143 }
144}
145
146#[wasm_bindgen]
148pub struct Sequencer(core::CoreSequencer);
149
150#[wasm_bindgen]
153pub fn sequencer(
154 bytes: &[u8],
155 sample_rate: u32,
156 synth: &str,
157 lang: &str,
158) -> Result<Sequencer, JsError> {
159 core::CoreSequencer::new(bytes.to_vec(), sample_rate, synth, lang)
160 .map(Sequencer)
161 .map_err(|e| JsError::new(&e))
162}
163
164#[wasm_bindgen]
165impl Sequencer {
166 pub fn render(&mut self, left: &mut [f32], right: &mut [f32]) {
168 self.0.render(left, right);
169 }
170
171 pub fn command(&mut self, name: &str, a: f64, b: f64) -> bool {
173 self.0.command(name, a, b)
174 }
175
176 pub fn notify(&mut self, names: &str) -> bool {
178 self.0.notify(names)
179 }
180
181 #[wasm_bindgen(js_name = pollNotice)]
183 pub fn poll_notice(&mut self) -> Option<String> {
184 self.0.poll_notice()
185 }
186
187 #[wasm_bindgen(getter)]
189 pub fn position(&self) -> f64 {
190 self.0.position() as f64
191 }
192
193 #[wasm_bindgen(getter, js_name = isFinished)]
195 pub fn is_finished(&self) -> bool {
196 self.0.is_finished()
197 }
198}