vibelang-http 0.3.0

HTTP REST API server for VibeLang
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Request and response models for the HTTP API.
//!
//! These models match the TypeScript types expected by the VSCode extension.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// =============================================================================
// Source Location (for navigation to code)
// =============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceLocation {
    pub file: Option<String>,
    pub line: Option<u32>,
    pub column: Option<u32>,
}

// =============================================================================
// Transport
// =============================================================================

#[derive(Debug, Serialize)]
pub struct TransportState {
    pub bpm: f64,
    pub time_signature: TimeSignature,
    pub running: bool,
    pub current_beat: f64,
    pub quantization_beats: f64,
    /// Loop length in beats from longest active sequence (null if no sequences).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub loop_beats: Option<f64>,
    /// Current beat position within the loop (current_beat % loop_beats).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub loop_beat: Option<f64>,
    /// Server timestamp when this state was captured (milliseconds since Unix epoch).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub server_time_ms: Option<u64>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TimeSignature {
    pub numerator: u8,
    pub denominator: u8,
}

#[derive(Debug, Deserialize)]
pub struct TransportUpdate {
    pub bpm: Option<f64>,
    pub time_signature: Option<TimeSignature>,
    pub quantization_beats: Option<f64>,
}

#[derive(Debug, Deserialize)]
pub struct SeekRequest {
    pub beat: f64,
}

// =============================================================================
// Groups
// =============================================================================

#[derive(Debug, Serialize)]
pub struct Group {
    pub name: String,
    pub path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_path: Option<String>,
    pub children: Vec<String>,
    pub node_id: i32,
    pub audio_bus: i32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub link_synth_node_id: Option<i32>,
    pub muted: bool,
    pub soloed: bool,
    pub params: HashMap<String, f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub synth_node_ids: Option<Vec<i32>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_location: Option<SourceLocation>,
}

#[derive(Debug, Deserialize)]
pub struct GroupCreate {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_path: Option<String>,
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

#[derive(Debug, Deserialize)]
pub struct GroupUpdate {
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

#[derive(Debug, Deserialize)]
pub struct ParamSet {
    pub value: f32,
    pub fade_beats: Option<f64>,
}

// =============================================================================
// Voices
// =============================================================================

#[derive(Debug, Serialize)]
pub struct Voice {
    pub name: String,
    pub synth_name: String,
    pub polyphony: u8,
    pub gain: f32,
    pub group_path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_bus: Option<i32>,
    pub muted: bool,
    pub soloed: bool,
    pub params: HashMap<String, f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sfz_instrument: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vst_instrument: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_notes: Option<Vec<u8>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sustained_notes: Option<Vec<u8>>,
    pub running: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub running_node_id: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_location: Option<SourceLocation>,
}

#[derive(Debug, Deserialize)]
pub struct VoiceCreate {
    pub name: Option<String>,
    #[serde(alias = "synthdef")]
    pub synth_name: Option<String>,
    pub polyphony: Option<u8>,
    pub gain: Option<f32>,
    #[serde(alias = "group_id")]
    pub group_path: Option<String>,
    #[serde(default)]
    pub params: HashMap<String, f32>,
    pub sample: Option<String>,
    pub sfz: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct VoiceUpdate {
    pub synth_name: Option<String>,
    pub polyphony: Option<u8>,
    pub gain: Option<f32>,
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

#[derive(Debug, Deserialize)]
pub struct TriggerRequest {
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

#[derive(Debug, Deserialize)]
pub struct NoteOnRequest {
    pub note: u8,
    #[serde(default = "default_velocity")]
    pub velocity: f32,
}

fn default_velocity() -> f32 {
    0.8
}

#[derive(Debug, Deserialize)]
pub struct NoteOffRequest {
    pub note: u8,
}

// =============================================================================
// Loop Status (for patterns and melodies)
// =============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LoopState {
    Stopped,
    Queued,
    Playing,
    QueuedStop,
}

#[derive(Debug, Clone, Serialize)]
pub struct LoopStatus {
    pub state: LoopState,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_beat: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_beat: Option<f64>,
}

// =============================================================================
// Patterns
// =============================================================================

#[derive(Debug, Serialize)]
pub struct Pattern {
    pub name: String,
    pub voice_name: String,
    pub group_path: String,
    pub loop_beats: f64,
    pub events: Vec<PatternEvent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<HashMap<String, f32>>,
    pub status: LoopStatus,
    pub is_looping: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_location: Option<SourceLocation>,
    /// Original step pattern string (e.g., "x..x..x.|x.x.x.x.") for visual editing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_pattern: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PatternEvent {
    pub beat: f64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub params: Option<HashMap<String, f32>>,
}

#[derive(Debug, Deserialize)]
pub struct PatternUpdate {
    pub events: Option<Vec<PatternEvent>>,
    pub pattern_string: Option<String>,
    pub loop_beats: Option<f64>,
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

#[derive(Debug, Deserialize)]
pub struct PatternCreate {
    /// Pattern name/ID.
    pub name: String,
    /// Voice name to trigger.
    pub voice_name: String,
    /// Loop length in beats.
    #[serde(default = "default_loop_beats")]
    pub loop_beats: f64,
    /// Pattern events.
    #[serde(default)]
    pub events: Vec<PatternEvent>,
    /// Step pattern string (e.g., "x..x..x.").
    pub pattern_string: Option<String>,
    /// Default parameters for all events.
    #[serde(default)]
    pub params: HashMap<String, f32>,
    /// Swing amount (0.0 to 1.0).
    #[serde(default)]
    pub swing: f32,
}

fn default_loop_beats() -> f64 {
    4.0
}

#[derive(Debug, Deserialize)]
pub struct StartRequest {
    pub quantize_beats: Option<f64>,
}

#[derive(Debug, Deserialize)]
pub struct StopRequest {
    pub quantize_beats: Option<f64>,
}

// =============================================================================
// Melodies
// =============================================================================

#[derive(Debug, Serialize)]
pub struct Melody {
    pub name: String,
    pub voice_name: String,
    pub group_path: String,
    pub loop_beats: f64,
    pub events: Vec<MelodyEvent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<HashMap<String, f32>>,
    pub status: LoopStatus,
    pub is_looping: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_location: Option<SourceLocation>,
    /// Notes pattern strings for visual editing (one per lane).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub notes_patterns: Option<Vec<String>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MelodyEvent {
    pub beat: f64,
    pub note: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub velocity: Option<f32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub params: Option<HashMap<String, f32>>,
}

#[derive(Debug, Deserialize)]
pub struct MelodyCreate {
    /// Melody name/ID.
    pub name: String,
    /// Voice name to trigger notes on.
    pub voice_name: String,
    /// Loop length in beats.
    #[serde(default = "default_loop_beats")]
    pub loop_beats: f64,
    /// Melody events (notes with beat positions).
    #[serde(default)]
    pub events: Vec<MelodyEvent>,
    /// Melody string notation (e.g., "C4 D4 E4").
    pub melody_string: Option<String>,
    /// Default parameters for all notes.
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

#[derive(Debug, Deserialize)]
pub struct MelodyUpdate {
    pub events: Option<Vec<MelodyEvent>>,
    pub melody_string: Option<String>,
    pub lanes: Option<Vec<String>>,
    pub loop_beats: Option<f64>,
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

// =============================================================================
// Sequences
// =============================================================================

#[derive(Debug, Serialize)]
pub struct Sequence {
    pub name: String,
    pub loop_beats: f64,
    pub clips: Vec<SequenceClip>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub play_once: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_location: Option<SourceLocation>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SequenceClip {
    #[serde(rename = "type")]
    pub clip_type: String,
    pub name: String,
    pub start_beat: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_beat: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub duration_beats: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub once: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct SequenceCreate {
    /// Sequence name/ID.
    pub name: String,
    /// Loop length in beats.
    #[serde(default = "default_sequence_length")]
    pub loop_beats: f64,
    /// Clips in the sequence (patterns, melodies, fades, nested sequences).
    #[serde(default)]
    pub clips: Vec<SequenceClip>,
}

fn default_sequence_length() -> f64 {
    16.0
}

#[derive(Debug, Deserialize)]
pub struct SequenceUpdate {
    pub loop_beats: Option<f64>,
    pub clips: Option<Vec<SequenceClip>>,
}

#[derive(Debug, Deserialize)]
pub struct SequenceStartRequest {
    #[serde(default)]
    pub play_once: bool,
}

// =============================================================================
// Effects
// =============================================================================

#[derive(Debug, Serialize)]
pub struct Effect {
    pub id: String,
    pub synthdef_name: String,
    pub group_path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub node_id: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bus_in: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bus_out: Option<i32>,
    pub params: HashMap<String, f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vst_plugin: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_location: Option<SourceLocation>,
}

#[derive(Debug, Deserialize)]
pub struct EffectCreate {
    pub id: Option<String>,
    pub synthdef_name: String,
    pub group_path: String,
    #[serde(default)]
    pub params: HashMap<String, f32>,
    pub position: Option<i32>,
}

#[derive(Debug, Deserialize)]
pub struct EffectUpdate {
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

// =============================================================================
// Samples
// =============================================================================

#[derive(Debug, Serialize)]
pub struct SampleSlice {
    pub name: String,
    pub start_frame: i64,
    pub end_frame: i64,
}

#[derive(Debug, Serialize)]
pub struct Sample {
    pub id: String,
    pub path: String,
    pub buffer_id: i32,
    pub num_channels: i32,
    pub num_frames: i64,
    pub sample_rate: f32,
    pub synthdef_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slices: Option<Vec<SampleSlice>>,
}

#[derive(Debug, Deserialize)]
pub struct SampleLoad {
    /// Path to the audio file.
    pub path: String,
    /// Optional sample ID (auto-generated if not provided).
    pub id: Option<String>,
}

// =============================================================================
// SynthDefs
// =============================================================================

#[derive(Debug, Serialize)]
pub struct SynthDefParam {
    pub name: String,
    pub default_value: f32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_value: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_value: Option<f32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SynthDefSource {
    Builtin,
    User,
    Stdlib,
}

#[derive(Debug, Serialize)]
pub struct SynthDefInfo {
    pub name: String,
    pub params: Vec<SynthDefParam>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<SynthDefSource>,
}

// =============================================================================
// Fades
// =============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FadeTargetType {
    Group,
    Voice,
    Effect,
}

#[derive(Debug, Serialize)]
pub struct ActiveFade {
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub target_type: FadeTargetType,
    pub target_name: String,
    pub param_name: String,
    pub start_value: f32,
    pub target_value: f32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current_value: Option<f32>,
    pub duration_beats: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_beat: Option<f64>,
    pub progress: f64,
}

#[derive(Debug, Deserialize)]
pub struct FadeCreate {
    pub target_type: FadeTargetType,
    pub target_name: String,
    pub param_name: String,
    pub start_value: Option<f32>,
    pub target_value: f32,
    pub duration_beats: f64,
}

// =============================================================================
// Live State
// =============================================================================

#[derive(Debug, Serialize)]
pub struct ActiveSynth {
    pub node_id: i32,
    pub synthdef_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group_path: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at_beat: Option<f64>,
}

#[derive(Debug, Serialize)]
pub struct ActiveSequence {
    pub name: String,
    pub start_beat: f64,
    pub current_position: f64,
    pub loop_beats: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub iteration: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub play_once: Option<bool>,
}

#[derive(Debug, Serialize)]
pub struct LiveState {
    pub transport: TransportState,
    pub active_synths: Vec<ActiveSynth>,
    pub active_sequences: Vec<ActiveSequence>,
    pub active_fades: Vec<ActiveFade>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub active_notes: Option<HashMap<String, Vec<u8>>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub patterns_status: Option<HashMap<String, LoopStatus>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub melodies_status: Option<HashMap<String, LoopStatus>>,
}

// =============================================================================
// Metering
// =============================================================================

#[derive(Debug, Serialize)]
pub struct MeterLevel {
    pub peak_left: f32,
    pub peak_right: f32,
    pub rms_left: f32,
    pub rms_right: f32,
}

/// MeterLevels is a map from group path to meter level.
pub type MeterLevels = HashMap<String, MeterLevel>;

// =============================================================================
// Error Response
// =============================================================================

#[derive(Debug, Serialize)]
pub struct ErrorResponse {
    pub error: String,
    pub message: String,
}

impl ErrorResponse {
    pub fn new(error: &str, message: &str) -> Self {
        Self {
            error: error.to_string(),
            message: message.to_string(),
        }
    }

    pub fn not_found(message: &str) -> Self {
        Self::new("not_found", message)
    }

    pub fn bad_request(message: &str) -> Self {
        Self::new("bad_request", message)
    }

    pub fn internal(message: &str) -> Self {
        Self::new("internal_error", message)
    }
}