tunes 1.1.0

A music composition, synthesis, and audio generation 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
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! Noise generators for synthesis and modulation
//!
//! This module provides various noise generators useful for:
//! - **Audio synthesis**: Percussion, textures, ambient sounds
//! - **Modulation**: Random parameter variation
//! - **Sound design**: Natural, organic timbres
//!
//! # Examples
//!
//! ```
//! use tunes::synthesis::noise::{WhiteNoise, BrownNoise};
//!
//! // Generate white noise for hi-hat sound
//! let mut white = WhiteNoise::new();
//! let samples = white.generate(1000);
//!
//! // Generate brown noise for rumble/bass texture
//! let mut brown = BrownNoise::new();
//! let bass_texture = brown.generate(1000);
//! ```

use rand::{Rng, SeedableRng};

/// Noise types available for synthesis
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NoiseType {
    /// White noise - equal energy at all frequencies
    White,
    /// Brown noise - more low-frequency energy, random walk
    Brown,
    /// Pink noise - 1/f spectrum, balanced across octaves
    Pink,
    /// Blue noise - emphasis on high frequencies
    Blue,
    /// Green noise - tuned to human hearing, midrange emphasis
    Green,
    /// Perlin noise - coherent gradient noise, smooth and organic
    Perlin,
}

impl NoiseType {
    /// Generate noise samples of this type
    ///
    /// # Arguments
    /// * `length` - Number of samples to generate
    ///
    /// # Returns
    /// Vector of samples in the range [-1.0, 1.0]
    pub fn generate(self, length: usize) -> Vec<f32> {
        match self {
            NoiseType::White => {
                let mut gen = WhiteNoise::new();
                gen.generate(length)
            }
            NoiseType::Brown => {
                let mut gen = BrownNoise::new();
                gen.generate(length)
            }
            NoiseType::Pink => {
                let mut gen = PinkNoise::new();
                gen.generate(length)
            }
            NoiseType::Blue => {
                let mut gen = BlueNoise::new();
                gen.generate(length)
            }
            NoiseType::Green => {
                let mut gen = GreenNoise::new();
                gen.generate(length)
            }
            NoiseType::Perlin => {
                let mut gen = PerlinNoise::new();
                gen.generate(length)
            }
        }
    }
}

/// Trait for noise generators
///
/// This trait allows any noise generator to be used with TrackBuilder
/// and other parts of the library. Implement this trait for custom
/// noise generators.
pub trait NoiseGenerator {
    /// Generate a single sample in the range [-1.0, 1.0]
    fn sample(&mut self) -> f32;

    /// Generate multiple samples
    ///
    /// Default implementation calls `sample()` repeatedly.
    /// Override for more efficient bulk generation if needed.
    fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }
}

/// White noise generator - equal energy at all frequencies
///
/// White noise contains all frequencies with equal amplitude, creating
/// a "static" or "hiss" sound. It's the acoustic equivalent of white light.
///
/// # Musical Applications
/// - **Hi-hats**: Short bursts of filtered white noise
/// - **Snare drums**: Mixed with tonal components for realistic snares
/// - **Wind/rain**: Natural environmental textures
/// - **Transitions**: Noise sweeps between sections
///
/// # Example
/// ```
/// use tunes::synthesis::noise::WhiteNoise;
///
/// let mut white = WhiteNoise::new();
///
/// // Generate single samples
/// let sample1 = white.sample();
/// let sample2 = white.sample();
///
/// // Generate multiple samples at once
/// let samples = white.generate(44100); // 1 second at 44.1kHz
/// ```
#[derive(Debug)]
pub struct WhiteNoise {
    rng: rand::rngs::StdRng,
}

impl WhiteNoise {
    /// Create a new white noise generator with random seed
    pub fn new() -> Self {
        Self {
            rng: rand::rngs::StdRng::from_rng(&mut rand::rng()),
        }
    }

    /// Create a new white noise generator with a specific seed
    ///
    /// Using a seed makes the noise repeatable/deterministic.
    ///
    /// # Example
    /// ```
    /// use tunes::synthesis::noise::WhiteNoise;
    ///
    /// let mut noise1 = WhiteNoise::with_seed(12345);
    /// let mut noise2 = WhiteNoise::with_seed(12345);
    ///
    /// // Both generators produce identical output
    /// assert_eq!(noise1.sample(), noise2.sample());
    /// ```
    pub fn with_seed(seed: u64) -> Self {
        Self {
            rng: rand::rngs::StdRng::seed_from_u64(seed),
        }
    }

    /// Generate a single sample in the range [-1.0, 1.0]
    #[inline]
    pub fn sample(&mut self) -> f32 {
        self.rng.random_range(-1.0..1.0)
    }

    /// Generate multiple samples
    ///
    /// Returns a vector of `length` samples, each in the range [-1.0, 1.0].
    pub fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }
}

impl Default for WhiteNoise {
    fn default() -> Self {
        Self::new()
    }
}

/// Brown noise generator (Brownian motion / random walk)
///
/// Brown noise (also called red noise or Brownian noise) is generated by
/// integrating white noise, creating a random walk. This produces a sound
/// with more energy at low frequencies, making it deeper and more "rumbling"
/// than white noise.
///
/// The frequency spectrum decreases by 6dB per octave (1/f²), giving it
/// a warm, bassy character.
///
/// # Musical Applications
/// - **Bass textures**: Deep, organic rumbles
/// - **Ocean sounds**: Waves, water movement
/// - **Thunder**: Low-frequency atmospheric effects
/// - **Ambient drones**: Evolving low-end foundation
///
/// # Example
/// ```
/// use tunes::synthesis::noise::BrownNoise;
///
/// let mut brown = BrownNoise::new();
///
/// // Generate single samples
/// let sample = brown.sample();
///
/// // Generate bass rumble texture
/// let rumble = brown.generate(44100); // 1 second
/// ```
#[derive(Debug)]
pub struct BrownNoise {
    current: f32,
    step_size: f32,
    rng: rand::rngs::StdRng,
}

impl BrownNoise {
    /// Create a new brown noise generator with random seed
    ///
    /// Uses a default step size of 0.05 which provides good balance
    /// between smoothness and variation.
    pub fn new() -> Self {
        Self {
            current: 0.0,
            step_size: 0.05,
            rng: rand::rngs::StdRng::from_rng(&mut rand::rng()),
        }
    }

    /// Create a new brown noise generator with a specific seed
    ///
    /// Using a seed makes the noise repeatable/deterministic.
    ///
    /// # Example
    /// ```
    /// use tunes::synthesis::noise::BrownNoise;
    ///
    /// let mut noise1 = BrownNoise::with_seed(54321);
    /// let mut noise2 = BrownNoise::with_seed(54321);
    ///
    /// // Both generators produce identical output
    /// assert_eq!(noise1.sample(), noise2.sample());
    /// ```
    pub fn with_seed(seed: u64) -> Self {
        Self {
            current: 0.0,
            step_size: 0.05,
            rng: rand::rngs::StdRng::seed_from_u64(seed),
        }
    }

    /// Create a brown noise generator with custom step size
    ///
    /// Larger step sizes create more variation but can drift faster.
    /// Smaller step sizes create smoother, slower evolution.
    ///
    /// # Arguments
    /// * `step_size` - How much the value can change per sample (typically 0.01 - 0.1)
    ///
    /// # Example
    /// ```
    /// use tunes::synthesis::noise::BrownNoise;
    ///
    /// // Slower, smoother brown noise
    /// let mut smooth = BrownNoise::with_step_size(0.01);
    ///
    /// // Faster, more chaotic brown noise
    /// let mut rough = BrownNoise::with_step_size(0.1);
    /// ```
    pub fn with_step_size(step_size: f32) -> Self {
        Self {
            current: 0.0,
            step_size,
            rng: rand::rngs::StdRng::from_rng(&mut rand::rng()),
        }
    }

    /// Generate a single sample in the range [-1.0, 1.0]
    ///
    /// Uses a random walk: current value changes by a small random amount
    /// each sample, with clamping to keep output in valid range.
    #[inline]
    pub fn sample(&mut self) -> f32 {
        // Take a random step
        let delta = self.rng.random_range(-self.step_size..self.step_size);
        self.current += delta;

        // Clamp to prevent drift beyond [-1, 1]
        // Use soft clamping: reflect at boundaries for smoother behavior
        if self.current > 1.0 {
            self.current = 2.0 - self.current; // Reflect
        } else if self.current < -1.0 {
            self.current = -2.0 - self.current; // Reflect
        }

        // Additional hard clamp for safety
        self.current = self.current.clamp(-1.0, 1.0);

        self.current
    }

    /// Generate multiple samples
    ///
    /// Returns a vector of `length` samples, each in the range [-1.0, 1.0].
    pub fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }

    /// Reset the internal state to zero
    ///
    /// Useful if you want to restart the random walk from the center.
    pub fn reset(&mut self) {
        self.current = 0.0;
    }
}

impl Default for BrownNoise {
    fn default() -> Self {
        Self::new()
    }
}

// Implement NoiseGenerator trait for built-in noise types
impl NoiseGenerator for WhiteNoise {
    fn sample(&mut self) -> f32 {
        WhiteNoise::sample(self)
    }

    fn generate(&mut self, length: usize) -> Vec<f32> {
        WhiteNoise::generate(self, length)
    }
}

impl NoiseGenerator for BrownNoise {
    fn sample(&mut self) -> f32 {
        BrownNoise::sample(self)
    }

    fn generate(&mut self, length: usize) -> Vec<f32> {
        BrownNoise::generate(self, length)
    }
}

/// Pink noise generator (1/f spectrum)
///
/// Pink noise has equal energy per octave, making it sound more "balanced"
/// than white noise. The spectrum decreases by 3dB per octave (1/f).
///
/// Commonly used in:
/// - **Audio testing**: More representative of real-world sounds
/// - **Ambient soundscapes**: Natural, organic textures
/// - **Background textures**: Gentle, non-fatiguing sound
///
/// Uses the Voss-McCartney algorithm with 7 octaves for high-quality pink noise.
#[derive(Debug)]
pub struct PinkNoise {
    white: WhiteNoise,
    rows: [f32; 7],
    running_sum: f32,
    updates: usize,
}

impl PinkNoise {
    /// Create a new pink noise generator
    pub fn new() -> Self {
        Self {
            white: WhiteNoise::new(),
            rows: [0.0; 7],
            running_sum: 0.0,
            updates: 0,
        }
    }

    /// Create with a specific seed for deterministic output
    pub fn with_seed(seed: u64) -> Self {
        Self {
            white: WhiteNoise::with_seed(seed),
            rows: [0.0; 7],
            running_sum: 0.0,
            updates: 0,
        }
    }

    /// Generate a single sample
    #[inline]
    pub fn sample(&mut self) -> f32 {
        // Voss-McCartney algorithm
        let white_value = self.white.sample();

        // Update one of the generators at different rates
        // This creates the 1/f spectral characteristic
        for (i, row) in self.rows.iter_mut().enumerate() {
            if self.updates & (1 << i) == 0 {
                self.running_sum -= *row;
                *row = self.white.sample();
                self.running_sum += *row;
                break;
            }
        }
        self.updates += 1;

        // Mix white noise with running sum and normalize
        (white_value + self.running_sum) / 8.0
    }

    /// Generate multiple samples
    pub fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }
}

impl Default for PinkNoise {
    fn default() -> Self {
        Self::new()
    }
}

impl NoiseGenerator for PinkNoise {
    fn sample(&mut self) -> f32 {
        PinkNoise::sample(self)
    }

    fn generate(&mut self, length: usize) -> Vec<f32> {
        PinkNoise::generate(self, length)
    }
}

/// Blue noise generator (high-frequency emphasis)
///
/// Blue noise increases in energy at higher frequencies (+3dB per octave),
/// the opposite of pink noise. It sounds "crispy" or "sizzling".
///
/// Useful for:
/// - **Dithering**: In digital audio processing
/// - **High-frequency textures**: Sizzle, air, breath sounds
/// - **Complementary to bass**: Adds brightness without low-end buildup
#[derive(Debug)]
pub struct BlueNoise {
    white: WhiteNoise,
    prev: f32,
}

impl BlueNoise {
    /// Create a new blue noise generator
    pub fn new() -> Self {
        Self {
            white: WhiteNoise::new(),
            prev: 0.0,
        }
    }

    /// Create with a specific seed
    pub fn with_seed(seed: u64) -> Self {
        Self {
            white: WhiteNoise::with_seed(seed),
            prev: 0.0,
        }
    }

    /// Generate a single sample
    #[inline]
    pub fn sample(&mut self) -> f32 {
        // Blue noise is the derivative of white noise (differentiation emphasizes high frequencies)
        let current = self.white.sample();
        let output = current - self.prev;
        self.prev = current;

        // Normalize
        output * 0.5
    }

    /// Generate multiple samples
    pub fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }
}

impl Default for BlueNoise {
    fn default() -> Self {
        Self::new()
    }
}

impl NoiseGenerator for BlueNoise {
    fn sample(&mut self) -> f32 {
        BlueNoise::sample(self)
    }

    fn generate(&mut self, length: usize) -> Vec<f32> {
        BlueNoise::generate(self, length)
    }
}

/// Green noise generator (natural/organic sound)
///
/// Green noise emphasizes frequencies in the midrange (around 500Hz),
/// resembling natural environmental sounds. It's similar to pink noise
/// but with a peak tuned to human hearing sensitivity.
///
/// Applications:
/// - **Nature sounds**: Rustling leaves, gentle rain
/// - **Relaxation/meditation**: Pleasant, non-fatiguing background
/// - **Organic textures**: Natural-sounding synthesis
#[derive(Debug)]
pub struct GreenNoise {
    pink: PinkNoise,
    prev: f32,
}

impl GreenNoise {
    /// Create a new green noise generator
    pub fn new() -> Self {
        Self {
            pink: PinkNoise::new(),
            prev: 0.0,
        }
    }

    /// Create with a specific seed
    pub fn with_seed(seed: u64) -> Self {
        Self {
            pink: PinkNoise::with_seed(seed),
            prev: 0.0,
        }
    }

    /// Generate a single sample
    #[inline]
    pub fn sample(&mut self) -> f32 {
        // Green noise is pink noise with additional midrange emphasis
        // Simple approach: pink noise with a gentle highpass
        let pink_sample = self.pink.sample();

        // Light highpass filter to remove very low frequencies
        let highpassed = pink_sample - 0.5 * self.prev;
        self.prev = pink_sample;

        // Mix back some of the original for warmth
        0.7 * pink_sample + 0.3 * highpassed
    }

    /// Generate multiple samples
    pub fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }
}

impl Default for GreenNoise {
    fn default() -> Self {
        Self::new()
    }
}

impl NoiseGenerator for GreenNoise {
    fn sample(&mut self) -> f32 {
        GreenNoise::sample(self)
    }

    fn generate(&mut self, length: usize) -> Vec<f32> {
        GreenNoise::generate(self, length)
    }
}

/// Perlin noise generator (coherent gradient noise)
///
/// Perlin noise produces smooth, organic, continuous noise using gradient
/// interpolation. Unlike spectral noise types (white, pink, etc.), Perlin
/// noise is coherent - values change smoothly over time creating natural-
/// sounding curves.
///
/// This implementation uses 1D Perlin noise optimized for audio synthesis,
/// using the same algorithm as the project's generative sequences.
///
/// Applications:
/// - **Modulation**: Smooth, organic parameter variation (vibrato, filter sweeps)
/// - **Wind/breath sounds**: Natural, flowing air sounds
/// - **Organic textures**: Evolving pads, ambient drones
/// - **LFO replacement**: More natural than sine/triangle waves
///
/// # Example
/// ```
/// use tunes::synthesis::noise::PerlinNoise;
///
/// // Create Perlin noise for smooth modulation
/// let mut perlin = PerlinNoise::new();
///
/// // Adjust frequency for faster/slower variation
/// let mut fast = PerlinNoise::with_frequency(0.1); // Faster changes
/// let mut slow = PerlinNoise::with_frequency(0.01); // Slower changes
/// ```
#[derive(Debug)]
pub struct PerlinNoise {
    /// Current position in noise space
    x: f32,
    /// How fast to move through noise space (frequency)
    frequency: f32,
    /// Random seed for gradient generation
    seed: u32,
}

impl PerlinNoise {
    /// Create a new Perlin noise generator with random seed
    pub fn new() -> Self {
        let mut rng = rand::rngs::StdRng::from_rng(&mut rand::rng());
        Self {
            x: 0.0,
            frequency: 0.02, // Default frequency for smooth variation
            seed: rng.random_range(0..u32::MAX),
        }
    }

    /// Create with a specific seed for deterministic output
    pub fn with_seed(seed: u32) -> Self {
        Self {
            x: 0.0,
            frequency: 0.02,
            seed,
        }
    }

    /// Create with custom frequency
    ///
    /// # Arguments
    /// * `frequency` - Speed of variation (typically 0.001 - 0.1)
    ///   - Lower values = slower, smoother changes
    ///   - Higher values = faster, more rapid variation
    ///
    /// # Example
    /// ```
    /// use tunes::synthesis::noise::PerlinNoise;
    ///
    /// // Very slow, smooth modulation
    /// let mut lfo = PerlinNoise::with_frequency(0.005);
    ///
    /// // Faster variation for texture
    /// let mut texture = PerlinNoise::with_frequency(0.05);
    /// ```
    pub fn with_frequency(frequency: f32) -> Self {
        let mut rng = rand::rngs::StdRng::from_rng(&mut rand::rng());
        Self {
            x: 0.0,
            frequency,
            seed: rng.random_range(0..u32::MAX),
        }
    }

    /// Set the frequency (speed of variation)
    pub fn set_frequency(&mut self, frequency: f32) {
        self.frequency = frequency;
    }

    /// Reset position to the beginning
    pub fn reset(&mut self) {
        self.x = 0.0;
    }

    /// Generate pseudo-random gradient at integer point
    /// Uses same hash function as sequences::perlin_noise for consistency
    #[inline]
    fn gradient(&self, x: i32) -> f32 {
        let mut hash = x.wrapping_mul(374761393) as u32;
        hash = hash.wrapping_add(self.seed);
        hash = hash.wrapping_mul(1103515245);
        hash = hash.wrapping_add(12345);
        hash = (hash >> 16) & 0x7fff;

        // Map to [-1, 1] gradient
        (hash as f32 / 16383.5) - 1.0
    }

    /// Fade function for smooth interpolation (smoothstep)
    /// Same as sequences::perlin_noise
    #[inline]
    fn fade(t: f32) -> f32 {
        // 6t^5 - 15t^4 + 10t^3
        t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
    }

    /// Linear interpolation
    #[inline]
    fn lerp(a: f32, b: f32, t: f32) -> f32 {
        a + t * (b - a)
    }

    /// Generate a single sample in range [-1.0, 1.0]
    #[inline]
    pub fn sample(&mut self) -> f32 {
        // Get integer and fractional parts
        let xi = self.x.floor() as i32;
        let xf = self.x - xi as f32;

        // Smooth interpolation curve
        let sx = Self::fade(xf);

        // Get gradients at integer points
        let g0 = self.gradient(xi);
        let g1 = self.gradient(xi + 1);

        // Calculate dot products (in 1D, just multiplication)
        let d0 = g0 * xf;
        let d1 = g1 * (xf - 1.0);

        // Interpolate
        let result = Self::lerp(d0, d1, sx);

        // Advance position
        self.x += self.frequency;

        result
    }

    /// Generate multiple samples
    pub fn generate(&mut self, length: usize) -> Vec<f32> {
        (0..length).map(|_| self.sample()).collect()
    }
}

impl Default for PerlinNoise {
    fn default() -> Self {
        Self::new()
    }
}

impl NoiseGenerator for PerlinNoise {
    fn sample(&mut self) -> f32 {
        PerlinNoise::sample(self)
    }

    fn generate(&mut self, length: usize) -> Vec<f32> {
        PerlinNoise::generate(self, length)
    }
}

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

    #[test]
    fn test_white_noise_range() {
        let mut white = WhiteNoise::new();

        // Generate 1000 samples and verify all are in valid range
        for _ in 0..1000 {
            let sample = white.sample();
            assert!(
                sample >= -1.0 && sample <= 1.0,
                "White noise sample {} out of range",
                sample
            );
        }
    }

    #[test]
    fn test_white_noise_is_random() {
        let mut white = WhiteNoise::new();

        // Generate samples and verify they're not all the same
        let samples: Vec<f32> = (0..100).map(|_| white.sample()).collect();

        // Check that we have variation (not all samples identical)
        let first = samples[0];
        let has_variation = samples.iter().any(|&s| (s - first).abs() > 0.1);
        assert!(has_variation, "White noise should have variation");
    }

    #[test]
    fn test_white_noise_seeded_deterministic() {
        let mut noise1 = WhiteNoise::with_seed(12345);
        let mut noise2 = WhiteNoise::with_seed(12345);

        // Same seed should produce identical output
        for _ in 0..100 {
            assert_eq!(
                noise1.sample(),
                noise2.sample(),
                "Seeded white noise should be deterministic"
            );
        }
    }

    #[test]
    fn test_white_noise_generate() {
        let mut white = WhiteNoise::new();
        let samples = white.generate(500);

        assert_eq!(samples.len(), 500);

        // All samples should be in valid range
        for &sample in &samples {
            assert!(sample >= -1.0 && sample <= 1.0);
        }
    }

    #[test]
    fn test_brown_noise_range() {
        let mut brown = BrownNoise::new();

        // Generate 10000 samples to test clamping under stress
        for i in 0..10000 {
            let sample = brown.sample();
            assert!(
                sample >= -1.0 && sample <= 1.0,
                "Brown noise sample {} out of range at iteration {}",
                sample,
                i
            );
        }
    }

    #[test]
    fn test_brown_noise_continuity() {
        let mut brown = BrownNoise::new();

        // Brown noise should be continuous (adjacent samples close together)
        let mut prev = brown.sample();

        for _ in 0..100 {
            let current = brown.sample();

            // Adjacent samples shouldn't differ by more than step_size
            let diff = (current - prev).abs();
            assert!(
                diff <= 0.1, // step_size default is 0.05, but with reflection can be slightly more
                "Brown noise discontinuity: {} to {} (diff: {})",
                prev,
                current,
                diff
            );

            prev = current;
        }
    }

    #[test]
    fn test_brown_noise_seeded_deterministic() {
        let mut noise1 = BrownNoise::with_seed(54321);
        let mut noise2 = BrownNoise::with_seed(54321);

        // Same seed should produce identical output
        for _ in 0..100 {
            assert_eq!(
                noise1.sample(),
                noise2.sample(),
                "Seeded brown noise should be deterministic"
            );
        }
    }

    #[test]
    fn test_brown_noise_reset() {
        let mut brown = BrownNoise::new();

        // Generate some samples to move away from zero
        for _ in 0..100 {
            brown.sample();
        }

        // Reset should return to zero
        brown.reset();
        assert_eq!(brown.current, 0.0, "Reset should set current to 0");
    }

    #[test]
    fn test_brown_noise_step_size() {
        let mut smooth = BrownNoise::with_step_size(0.01);
        let mut rough = BrownNoise::with_step_size(0.1);

        // Smooth should have smaller variations
        let smooth_samples = smooth.generate(100);
        let rough_samples = rough.generate(100);

        // Calculate variance
        let smooth_variance = calculate_variance(&smooth_samples);
        let rough_variance = calculate_variance(&rough_samples);

        // Rough should generally have more variance
        // (This is probabilistic, but with 100 samples should be reliable)
        assert!(
            rough_variance > smooth_variance * 0.5,
            "Larger step size should create more variance"
        );
    }

    #[test]
    fn test_brown_noise_generate() {
        let mut brown = BrownNoise::new();
        let samples = brown.generate(500);

        assert_eq!(samples.len(), 500);

        // All samples should be in valid range
        for &sample in &samples {
            assert!(sample >= -1.0 && sample <= 1.0);
        }
    }

    #[test]
    fn test_brown_noise_low_frequency_bias() {
        // Use seeded generator for deterministic test
        let mut brown = BrownNoise::with_seed(42);
        // Generate more samples for stable statistics
        let samples = brown.generate(10000);

        // Brown noise should be more "smooth" than white noise
        // Check that adjacent samples are correlated
        let mut correlation_sum = 0.0;
        for i in 0..samples.len() - 1 {
            correlation_sum += samples[i] * samples[i + 1];
        }
        let avg_correlation = correlation_sum / (samples.len() - 1) as f32;

        // Adjacent samples should be positively correlated (smooth evolution)
        // White noise would have near-zero correlation
        // With 10k samples and seed 42, correlation should be strong and stable
        assert!(
            avg_correlation.abs() > 0.05,
            "Brown noise should show adjacent sample correlation, got: {}",
            avg_correlation
        );
    }

    // Helper function for variance calculation
    fn calculate_variance(samples: &[f32]) -> f32 {
        let mean: f32 = samples.iter().sum::<f32>() / samples.len() as f32;
        let variance: f32 = samples
            .iter()
            .map(|&x| {
                let diff = x - mean;
                diff * diff
            })
            .sum::<f32>()
            / samples.len() as f32;
        variance
    }

    #[test]
    fn test_pink_noise_range() {
        let mut pink = PinkNoise::new();

        for _ in 0..1000 {
            let sample = pink.sample();
            assert!(
                sample >= -1.0 && sample <= 1.0,
                "Pink noise sample {} out of range",
                sample
            );
        }
    }

    #[test]
    fn test_pink_noise_seeded_deterministic() {
        let mut noise1 = PinkNoise::with_seed(12345);
        let mut noise2 = PinkNoise::with_seed(12345);

        for _ in 0..100 {
            assert_eq!(
                noise1.sample(),
                noise2.sample(),
                "Seeded pink noise should be deterministic"
            );
        }
    }

    #[test]
    fn test_blue_noise_range() {
        let mut blue = BlueNoise::new();

        for _ in 0..1000 {
            let sample = blue.sample();
            assert!(
                sample >= -1.0 && sample <= 1.0,
                "Blue noise sample {} out of range",
                sample
            );
        }
    }

    #[test]
    fn test_blue_noise_seeded_deterministic() {
        let mut noise1 = BlueNoise::with_seed(54321);
        let mut noise2 = BlueNoise::with_seed(54321);

        for _ in 0..100 {
            assert_eq!(
                noise1.sample(),
                noise2.sample(),
                "Seeded blue noise should be deterministic"
            );
        }
    }

    #[test]
    fn test_green_noise_range() {
        let mut green = GreenNoise::new();

        for _ in 0..1000 {
            let sample = green.sample();
            assert!(
                sample >= -1.0 && sample <= 1.0,
                "Green noise sample {} out of range",
                sample
            );
        }
    }

    #[test]
    fn test_green_noise_seeded_deterministic() {
        let mut noise1 = GreenNoise::with_seed(11111);
        let mut noise2 = GreenNoise::with_seed(11111);

        for _ in 0..100 {
            assert_eq!(
                noise1.sample(),
                noise2.sample(),
                "Seeded green noise should be deterministic"
            );
        }
    }

    #[test]
    fn test_perlin_noise_range() {
        let mut perlin = PerlinNoise::new();

        for _ in 0..1000 {
            let sample = perlin.sample();
            assert!(
                sample >= -1.0 && sample <= 1.0,
                "Perlin noise sample {} out of range",
                sample
            );
        }
    }

    #[test]
    fn test_perlin_noise_smoothness() {
        // Perlin noise should be smooth - no huge jumps
        let mut perlin = PerlinNoise::with_seed(42);
        let samples = perlin.generate(200);

        for i in 1..samples.len() {
            let diff = (samples[i] - samples[i - 1]).abs();
            // With default frequency 0.02, changes should be very small
            assert!(
                diff < 0.1,
                "Perlin noise jump too large: {} at index {}",
                diff,
                i
            );
        }
    }

    #[test]
    fn test_perlin_noise_seeded_deterministic() {
        let mut noise1 = PerlinNoise::with_seed(99999);
        let mut noise2 = PerlinNoise::with_seed(99999);

        for _ in 0..100 {
            assert_eq!(
                noise1.sample(),
                noise2.sample(),
                "Seeded Perlin noise should be deterministic"
            );
        }
    }

    #[test]
    fn test_perlin_noise_frequency() {
        // Higher frequency should create faster variation
        let mut slow = PerlinNoise::with_seed(42);
        slow.set_frequency(0.01);

        let mut fast = PerlinNoise::with_seed(42);
        fast.set_frequency(0.1);

        let slow_samples = slow.generate(100);
        let fast_samples = fast.generate(100);

        // Measure total variation
        let slow_var: f32 = slow_samples.windows(2).map(|w| (w[1] - w[0]).abs()).sum();
        let fast_var: f32 = fast_samples.windows(2).map(|w| (w[1] - w[0]).abs()).sum();

        assert!(
            fast_var > slow_var,
            "Higher frequency should create more variation: slow={}, fast={}",
            slow_var,
            fast_var
        );
    }

    #[test]
    fn test_perlin_noise_reset() {
        let mut perlin = PerlinNoise::with_seed(777);

        let first_sample = perlin.sample();

        // Generate more samples
        for _ in 0..50 {
            perlin.sample();
        }

        // Reset and check we get the same first sample
        perlin.reset();
        let reset_sample = perlin.sample();

        assert_eq!(
            first_sample,
            reset_sample,
            "Reset should return to beginning"
        );
    }

    #[test]
    fn test_noise_type_enum() {
        // Test that all noise types can be generated
        let white_samples = NoiseType::White.generate(10);
        let brown_samples = NoiseType::Brown.generate(10);
        let pink_samples = NoiseType::Pink.generate(10);
        let blue_samples = NoiseType::Blue.generate(10);
        let green_samples = NoiseType::Green.generate(10);
        let perlin_samples = NoiseType::Perlin.generate(10);

        assert_eq!(white_samples.len(), 10);
        assert_eq!(brown_samples.len(), 10);
        assert_eq!(pink_samples.len(), 10);
        assert_eq!(blue_samples.len(), 10);
        assert_eq!(green_samples.len(), 10);
        assert_eq!(perlin_samples.len(), 10);
    }
}