rea-rs-low 0.1.2

Bindings for the REAPER C++ API - low-level API
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
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use super::bindings::root::reaper_pcm_source::*;
use crate::{firewall, raw};
use std::os::raw::{c_char, c_int, c_void};
use std::ptr::{null, null_mut, NonNull};

impl raw::PCM_source {
    pub fn GetLength(&self) -> f64 {
        unsafe { rust_to_cpp_PCM_source_GetLength(self as *const _ as _) }
    }

    pub fn IsAvailable(&self) -> bool {
        unsafe { rust_to_cpp_PCM_source_IsAvailable(self as *const _ as _) }
    }

    pub fn Duplicate(&self) -> *mut raw::PCM_source {
        unsafe { rust_to_cpp_PCM_source_Duplicate(self as *const _ as _) }
    }

    pub fn GetType(&self) -> *const c_char {
        unsafe { rust_to_cpp_PCM_source_GetType(self as *const _ as _) }
    }

    pub fn GetFileName(&self) -> *const c_char {
        unsafe { rust_to_cpp_PCM_source_GetFileName(self as *const _ as _) }
    }

    pub fn GetSource(&self) -> *mut raw::PCM_source {
        unsafe { rust_to_cpp_PCM_source_GetSource(self as *const _ as _) }
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn Extended(
        &self,
        call: c_int,
        parm1: *mut c_void,
        parm2: *mut c_void,
        parm3: *mut c_void,
    ) -> c_int {
        rust_to_cpp_PCM_source_Extended(
            self as *const _ as _,
            call,
            parm1,
            parm2,
            parm3,
        )
    }

    pub fn SetAvailable(&self, avail: bool) {
        unsafe {
            rust_to_cpp_PCM_source_SetAvailable(self as *const _ as _, avail);
        }
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn SetFileName(
        &self,
        newfn: *const ::std::os::raw::c_char,
    ) -> bool {
        rust_to_cpp_PCM_source_SetFileName(self as *const _ as _, newfn)
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn SetSource(&self, src: *mut raw::PCM_source) {
        rust_to_cpp_PCM_source_SetSource(self as *const _ as _, src);
    }

    pub fn GetNumChannels(&self) -> ::std::os::raw::c_int {
        unsafe { rust_to_cpp_PCM_source_GetNumChannels(self as *const _ as _) }
    }

    pub fn GetSampleRate(&self) -> f64 {
        unsafe { rust_to_cpp_PCM_source_GetSampleRate(self as *const _ as _) }
    }

    pub fn GetLengthBeats(&self) -> f64 {
        unsafe { rust_to_cpp_PCM_source_GetLengthBeats(self as *const _ as _) }
    }

    pub fn GetBitsPerSample(&self) -> ::std::os::raw::c_int {
        unsafe {
            rust_to_cpp_PCM_source_GetBitsPerSample(self as *const _ as _)
        }
    }

    pub fn GetPreferredPosition(&self) -> f64 {
        unsafe {
            rust_to_cpp_PCM_source_GetPreferredPosition(self as *const _ as _)
        }
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn PropertiesWindow(
        &self,
        hwndParent: raw::HWND,
    ) -> ::std::os::raw::c_int {
        rust_to_cpp_PCM_source_PropertiesWindow(
            self as *const _ as _,
            hwndParent,
        )
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn GetSamples(&self, block: *mut raw::PCM_source_transfer_t) {
        rust_to_cpp_PCM_source_GetSamples(self as *const _ as _, block);
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn GetPeakInfo(
        &self,
        block: *mut raw::PCM_source_peaktransfer_t,
    ) {
        rust_to_cpp_PCM_source_GetPeakInfo(self as *const _ as _, block);
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn SaveState(&self, ctx: *mut raw::ProjectStateContext) {
        rust_to_cpp_PCM_source_SaveState(self as *const _ as _, ctx);
    }

    /// # Safety
    ///
    /// REAPER can crash if you pass an invalid pointer.
    pub unsafe fn LoadState(
        &self,
        firstline: *const ::std::os::raw::c_char,
        ctx: *mut raw::ProjectStateContext,
    ) -> ::std::os::raw::c_int {
        rust_to_cpp_PCM_source_LoadState(self as *const _ as _, firstline, ctx)
    }

    pub fn Peaks_Clear(&self, deleteFile: bool) {
        unsafe {
            rust_to_cpp_PCM_source_Peaks_Clear(
                self as *const _ as _,
                deleteFile,
            );
        }
    }

    pub fn PeaksBuild_Begin(&self) -> ::std::os::raw::c_int {
        unsafe {
            rust_to_cpp_PCM_source_PeaksBuild_Begin(self as *const _ as _)
        }
    }

    pub fn PeaksBuild_Run(&self) -> ::std::os::raw::c_int {
        unsafe { rust_to_cpp_PCM_source_PeaksBuild_Run(self as *const _ as _) }
    }

    pub fn PeaksBuild_Finish(&self) {
        unsafe {
            rust_to_cpp_PCM_source_PeaksBuild_Finish(self as *const _ as _);
        }
    }
}

/// This is the Rust analog to the C++ virtual base class `PCM_source`.
///
/// An implementation of this trait can be passed to
/// [`create_cpp_to_rust_pcm_source()`].
///
/// [`create_cpp_to_rust_pcm_source()`]: fn.create_cpp_to_rust_pcm_source.html
pub trait PCM_source {
    fn Duplicate(&mut self) -> *mut raw::PCM_source;

    fn IsAvailable(&mut self) -> bool;
    fn SetAvailable(&mut self, avail: bool) {
        let _ = avail;
    }
    fn GetType(&mut self) -> *const ::std::os::raw::c_char;
    fn GetFileName(&mut self) -> *const ::std::os::raw::c_char {
        null()
    }
    fn SetFileName(&mut self, newfn: *const ::std::os::raw::c_char) -> bool;

    fn GetSource(&mut self) -> *mut raw::PCM_source {
        null_mut()
    }
    fn SetSource(&mut self, src: *mut raw::PCM_source) {
        let _ = src;
    }
    fn GetNumChannels(&mut self) -> ::std::os::raw::c_int;
    fn GetSampleRate(&mut self) -> f64;
    fn GetLength(&mut self) -> f64;
    fn GetLengthBeats(&mut self) -> f64 {
        -1.0
    }
    fn GetBitsPerSample(&mut self) -> ::std::os::raw::c_int {
        0
    }
    fn GetPreferredPosition(&mut self) -> f64 {
        -1.0
    }

    fn PropertiesWindow(
        &mut self,
        hwndParent: raw::HWND,
    ) -> ::std::os::raw::c_int;

    fn GetSamples(&mut self, block: *mut raw::PCM_source_transfer_t);
    fn GetPeakInfo(&mut self, block: *mut raw::PCM_source_peaktransfer_t);

    fn SaveState(&mut self, ctx: *mut raw::ProjectStateContext);
    fn LoadState(
        &mut self,
        firstline: *const ::std::os::raw::c_char,
        ctx: *mut raw::ProjectStateContext,
    ) -> ::std::os::raw::c_int;

    fn Peaks_Clear(&mut self, deleteFile: bool);
    fn PeaksBuild_Begin(&mut self) -> ::std::os::raw::c_int;
    fn PeaksBuild_Run(&mut self) -> ::std::os::raw::c_int;
    fn PeaksBuild_Finish(&mut self);

    fn Extended(
        &mut self,
        call: ::std::os::raw::c_int,
        parm1: *mut ::std::os::raw::c_void,
        parm2: *mut ::std::os::raw::c_void,
        parm3: *mut ::std::os::raw::c_void,
    ) -> ::std::os::raw::c_int {
        let _ = call;
        let _ = parm1;
        let _ = parm2;
        let _ = parm3;
        0
    }
}

/// Creates a `PCM_source` object on C++ side and returns a pointer to it.
///
/// This function is provided because Rust structs can't implement C++ virtual
/// base classes.
///
/// # Example
///
/// See [`create_cpp_to_rust_control_surface()`]. Usage is very similar.
///
/// # Cleaning up
///
/// In order to avoid memory leaks, you must take care of removing the C++
/// counterpart PCM source by calling [`delete_cpp_pcm_source()`].
///
/// # Safety
///
/// This function is highly unsafe. Better use the medium-level API instead.
///
/// [`delete_cpp_pcm_source()`]: fn.delete_cpp_pcm_source.html
/// [`create_cpp_to_rust_control_surface()`]: fn.create_cpp_to_rust_control_surface.html
pub unsafe fn create_cpp_to_rust_pcm_source(
    callback_target: NonNull<Box<dyn PCM_source>>,
) -> NonNull<raw::PCM_source> {
    let instance = crate::bindings::root::reaper_pcm_source::create_cpp_to_rust_pcm_source(
        callback_target.as_ptr() as *mut c_void,
    );
    NonNull::new_unchecked(instance)
}

/// Destroys a C++ `PCM_source` object.
///
/// Intended to be used on pointers returned from
/// [`create_cpp_to_rust_pcm_source()`].
///
/// # Safety
///
/// REAPER can crash if you pass an invalid pointer because C++ will attempt to
/// free the wrong location in memory.
///
/// [`create_cpp_to_rust_pcm_source()`]: fn.create_cpp_to_rust_pcm_source.html
pub unsafe fn delete_cpp_pcm_source(source: NonNull<raw::PCM_source>) {
    crate::bindings::root::reaper_pcm_source::delete_pcm_source(
        source.as_ptr(),
    );
}

#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetLength(
    callback_target: *mut Box<dyn PCM_source>,
) -> f64 {
    firewall(|| unsafe { &mut *callback_target }.GetLength())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_Duplicate(
    callback_target: *mut Box<dyn PCM_source>,
) -> *mut raw::PCM_source {
    firewall(|| unsafe { &mut *callback_target }.Duplicate())
        .unwrap_or(null_mut())
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetType(
    callback_target: *mut Box<dyn PCM_source>,
) -> *const ::std::os::raw::c_char {
    firewall(|| unsafe { &mut *callback_target }.GetType()).unwrap_or(null())
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetFileName(
    callback_target: *mut Box<dyn PCM_source>,
) -> *const ::std::os::raw::c_char {
    firewall(|| unsafe { &mut *callback_target }.GetFileName())
        .unwrap_or(null())
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetSource(
    callback_target: *mut Box<dyn PCM_source>,
) -> *mut raw::PCM_source {
    firewall(|| unsafe { &mut *callback_target }.GetSource())
        .unwrap_or(null_mut())
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_IsAvailable(
    callback_target: *mut Box<dyn PCM_source>,
) -> bool {
    firewall(|| unsafe { &mut *callback_target }.IsAvailable())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_SetAvailable(
    callback_target: *mut Box<dyn PCM_source>,
    avail: bool,
) {
    firewall(|| unsafe { &mut *callback_target }.SetAvailable(avail));
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_SetFileName(
    callback_target: *mut Box<dyn PCM_source>,
    newfn: *const ::std::os::raw::c_char,
) -> bool {
    firewall(|| unsafe { &mut *callback_target }.SetFileName(newfn))
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_SetSource(
    callback_target: *mut Box<dyn PCM_source>,
    src: *mut raw::PCM_source,
) {
    firewall(|| unsafe { &mut *callback_target }.SetSource(src));
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetNumChannels(
    callback_target: *mut Box<dyn PCM_source>,
) -> ::std::os::raw::c_int {
    firewall(|| unsafe { &mut *callback_target }.GetNumChannels())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetSampleRate(
    callback_target: *mut Box<dyn PCM_source>,
) -> f64 {
    firewall(|| unsafe { &mut *callback_target }.GetSampleRate())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetLengthBeats(
    callback_target: *mut Box<dyn PCM_source>,
) -> f64 {
    firewall(|| unsafe { &mut *callback_target }.GetLengthBeats())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetBitsPerSample(
    callback_target: *mut Box<dyn PCM_source>,
) -> ::std::os::raw::c_int {
    firewall(|| unsafe { &mut *callback_target }.GetBitsPerSample())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetPreferredPosition(
    callback_target: *mut Box<dyn PCM_source>,
) -> f64 {
    firewall(|| unsafe { &mut *callback_target }.GetPreferredPosition())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_PropertiesWindow(
    callback_target: *mut Box<dyn PCM_source>,
    hwndParent: raw::HWND,
) -> ::std::os::raw::c_int {
    firewall(|| unsafe { &mut *callback_target }.PropertiesWindow(hwndParent))
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetSamples(
    callback_target: *mut Box<dyn PCM_source>,
    block: *mut raw::PCM_source_transfer_t,
) {
    firewall(|| unsafe { &mut *callback_target }.GetSamples(block));
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_GetPeakInfo(
    callback_target: *mut Box<dyn PCM_source>,
    block: *mut raw::PCM_source_peaktransfer_t,
) {
    firewall(|| unsafe { &mut *callback_target }.GetPeakInfo(block));
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_SaveState(
    callback_target: *mut Box<dyn PCM_source>,
    ctx: *mut raw::ProjectStateContext,
) {
    firewall(|| unsafe { &mut *callback_target }.SaveState(ctx));
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_LoadState(
    callback_target: *mut Box<dyn PCM_source>,
    firstline: *const ::std::os::raw::c_char,
    ctx: *mut raw::ProjectStateContext,
) -> ::std::os::raw::c_int {
    firewall(|| unsafe { &mut *callback_target }.LoadState(firstline, ctx))
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_Peaks_Clear(
    callback_target: *mut Box<dyn PCM_source>,
    deleteFile: bool,
) {
    firewall(|| unsafe { &mut *callback_target }.Peaks_Clear(deleteFile));
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_PeaksBuild_Begin(
    callback_target: *mut Box<dyn PCM_source>,
) -> ::std::os::raw::c_int {
    firewall(|| unsafe { &mut *callback_target }.PeaksBuild_Begin())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_PeaksBuild_Run(
    callback_target: *mut Box<dyn PCM_source>,
) -> ::std::os::raw::c_int {
    firewall(|| unsafe { &mut *callback_target }.PeaksBuild_Run())
        .unwrap_or_default()
}
#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_PeaksBuild_Finish(
    callback_target: *mut Box<dyn PCM_source>,
) {
    firewall(|| unsafe { &mut *callback_target }.PeaksBuild_Finish());
}

#[no_mangle]
extern "C" fn cpp_to_rust_PCM_source_Extended(
    callback_target: *mut Box<dyn PCM_source>,
    call: ::std::os::raw::c_int,
    parm1: *mut ::std::os::raw::c_void,
    parm2: *mut ::std::os::raw::c_void,
    parm3: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
    firewall(|| {
        unsafe { &mut *callback_target }.Extended(call, parm1, parm2, parm3)
    })
    .unwrap_or_default()
}

// Convenience functions
//
// At this point, the project state context stuff is not really usable from
// reaper-rs yet because I didn't bother to deal with the variadics (which are
// not supported in stable Rust at the time of this writing, at least not
// natively). I personally don't need to implement a project state context in
// Rust or use one with all of its bells and whistles, so that's not a big
// deal.
//
// However, I need to persist the state of PCM sources and restore it again. So
// here are some very specialized convenience functions for doing exactly that.
// It would be possible to make this more generic, e.g. to work on heap buffers
// and project state context in general, but let's do that as soon as
// necessary.

/// Creates a heap buffer and passes ownership to the caller.
pub fn create_heap_buf() -> *mut raw::WDL_HeapBuf {
    unsafe { rust_to_cpp_create_heap_buf() }
}

/// Saves the state of the given PCM source into the given heap buffer and
/// returns the size of the data written into the buffer.
///
/// # Safety
///
/// REAPER can crash if you pass an invalid pointer.
pub unsafe fn save_pcm_source_state_to_heap_buf(
    source: *mut raw::PCM_source,
    buf: *mut raw::WDL_HeapBuf,
) -> raw::WDL_INT64 {
    rust_to_cpp_save_pcm_source_state_to_heap_buf(source, buf)
}

/// Copies the content of the given heap buffer to the given output buffer
/// (which must be sized correctly).
///
/// Takes ownership of the passed buffer, so takes care of destruction!
///
/// # Safety
///
/// REAPER can crash if you pass an invalid pointer.
pub unsafe fn copy_heap_buf_to_buf(
    in_buf: *mut raw::WDL_HeapBuf,
    out_buf: *mut u8,
) {
    rust_to_cpp_copy_heap_buf_to_buf(in_buf, out_buf);
}

/// Restores the PCM source state from the given buffer.
///
/// Returns -1 on error.
///
/// # Safety
///
/// REAPER can crash if you pass an invalid pointer.
pub unsafe fn load_pcm_source_state_from_buf(
    source: *mut raw::PCM_source,
    first_line: *const ::std::os::raw::c_char,
    in_buf: *mut u8,
    in_buf_size: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int {
    rust_to_cpp_load_pcm_source_state_from_buf(
        source,
        first_line,
        in_buf,
        in_buf_size,
    )
}