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
use crate::{concat_c_strs, ReaperStringArg};
use c_str_macro::c_str;

use std::borrow::Cow;
use std::ffi::CStr;

/// Track attribute key which you can pass to [`get_set_media_track_info()`].
///
/// [`get_set_media_track_info()`]: struct.ReaperFunctions.html#method.get_set_media_track_info
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum TrackAttributeKey<'a> {
    /// Parent track (read-only).
    ///
    /// `*mut MediaTrack`
    ParTrack,
    /// Parent project (read-only).
    ///
    /// `*mut ReaProject`
    Project,
    /// Track name (on master returns `null_mut()`).
    ///
    /// `*mut char`
    Name,
    /// Track icon.
    ///
    /// `*const char`
    ///
    /// Full file name or relative to resource path / data / track icons.
    Icon,
    /// Layout name.
    ///
    /// `*const char`
    McpLayout,
    /// Layout name.
    ///
    /// `*const char`
    TcpLayout,
    /// Extension-specific persistent data.
    ///
    /// `*mut char`
    ///
    /// Use [`ext()`] to create this variant.
    ///
    /// [`ext()`]: #method.ext
    Ext(Cow<'a, CStr>),
    /// 6-byte GUID, can query or update.
    ///
    /// `*mut GUID`
    ///
    /// If using a `_string()` function, GUID is a string `{xyz-...}`.
    Guid,
    /// Muted.
    ///
    /// `*mut bool`
    Mute,
    /// Track phase inverted.
    ///
    /// `*mut bool`
    Phase,
    /// Track number
    ///
    /// `i32`
    ///
    /// 1-based, read-only, returns the i32 directly.
    ///
    /// - 0 → not found
    /// - -1 → master track
    TrackNumber,
    /// Soloed.
    ///
    /// `*mut i32`
    ///
    /// - 0 → not soloed
    /// - 1 → soloed
    /// - 2 → soloed in place
    /// - 5 → safe soloed
    /// - 6 → safe soloed in place
    Solo,
    /// FX enabled.
    ///
    /// `*mut i32`
    ///
    /// - 0 → bypassed
    /// - != 0 → FX active
    FxEn,
    /// Record armed.
    ///
    /// `*mut i32`
    ///
    /// - 0 → not record armed
    /// - 1 → record armed
    RecArm,
    /// Record input.
    ///
    /// `*mut i32`
    ///
    /// - <0 → no input
    /// - 0..=n → mono hardware input
    /// - 512 + n → rearoute input
    /// - &1024 → stereo input pair
    /// - &4096 → MIDI input, if set then low 5 bits represent channel (0 → all, 1 - 16 → only
    ///   channel), next 6 bits represent physical input (63 → all, 62 → VKB)
    RecInput,
    /// Record mode.
    ///
    /// `*mut i32`
    ///
    /// - 0 → input
    /// - 1 → stereo out
    /// - 2 → none
    /// - 3 → stereo out with latency compensation
    /// - 4 → midi output
    /// - 5 → mono out
    /// - 6 → mono out with latency compensation
    /// - 7 → MIDI overdub
    /// - 8 → MIDI replace
    RecMode,
    /// Record monitoring.
    ///
    /// `*mut i32`
    ///
    /// - 0 → off
    /// - 1 → normal
    /// - 2 → not when playing (tape style)
    RecMon,
    /// Monitor items while recording.
    ///
    /// `*mut i32`
    ///
    /// - 0 → off
    /// - 1 → on
    RecMonItems,
    /// Track automation mode.
    ///
    /// `*mut i32`
    ///
    /// - 0 → trim/off
    /// - 1 → read
    /// - 2 → touch
    /// - 3 → write
    /// - 4 → latch
    AutoMode,
    /// Number of track channels.
    ///
    /// `*mut i32`
    ///
    /// 2 - 64, even numbers only.
    Nchan,
    /// Track selected.
    ///
    /// `*mut i32`
    ///
    /// - 0 → unselected
    /// - 1 → selected
    Selected,
    /// Current TCP window height in pixels including envelopes (read-only).
    ///
    /// `*mut i32`
    WndH,
    /// Current TCP window height in pixels not including envelopes (read-only).
    ///
    /// `*mut i32`
    TcpH,
    /// Current TCP window Y-position in pixels relative to top of arrange view (read-only).
    ///
    /// `*mut i32`
    TcpY,
    /// Current MCP X-position in pixels relative to mixer container.
    ///
    /// `*mut i32`
    McpX,
    /// Current MCP Y-position in pixels relative to mixer container.
    ///
    /// `*mut i32`
    McpY,
    /// Current MCP width in pixels.
    ///
    /// `*mut i32`
    McpW,
    /// Current MCP height in pixels.
    ///
    /// `*mut i32`
    McpH,
    /// Folder depth change.
    ///
    /// `*mut i32`
    ///
    /// - 0 → normal
    /// - 1 → track is a folder parent
    /// - -1 → track is the last in the innermost folder
    /// - -2 → track is the last in the innermost and next-innermost folders
    /// - ...
    FolderDepth,
    /// Folder compacted state (only valid on folders).
    ///
    /// `*mut i32`
    ///
    /// - 0 → normal
    /// - 1 → small
    /// - 2 → tiny children
    FolderCompact,
    /// Track midi hardware output index.
    ///
    /// `*mut i32`
    ///
    /// Low 5 bits are which channels (1..=16, 0 → all), next 5 bits are output device index
    /// (0..=31). < 0 means disabled.
    MidiHwOut,
    /// Track performance flags.
    ///
    /// `*mut i32`
    ///
    /// &1 → no media buffering
    /// &2 → no anticipative FX
    PerfFlags,
    /// Custom color.
    ///
    /// `*mut i32`
    ///
    /// `<OS dependent color> | 0x100000` (i.e. `ColorToNative(r, g, b) | 0x100000`).
    /// If you don't do `| 0x100000`, then it will not be used, but will store the color anyway.
    CustomColor,
    /// Custom height override for TCP window.
    ///
    /// `*mut i32`
    ///
    /// 0 for none, otherwise size in pixels.
    HeightOverride,
    /// Track height lock.
    ///
    /// `*mut bool`
    ///
    /// Must set [`HeightOverride`] before locking.
    ///
    /// [`HeightOverride`]: #variant.HeightOverride
    HeightLock,
    /// Trim volume of track.
    ///
    /// `*mut f64`
    ///
    /// - 0 → -inf
    /// - 0.5 → -6dB
    /// - 1 → +0dB
    /// - 2 → +6dB
    /// - ...
    Vol,
    /// Trim pan of track
    ///
    /// `*mut f64`
    ///
    /// -1..=1.
    Pan,
    /// Width of track
    ///
    /// `*mut f64`
    ///
    /// -1..=1.
    Width,
    /// Dual pan position 1.
    ///
    /// `*mut f64`
    ///
    /// -1..=1, only if [`PanMode`] == 6.
    ///
    /// [`PanMode`]: #variant.PanMode
    DualPanL,
    /// Dual pan position 2.
    ///
    /// `*mut f64`
    ///
    /// -1..=1, only if [`PanMode`] == 6.
    ///
    /// [`PanMode`]: #variant.PanMode
    DualPanR,
    /// Pan mode.
    ///
    /// `*mut i32`
    ///
    /// - 0 → classic 3.x
    /// - 3 → new balance
    /// - 5 → stereo pan
    /// - 6 → dual pan
    PanMode,
    /// Pan law.
    ///
    /// `*mut f64`
    ///
    /// - < 0 → project default
    /// - 1 → +0 dB
    /// - ...
    PanLaw,
    /// TrackEnvelope (read only).
    ///
    /// `*mut TrackEnvelope`
    Env(EnvChunkName<'a>),
    /// Track control panel visible in mixer.
    ///
    /// `*mut bool`
    ///
    /// Do not use on master track.
    ShowInMixer,
    /// Track control panel visible in arrange view.
    ///
    /// `*mut bool`
    ///
    /// Do not use on master track.
    ShowInTcp,
    /// Track sends audio to parent.
    ///
    /// `*mut bool`
    MainSend,
    /// Channel offset of track send to parent.
    ///
    /// `*mut char`
    MainSendOffs,
    /// Track free item positioning enabled
    ///
    /// `*mut bool`
    ///
    /// Call [`update_timeline`] after changing.
    ///
    /// [`update_timeline`]: struct.Reaper.html#method.update_timeline
    FreeMode,
    /// Track timebase.
    ///
    /// `*mut char`
    ///
    /// - -1 → project default
    /// - 0 → time
    /// - 1 → beats (position, length, rate)
    /// - 2 → beats (position only)
    BeatAttachMode,
    /// Scale of FX and send area in MCP.
    ///
    /// `*mut f32`
    ///
    /// - 0 → minimum allowed
    /// - 1 → maximum allowed
    McpFxSendScale,
    /// Scale of send area as proportion of the FX and send total area.
    ///
    /// `*mut f32`
    ///
    /// - 0 → minimum allowed
    /// - 1 → maximum allowed
    McpSendRgnScale,
    /// Track playback offset state.
    ///
    /// `*mut i32`
    ///
    /// - &1 → bypassed
    /// - &2 → offset
    ///
    /// Value is measured in samples (otherwise measured in seconds).
    PlayOffsetFlag,
    /// Track playback offset.
    ///
    /// `*mut f64`
    ///
    /// Units depend on [`PlayOffsetFlag`].
    ///
    /// [`PlayOffsetFlag`]: #variant.PlayOffsetFlag
    PlayOffset,
    /// If a variant is missing in this enum, you can use this custom one as a resort.
    ///
    /// Use [`custom()`] to create this variant.
    ///
    /// [`custom()`]: #method.custom
    Custom(Cow<'a, CStr>),
}

impl<'a> TrackAttributeKey<'a> {
    /// Convenience function for creating an [`Ext`] key.
    ///
    /// [`Ext`]: #variant.Ext
    pub fn ext(key: impl Into<ReaperStringArg<'a>>) -> TrackAttributeKey<'a> {
        TrackAttributeKey::Ext(key.into().into_inner())
    }

    /// Convenience function for creating a [`Custom`] key.
    ///
    /// [`Custom`]: #variant.Custom
    pub fn custom(key: impl Into<ReaperStringArg<'a>>) -> TrackAttributeKey<'a> {
        TrackAttributeKey::Custom(key.into().into_inner())
    }

    pub(crate) fn into_raw(self) -> Cow<'a, CStr> {
        use TrackAttributeKey::*;
        match self {
            FreeMode => c_str!("B_FREEMODE").into(),
            HeightLock => c_str!("B_HEIGHTLOCK").into(),
            MainSend => c_str!("B_MAINSEND").into(),
            Mute => c_str!("B_MUTE").into(),
            Phase => c_str!("B_PHASE").into(),
            ShowInMixer => c_str!("B_SHOWINMIXER").into(),
            ShowInTcp => c_str!("B_SHOWINTCP").into(),
            BeatAttachMode => c_str!("C_BEATATTACHMODE").into(),
            MainSendOffs => c_str!("C_MAINSEND_OFFS").into(),
            DualPanL => c_str!("D_DUALPANL").into(),
            DualPanR => c_str!("D_DUALPANR").into(),
            Pan => c_str!("D_PAN").into(),
            PanLaw => c_str!("D_PANLAW").into(),
            PlayOffset => c_str!("D_PLAY_OFFSET").into(),
            Vol => c_str!("D_VOL").into(),
            Width => c_str!("D_WIDTH").into(),
            McpFxSendScale => c_str!("F_MCP_FXSEND_SCALE").into(),
            McpSendRgnScale => c_str!("F_MCP_SENDRGN_SCALE").into(),
            Guid => c_str!("GUID").into(),
            AutoMode => c_str!("I_AUTOMODE").into(),
            CustomColor => c_str!("I_CUSTOMCOLOR").into(),
            FolderCompact => c_str!("I_FOLDERCOMPACT").into(),
            FolderDepth => c_str!("I_FOLDERDEPTH").into(),
            FxEn => c_str!("I_FXEN").into(),
            HeightOverride => c_str!("I_HEIGHTOVERRIDE").into(),
            McpH => c_str!("I_MCPH").into(),
            McpW => c_str!("I_MCPW").into(),
            McpX => c_str!("I_MCPX").into(),
            McpY => c_str!("I_MCPY").into(),
            MidiHwOut => c_str!("I_MIDIHWOUT").into(),
            Nchan => c_str!("I_NCHAN").into(),
            PanMode => c_str!("I_PANMODE").into(),
            PerfFlags => c_str!("I_PERFFLAGS").into(),
            PlayOffsetFlag => c_str!("I_PLAY_OFFSET_FLAG").into(),
            RecArm => c_str!("I_RECARM").into(),
            RecInput => c_str!("I_RECINPUT").into(),
            RecMode => c_str!("I_RECMODE").into(),
            RecMon => c_str!("I_RECMON").into(),
            RecMonItems => c_str!("I_RECMONITEMS").into(),
            Selected => c_str!("I_SELECTED").into(),
            Solo => c_str!("I_SOLO").into(),
            TcpH => c_str!("I_TCPH").into(),
            TcpY => c_str!("I_TCPY").into(),
            WndH => c_str!("I_WNDH").into(),
            TrackNumber => c_str!("IP_TRACKNUMBER").into(),
            Env(env_chunk_name) => {
                concat_c_strs(c_str!("P_ENV:<"), env_chunk_name.into_raw().as_ref()).into()
            }
            Ext(extension_specific_key) => {
                concat_c_strs(c_str!("P_EXT:"), extension_specific_key.as_ref()).into()
            }
            Icon => c_str!("P_ICON").into(),
            McpLayout => c_str!("P_MCP_LAYOUT").into(),
            Name => c_str!("P_NAME").into(),
            ParTrack => c_str!("P_PARTRACK").into(),
            Project => c_str!("P_PROJECT").into(),
            TcpLayout => c_str!("P_TCP_LAYOUT").into(),
            Custom(key) => key,
        }
    }
}

/// Track send attribute key which you can pass to [`get_set_track_send_info()`].
///
/// [`get_set_track_send_info()`]: struct.ReaperFunctions.html#method.get_set_track_send_info
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum TrackSendAttributeKey<'a> {
    /// Destination track (read-only).
    ///
    /// `*mut MediaTrack`
    ///
    /// Only applies for sends/receives.
    DestTrack,
    /// Source track (read-only).
    ///
    /// `*mut MediaTrack`
    ///
    /// Only applies for sends/receives.
    SrcTrack,
    /// Corresponding track send envelope.
    ///
    /// `*mut TrackEnvelope`
    Env(EnvChunkName<'a>),
    /// Extension-specific persistent data.
    ///
    /// `*mut char`
    ///
    /// Use [`ext()`] to create this variant.
    ///
    /// [`ext()`]: #method.ext
    Ext(Cow<'a, CStr>),
    /// Muted.
    ///
    /// `*mut bool`
    Mute,
    /// Phase.
    ///
    /// `*mut bool`
    ///
    /// `true` to flip phase.
    Phase,
    /// Mono.
    ///
    /// `*mut bool`
    Mono,
    /// Volume.
    ///
    /// `*mut f64`
    ///
    /// 1.0 → +0 dB etc.
    Vol,
    /// Pan.
    ///
    /// `*mut f64`
    ///
    /// -1..=1
    Pan,
    /// Pan law.
    ///
    /// `*mut f64`
    ///
    /// - 1.0 → +0.0 dB
    /// - 0.5 → -6 dB
    /// - -1.0 → value defined in project
    PanLaw,
    /// Send mode.
    ///
    /// `*mut i32`
    ///
    /// - 0 → post-fader
    /// - 1 → pre-fx
    /// - 2 → post-fx (deprecated)
    /// - 3 → post-fx
    SendMode,
    /// Automation mode.
    ///
    /// `*mut i32`
    ///
    /// - -1 → use track automation mode
    /// - 0 → trim/off
    /// - 1 → read
    /// - 2 → touch
    /// - 3 → write
    /// - 4 → latch
    AutoMode,
    /// Source channel.
    ///
    /// `*mut i32`
    ///
    /// Index, &1024 → mono, -1 → none
    SrcChan,
    /// Destination channel.
    ///
    /// `*mut i32`
    /// Index, &1024 → mono, otherwise stereo pair, hwout: &512 → rearoute
    DstChan,
    /// MIDI flags.
    ///
    /// `*mut i32`
    ///
    /// - Low 5 bits → source channel (0 → all, 1..=16)
    /// - Next 5 bits → destination channel (0 → original, 1..=16)
    MidiFlags,
    /// If a variant is missing in this enum, you can use this custom one as a resort.
    ///
    /// Use [`custom()`] to create this variant.
    ///
    /// [`custom()`]: #method.custom
    Custom(Cow<'a, CStr>),
}

impl<'a> TrackSendAttributeKey<'a> {
    /// Convenience function for creating an [`Ext`] key.
    ///
    /// [`Ext`]: #variant.Ext
    pub fn ext(key: impl Into<ReaperStringArg<'a>>) -> TrackSendAttributeKey<'a> {
        TrackSendAttributeKey::Ext(key.into().into_inner())
    }

    /// Convenience function for creating a [`Custom`] key.
    ///
    /// [`Custom`]: #variant.Custom
    pub fn custom(key: impl Into<ReaperStringArg<'a>>) -> TrackSendAttributeKey<'a> {
        TrackSendAttributeKey::Custom(key.into().into_inner())
    }

    pub(crate) fn into_raw(self) -> Cow<'a, CStr> {
        use TrackSendAttributeKey::*;
        match self {
            Mono => c_str!("B_MONO").into(),
            Mute => c_str!("B_MUTE").into(),
            Phase => c_str!("B_PHASE").into(),
            Pan => c_str!("D_PAN").into(),
            PanLaw => c_str!("D_PANLAW").into(),
            Vol => c_str!("D_VOL").into(),
            AutoMode => c_str!("I_AUTOMODE").into(),
            DstChan => c_str!("I_DSTCHAN").into(),
            MidiFlags => c_str!("I_MIDIFLAGS").into(),
            SendMode => c_str!("I_SENDMODE").into(),
            SrcChan => c_str!("I_SRCCHAN").into(),
            DestTrack => c_str!("P_DESTTRACK").into(),
            SrcTrack => c_str!("P_SRCTRACK").into(),
            Env(env_chunk_name) => {
                concat_c_strs(c_str!("P_ENV:<"), env_chunk_name.into_raw().as_ref()).into()
            }
            Ext(key) => concat_c_strs(c_str!("P_EXT:"), key.as_ref()).into(),
            Custom(key) => key.into(),
        }
    }
}

/// Envelope chunk name which you can pass e.g. to [`TrackAttributeKey::Env()`].
///
/// [`TrackAttributeKey::Env()`]: enum.TrackAttributeKey.html#variant.Env
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum EnvChunkName<'a> {
    /// Volume (Pre-FX)
    VolEnv,
    /// Pan (Pre-FX)
    PanEnv,
    /// Volume
    VolEnv2,
    /// Pan
    PanEnv2,
    /// Width (Pre-FX)
    WidthEnv,
    /// Width
    WidthEnv2,
    /// Trim Volume
    VolEnv3,
    /// Mute
    MuteEnv,
    /// Use this for all non-common envelope names.
    ///
    /// Use [`custom()`] to create this variant.
    ///
    /// [`custom()`]: #method.custom
    Custom(Cow<'a, CStr>),
}

impl<'a> EnvChunkName<'a> {
    /// Convenience function for creating a [`Custom`] name.
    ///
    /// [`Custom`]: #variant.Custom
    pub fn custom(name: impl Into<ReaperStringArg<'a>>) -> EnvChunkName<'a> {
        EnvChunkName::Custom(name.into().into_inner())
    }

    pub(crate) fn into_raw(self) -> Cow<'a, CStr> {
        use EnvChunkName::*;
        match self {
            VolEnv => c_str!("VOLENV").into(),
            PanEnv => c_str!("PANENV").into(),
            VolEnv2 => c_str!("VOLENV2").into(),
            PanEnv2 => c_str!("PANENV2").into(),
            WidthEnv => c_str!("WIDTHENV").into(),
            WidthEnv2 => c_str!("WIDTHENV2").into(),
            VolEnv3 => c_str!("VOLENV3").into(),
            MuteEnv => c_str!("MUTEENV").into(),
            Custom(name) => name,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serialize_track_attribute_key() {
        use TrackAttributeKey::*;
        assert_eq!(Mute.into_raw().as_ref(), c_str!("B_MUTE"));
        assert_eq!(
            Env(EnvChunkName::VolEnv).into_raw().as_ref(),
            c_str!("P_ENV:<VOLENV")
        );
        assert_eq!(
            Env(EnvChunkName::Custom(c_str!("MYENV").into()))
                .into_raw()
                .as_ref(),
            c_str!("P_ENV:<MYENV")
        );
        assert_eq!(
            TrackAttributeKey::ext("SWS_FOO").into_raw().as_ref(),
            c_str!("P_EXT:SWS_FOO")
        );
        assert_eq!(
            TrackAttributeKey::custom(c_str!("BLA")).into_raw().as_ref(),
            c_str!("BLA")
        );
    }
}