raylib 6.0.0-rc.2

Safe Rust bindings for Raylib.
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! Contains code related to audio. [`RaylibAudio`] plays sounds and music.

use crate::{
    error::{AudioInitError, LoadSoundError, UpdateAudioStreamError},
    ffi,
};
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::path::Path;

use super::error::ExportWaveError;

make_thin_wrapper_lifetime!(
    /// CPU-side waveform data loaded into RAM.
    ///
    /// A `Wave` holds raw PCM samples (and metadata) without occupying any audio-device
    /// resources. Convert it to a [`Sound`] via [`RaylibAudio::new_sound_from_wave`] for
    /// repeated low-latency playback, or export it to disk with [`Wave::export`].
    ///
    /// Freed via `UnloadWave` on drop. Lifetime is bound to the owning [`RaylibAudio`].
    Wave,
    ffi::Wave,
    RaylibAudio,
    ffi::UnloadWave
);

make_thin_wrapper_lifetime!(
    /// Audio-device-ready playable sound sample.
    ///
    /// A `Sound` is a fully decoded, GPU/audio-card-buffered sample suitable for repeated
    /// low-latency playback (e.g. effects and short clips). Load one from a file with
    /// [`RaylibAudio::new_sound`] or from a [`Wave`] with
    /// [`RaylibAudio::new_sound_from_wave`].
    ///
    /// Freed via `UnloadSound` on drop. Lifetime is bound to the owning [`RaylibAudio`].
    ///
    /// # Examples
    ///
    /// Load and play a sound effect:
    ///
    /// ```rust,no_run
    /// use raylib::prelude::*;
    /// let audio = RaylibAudio::init_audio_device().unwrap();
    /// let sound = audio.new_sound("assets/click.wav").unwrap();
    /// // Later, trigger playback:
    /// unsafe { raylib::ffi::PlaySound(*sound) };
    /// ```
    Sound,
    ffi::Sound,
    RaylibAudio,
    (ffi::UnloadSound),
    true
);
make_thin_wrapper_lifetime!(
    /// Streamed audio for long-form playback.
    ///
    /// `Music` streams data from disk (or memory) in chunks, making it suitable for
    /// background music or any audio exceeding ~10 seconds. Load via
    /// [`RaylibAudio::new_music`] and update each frame with the raylib
    /// `UpdateMusicStream` / `PlayMusicStream` calls.
    ///
    /// Freed via `UnloadMusicStream` on drop. Lifetime is bound to the owning
    /// [`RaylibAudio`].
    Music,
    ffi::Music,
    RaylibAudio,
    ffi::UnloadMusicStream
);
make_thin_wrapper_lifetime!(
    /// Low-level raw PCM streaming primitive.
    ///
    /// `AudioStream` lets you push arbitrary audio data to the audio device one
    /// buffer at a time, giving full control over sample rate, bit depth, and channel
    /// count. This is the replacement for the audio-callback API that was removed in
    /// raylib 6.0 — instead of registering a callback you periodically check
    /// `IsAudioStreamProcessed` and push the next buffer of samples.
    ///
    /// Create via [`RaylibAudio::new_audio_stream`]. Freed via `UnloadAudioStream` on
    /// drop. Lifetime is bound to the owning [`RaylibAudio`].
    ///
    /// # Examples
    ///
    /// Create a stereo 44.1 kHz stream and push silence each frame:
    ///
    /// ```rust,no_run
    /// use raylib::prelude::*;
    /// let audio = RaylibAudio::init_audio_device().unwrap();
    /// let stream = audio.new_audio_stream(44100, 16, 2);
    /// // Push PCM data when the device is ready for more samples.
    /// let silence: Vec<i16> = vec![0; 4096];
    /// unsafe { raylib::ffi::UpdateAudioStream(*stream, silence.as_ptr() as *const _, silence.len() as i32) };
    /// ```
    AudioStream,
    ffi::AudioStream,
    RaylibAudio,
    ffi::UnloadAudioStream
);

/// Owned buffer of decoded PCM samples for a [`Wave`], freed via `UnloadWaveSamples` on drop.
///
/// Returned by [`Wave::load_samples`]. Values are normalised to `[-1.0, 1.0]` and always
/// 32-bit float regardless of the source wave's `sample_size` — raylib does the conversion
/// on load. Length is `frame_count` samples (channel interleaving follows the underlying
/// wave's `channels`). Access the underlying slice via [`AsRef::<[f32]>::as_ref`]; the
/// allocation is freed when this guard drops.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let audio = RaylibAudio::init_audio_device().expect("audio init");
/// let wave = audio.new_wave("assets/click.wav").expect("wave load");
/// let samples = wave.load_samples();
/// let pcm: &[f32] = samples.as_ref();
/// println!("{} samples decoded", pcm.len());
/// // `samples` drops here → UnloadWaveSamples frees the buffer.
/// ```
///
/// # See also
///
/// - [`Wave::load_samples`] — constructor.
/// - [`Wave`] — owning wave handle whose samples this buffer decodes.
pub struct WaveSamples(*mut f32, usize);

impl AsRef<[f32]> for WaveSamples {
    fn as_ref(&self) -> &[f32] {
        unsafe { std::slice::from_raw_parts(self.0, self.1) }
    }
}

impl Drop for WaveSamples {
    fn drop(&mut self) {
        unsafe { ffi::UnloadWaveSamples(self.0) }
    }
}

/// A marker trait specifying an audio sample (`u8`, `i16`, or `f32`).
pub trait AudioSample: private::AudioSample {}
impl<T: private::AudioSample> AudioSample for T {}

mod private {
    pub trait AudioSample {}
    impl AudioSample for u8 {}
    impl AudioSample for i16 {}
    impl AudioSample for f32 {}
}

/// Audio subsystem handle — initializes the audio device and owns all audio resources.
///
/// `RaylibAudio` is a separate handle from [`RaylibHandle`](crate::core::RaylibHandle).
/// Obtain one with [`RaylibAudio::init_audio_device`].  All audio resource types
/// ([`Wave`], [`Sound`], [`Music`], [`AudioStream`]) are lifetime-bound to the
/// `RaylibAudio` that created them; the Rust borrow checker enforces this statically, so
/// audio resources cannot outlive the device.
///
/// The audio device is closed (`CloseAudioDevice`) when the `RaylibAudio` is dropped.
///
/// # Examples
///
/// Initialize the audio device and load a sound:
///
/// ```rust,no_run
/// use raylib::prelude::*;
/// let audio = RaylibAudio::init_audio_device().expect("audio init failed");
/// let sound = audio.new_sound("assets/click.wav").expect("sound load failed");
/// // `sound` borrows `audio` and cannot outlive it.
/// ```
#[derive(Debug, Clone)]
pub struct RaylibAudio(PhantomData<()>);

impl RaylibAudio {
    /// Initializes audio device and context.
    #[inline]
    pub fn init_audio_device() -> Result<RaylibAudio, AudioInitError> {
        unsafe {
            if ffi::IsAudioDeviceReady() {
                return Err(AudioInitError::DoubleInit);
            }
            ffi::InitAudioDevice();
            if !ffi::IsAudioDeviceReady() {
                return Err(AudioInitError::InitFailed);
            }
        }
        Ok(RaylibAudio(PhantomData))
    }

    /// Checks if audio device is ready.
    #[inline]
    #[must_use]
    pub fn is_audio_device_ready(&self) -> bool {
        unsafe { ffi::IsAudioDeviceReady() }
    }

    /// Get master volume (listener)
    #[inline]
    #[must_use]
    pub fn get_master_volume(&self) -> f32 {
        unsafe { ffi::GetMasterVolume() }
    }

    /// Sets master volume (listener).
    #[inline]
    pub fn set_master_volume(&self, volume: f32) {
        unsafe { ffi::SetMasterVolume(volume) }
    }

    /// Sets default audio buffer size for new audio streams.
    #[inline]
    pub fn set_audio_stream_buffer_size_default(&self, size: i32) {
        unsafe {
            ffi::SetAudioStreamBufferSizeDefault(size);
        }
    }

    /// Loads a new sound from file.
    #[inline]
    pub fn new_sound<'aud>(&'aud self, filename: &str) -> Result<Sound<'aud>, LoadSoundError> {
        let c_filename = CString::new(filename).unwrap();
        let s = unsafe { ffi::LoadSound(c_filename.as_ptr()) };
        if s.stream.buffer.is_null() {
            return Err(LoadSoundError::LoadFailed {
                path: filename.into(),
            });
        }

        Ok(Sound(s, self))
    }

    /// Loads sound from wave data.
    #[inline]
    pub fn new_sound_from_wave<'aud>(
        &'aud self,
        wave: &Wave,
    ) -> Result<Sound<'aud>, LoadSoundError> {
        let s = unsafe { ffi::LoadSoundFromWave(wave.0) };
        if s.stream.buffer.is_null() {
            return Err(LoadSoundError::LoadFromWaveFailed);
        }
        Ok(Sound(s, self))
    }
    /// Loads wave data from file into RAM.
    #[inline]
    pub fn new_wave<'aud>(&'aud self, filename: &str) -> Result<Wave<'aud>, LoadSoundError> {
        let c_filename = CString::new(filename).unwrap();
        let w = unsafe { ffi::LoadWave(c_filename.as_ptr()) };
        if w.data.is_null() {
            return Err(LoadSoundError::LoadWaveFromFileFailed {
                path: filename.into(),
            });
        }
        Ok(Wave(w, self))
    }

    /// Load wave from memory buffer, fileType refers to extension: i.e. '.wav'
    #[inline]
    pub fn new_wave_from_memory<'aud>(
        &'aud self,
        filetype: &str,
        bytes: &[u8],
    ) -> Result<Wave<'aud>, LoadSoundError> {
        let c_filetype = CString::new(filetype).unwrap();
        let w = unsafe {
            ffi::LoadWaveFromMemory(c_filetype.as_ptr(), bytes.as_ptr(), bytes.len() as i32)
        };
        if w.data.is_null() {
            return Err(LoadSoundError::Null);
        };
        Ok(Wave(w, self))
    }

    /// Loads music stream from file.
    #[inline]
    pub fn new_music<'aud>(&'aud self, filename: &str) -> Result<Music<'aud>, LoadSoundError> {
        let c_filename = CString::new(filename).unwrap();
        let m = unsafe { ffi::LoadMusicStream(c_filename.as_ptr()) };
        if m.stream.buffer.is_null() {
            return Err(LoadSoundError::LoadMusicFromFileFailed {
                path: filename.into(),
            });
        }
        Ok(Music(m, self))
    }

    /// Load music stream from data
    #[inline]
    pub fn new_music_from_memory<'aud>(
        &'aud self,
        filetype: &str,
        bytes: &[u8],
    ) -> Result<Music<'aud>, LoadSoundError> {
        let c_filetype = CString::new(filetype).unwrap();
        let w = unsafe {
            ffi::LoadMusicStreamFromMemory(c_filetype.as_ptr(), bytes.as_ptr(), bytes.len() as i32)
        };
        if w.stream.buffer.is_null() {
            return Err(LoadSoundError::MusicNull);
        };
        Ok(Music(w, self))
    }

    /// Initializes audio stream (to stream raw PCM data).
    #[inline]
    #[must_use]
    pub fn new_audio_stream(
        &self,
        sample_rate: u32,
        sample_size: u32,
        channels: u32,
    ) -> AudioStream<'_> {
        unsafe {
            AudioStream(
                ffi::LoadAudioStream(sample_rate, sample_size, channels),
                self,
            )
        }
    }
}

impl Drop for RaylibAudio {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::CloseAudioDevice() }
    }
}

impl Wave<'_> {
    /// Total number of frames (considering channels)
    #[inline]
    #[must_use]
    pub const fn frame_count(&self) -> u32 {
        self.0.frameCount
    }
    /// Frequency (samples per second)
    #[inline]
    #[must_use]
    pub const fn sample_rate(&self) -> u32 {
        self.0.sampleRate
    }
    /// Bit depth (bits per sample): 8, 16, 32 (24 not supported)
    #[inline]
    #[must_use]
    pub const fn sample_size(&self) -> u32 {
        self.0.sampleSize
    }
    /// Number of channels (1-mono, 2-stereo, ...)
    #[inline]
    #[must_use]
    pub const fn channels(&self) -> u32 {
        self.0.channels
    }
    /// # Safety
    ///
    /// Caller takes ownership of the raw wave data and must free it via `UnloadWave`.
    #[inline]
    #[must_use]
    pub unsafe fn inner(self) -> ffi::Wave {
        let inner = self.0;
        std::mem::forget(self);
        inner
    }

    /// Checks if wave data is valid (data loaded and parameters)
    #[inline]
    #[must_use]
    pub fn is_wave_valid(&self) -> bool {
        unsafe { ffi::IsWaveValid(self.0) }
    }

    /// Export wave file. Extension must be .wav or .raw
    #[inline]
    pub fn export(&self, filename: impl AsRef<Path>) -> Result<(), ExportWaveError> {
        let c_filename = CString::new(filename.as_ref().to_string_lossy().as_bytes()).unwrap();
        let success = unsafe { ffi::ExportWave(self.0, c_filename.as_ptr()) };
        if success {
            Ok(())
        } else {
            // const WAV: &CStr = c".wav";
            const QOA: &CStr = c".qoa";
            // const RAW: &CStr = c".raw";
            let is_qoa = unsafe { ffi::IsFileExtension(c_filename.as_ptr(), QOA.as_ptr()) };
            if is_qoa {
                let samples = self.0.sampleSize as i32;
                if samples != 16 {
                    return Err(ExportWaveError::QoaBadSamples(self.0.sampleSize as i32));
                }
            }
            Err(ExportWaveError::ExportFailed)
        }
    }

    /*/// Export wave sample data to code (.h)
    #[inline]
    pub fn export_wave_as_code(&self, filename: &str) -> bool {
        let c_filename = CString::new(filename).unwrap();
        unsafe { ffi::ExportWaveAsCode(self.0, c_filename.as_ptr()) }
    }*/

    /// Copies a wave to a new wave.
    #[inline]
    #[must_use]
    pub(crate) fn copy(&'_ self) -> Wave<'_> {
        unsafe { Wave(ffi::WaveCopy(self.0), self.1) }
    }

    /// Converts wave data to desired format.
    #[inline]
    pub fn format(&mut self, sample_rate: i32, sample_size: i32, channels: i32) {
        unsafe { ffi::WaveFormat(&mut self.0, sample_rate, sample_size, channels) }
    }

    /// Crops a wave to defined sample range.
    #[inline]
    pub fn crop(&mut self, init_sample: i32, final_sample: i32) {
        unsafe { ffi::WaveCrop(&mut self.0, init_sample, final_sample) }
    }

    /// Load samples data from wave as a floats array
    /// NOTE 1: Returned sample values are normalized to range [-1..1]
    /// NOTE 2: Sample data allocated should be freed with UnloadWaveSamples()
    #[inline]
    #[must_use]
    pub fn load_samples(&self) -> WaveSamples {
        WaveSamples(
            unsafe { ffi::LoadWaveSamples(self.0) },
            self.frameCount as usize,
        )
    }
}

impl Sound<'_> {
    /// Checks if a sound is valid (data loaded and buffers initialized)
    #[inline]
    #[must_use]
    pub fn is_sound_valid(&self) -> bool {
        unsafe { ffi::IsSoundValid(self.0) }
    }

    /// Total number of frames (considering channels)
    #[inline]
    #[must_use]
    pub const fn frame_count(&self) -> u32 {
        self.0.frameCount
    }
    /// # Safety
    ///
    /// Caller takes ownership of the raw sound data and must free it via `UnloadSound`.
    #[inline]
    #[must_use]
    pub unsafe fn inner(self) -> ffi::Sound {
        let inner = self.0;
        std::mem::forget(self);
        inner
    }

    /// Plays a sound.
    #[inline]
    pub fn play(&self) {
        unsafe { ffi::PlaySound(self.0) }
    }

    /// Pauses a sound.
    #[inline]
    pub fn pause(&self) {
        unsafe { ffi::PauseSound(self.0) }
    }

    /// Resumes a paused sound.
    #[inline]
    pub fn resume(&self) {
        unsafe { ffi::ResumeSound(self.0) }
    }

    /// Stops playing a sound.
    #[inline]
    pub fn stop(&self) {
        unsafe { ffi::StopSound(self.0) }
    }

    /// Checks if a sound is currently playing.
    #[inline]
    #[must_use]
    pub fn is_playing(&self) -> bool {
        unsafe { ffi::IsSoundPlaying(self.0) }
    }

    /// Sets volume for a sound (`1.0` is max level).
    #[inline]
    pub fn set_volume(&self, volume: f32) {
        unsafe { ffi::SetSoundVolume(self.0, volume) }
    }

    /// Sets pitch for a sound (`1.0` is base level).
    #[inline]
    pub fn set_pitch(&self, pitch: f32) {
        unsafe { ffi::SetSoundPitch(self.0, pitch) }
    }

    /// Set pan for a sound (0.5 is center)
    #[inline]
    pub fn set_pan(&self, pan: f32) {
        unsafe { ffi::SetSoundPan(self.0, pan) }
    }

    /// Updates sound buffer with new data.
    /// **Notes** (iann):
    /// 1. raylib’s `UpdateSound` is a raw `memcpy` without size checks, we add safety checks to  here to prevent invalid memory writes.
    ///     - potential upstream raylib discussion: "too many frames" doesn't exist for the `Sound`'s `AudioStream`
    ///     - potential upstream raylib discussion: adding sampleSize checks for the `memcpy`
    /// 2. raylib's `Sound`'s `AudioStream` always gets 32-bit sample size (so we always catch non-32-bit `Sound`'s with a `SampleSizeMismatch`)
    ///     - 32-bit fixed in config here: <https://github.com/raysan5/raylib/blob/master/src/config.h#L282>
    ///     - device format set here: <https://github.com/raysan5/raylib/blob/master/src/raudio.c#L288>
    ///     - potential upstream raylib discussion: allowing for other samplesSizes for `Sound`
    #[inline]
    pub fn update<T: AudioSample>(&mut self, data: &[T]) -> Result<(), UpdateAudioStreamError> {
        let expected_sample_size_bits =
            usize::try_from(self.stream.sampleSize).expect("sampleSize should be 8, 16, or 32");
        let provided_sample_size_bits = size_of::<T>() * u8::BITS as usize;
        if provided_sample_size_bits != expected_sample_size_bits {
            return Err(UpdateAudioStreamError::SampleSizeMismatch {
                expected: expected_sample_size_bits,
                provided: provided_sample_size_bits,
            });
        }
        let max_frame_count = usize::try_from(self.frameCount)
            .expect("frameCount should be a valid memory allocation size");
        let provided_frame_count = data.len();
        if provided_frame_count > max_frame_count {
            return Err(UpdateAudioStreamError::TooManyFrames {
                max: max_frame_count,
                provided: provided_frame_count,
            });
        }
        unsafe {
            ffi::UpdateSound(
                self.0,
                data.as_ptr() as *const std::os::raw::c_void,
                provided_frame_count.try_into().unwrap(),
            );
        }
        Ok(())
    }
}

impl SoundAlias<'_, '_> {
    /// Checks if a sound is valid (data loaded and buffers initialized)
    #[inline]
    #[must_use]
    pub fn is_sound_valid(&self) -> bool {
        unsafe { ffi::IsSoundValid(self.0) }
    }

    /// Total number of frames (considering channels)
    #[inline]
    #[must_use]
    pub const fn frame_count(&self) -> u32 {
        self.0.frameCount
    }
    /// # Safety
    ///
    /// Caller takes ownership of the raw sound alias data and must free it via `UnloadSoundAlias`.
    #[must_use]
    pub unsafe fn inner(self) -> ffi::Sound {
        let inner = self.0;
        std::mem::forget(self);
        inner
    }

    /// Plays a sound.
    #[inline]
    pub fn play(&self) {
        unsafe { ffi::PlaySound(self.0) }
    }

    /// Pauses a sound.
    #[inline]
    pub fn pause(&self) {
        unsafe { ffi::PauseSound(self.0) }
    }

    /// Resumes a paused sound.
    #[inline]
    pub fn resume(&self) {
        unsafe { ffi::ResumeSound(self.0) }
    }

    /// Stops playing a sound.
    #[inline]
    pub fn stop(&self) {
        unsafe { ffi::StopSound(self.0) }
    }

    /// Checks if a sound is currently playing.
    #[inline]
    #[must_use]
    pub fn is_playing(&self) -> bool {
        unsafe { ffi::IsSoundPlaying(self.0) }
    }

    /// Sets volume for a sound (`1.0` is max level).
    #[inline]
    pub fn set_volume(&self, volume: f32) {
        unsafe { ffi::SetSoundVolume(self.0, volume) }
    }

    /// Sets pitch for a sound (`1.0` is base level).
    #[inline]
    pub fn set_pitch(&self, pitch: f32) {
        unsafe { ffi::SetSoundPitch(self.0, pitch) }
    }

    /// Set pan for a sound (0.5 is center)
    #[inline]
    pub fn set_pan(&self, pan: f32) {
        unsafe { ffi::SetSoundPan(self.0, pan) }
    }
}

impl Drop for SoundAlias<'_, '_> {
    fn drop(&mut self) {
        unsafe { ffi::UnloadSoundAlias(self.0) }
    }
}

impl Music<'_> {
    /// Starts music playing.
    #[inline]
    pub fn play_stream(&self) {
        unsafe { ffi::PlayMusicStream(self.0) }
    }

    /// Updates buffers for music streaming.
    #[inline]
    pub fn update_stream(&self) {
        unsafe { ffi::UpdateMusicStream(self.0) }
    }

    /// Stops music playing.
    #[inline]
    pub fn stop_stream(&self) {
        unsafe { ffi::StopMusicStream(self.0) }
    }

    /// Pauses music playing.
    #[inline]
    pub fn pause_stream(&self) {
        unsafe { ffi::PauseMusicStream(self.0) }
    }

    /// Resumes playing paused music.
    #[inline]
    pub fn resume_stream(&self) {
        unsafe { ffi::ResumeMusicStream(self.0) }
    }

    /// Checks if music is playing.
    #[inline]
    #[must_use]
    pub fn is_stream_playing(&self) -> bool {
        unsafe { ffi::IsMusicStreamPlaying(self.0) }
    }

    /// Sets volume for music (`1.0` is max level).
    #[inline]
    pub fn set_volume(&self, volume: f32) {
        unsafe { ffi::SetMusicVolume(self.0, volume) }
    }

    /// Sets pitch for music (`1.0` is base level).
    #[inline]
    pub fn set_pitch(&self, pitch: f32) {
        unsafe { ffi::SetMusicPitch(self.0, pitch) }
    }

    /// Gets music time length in seconds.
    #[inline]
    #[must_use]
    pub fn get_time_length(&self) -> f32 {
        unsafe { ffi::GetMusicTimeLength(self.0) }
    }

    /// Gets current music time played in seconds.
    #[inline]
    #[must_use]
    pub fn get_time_played(&self) -> f32 {
        unsafe { ffi::GetMusicTimePlayed(self.0) }
    }

    /// Seek music to a position (in seconds)
    #[inline]
    pub fn seek_stream(&self, position: f32) {
        unsafe { ffi::SeekMusicStream(self.0, position) }
    }

    /// Set pan for a music (0.5 is center)
    #[inline]
    pub fn set_pan(&self, pan: f32) {
        unsafe { ffi::SetMusicPan(self.0, pan) }
    }

    /// Checks if a music stream is valid (context and buffers initialized)
    #[inline]
    #[must_use]
    pub fn is_music_valid(&self) -> bool {
        unsafe { ffi::IsMusicValid(self.0) }
    }
}

impl AudioStream<'_> {
    /// Checks if an audio stream is valid (buffers initialized)
    #[inline]
    #[must_use]
    pub fn is_audio_stream_valid(&self) -> bool {
        unsafe { ffi::IsAudioStreamValid(self.0) }
    }
    /// Frequency (samples per second)
    #[inline]
    #[must_use]
    pub const fn sample_rate(&self) -> u32 {
        self.0.sampleRate
    }
    /// Bit depth (bits per sample): 8, 16, 32 (24 not supported)
    #[inline]
    #[must_use]
    pub const fn sample_size(&self) -> u32 {
        self.0.sampleSize
    }
    /// Number of channels (1-mono, 2-stereo, ...)
    #[inline]
    #[must_use]
    pub const fn channels(&self) -> u32 {
        self.0.channels
    }

    /// # Safety
    ///
    /// Caller takes ownership of the raw audio stream and must free it via `UnloadAudioStream`.
    #[must_use]
    pub unsafe fn inner(self) -> ffi::AudioStream {
        let inner = self.0;
        std::mem::forget(self);
        inner
    }

    /// Updates audio stream buffers with data.
    #[inline]
    pub fn update<T: AudioSample>(&mut self, data: &[T]) -> Result<(), UpdateAudioStreamError> {
        let expected_sample_size =
            usize::try_from(self.sampleSize).expect("sampleSize should be 8, 16, or 32");
        let provided_sample_size_bits = size_of::<T>() * u8::BITS as usize;
        if provided_sample_size_bits != expected_sample_size {
            return Err(UpdateAudioStreamError::SampleSizeMismatch {
                expected: expected_sample_size,
                provided: provided_sample_size_bits,
            });
        }
        let provided_frame_count = data.len();

        unsafe {
            ffi::UpdateAudioStream(
                self.0,
                data.as_ptr() as *const std::os::raw::c_void,
                provided_frame_count.try_into().unwrap(),
            );
        }
        Ok(())
    }

    /// Plays audio stream.
    #[inline]
    pub fn play(&self) {
        unsafe {
            ffi::PlayAudioStream(self.0);
        }
    }

    /// Pauses audio stream.
    #[inline]
    pub fn pause(&self) {
        unsafe {
            ffi::PauseAudioStream(self.0);
        }
    }

    /// Resumes audio stream.
    #[inline]
    pub fn resume(&self) {
        unsafe {
            ffi::ResumeAudioStream(self.0);
        }
    }

    /// Checks if audio stream is currently playing.
    #[inline]
    #[must_use]
    pub fn is_playing(&self) -> bool {
        unsafe { ffi::IsAudioStreamPlaying(self.0) }
    }

    /// Stops audio stream.
    #[inline]
    pub fn stop(&self) {
        unsafe {
            ffi::StopAudioStream(self.0);
        }
    }

    /// Sets volume for audio stream (`1.0` is max level).
    #[inline]
    pub fn set_volume(&self, volume: f32) {
        unsafe {
            ffi::SetAudioStreamVolume(self.0, volume);
        }
    }

    /// Sets pitch for audio stream (`1.0` is base level).
    #[inline]
    pub fn set_pitch(&self, pitch: f32) {
        unsafe {
            ffi::SetAudioStreamPitch(self.0, pitch);
        }
    }

    /// Sets pitch for audio stream (`1.0` is base level).
    #[inline]
    #[must_use]
    pub fn is_processed(&self) -> bool {
        unsafe { ffi::IsAudioStreamProcessed(self.0) }
    }

    /// Set pan for audio stream (0.5 is centered)
    #[inline]
    pub fn set_pan(&self, pan: f32) {
        unsafe {
            ffi::SetAudioStreamPan(self.0, pan);
        }
    }
}

impl<'bind> Sound<'bind> {
    /// Clone sound from existing sound data, clone does not own wave data
    // NOTE: Wave data must be unallocated manually and will be shared across all clones
    pub fn alias<'snd>(&'snd self) -> Result<SoundAlias<'snd, 'bind>, LoadSoundError> {
        let s = unsafe { ffi::LoadSoundAlias(self.0) };
        if s.stream.buffer.is_null() {
            return Err(LoadSoundError::LoadFromWaveFailed);
        }
        Ok(SoundAlias(s, PhantomData))
    }
}

/// A lightweight alias handle to a [`Sound`] that shares the same audio buffer without owning it.
///
/// Returned by [`Sound::alias`]. Wraps `LoadSoundAlias` — the source [`Sound`] owns the audio
/// buffer and remains responsible for unloading it; the alias holds an independent playback
/// head (volume, pitch, pan, play position) so multiple overlapping instances of the same
/// sound can play concurrently without allocating a fresh decoded buffer per voice.
///
/// The `'snd` lifetime borrows the parent [`Sound`] (statically enforced) so the alias
/// cannot outlive the buffer it points at; the `'bind` lifetime tracks the parent's audio
/// device.
///
/// # Examples
///
/// ```no_run
/// use raylib::prelude::*;
///
/// let audio = RaylibAudio::init_audio_device().expect("audio init");
/// let sound = audio.new_sound("assets/click.wav").expect("sound load");
/// let voice_a = sound.alias().expect("alias");
/// let voice_b = sound.alias().expect("alias");
/// // `voice_a` and `voice_b` can be played simultaneously and tuned independently.
/// ```
///
/// # See also
///
/// - [`Sound::alias`] — constructor.
/// - [`Sound`] — owning sound handle whose buffer this alias shares.
pub struct SoundAlias<'snd, 'bind>(ffi::Sound, PhantomData<&'snd Sound<'bind>>);