maudio 0.1.5

Rust bindings to the miniaudio library
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
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
//! White, Pink or Brown noise generator
use std::{marker::PhantomData, mem::MaybeUninit, sync::Arc};

use maudio_sys::ffi as sys;

use crate::{
    audio::formats::{Format, SampleBuffer},
    engine::AllocationCallbacks,
    pcm_frames::{PcmFormat, S24Packed, S24},
    AsRawRef, Binding, ErrorKinds, MaResult, MaudioError,
};

pub struct Noise<F: PcmFormat> {
    inner: *mut sys::ma_noise,
    channels: u32,
    alloc_cb: Option<Arc<AllocationCallbacks>>,
    _sample_format: PhantomData<F>,
}

impl<F: PcmFormat> Binding for Noise<F> {
    type Raw = *mut sys::ma_noise;

    /// !!! unimplemented !!!
    fn from_ptr(_raw: Self::Raw) -> Self {
        unimplemented!()
    }

    fn to_raw(&self) -> Self::Raw {
        self.inner
    }
}

impl<F: PcmFormat> Noise<F> {
    /// Generates PCM frames into `dst`, returning the number of frames written.
    pub fn read_pcm_frames_into(&mut self, dst: &mut [F::PcmUnit]) -> MaResult<usize> {
        noise_ffi::ma_noise_read_pcm_frames_into::<F>(self, dst)
    }

    /// Allocates and generates `frames` PCM frames.
    pub fn read_pcm_frames(&mut self, frames: u64) -> MaResult<SampleBuffer<F>> {
        noise_ffi::ma_noise_read_pcm_frames(self, frames)
    }

    /// Sets the output amplitude of the noise generator.
    ///
    /// Larger values produce louder noise. The exact practical range depends on
    /// the sample format and how the underlying miniaudio noise source is used.
    pub fn set_amplitude(&mut self, amplitude: f64) -> MaResult<()> {
        noise_ffi::ma_noise_set_amplitude(self, amplitude)
    }

    /// Sets the random seed used by the generator.
    ///
    /// A fixed seed is useful for reproducible output, especially in tests.
    /// A seed of `0` uses miniaudio's randomized default behavior.
    pub fn set_seed(&mut self, seed: i32) -> MaResult<()> {
        noise_ffi::ma_noise_set_seed(self, seed)
    }
}

mod noise_ffi {
    use std::sync::Arc;

    use maudio_sys::ffi as sys;

    use crate::{
        audio::formats::SampleBuffer,
        data_source::sources::noise::{Noise, NoiseBuilder},
        engine::AllocationCallbacks,
        pcm_frames::{PcmFormat, PcmFormatInternal},
        AsRawRef, Binding, MaResult, MaudioError,
    };

    // Only used for custom allocators (when alloc is done by rust)
    #[inline]
    #[allow(dead_code)]
    pub fn ma_noise_get_heap_size(config: &NoiseBuilder) -> MaResult<usize> {
        let mut heap_size: usize = 0;
        let res = unsafe { sys::ma_noise_get_heap_size(config.as_raw_ptr(), &mut heap_size) };
        MaudioError::check(res)?;
        Ok(heap_size)
    }

    // Only used for custom allocators (when alloc is done by rust)
    #[inline]
    #[allow(dead_code)]
    pub fn ma_noise_init_preallocated(
        config: &NoiseBuilder,
        heap_alloc: *mut core::ffi::c_void,
        noise: *mut sys::ma_noise,
    ) -> MaResult<()> {
        let res =
            unsafe { sys::ma_noise_init_preallocated(config.as_raw_ptr(), heap_alloc, noise) };
        MaudioError::check(res)
    }

    #[inline]
    pub fn ma_noise_init(
        config: &NoiseBuilder,
        alloc: Option<Arc<AllocationCallbacks>>,
        noise: *mut sys::ma_noise,
    ) -> MaResult<()> {
        let alloc_cb: *const sys::ma_allocation_callbacks =
            alloc.clone().map_or(core::ptr::null(), |c| c.as_raw_ptr());

        let res = unsafe { sys::ma_noise_init(config.as_raw_ptr(), alloc_cb, noise) };
        MaudioError::check(res)
    }

    #[inline]
    pub fn ma_noise_uninit<F: PcmFormat>(noise: &mut Noise<F>) {
        let alloc_cb: *const sys::ma_allocation_callbacks = noise
            .alloc_cb
            .clone()
            .map_or(core::ptr::null(), |c| c.as_raw_ptr());

        unsafe {
            sys::ma_noise_uninit(noise.to_raw(), alloc_cb);
        }
    }

    pub fn ma_noise_read_pcm_frames_into<F: PcmFormat>(
        noise: &mut Noise<F>,
        dst: &mut [F::PcmUnit],
    ) -> MaResult<usize> {
        let channels = noise.channels;
        if channels == 0 {
            return Err(MaudioError::from_ma_result(sys::ma_result_MA_INVALID_ARGS));
        }

        let frame_count = (dst.len() / channels as usize / F::VEC_PCM_UNITS_PER_FRAME) as u64;

        match F::DIRECT_READ {
            true => {
                let frames_read = ma_noise_read_pcm_frames_internal(
                    noise,
                    frame_count,
                    dst.as_mut_ptr() as *mut core::ffi::c_void,
                )?;
                Ok(frames_read as usize)
            }
            false => {
                let tmp_len = SampleBuffer::<F>::required_len(
                    frame_count as usize,
                    channels,
                    F::VEC_STORE_UNITS_PER_FRAME,
                )?;

                let mut tmp = vec![F::StorageUnit::default(); tmp_len];
                let frames_read = ma_noise_read_pcm_frames_internal(
                    noise,
                    frame_count,
                    tmp.as_mut_ptr() as *mut core::ffi::c_void,
                )?;

                let _ = <F as PcmFormatInternal>::read_from_storage_internal(
                    &tmp,
                    dst,
                    frames_read as usize,
                    channels as usize,
                )?;

                Ok(frames_read as usize)
            }
        }
    }

    pub fn ma_noise_read_pcm_frames<F: PcmFormat>(
        noise: &mut Noise<F>,
        frame_count: u64,
    ) -> MaResult<SampleBuffer<F>> {
        let mut buffer = SampleBuffer::<F>::new_zeroed(frame_count as usize, noise.channels)?;

        let frames_read = ma_noise_read_pcm_frames_internal(
            noise,
            frame_count,
            buffer.as_mut_ptr() as *mut core::ffi::c_void,
        )?;

        SampleBuffer::<F>::from_storage(buffer, frames_read as usize, noise.channels)
    }

    #[inline]
    fn ma_noise_read_pcm_frames_internal<F: PcmFormat>(
        noise: &mut Noise<F>,
        frame_count: u64,
        buffer: *mut core::ffi::c_void,
    ) -> MaResult<u64> {
        let mut frames_read = 0;
        let res = unsafe {
            sys::ma_noise_read_pcm_frames(noise.to_raw(), buffer, frame_count, &mut frames_read)
        };
        MaudioError::check(res)?;
        Ok(frames_read)
    }

    #[inline]
    pub fn ma_noise_set_amplitude<F: PcmFormat>(
        noise: &mut Noise<F>,
        amplitude: f64,
    ) -> MaResult<()> {
        let res = unsafe { sys::ma_noise_set_amplitude(noise.to_raw(), amplitude) };
        MaudioError::check(res)
    }

    #[inline]
    pub fn ma_noise_set_seed<F: PcmFormat>(noise: &mut Noise<F>, seed: i32) -> MaResult<()> {
        let res = unsafe { sys::ma_noise_set_seed(noise.to_raw(), seed) };
        MaudioError::check(res)
    }
}

// !!! This does not check for any extra alloc done in rust. They are not possible (yet)
impl<F: PcmFormat> Drop for Noise<F> {
    fn drop(&mut self) {
        noise_ffi::ma_noise_uninit(self);
        drop(unsafe { Box::from_raw(self.to_raw()) });
    }
}

/// Procedural noise generator backed by miniaudio's `ma_noise`.
///
/// A `Noise` value generates PCM frames on demand in the sample format `F`.
/// It can be used anywhere a synthetic audio source is useful, such as:
///
/// - test signals
/// - placeholder audio
/// - procedural sound design
/// - generating white, pink, or brown noise buffers
///
/// The generator is stateful. Repeated reads continue the stream from the
/// current internal state rather than restarting from the beginning.
///
/// `Noise` owns the underlying native `ma_noise` instance and cleans it up
/// automatically on drop.
pub struct NoiseBuilder {
    inner: sys::ma_noise_config,
    channels: u32,
    seed: i32,
}

impl AsRawRef for NoiseBuilder {
    type Raw = sys::ma_noise_config;

    fn as_raw(&self) -> &Self::Raw {
        &self.inner
    }
}

impl NoiseBuilder {
    /// Sets the random seed for the noise generator.
    ///
    /// The default seed is `0`, which tells miniaudio to randomize the seed.
    /// Set an explicit seed when reproducible output is required, such as in
    /// tests or examples.
    ///
    /// This can also be changed after `Noise` is created
    pub fn seed(&mut self, seed: i32) -> &mut Self {
        self.inner.seed = seed;
        self.seed = seed;
        self
    }

    /// Controls whether all output channels receive the same generated signal.
    ///
    /// By default, miniaudio generates different noise for each channel.
    /// Setting this to `true` duplicates the same noise signal across channels.
    pub fn duplicate_channels(&mut self, yes: bool) -> &mut Self {
        self.inner.duplicateChannels = yes as u32;
        self
    }

    pub fn new(channels: u32, noise_type: NoiseType, amplitude: f64) -> Self {
        // Format::U8 is a placeholder
        let inner = unsafe {
            sys::ma_noise_config_init(Format::U8.into(), channels, noise_type.into(), 0, amplitude)
        };
        Self {
            inner,
            channels,
            seed: 0,
        }
    }

    /// Builds a noise generator that outputs `u8` PCM samples.
    pub fn build_u8(&mut self) -> MaResult<Noise<u8>> {
        self.inner.format = Format::U8.into();

        let inner = self.new_inner()?;

        Ok(Noise {
            inner,
            channels: self.channels,
            alloc_cb: None,
            _sample_format: PhantomData,
        })
    }

    /// Builds a noise generator that outputs `i16` PCM samples.
    pub fn build_i16(&mut self) -> MaResult<Noise<i16>> {
        self.inner.format = Format::S16.into();

        let inner = self.new_inner()?;

        Ok(Noise {
            inner,
            channels: self.channels,
            alloc_cb: None,
            _sample_format: PhantomData,
        })
    }

    /// Builds a noise generator that outputs `i32` PCM samples.
    pub fn build_i32(&mut self) -> MaResult<Noise<i32>> {
        self.inner.format = Format::S32.into();

        let inner = self.new_inner()?;

        Ok(Noise {
            inner,
            channels: self.channels,
            alloc_cb: None,
            _sample_format: PhantomData,
        })
    }

    /// Builds a noise generator that outputs 24-bit samples represented as i32 with extended sign.
    ///
    /// Internally, miniaudio uses packed 24-bit storage and conversion may be
    /// performed when reading into Rust-facing sample units.
    pub fn build_s24(&mut self) -> MaResult<Noise<S24>> {
        self.inner.format = Format::S24Packed.into();

        let inner = self.new_inner()?;

        Ok(Noise {
            inner,
            channels: self.channels,
            alloc_cb: None,
            _sample_format: PhantomData,
        })
    }

    /// Builds a noise generator that outputs 3-byte packed 24-bit samples via
    /// [`S24Packed`].
    pub fn build_s24_packed(&mut self) -> MaResult<Noise<S24Packed>> {
        self.inner.format = Format::S24Packed.into();

        let inner = self.new_inner()?;

        Ok(Noise {
            inner,
            channels: self.channels,
            alloc_cb: None,
            _sample_format: PhantomData,
        })
    }

    /// Builds a noise generator that outputs `f32` PCM samples.
    pub fn build_f32(&mut self) -> MaResult<Noise<f32>> {
        self.inner.format = Format::F32.into();

        let inner = self.new_inner()?;

        Ok(Noise {
            inner,
            channels: self.channels,
            alloc_cb: None,
            _sample_format: PhantomData,
        })
    }

    fn new_inner(&self) -> MaResult<*mut sys::ma_noise> {
        let mut mem: Box<std::mem::MaybeUninit<sys::ma_noise>> = Box::new(MaybeUninit::uninit());

        noise_ffi::ma_noise_init(self, None, mem.as_mut_ptr())?;

        let inner: *mut sys::ma_noise = Box::into_raw(mem) as *mut sys::ma_noise;

        Ok(inner)
    }
}

/// Type of noise generated by [`Noise`] / [`NoiseBuilder`].
///
/// - [`White`](NoiseType::White): spectrally flat random noise
/// - [`Pink`](NoiseType::Pink): energy decreases with frequency
/// - [`Brown`](NoiseType::Brown): stronger low-frequency emphasis than pink
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum NoiseType {
    White,
    Brown,
    Pink,
}

impl From<NoiseType> for sys::ma_noise_type {
    fn from(value: NoiseType) -> Self {
        match value {
            NoiseType::White => sys::ma_noise_type_ma_noise_type_white,
            NoiseType::Brown => sys::ma_noise_type_ma_noise_type_brownian,
            NoiseType::Pink => sys::ma_noise_type_ma_noise_type_pink,
        }
    }
}

impl TryFrom<sys::ma_noise_type> for NoiseType {
    type Error = MaudioError;

    fn try_from(value: sys::ma_noise_type) -> Result<Self, Self::Error> {
        match value {
            sys::ma_noise_type_ma_noise_type_white => Ok(NoiseType::White),
            sys::ma_noise_type_ma_noise_type_brownian => Ok(NoiseType::Brown),
            sys::ma_noise_type_ma_noise_type_pink => Ok(NoiseType::Pink),
            other => Err(MaudioError::new_ma_error(ErrorKinds::unknown_enum::<
                NoiseType,
            >(other as i64))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::audio::formats::SampleBuffer;

    fn assert_nonzero_frames<F: PcmFormat>(result: MaResult<SampleBuffer<F>>) {
        let buf = result.expect("expected noise read to succeed");
        assert!(buf.frames() > 0);
    }

    #[test]
    fn test_noise_type_into_raw_white() {
        let raw: sys::ma_noise_type = NoiseType::White.into();
        assert_eq!(raw, sys::ma_noise_type_ma_noise_type_white);
    }

    #[test]
    fn test_noise_type_into_raw_brown() {
        let raw: sys::ma_noise_type = NoiseType::Brown.into();
        assert_eq!(raw, sys::ma_noise_type_ma_noise_type_brownian);
    }

    #[test]
    fn test_noise_type_into_raw_pink() {
        let raw: sys::ma_noise_type = NoiseType::Pink.into();
        assert_eq!(raw, sys::ma_noise_type_ma_noise_type_pink);
    }

    #[test]
    fn test_noise_type_try_from_raw_white() {
        let ty = NoiseType::try_from(sys::ma_noise_type_ma_noise_type_white).unwrap();
        assert_eq!(ty, NoiseType::White);
    }

    #[test]
    fn test_noise_type_try_from_raw_brown() {
        let ty = NoiseType::try_from(sys::ma_noise_type_ma_noise_type_brownian).unwrap();
        assert_eq!(ty, NoiseType::Brown);
    }

    #[test]
    fn test_noise_type_try_from_raw_pink() {
        let ty = NoiseType::try_from(sys::ma_noise_type_ma_noise_type_pink).unwrap();
        assert_eq!(ty, NoiseType::Pink);
    }

    #[test]
    fn test_noise_type_try_from_raw_invalid() {
        let invalid = -12345i32 as sys::ma_noise_type;
        let err = NoiseType::try_from(invalid).unwrap_err();
        let _ = err;
    }

    #[test]
    fn test_noise_builder_new_stores_basic_config() {
        let builder = NoiseBuilder::new(2, NoiseType::White, 0.25);

        assert_eq!(builder.channels, 2);
        assert_eq!(builder.seed, 0);
    }

    #[test]
    fn test_noise_builder_seed_sets_seed() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.25);
        let returned = builder.seed(1234) as *mut _;
        let builder_ptr = &mut builder as *mut _;

        assert_eq!(returned, builder_ptr);
        assert_eq!(builder.seed, 1234);
        assert_eq!(builder.inner.seed, 1234);
    }

    #[test]
    fn test_noise_builder_duplicate_channels_true() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.25);
        let returned = builder.duplicate_channels(true) as *mut _;
        let builder_ptr = &mut builder as *mut _;

        assert_eq!(returned, builder_ptr);
        assert_eq!(builder.inner.duplicateChannels, 1);
    }

    #[test]
    fn test_noise_builder_duplicate_channels_false() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.25);
        builder.duplicate_channels(false);

        assert_eq!(builder.inner.duplicateChannels, 0);
    }

    #[test]
    fn test_noise_build_u8_and_read() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_u8().expect("build_u8 should succeed");

        assert_eq!(noise.channels, 2);
        assert_nonzero_frames(noise.read_pcm_frames(64));
    }

    #[test]
    fn test_noise_build_i16_and_read() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_i16().expect("build_i16 should succeed");

        assert_eq!(noise.channels, 2);
        assert_nonzero_frames(noise.read_pcm_frames(64));
    }

    #[test]
    fn test_noise_build_i32_and_read() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_i32().expect("build_i32 should succeed");

        assert_eq!(noise.channels, 2);
        assert_nonzero_frames(noise.read_pcm_frames(64));
    }

    #[test]
    fn test_noise_build_s24_and_read() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_s24().expect("build_s24 should succeed");

        assert_eq!(noise.channels, 2);
        assert_nonzero_frames(noise.read_pcm_frames(64));
    }

    #[test]
    fn test_noise_build_s24_packed_and_read() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder
            .build_s24_packed()
            .expect("build_s24_packed should succeed");

        assert_eq!(noise.channels, 2);
        assert_nonzero_frames(noise.read_pcm_frames(64));
    }

    #[test]
    fn test_noise_build_f32_and_read() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_f32().expect("build_f32 should succeed");

        assert_eq!(noise.channels, 2);
        assert_nonzero_frames(noise.read_pcm_frames(64));
    }

    #[test]
    fn test_noise_read_pcm_frames_into_u8() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_u8().unwrap();

        let mut dst = vec![0u8; 64 * 2];
        let frames = noise
            .read_pcm_frames_into(&mut dst)
            .expect("read_pcm_frames_into should succeed");

        assert_eq!(frames, 64);
    }

    #[test]
    fn test_noise_read_pcm_frames_into_i16() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_i16().unwrap();

        let mut dst = vec![0i16; 64 * 2];
        let frames = noise
            .read_pcm_frames_into(&mut dst)
            .expect("read_pcm_frames_into should succeed");

        assert_eq!(frames, 64);
    }

    #[test]
    fn test_noise_read_pcm_frames_into_i32() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_i32().unwrap();

        let mut dst = vec![0i32; 64 * 2];
        let frames = noise
            .read_pcm_frames_into(&mut dst)
            .expect("read_pcm_frames_into should succeed");

        assert_eq!(frames, 64);
    }

    #[test]
    fn test_noise_read_pcm_frames_into_s24() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_s24().unwrap();

        let mut dst = vec![i32::default(); 64 * 2];
        let frames = noise
            .read_pcm_frames_into(&mut dst)
            .expect("read_pcm_frames_into should succeed");

        assert_eq!(frames, 64);
    }

    #[test]
    fn test_noise_read_pcm_frames_into_s24_packed() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_s24_packed().unwrap();

        let mut dst = vec![S24Packed::SILENCE; 64 * 3 * 2];
        let frames = noise
            .read_pcm_frames_into(&mut dst)
            .expect("read_pcm_frames_into should succeed");

        assert_eq!(frames, 64);
    }

    #[test]
    fn test_noise_read_pcm_frames_into_f32() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_f32().unwrap();

        let mut dst = vec![0.0f32; 64 * 2];
        let frames = noise
            .read_pcm_frames_into(&mut dst)
            .expect("read_pcm_frames_into should succeed");

        assert_eq!(frames, 64);
    }

    #[test]
    fn test_noise_read_pcm_frames_zero_frames() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_f32().unwrap();

        let buf = noise.read_pcm_frames(0);
        assert!(buf.is_err());
    }

    #[test]
    fn test_noise_read_pcm_frames_into_empty_slice_returns_error() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_i16().unwrap();

        let mut dst: Vec<i16> = Vec::new();

        let res = noise.read_pcm_frames_into(&mut dst);

        assert!(res.is_err());
    }

    #[test]
    fn test_noise_set_amplitude_after_build() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.1);
        let mut noise = builder.build_f32().unwrap();

        noise
            .set_amplitude(0.75)
            .expect("set_amplitude should succeed");

        let mut dst = vec![0.0f32; 32 * 2];
        let frames = noise.read_pcm_frames_into(&mut dst).unwrap();
        assert_eq!(frames, 32);
    }

    #[test]
    fn test_noise_set_seed_after_build() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.1);
        let mut noise = builder.build_f32().unwrap();

        noise.set_seed(42).expect("set_seed should succeed");

        let mut dst = vec![0.0f32; 32 * 2];
        let frames = noise.read_pcm_frames_into(&mut dst).unwrap();
        assert_eq!(frames, 32);
    }

    #[test]
    fn test_noise_seed_reproducible_for_f32() {
        let mut builder_a = NoiseBuilder::new(2, NoiseType::White, 0.5);
        builder_a.seed(12345);
        let mut noise_a = builder_a.build_f32().unwrap();

        let mut builder_b = NoiseBuilder::new(2, NoiseType::White, 0.5);
        builder_b.seed(12345);
        let mut noise_b = builder_b.build_f32().unwrap();

        let a = noise_a.read_pcm_frames(128).unwrap();
        let b = noise_b.read_pcm_frames(128).unwrap();

        assert_eq!(a.as_ref(), b.as_ref());
    }

    #[test]
    fn test_noise_duplicate_channels_true_produces_equal_channels_for_f32() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        builder.seed(999).duplicate_channels(true);
        let mut noise = builder.build_f32().unwrap();

        let buf = noise.read_pcm_frames(128).unwrap();

        for frame in buf.as_ref().chunks_exact(2) {
            assert_eq!(frame[0], frame[1]);
        }
    }

    #[test]
    fn test_noise_white_build_read_drop() {
        let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
        let mut noise = builder.build_i16().unwrap();

        let _ = noise.read_pcm_frames(256).unwrap();
    }

    #[test]
    fn test_noise_pink_build_read_drop() {
        let mut builder = NoiseBuilder::new(2, NoiseType::Pink, 0.5);
        let mut noise = builder.build_i16().unwrap();

        let _ = noise.read_pcm_frames(256).unwrap();
    }

    #[test]
    fn test_noise_brown_build_read_drop() {
        let mut builder = NoiseBuilder::new(2, NoiseType::Brown, 0.5);
        let mut noise = builder.build_i16().unwrap();

        let _ = noise.read_pcm_frames(256).unwrap();
    }

    #[test]
    fn test_noise_repeated_create_read_drop_loop() {
        for _ in 0..100 {
            let mut builder = NoiseBuilder::new(2, NoiseType::White, 0.5);
            builder.seed(7);

            let mut noise = builder.build_f32().unwrap();
            let _ = noise.read_pcm_frames(128).unwrap();

            let mut dst = vec![0.0f32; 128 * 2];
            let _ = noise.read_pcm_frames_into(&mut dst).unwrap();

            noise.set_amplitude(0.25).unwrap();
            noise.set_seed(99).unwrap();

            let _ = noise.read_pcm_frames(32).unwrap();
        }
    }
}