vibelang-http 0.1.1

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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! Request and response models for the HTTP API.

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<usize>,
    pub column: Option<usize>,
}

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

#[derive(Debug, Serialize)]
pub struct TransportState {
    pub bpm: f32,
    pub time_signature: TimeSignature,
    pub running: bool,
    pub current_beat: f64,
    pub quantization_beats: f64,
    /// The loop length in beats (from the longest active sequence), or None if no sequences.
    /// UI can use this to calculate loop position for display.
    pub loop_beats: Option<f64>,
    /// The current beat position within the loop (current_beat % loop_beats).
    /// None if no loop is active.
    pub loop_beat: Option<f64>,
    /// Server timestamp when this state was captured (milliseconds since Unix epoch).
    /// Clients can use this to compensate for network latency.
    pub server_time_ms: u64,
}

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

#[derive(Debug, Deserialize)]
pub struct TransportUpdate {
    pub bpm: Option<f32>,
    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,
    pub parent_path: Option<String>,
    pub children: Vec<String>,
    pub node_id: Option<i32>,
    pub audio_bus: i32,
    pub link_synth_node_id: Option<i32>,
    pub muted: bool,
    pub soloed: bool,
    pub params: HashMap<String, f32>,
    pub synth_node_ids: Vec<i32>,
    pub source_location: Option<SourceLocation>,
}

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

fn default_main() -> String {
    "main".to_string()
}

#[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: usize,
    pub gain: f32,
    pub group_path: String,
    pub group_name: String,
    pub output_bus: Option<i32>,
    pub muted: bool,
    pub soloed: bool,
    pub params: HashMap<String, f32>,
    pub sfz_instrument: Option<String>,
    pub vst_instrument: Option<String>,
    pub active_notes: Vec<u8>,
    pub sustained_notes: Vec<u8>,
    pub running: bool,
    pub running_node_id: Option<i32>,
    pub source_location: Option<SourceLocation>,
}

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

fn default_polyphony() -> usize {
    8
}

fn default_gain() -> f32 {
    1.0
}

#[derive(Debug, Deserialize)]
pub struct VoiceUpdate {
    pub synth_name: Option<String>,
    pub polyphony: Option<usize>,
    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: u8,
}

fn default_velocity() -> u8 {
    100
}

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

// =============================================================================
// 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>,
    pub params: HashMap<String, f32>,
    pub status: LoopStatus,
    pub is_looping: bool,
    pub source_location: Option<SourceLocation>,
    /// Original step pattern string (e.g., "x..x..x.|x.x.x.x.") for visual editing
    pub step_pattern: Option<String>,
}

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

#[derive(Debug, Serialize)]
pub struct LoopStatus {
    pub state: String,
    pub start_beat: Option<f64>,
    pub stop_beat: Option<f64>,
}

#[derive(Debug, Deserialize)]
pub struct PatternCreate {
    pub name: String,
    pub voice_name: String,
    pub group_path: Option<String>,
    #[serde(default = "default_loop_beats")]
    pub loop_beats: f64,
    #[serde(default)]
    pub events: Vec<PatternEvent>,
    pub pattern_string: Option<String>,
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

fn default_loop_beats() -> f64 {
    4.0
}

#[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 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>,
    pub params: HashMap<String, f32>,
    pub status: LoopStatus,
    pub is_looping: bool,
    pub source_location: Option<SourceLocation>,
    /// Original notes pattern strings for visual editing (one per lane).
    /// Multiple lanes support polyphonic melodies.
    pub notes_patterns: Vec<String>,
}

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

#[derive(Debug, Deserialize)]
pub struct MelodyCreate {
    pub name: String,
    pub voice_name: String,
    pub group_path: Option<String>,
    #[serde(default = "default_loop_beats")]
    pub loop_beats: f64,
    #[serde(default)]
    pub events: Vec<MelodyEvent>,
    /// Single melody string (backward compatible).
    pub melody_string: Option<String>,
    /// Multiple lanes for polyphonic melodies.
    pub lanes: Option<Vec<String>>,
    #[serde(default)]
    pub params: HashMap<String, f32>,
}

#[derive(Debug, Deserialize)]
pub struct MelodyUpdate {
    pub events: Option<Vec<MelodyEvent>>,
    /// Single melody string (backward compatible, adds to lanes).
    pub melody_string: Option<String>,
    /// Multiple lanes for polyphonic melodies.
    /// If provided, replaces all existing lanes.
    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>,
    pub play_once: bool,
    pub active: bool,
    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,
    pub end_beat: f64,
    pub mode: String,
}

#[derive(Debug, Deserialize)]
pub struct SequenceCreate {
    pub name: String,
    #[serde(default = "default_sequence_loop_beats")]
    pub loop_beats: f64,
    #[serde(default)]
    pub clips: Vec<SequenceClip>,
}

fn default_sequence_loop_beats() -> 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,
    pub node_id: Option<i32>,
    pub bus_in: Option<i32>,
    pub bus_out: Option<i32>,
    pub params: HashMap<String, f32>,
    pub position: usize,
    pub vst_plugin: Option<String>,
    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<usize>,
}

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

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

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

#[derive(Debug, Serialize)]
pub struct SampleSlice {
    pub index: usize,
    pub start_frame: i32,
    pub end_frame: i32,
    pub synthdef_name: String,
}

#[derive(Debug, Deserialize)]
pub struct SampleLoad {
    pub id: Option<String>,
    pub path: String,
}

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

#[derive(Debug, Serialize)]
pub struct SynthDef {
    pub name: String,
    pub params: Vec<SynthDefParam>,
    pub source: String,
}

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

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

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

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

// =============================================================================
// MIDI
// =============================================================================

#[derive(Debug, Clone, Serialize)]
pub struct MidiDeviceInfo {
    pub name: String,
    pub port_index: usize,
    pub backend: String,
}

#[derive(Debug, Serialize)]
pub struct MidiDeviceState {
    pub id: u32,
    pub info: MidiDeviceInfo,
    pub backend: String,
}

#[derive(Debug, Serialize)]
pub struct MidiDevicesResponse {
    pub available: Vec<MidiDeviceInfo>,
    pub connected: Vec<MidiDeviceState>,
}

#[derive(Debug, Deserialize)]
pub struct MidiConnectRequest {
    #[serde(default = "default_alsa")]
    pub backend: String,
}

fn default_alsa() -> String {
    "alsa".to_string()
}

#[derive(Debug, Serialize)]
pub struct MidiRouting {
    pub keyboard_routes: Vec<KeyboardRoute>,
    pub note_routes: Vec<NoteRoute>,
    pub cc_routes: Vec<CcRoute>,
    pub pitch_bend_routes: Vec<CcRoute>,
    pub aftertouch_routes: Vec<CcRoute>,
    pub choke_groups: HashMap<String, Vec<String>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct KeyboardRoute {
    pub channel: Option<u8>,
    pub voice_name: String,
    #[serde(default)]
    pub transpose: i32,
    #[serde(default = "default_velocity_curve")]
    pub velocity_curve: String,
    #[serde(default)]
    pub note_range_low: u8,
    #[serde(default = "default_note_high")]
    pub note_range_high: u8,
}

fn default_velocity_curve() -> String {
    "linear".to_string()
}

fn default_note_high() -> u8 {
    127
}

#[derive(Debug, Serialize, Deserialize)]
pub struct NoteRoute {
    pub channel: u8,
    pub note: u8,
    pub voice_name: String,
    pub choke_group: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CcRoute {
    pub channel: u8,
    pub cc_number: u8,
    pub target_type: String,
    pub target_name: String,
    pub param_name: String,
    #[serde(default)]
    pub min_value: f32,
    #[serde(default = "default_gain")]
    pub max_value: f32,
}

#[derive(Debug, Serialize)]
pub struct MidiCallback {
    pub id: u64,
    pub callback_type: String,
    pub channel: Option<u8>,
    pub note: Option<u8>,
    pub cc_number: Option<u8>,
    pub threshold: Option<u8>,
    pub above_threshold: Option<bool>,
}

#[derive(Debug, Serialize)]
pub struct MidiRecordingState {
    pub recording_enabled: bool,
    pub quantization: u8,
    pub max_history_bars: u16,
    pub note_count: usize,
    pub oldest_beat: f64,
    pub pending_notes: usize,
}

#[derive(Debug, Deserialize)]
pub struct MidiRecordingUpdate {
    pub recording_enabled: Option<bool>,
    pub quantization: Option<u8>,
    pub max_history_bars: Option<u16>,
}

#[derive(Debug, Serialize)]
pub struct RecordedMidiNote {
    pub beat: f64,
    pub note: u8,
    pub velocity: u8,
    pub duration: f64,
    pub raw_beat: f64,
    pub channel: u8,
    pub voice_name: String,
}

#[derive(Debug, Deserialize)]
pub struct RecordedNotesQuery {
    pub start_beat: Option<f64>,
    pub end_beat: Option<f64>,
    pub voice: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct ExportQuery {
    pub start_beat: Option<f64>,
    pub bars: Option<u32>,
    pub voice: Option<String>,
    #[serde(default = "default_melody")]
    pub format: String,
}

fn default_melody() -> String {
    "melody".to_string()
}

#[derive(Debug, Deserialize)]
pub struct MonitorRequest {
    pub enabled: bool,
}

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

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

#[derive(Debug, Serialize)]
pub struct ActiveSynth {
    pub node_id: i32,
    pub synthdef_name: String,
    pub voice_name: Option<String>,
    pub group_path: Option<String>,
    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,
    pub iteration: u32,
    pub play_once: bool,
}

// =============================================================================
// Audio Metering
// =============================================================================

/// Audio meter level for a group (stereo).
#[derive(Debug, Clone, Serialize)]
pub struct MeterLevel {
    /// Peak level for left channel (0.0 to 1.0+, can exceed 1.0 for clipping).
    pub peak_left: f32,
    /// Peak level for right channel (0.0 to 1.0+).
    pub peak_right: f32,
    /// RMS level for left channel (0.0 to 1.0+).
    pub rms_left: f32,
    /// RMS level for right channel (0.0 to 1.0+).
    pub rms_right: f32,
}

// =============================================================================
// 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 conflict(message: &str) -> Self {
        Self::new("conflict", message)
    }

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