dasp_signal/
lib.rs

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
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
//! Use the [**Signal**](./trait.Signal.html) trait to abstract over infinite-iterator-like types
//! that yield **Frame**s. The **Signal** trait provides methods for adding, scaling, offsetting,
//! multiplying, clipping, generating frame iterators and more.
//!
//! You may also find a series of **Signal** source functions, including:
//!
//! - [equilibrium](./fn.equilibrium.html) for generating "silent" frames.
//! - [phase](./fn.phase.html) for a stepping phase, useful for oscillators.
//! - [sine](./fn.sine.html) for generating a sine waveform.
//! - [saw](./fn.saw.html) for generating a sawtooth waveform.
//! - [square](./fn.square.html) for generating a square waveform.
//! - [noise](./fn.noise.html) for generating a noise waveform.
//! - [noise_simplex](./fn.noise_simplex.html) for generating a 1D simplex noise waveform.
//! - [gen](./fn.gen.html) for generating frames of type F from some `Fn() -> F`.
//! - [gen_mut](./fn.gen_mut.html) for generating frames of type F from some `FnMut() -> F`.
//! - [from_iter](./fn.from_iter.html) for converting an iterator yielding frames to a signal.
//! - [from_interleaved_samples_iter](./fn.from_interleaved_samples_iter.html) for converting an
//! iterator yielding interleaved samples to a signal.
//!
//! Working with **Signal**s allows for easy, readable creation of rich and complex DSP graphs with
//! a simple and familiar API.
//!
//! ### Optional Features
//!
//! - The **boxed** feature (or **signal-boxed** feature if using `dasp`) provides a **Signal**
//!   implementation for `Box<dyn Signal>`.
//! - The **bus** feature (or **signal-bus** feature if using `dasp`) provides the
//!   [**SignalBus**](./bus/trait.SignalBus.html) trait.
//! - The **envelope** feature (or **signal-envelope** feature if using `dasp`) provides the
//!   [**SignalEnvelope**](./envelope/trait.SignalEnvelope.html) trait.
//! - The **rms** feature (or **signal-rms** feature if using `dasp`) provides the
//!   [**SignalRms**](./rms/trait.SignalRms.html) trait.
//! - The **window** feature (or **signal-window** feature if using `dasp`) provides the
//!   [**window**](./window/index.html) module.
//!
//! ### no_std
//!
//! If working in a `no_std` context, you can disable the default **std** feature with
//! `--no-default-features`.
//!
//! To enable all of the above features in a `no_std` context, enable the **all-no-std** feature.

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]

#[cfg(not(feature = "std"))]
extern crate alloc;

use core;
use core::cell::RefCell;
use dasp_frame::Frame;
use dasp_interpolate::Interpolator;
use dasp_ring_buffer as ring_buffer;
use dasp_sample::{Duplex, Sample};
use interpolate::Converter;

pub mod interpolate;
mod ops;

#[cfg(features = "boxed")]
mod boxed;
#[cfg(feature = "bus")]
pub mod bus;
#[cfg(feature = "envelope")]
pub mod envelope;
#[cfg(feature = "rms")]
pub mod rms;
#[cfg(feature = "window")]
pub mod window;

#[cfg(not(feature = "std"))]
type Rc<T> = alloc::rc::Rc<T>;
#[cfg(feature = "std")]
type Rc<T> = std::rc::Rc<T>;

/// Types that yield `Frame`s of a one-or-more-channel PCM signal.
///
/// For example, `Signal` allows us to add two signals, modulate a signal's amplitude by another
/// signal, scale a signal's amplitude and much more.
///
/// The **Signal** trait is inspired by the `Iterator` trait but is different in the sense that it
/// will always yield frames and will never return `None`. That said, implementors of `Signal` may
/// optionally indicate exhaustian via the `is_exhausted` method. This allows for converting
/// exhaustive signals back to iterators that are well behaved. Calling **next** on an exhausted
/// signal should always yield `Self::Frame::EQUILIBRIUM`.
pub trait Signal {
    /// The `Frame` type returned by the `Signal`.
    type Frame: Frame;

    /// Yield the next `Frame` in the `Signal`.
    ///
    /// # Example
    ///
    /// An example of a mono (single-channel) signal.
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0.2, -0.6, 0.4];
    ///     let mut signal = signal::from_iter(frames.iter().cloned());
    ///     assert_eq!(signal.next(), 0.2);
    ///     assert_eq!(signal.next(), -0.6);
    ///     assert_eq!(signal.next(), 0.4);
    /// }
    /// ```
    ///
    /// An example of a stereo (dual-channel) signal.
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [[0.2, 0.2], [-0.6, -0.6], [0.4, 0.4]];
    ///     let mut signal = signal::from_iter(frames.iter().cloned());
    ///     assert_eq!(signal.next(), [0.2, 0.2]);
    ///     assert_eq!(signal.next(), [-0.6, -0.6]);
    ///     assert_eq!(signal.next(), [0.4, 0.4]);
    /// }
    /// ```
    fn next(&mut self) -> Self::Frame;

    /// Whether or not the signal is exhausted of meaningful frames.
    ///
    /// By default, this returns `false` and assumes that the `Signal` is infinite.
    ///
    /// As an example, `signal::FromIterator` becomes exhausted once the inner `Iterator` has been
    /// exhausted. `Sine` on the other hand will always return `false` as it will produce
    /// meaningful values infinitely.
    ///
    /// It should be rare for users to need to call this method directly, unless they are
    /// implementing their own custom `Signal`s. Instead, idiomatic code will tend toward the
    /// `Signal::until_exhasted` method which produces an `Iterator` that yields `Frame`s until
    /// `Signal::is_exhausted` returns `true`.
    ///
    /// Adaptors that source frames from more than one signal (`AddAmp`, `MulHz`, etc) will return
    /// `true` if *any* of the source signals return `true`. In this sense exhaustiveness is
    /// contagious. This can be likened to the way that `Iterator::zip` begins returning `None`
    /// when either `A` or `B` begins returning `None`.
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     // Infinite signals always return `false`.
    ///     let sine_signal = signal::rate(44_100.0).const_hz(400.0).sine();
    ///     assert_eq!(sine_signal.is_exhausted(), false);
    ///
    ///     // Signals over iterators return `true` when the inner iterator is exhausted.
    ///     let frames = [0.2, -0.6, 0.4];
    ///     let mut iter_signal = signal::from_iter(frames.iter().cloned());
    ///     assert_eq!(iter_signal.is_exhausted(), false);
    ///     iter_signal.by_ref().take(3).count();
    ///     assert_eq!(iter_signal.is_exhausted(), true);
    ///
    ///     // Adaptors return `true` when the first signal becomes exhausted.
    ///     let a = [1, 2];
    ///     let b = [1, 2, 3, 4];
    ///     let a_signal = signal::from_iter(a.iter().cloned());
    ///     let b_signal = signal::from_iter(b.iter().cloned());
    ///     let mut added = a_signal.add_amp(b_signal);
    ///     assert_eq!(added.is_exhausted(), false);
    ///     added.by_ref().take(2).count();
    ///     assert_eq!(added.is_exhausted(), true);
    /// }
    /// ```
    #[inline]
    fn is_exhausted(&self) -> bool {
        false
    }

    /// A signal that maps one set of frames to another.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = signal::gen(|| 0.5);
    ///     let mut mapper = frames.map(|f| [f, 0.25]);
    ///     assert_eq!(mapper.next(), [0.5, 0.25]);
    ///     assert_eq!(mapper.next(), [0.5, 0.25]);
    ///     assert_eq!(mapper.next(), [0.5, 0.25]);
    /// }
    /// ```
    ///
    /// This can also be useful for monitoring the peak values of a signal.
    ///
    /// ```
    /// use dasp_frame::Frame;
    /// use dasp_peak as peak;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let sine_wave = signal::rate(4.0).const_hz(1.0).sine();
    ///     let mut peak = sine_wave
    ///         .map(peak::full_wave)
    ///         .map(|f| f.round());
    ///     assert_eq!(
    ///         peak.take(4).collect::<Vec<_>>(),
    ///         vec![0.0, 1.0, 0.0, 1.0]
    ///     );
    /// }
    /// ```
    fn map<M, F>(self, map: M) -> Map<Self, M, F>
    where
        Self: Sized,
        M: FnMut(Self::Frame) -> F,
        F: Frame,
    {
        Map {
            signal: self,
            map: map,
            frame: core::marker::PhantomData,
        }
    }

    /// A signal that maps one set of frames to another.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = signal::gen(|| 0.5);
    ///     let more_frames = signal::gen(|| 0.25);
    ///     let mut mapper = frames.zip_map(more_frames, |f, o| [f, o]);
    ///     assert_eq!(mapper.next(), [0.5, 0.25]);
    ///     assert_eq!(mapper.next(), [0.5, 0.25]);
    ///     assert_eq!(mapper.next(), [0.5, 0.25]);
    /// }
    /// ```
    fn zip_map<O, M, F>(self, other: O, map: M) -> ZipMap<Self, O, M, F>
    where
        Self: Sized,
        M: FnMut(Self::Frame, O::Frame) -> F,
        O: Signal,
        F: Frame,
    {
        ZipMap {
            this: self,
            map: map,
            other: other,
            frame: core::marker::PhantomData,
        }
    }

    /// Provides an iterator that yields the sum of the frames yielded by both `other` and `self`
    /// in lock-step.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let a = [0.2, -0.6, 0.4];
    ///     let b = [0.2, 0.1, -0.8];
    ///     let a_signal = signal::from_iter(a.iter().cloned());
    ///     let b_signal = signal::from_iter(b.iter().cloned());
    ///     let added: Vec<_> = a_signal.add_amp(b_signal).take(3).collect();
    ///     assert_eq!(added, vec![0.4, -0.5, -0.4]);
    /// }
    /// ```
    #[inline]
    fn add_amp<S>(self, other: S) -> AddAmp<Self, S>
    where
        Self: Sized,
        S: Signal,
        S::Frame: Frame<
            Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed,
            NumChannels = <Self::Frame as Frame>::NumChannels,
        >,
    {
        AddAmp { a: self, b: other }
    }

    /// Provides an iterator that yields the product of the frames yielded by both `other` and
    /// `self` in lock-step.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let a = [0.25, -0.8, -0.5];
    ///     let b = [0.2, 0.5, 0.8];
    ///     let a_signal = signal::from_iter(a.iter().cloned());
    ///     let b_signal = signal::from_iter(b.iter().cloned());
    ///     let added: Vec<_> = a_signal.mul_amp(b_signal).take(3).collect();
    ///     assert_eq!(added, vec![0.05, -0.4, -0.4]);
    /// }
    /// ```
    #[inline]
    fn mul_amp<S>(self, other: S) -> MulAmp<Self, S>
    where
        Self: Sized,
        S: Signal,
        S::Frame: Frame<
            Sample = <<Self::Frame as Frame>::Sample as Sample>::Float,
            NumChannels = <Self::Frame as Frame>::NumChannels,
        >,
    {
        MulAmp { a: self, b: other }
    }

    /// Provides an iterator that offsets the amplitude of every channel in each frame of the
    /// signal by some sample value and yields the resulting frames.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [[0.25, 0.4], [-0.2, -0.5]];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let offset: Vec<_> = signal.offset_amp(0.5).take(2).collect();
    ///     assert_eq!(offset, vec![[0.75, 0.9], [0.3, 0.0]]);
    /// }
    /// ```
    #[inline]
    fn offset_amp(
        self,
        offset: <<Self::Frame as Frame>::Sample as Sample>::Signed,
    ) -> OffsetAmp<Self>
    where
        Self: Sized,
    {
        OffsetAmp {
            signal: self,
            offset: offset,
        }
    }

    /// Produces an `Iterator` that scales the amplitude of the sample of each channel in every
    /// `Frame` yielded by `self` by the given amplitude.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0.2, -0.5, -0.4, 0.3];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let scaled: Vec<_> = signal.scale_amp(2.0).take(4).collect();
    ///     assert_eq!(scaled, vec![0.4, -1.0, -0.8, 0.6]);
    /// }
    /// ```
    #[inline]
    fn scale_amp(self, amp: <<Self::Frame as Frame>::Sample as Sample>::Float) -> ScaleAmp<Self>
    where
        Self: Sized,
    {
        ScaleAmp {
            signal: self,
            amp: amp,
        }
    }

    /// Produces a new `Signal` that offsets the amplitude of every `Frame` in `self` by the
    /// respective amplitudes in each channel of the given `amp_frame`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [[0.5, 0.3], [-0.25, 0.9]];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let offset: Vec<_> = signal.offset_amp_per_channel([0.25, -0.5]).take(2).collect();
    ///     assert_eq!(offset, vec![[0.75, -0.2], [0.0, 0.4]]);
    /// }
    /// ```
    #[inline]
    fn offset_amp_per_channel<F>(self, amp_frame: F) -> OffsetAmpPerChannel<Self, F>
    where
        Self: Sized,
        F: Frame<
            Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed,
            NumChannels = <Self::Frame as Frame>::NumChannels,
        >,
    {
        OffsetAmpPerChannel {
            signal: self,
            amp_frame: amp_frame,
        }
    }

    /// Produces a new `Signal` that scales the amplitude of every `Frame` in `self` by the
    /// respective amplitudes in each channel of the given `amp_frame`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [[0.2, -0.5], [-0.4, 0.3]];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let scaled: Vec<_> = signal.scale_amp_per_channel([0.5, 2.0]).take(2).collect();
    ///     assert_eq!(scaled, vec![[0.1, -1.0], [-0.2, 0.6]]);
    /// }
    /// ```
    #[inline]
    fn scale_amp_per_channel<F>(self, amp_frame: F) -> ScaleAmpPerChannel<Self, F>
    where
        Self: Sized,
        F: Frame<
            Sample = <<Self::Frame as Frame>::Sample as Sample>::Float,
            NumChannels = <Self::Frame as Frame>::NumChannels,
        >,
    {
        ScaleAmpPerChannel {
            signal: self,
            amp_frame: amp_frame,
        }
    }

    /// Multiplies the rate at which frames of `self` are yielded by the given `signal`.
    ///
    /// This happens by wrapping `self` in a `rate::Converter` and calling `set_playback_hz_scale`
    /// with each value yielded by `signal`
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_interpolate::linear::Linear;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let foo = [0.0, 1.0, 0.0, -1.0];
    ///     let mul = [1.0, 1.0, 0.5, 0.5, 0.5, 0.5];
    ///     let mut source = signal::from_iter(foo.iter().cloned());
    ///     let a = source.next();
    ///     let b = source.next();
    ///     let interp = Linear::new(a, b);
    ///     let hz_signal = signal::from_iter(mul.iter().cloned());
    ///     let frames: Vec<_> = source.mul_hz(interp, hz_signal).take(6).collect();
    ///     assert_eq!(&frames[..], &[0.0, 1.0, 0.0, -0.5, -1.0, -0.5][..]);
    /// }
    /// ```
    fn mul_hz<M, I>(self, interpolator: I, mul_per_frame: M) -> MulHz<Self, M, I>
    where
        Self: Sized,
        M: Signal<Frame = f64>,
        I: Interpolator,
    {
        MulHz {
            signal: Converter::scale_playback_hz(self, interpolator, 1.0),
            mul_per_frame: mul_per_frame,
        }
    }

    /// Converts the rate at which frames of the `Signal` are yielded using interpolation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_interpolate::linear::Linear;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let foo = [0.0, 1.0, 0.0, -1.0];
    ///     let mut source = signal::from_iter(foo.iter().cloned());
    ///     let a = source.next();
    ///     let b = source.next();
    ///     let interp = Linear::new(a, b);
    ///     let frames: Vec<_> = source.from_hz_to_hz(interp, 1.0, 2.0).take(8).collect();
    ///     assert_eq!(&frames[..], &[0.0, 0.5, 1.0, 0.5, 0.0, -0.5, -1.0, -0.5][..]);
    /// }
    /// ```
    fn from_hz_to_hz<I>(self, interpolator: I, source_hz: f64, target_hz: f64) -> Converter<Self, I>
    where
        Self: Sized,
        I: Interpolator,
    {
        Converter::from_hz_to_hz(self, interpolator, source_hz, target_hz)
    }

    /// Multiplies the rate at which frames of the `Signal` are yielded by the given value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_interpolate::linear::Linear;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let foo = [0.0, 1.0, 0.0, -1.0];
    ///     let mut source = signal::from_iter(foo.iter().cloned());
    ///     let a = source.next();
    ///     let b = source.next();
    ///     let interp = Linear::new(a, b);
    ///     let frames: Vec<_> = source.scale_hz(interp, 0.5).take(8).collect();
    ///     assert_eq!(&frames[..], &[0.0, 0.5, 1.0, 0.5, 0.0, -0.5, -1.0, -0.5][..]);
    /// }
    /// ```
    fn scale_hz<I>(self, interpolator: I, multi: f64) -> Converter<Self, I>
    where
        Self: Sized,
        I: Interpolator,
    {
        Converter::scale_playback_hz(self, interpolator, multi)
    }

    /// Delays the `Signal` by the given number of frames.
    ///
    /// The delay is performed by yielding `Frame::EQUILIBRIUM` `n_frames` times before
    /// continuing to yield frames from `signal`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0.2, 0.4];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let delayed: Vec<_> = signal.delay(2).take(4).collect();
    ///     assert_eq!(delayed, vec![0.0, 0.0, 0.2, 0.4]);
    /// }
    /// ```
    fn delay(self, n_frames: usize) -> Delay<Self>
    where
        Self: Sized,
    {
        Delay {
            signal: self,
            n_frames: n_frames,
        }
    }

    /// Converts a `Signal` into a type that yields the interleaved `Sample`s.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [[0.1, 0.2], [0.3, 0.4]];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let samples = signal.into_interleaved_samples();
    ///     let samples: Vec<_> = samples.into_iter().take(4).collect();
    ///     assert_eq!(samples, vec![0.1, 0.2, 0.3, 0.4]);
    /// }
    /// ```
    fn into_interleaved_samples(mut self) -> IntoInterleavedSamples<Self>
    where
        Self: Sized,
    {
        let first = self.next().channels();
        IntoInterleavedSamples {
            signal: self,
            current_frame: first,
        }
    }

    /// Clips the amplitude of each channel in each `Frame` yielded by `self` to the given
    /// threshold amplitude.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [[1.2, 0.8], [-0.7, -1.4]];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let clipped: Vec<_> = signal.clip_amp(0.9).take(2).collect();
    ///     assert_eq!(clipped, vec![[0.9, 0.8], [-0.7, -0.9]]);
    /// }
    /// ```
    fn clip_amp(self, thresh: <<Self::Frame as Frame>::Sample as Sample>::Signed) -> ClipAmp<Self>
    where
        Self: Sized,
    {
        ClipAmp {
            signal: self,
            thresh: thresh,
        }
    }

    /// Create a new `Signal` that calls the enclosing function on each iteration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let mut f = 0.0;
    ///     let mut signal = signal::gen_mut(move || {
    ///         f += 0.1;
    ///         f
    ///     });
    ///     let func = |x: &f64| {
    ///         assert_eq!(*x, 0.1);
    ///     };
    ///     let mut inspected = signal.inspect(func);
    ///     let out = inspected.next();
    ///     assert_eq!(out, 0.1);
    /// }
    /// ```
    fn inspect<F>(self, inspect: F) -> Inspect<Self, F>
    where
        Self: Sized,
        F: FnMut(&Self::Frame),
    {
        Inspect {
            signal: self,
            inspect: inspect,
        }
    }

    /// Forks `Self` into two signals that produce the same frames.
    ///
    /// The given `ring_buffer` must be empty to ensure correct behaviour.
    ///
    /// Each time a frame is requested from the signal on one branch, that frame will be pushed to
    /// the given `ring_buffer` of pending frames to be collected by the other branch and a flag
    /// will be set to indicate that there are pending frames.
    ///
    /// **Fork** can be used to share the queue between the two branches by reference
    /// `fork.by_ref()` or via a reference counted pointer `fork.by_rc()`.
    ///
    /// **Fork** is a slightly more efficient alternative to **Bus** when only two branches are
    /// required.
    ///
    /// **Note:** It is up to the user to ensure that there are never more than
    /// `ring_buffer.max_len()` pending frames - otherwise the oldest frames will be overridden and
    /// glitching may occur on the lagging branch.
    ///
    /// **Panic!**s if the given `ring_buffer` is not empty in order to guarantee correct
    /// behaviour.
    ///
    /// ```
    /// use dasp_ring_buffer as ring_buffer;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let signal = signal::rate(44_100.0).const_hz(440.0).sine();
    ///     let ring_buffer = ring_buffer::Bounded::from([0f64; 64]);
    ///     let mut fork = signal.fork(ring_buffer);
    ///
    ///     // Forks can be split into their branches via reference.
    ///     {
    ///         let (mut a, mut b) = fork.by_ref();
    ///         assert_eq!(a.next(), b.next());
    ///         assert_eq!(a.by_ref().take(64).collect::<Vec<_>>(),
    ///                    b.by_ref().take(64).collect::<Vec<_>>());
    ///     }
    ///
    ///     // Forks can also be split via reference counted pointer.
    ///     let (mut a, mut b) = fork.by_rc();
    ///     assert_eq!(a.next(), b.next());
    ///     assert_eq!(a.by_ref().take(64).collect::<Vec<_>>(),
    ///                b.by_ref().take(64).collect::<Vec<_>>());
    ///
    ///     // The lagging branch will be missing frames if we exceed `ring_buffer.max_len()`
    ///     // pending frames.
    ///     assert!(a.by_ref().take(67).collect::<Vec<_>>() !=
    ///             b.by_ref().take(67).collect::<Vec<_>>())
    /// }
    /// ```
    fn fork<S>(self, ring_buffer: ring_buffer::Bounded<S>) -> Fork<Self, S>
    where
        Self: Sized,
        S: ring_buffer::SliceMut<Element = Self::Frame>,
    {
        assert!(ring_buffer.is_empty());
        let shared = ForkShared {
            signal: self,
            ring_buffer: ring_buffer,
            pending: Fork::<Self, S>::B,
        };
        Fork {
            shared: RefCell::new(shared),
        }
    }

    /// Converts the `Signal` into an `Iterator` that will yield the given number for `Frame`s
    /// before returning `None`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0.1, 0.2, 0.3, 0.4];
    ///     let mut signal = signal::from_iter(frames.iter().cloned()).take(2);
    ///     assert_eq!(signal.next(), Some(0.1));
    ///     assert_eq!(signal.next(), Some(0.2));
    ///     assert_eq!(signal.next(), None);
    /// }
    /// ```
    fn take(self, n: usize) -> Take<Self>
    where
        Self: Sized,
    {
        Take { signal: self, n: n }
    }

    /// Converts the `Signal` into an `Iterator` yielding frames until the `signal.is_exhausted()`
    /// returns `true`.
    ///
    /// # Example
    ///
    /// ```
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [1, 2];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     assert_eq!(signal.until_exhausted().count(), 2);
    /// }
    /// ```
    fn until_exhausted(self) -> UntilExhausted<Self>
    where
        Self: Sized,
    {
        UntilExhausted { signal: self }
    }

    /// Buffers the signal using the given ring buffer.
    ///
    /// When `next` is called on the returned signal, it will first check if the ring buffer is
    /// empty. If so, it will completely fill the ring buffer with the inner signal before yielding
    /// the next value. If the ring buffer still contains un-yielded values, the next frame will be
    /// popped from the front of the ring buffer and immediately returned.
    ///
    /// ```
    /// use dasp_ring_buffer as ring_buffer;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0.1, 0.2, 0.3, 0.4];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let ring_buffer = ring_buffer::Bounded::from([0f32; 2]);
    ///     let mut buffered_signal = signal.buffered(ring_buffer);
    ///     assert_eq!(buffered_signal.next(), 0.1);
    ///     assert_eq!(buffered_signal.next(), 0.2);
    ///     assert_eq!(buffered_signal.next(), 0.3);
    ///     assert_eq!(buffered_signal.next(), 0.4);
    ///     assert_eq!(buffered_signal.next(), 0.0);
    /// }
    /// ```
    ///
    /// If the given ring buffer already contains frames, those will be yielded first.
    ///
    /// ```
    /// use dasp_ring_buffer as ring_buffer;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0.1, 0.2, 0.3, 0.4];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let ring_buffer = ring_buffer::Bounded::from_full([0.8, 0.9]);
    ///     let mut buffered_signal = signal.buffered(ring_buffer);
    ///     assert_eq!(buffered_signal.next(), 0.8);
    ///     assert_eq!(buffered_signal.next(), 0.9);
    ///     assert_eq!(buffered_signal.next(), 0.1);
    ///     assert_eq!(buffered_signal.next(), 0.2);
    ///     assert_eq!(buffered_signal.next(), 0.3);
    ///     assert_eq!(buffered_signal.next(), 0.4);
    ///     assert_eq!(buffered_signal.next(), 0.0);
    /// }
    /// ```
    fn buffered<S>(self, ring_buffer: ring_buffer::Bounded<S>) -> Buffered<Self, S>
    where
        Self: Sized,
        S: ring_buffer::Slice<Element = Self::Frame> + ring_buffer::SliceMut,
    {
        Buffered {
            signal: self,
            ring_buffer: ring_buffer,
        }
    }

    /// Borrows a Signal rather than consuming it.
    ///
    /// This is useful to allow applying signal adaptors while still retaining ownership of the
    /// original signal.
    ///
    /// # Example
    ///
    /// ```rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0, 1, 2, 3, 4];
    ///     let mut signal = signal::from_iter(frames.iter().cloned());
    ///     assert_eq!(signal.next(), 0);
    ///     assert_eq!(signal.by_ref().take(2).collect::<Vec<_>>(), vec![1, 2]);
    ///     assert_eq!(signal.next(), 3);
    ///     assert_eq!(signal.next(), 4);
    /// }
    /// ```
    fn by_ref(&mut self) -> &mut Self
    where
        Self: Sized,
    {
        self
    }
}

/// Consumes the given `Iterator`, converts it to a `Signal`, applies the given function to the
/// `Signal` and returns an `Iterator` that will become exhausted when the consumed `Iterator`
/// does.
///
/// This is particularly useful when you want to apply `Signal` methods to an `Iterator` yielding
/// `Frame`s and return an `Iterator` as a result.
///
/// # Example
///
/// ```
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let frames = vec![0, 1, 2, 3];
///     let offset_frames = signal::lift(frames, |signal| signal.offset_amp(2));
///     assert_eq!(offset_frames.collect::<Vec<_>>(), vec![2, 3, 4, 5]);
/// }
/// ```
pub fn lift<I, F, S>(iter: I, f: F) -> UntilExhausted<S>
where
    I: IntoIterator,
    I::Item: Frame,
    F: FnOnce(FromIterator<I::IntoIter>) -> S,
    S: Signal<Frame = I::Item>,
{
    let iter = iter.into_iter();
    let signal = from_iter(iter);
    let new_signal = f(signal);
    new_signal.until_exhausted()
}

///// Signal Types

/// An iterator that endlessly yields `Frame`s of type `F` at equilibrium.
#[derive(Clone)]
pub struct Equilibrium<F> {
    frame: core::marker::PhantomData<F>,
}

/// A signal that generates frames using the given function.
#[derive(Clone)]
pub struct Gen<G, F> {
    gen: G,
    frame: core::marker::PhantomData<F>,
}

/// A signal that generates frames using the given function which may mutate some state.
#[derive(Clone)]
pub struct GenMut<G, F> {
    gen_mut: G,
    frame: core::marker::PhantomData<F>,
}

/// A signal that maps from one signal to another
#[derive(Clone)]
pub struct Map<S, M, F> {
    signal: S,
    map: M,
    frame: core::marker::PhantomData<F>,
}

/// A signal that iterates two signals in parallel and combines them with a function.
///
/// `ZipMap::is_exhausted` returns `true` if *either* of the two signals returns `true`.
#[derive(Clone)]
pub struct ZipMap<S, O, M, F> {
    this: S,
    other: O,
    map: M,
    frame: core::marker::PhantomData<F>,
}

/// A type that wraps an Iterator and provides a `Signal` implementation for it.
#[derive(Clone)]
pub struct FromIterator<I>
where
    I: Iterator,
{
    iter: I,
    next: Option<I::Item>,
}

/// An iterator that converts an iterator of `Sample`s to an iterator of `Frame`s.
#[derive(Clone)]
pub struct FromInterleavedSamplesIterator<I, F>
where
    I: Iterator,
    I::Item: Sample,
    F: Frame<Sample = I::Item>,
{
    samples: I,
    next: Option<F>,
}

/// The rate at which phrase a **Signal** is sampled.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rate {
    hz: f64,
}

/// A constant phase step size.
#[derive(Clone)]
pub struct ConstHz {
    step: f64,
}

/// An iterator that yields the step size for a phase.
#[derive(Clone)]
pub struct Hz<S> {
    hz: S,
    rate: Rate,
}

/// An iterator that yields a phase, useful for waveforms like Sine or Saw.
#[derive(Clone)]
pub struct Phase<S> {
    step: S,
    next: f64,
}

/// A sine wave signal generator.
#[derive(Clone)]
pub struct Sine<S> {
    phase: Phase<S>,
}

/// A saw wave signal generator.
#[derive(Clone)]
pub struct Saw<S> {
    phase: Phase<S>,
}

/// A square wave signal generator.
#[derive(Clone)]
pub struct Square<S> {
    phase: Phase<S>,
}

/// A noise signal generator.
#[derive(Clone)]
pub struct Noise {
    seed: u64,
}

/// A 1D simplex-noise generator.
#[derive(Clone)]
pub struct NoiseSimplex<S> {
    phase: Phase<S>,
}

/// An iterator that yields the sum of the frames yielded by both `other` and `self` in lock-step.
#[derive(Clone)]
pub struct AddAmp<A, B> {
    a: A,
    b: B,
}

/// An iterator that yields the product of the frames yielded by both `other` and `self` in
/// lock-step.
#[derive(Clone)]
pub struct MulAmp<A, B> {
    a: A,
    b: B,
}

/// Provides an iterator that offsets the amplitude of every channel in each frame of the
/// signal by some sample value and yields the resulting frames.
#[derive(Clone)]
pub struct OffsetAmp<S>
where
    S: Signal,
{
    signal: S,
    offset: <<S::Frame as Frame>::Sample as Sample>::Signed,
}

/// An `Iterator` that scales the amplitude of the sample of each channel in every `Frame` yielded
/// by `self` by the given amplitude.
#[derive(Clone)]
pub struct ScaleAmp<S>
where
    S: Signal,
{
    signal: S,
    amp: <<S::Frame as Frame>::Sample as Sample>::Float,
}

/// An `Iterator` that scales the amplitude of every `Frame` in `self` by the respective amplitudes
/// in each channel of the given `amp` `Frame`.
#[derive(Clone)]
pub struct OffsetAmpPerChannel<S, F> {
    signal: S,
    amp_frame: F,
}

/// An `Iterator` that scales the amplitude of every `Frame` in `self` by the respective amplitudes
/// in each channel of the given `amp` `Frame`.
#[derive(Clone)]
pub struct ScaleAmpPerChannel<S, F> {
    signal: S,
    amp_frame: F,
}

/// Multiplies the rate at which frames of `self` are yielded by the given `signal`.
///
/// This happens by wrapping `self` in a `rate::Converter` and calling `set_playback_hz_scale`
/// with the value yielded by `signal`
#[derive(Clone)]
pub struct MulHz<S, M, I>
where
    S: Signal,
    I: Interpolator,
{
    signal: Converter<S, I>,
    mul_per_frame: M,
}

/// Delays the `signal` by the given number of frames.
///
/// The delay is performed by yielding `Frame::EQUILIBRIUM` `n_frames` times before
/// continuing to yield frames from `signal`.
#[derive(Clone)]
pub struct Delay<S> {
    signal: S,
    n_frames: usize,
}

/// A signal that calls its enclosing function and returns the original value. The signal may
/// mutate state.
#[derive(Clone)]
pub struct Inspect<S, F> {
    signal: S,
    inspect: F,
}

/// Converts a `Signal` to a type that yields the individual interleaved samples.
pub struct IntoInterleavedSamples<S>
where
    S: Signal,
{
    signal: S,
    current_frame: <S::Frame as Frame>::Channels,
}

/// Converts the `IntoInterleavedSamples` into an `Iterator` that always returns `Some`.
pub struct IntoInterleavedSamplesIterator<S>
where
    S: Signal,
{
    samples: IntoInterleavedSamples<S>,
}

/// Yields frames from the signal until the `signal.is_exhausted()` returns `true`.
#[derive(Clone)]
pub struct UntilExhausted<S>
where
    S: Signal,
{
    signal: S,
}

/// Clips samples in each frame yielded by `signal` to the given threshhold amplitude.
#[derive(Clone)]
pub struct ClipAmp<S>
where
    S: Signal,
{
    signal: S,
    thresh: <<S::Frame as Frame>::Sample as Sample>::Signed,
}

/// Represents a forked `Signal` that has not yet been split into its two branches.
///
/// A `Fork` can be split into its two branches via either of the following methods:
///
/// - `fork.by_rc()`: consumes self and shares the fork via `Rc<RefCell>`.
/// - `fork.by_ref()`: borrows self and shares the fork via `&RefCell`.
#[derive(Clone)]
pub struct Fork<S, D> {
    shared: RefCell<ForkShared<S, D>>,
}

#[derive(Clone)]
struct ForkShared<S, D> {
    signal: S,
    ring_buffer: ring_buffer::Bounded<D>,
    pending: bool,
}

impl<S, D> Fork<S, D> {
    const A: bool = true;
    const B: bool = false;

    /// Consumes the `Fork` and returns two branches that share the signal and inner ring buffer
    /// via a reference countered pointer (`Rc`).
    ///
    /// Note: This requires dynamical allocation as `Rc<RefCell<Self>>` is used to share the signal
    /// and ring buffer. A user may avoid this dynamic allocation by using the `Fork::by_ref`
    /// method instead, however this comes with the ergonomic cost of bounding the lifetime of the
    /// branches to the lifetime of the fork.
    /// `Fork::by_ref`
    pub fn by_rc(self) -> (BranchRcA<S, D>, BranchRcB<S, D>) {
        let Fork { shared } = self;
        let shared_fork = Rc::new(shared);
        let a = BranchRcA {
            shared_fork: shared_fork.clone(),
        };
        let b = BranchRcB {
            shared_fork: shared_fork,
        };
        (a, b)
    }

    /// Mutably borrows the `Fork` and returns two branches that share the signal and inner ring
    /// buffer via reference.
    ///
    /// This is more efficient than `Fork::by_rc` as it does not require `Rc`, however it may be
    /// less ergonomic in some cases as the returned branches are bound to the lifetime of `Fork`.
    pub fn by_ref(&mut self) -> (BranchRefA<S, D>, BranchRefB<S, D>) {
        let Fork { ref shared } = *self;
        let a = BranchRefA {
            shared_fork: shared,
        };
        let b = BranchRefB {
            shared_fork: shared,
        };
        (a, b)
    }
}

// A macro to simplify the boilerplate shared between the two branch types returned by `Fork`.
macro_rules! define_branch {
    ($TRc:ident, $TRef:ident, $SELF:ident, $OTHER:ident) => {
        /// One of the two `Branch` signals returned by `Fork::by_rc`.
        pub struct $TRc<S, D> {
            shared_fork: Rc<RefCell<ForkShared<S, D>>>,
        }

        /// One of the two `Branch` signals returned by `Fork::by_ref`.
        pub struct $TRef<'a, S: 'a, D: 'a> {
            shared_fork: &'a RefCell<ForkShared<S, D>>,
        }

        impl<S, D> Signal for $TRc<S, D>
        where
            S: Signal,
            D: ring_buffer::SliceMut<Element = S::Frame>,
        {
            type Frame = S::Frame;
            fn next(&mut self) -> Self::Frame {
                let mut fork = self.shared_fork.borrow_mut();
                if fork.pending == Fork::<S, D>::$SELF {
                    if let Some(frame) = fork.ring_buffer.pop() {
                        return frame;
                    }
                    fork.pending = Fork::<S, D>::$OTHER;
                }
                let frame = fork.signal.next();
                fork.ring_buffer.push(frame);
                frame
            }
        }

        impl<'a, S, D> Signal for $TRef<'a, S, D>
        where
            S: 'a + Signal,
            D: 'a + ring_buffer::SliceMut<Element = S::Frame>,
        {
            type Frame = S::Frame;
            fn next(&mut self) -> Self::Frame {
                let mut fork = self.shared_fork.borrow_mut();
                if fork.pending == Fork::<S, D>::$SELF {
                    if let Some(frame) = fork.ring_buffer.pop() {
                        return frame;
                    }
                    fork.pending = Fork::<S, D>::$OTHER;
                }
                let frame = fork.signal.next();
                fork.ring_buffer.push(frame);
                frame
            }
        }

        impl<S, D> $TRc<S, D>
        where
            D: ring_buffer::Slice,
            D::Element: Copy,
        {
            /// The number of frames that are pending collection by this branch.
            pub fn pending_frames(&self) -> usize {
                let fork = self.shared_fork.borrow();
                if fork.pending == Fork::<S, D>::$SELF {
                    fork.ring_buffer.len()
                } else {
                    0
                }
            }
        }

        impl<'a, S, D> $TRef<'a, S, D>
        where
            D: ring_buffer::Slice,
            D::Element: Copy,
        {
            /// The number of frames that are pending collection by this branch.
            pub fn pending_frames(&self) -> usize {
                let fork = self.shared_fork.borrow();
                if fork.pending == Fork::<S, D>::$SELF {
                    fork.ring_buffer.len()
                } else {
                    0
                }
            }
        }
    };
}

define_branch!(BranchRcA, BranchRefA, A, B);
define_branch!(BranchRcB, BranchRefB, B, A);

/// An iterator that yields `n` number of `Frame`s from the inner `signal`.
#[derive(Clone)]
pub struct Take<S>
where
    S: Signal,
{
    signal: S,
    n: usize,
}

/// Buffers the signal using the given ring buffer.
///
/// When `next` is called, `Buffered` will first check if the ring buffer is empty. If so, it will
/// completely fill the ring buffer with `signal` before yielding the next frame.
///
/// If `next` is called and the ring buffer still contains un-yielded values, the next frame will
/// be popped from the front of the ring buffer and immediately returned.
#[derive(Clone)]
pub struct Buffered<S, D> {
    signal: S,
    ring_buffer: ring_buffer::Bounded<D>,
}

/// An iterator that pops elements from the inner bounded ring buffer and yields them.
///
/// Returns `None` once the inner ring buffer is exhausted.
pub struct BufferedFrames<'a, D: 'a> {
    ring_buffer: &'a mut ring_buffer::Bounded<D>,
}

///// Signal Constructors

/// Provides an iterator that endlessly yields `Frame`s of type `F` at equilibrium.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let equilibrium: Vec<f32> = signal::equilibrium().take(4).collect();
///     assert_eq!(equilibrium, vec![0.0, 0.0, 0.0, 0.0]);
///
///     let equilibrium: Vec<[u8; 2]> = signal::equilibrium().take(3).collect();
///     assert_eq!(equilibrium, vec![[128, 128], [128, 128], [128, 128]]);
/// }
/// ```
pub fn equilibrium<F>() -> Equilibrium<F>
where
    F: Frame,
{
    Equilibrium {
        frame: core::marker::PhantomData,
    }
}

/// A signal that generates frames using the given function.
///
/// The resulting signal is assumed to be infinite and `is_exhausted` will always return `false`.
/// To create an exhaustive signal first create an `Iterator` and then use `from_iter`.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let mut frames = signal::gen(|| [0.5]);
///     assert_eq!(frames.next(), [0.5]);
///     assert_eq!(frames.next(), [0.5]);
///     assert_eq!(frames.next(), [0.5]);
/// }
/// ```
pub fn gen<G, F>(gen: G) -> Gen<G, F>
where
    G: Fn() -> F,
    F: Frame,
{
    Gen {
        gen: gen,
        frame: core::marker::PhantomData,
    }
}

/// A signal that generates frames using the given function which may mutate some state.
///
/// The resulting signal is assumed to be infinite and `is_exhausted` will always return `false`.
/// To create an exhaustive signal first create an `Iterator` and then use `from_iter`.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let mut f = [0.0];
///     let mut signal = signal::gen_mut(|| {
///         let r = f;
///         f[0] += 0.1;
///         r
///     });
///     assert_eq!(signal.next(), [0.0]);
///     assert_eq!(signal.next(), [0.1]);
///     assert_eq!(signal.next(), [0.2]);
/// }
/// ```
pub fn gen_mut<G, F>(gen_mut: G) -> GenMut<G, F>
where
    G: FnMut() -> F,
    F: Frame,
{
    GenMut {
        gen_mut: gen_mut,
        frame: core::marker::PhantomData,
    }
}

/// Create a new `Signal` from the given `Frame`-yielding `Iterator`.
///
/// When the `Iterator` is exhausted, the new `Signal` will yield `F::equilibrium`.
///
/// Note that `Iterator::next` will be called immediately so that `FromIterator` can store the next
/// pending frame and efficiently test for exhaustiveness.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let frames = [[1], [-3], [5], [6]];
///     let mut signal = signal::from_iter(frames.iter().cloned());
///     assert_eq!(signal.next(), [1]);
///     assert_eq!(signal.next(), [-3]);
///     assert_eq!(signal.next(), [5]);
///     assert_eq!(signal.next(), [6]);
///     assert_eq!(signal.next(), [0]);
/// }
/// ```
pub fn from_iter<I>(frames: I) -> FromIterator<I::IntoIter>
where
    I: IntoIterator,
    I::Item: Frame,
{
    let mut iter = frames.into_iter();
    let next = iter.next();
    FromIterator {
        iter: iter,
        next: next,
    }
}

/// Create a new `Signal` from the given `Frame`-yielding `Iterator`.
///
/// When the `Iterator` is exhausted, the new `Signal` will yield `F::equilibrium`.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let foo = [0, 1, 2, 3];
///     let mut signal = signal::from_interleaved_samples_iter::<_, [i32; 2]>(foo.iter().cloned());
///     assert_eq!(signal.next(), [0, 1]);
///     assert_eq!(signal.next(), [2, 3]);
///     assert_eq!(signal.next(), [0, 0]);
///
///     let bar = [0, 1, 2];
///     let mut signal = signal::from_interleaved_samples_iter::<_, [i32; 2]>(bar.iter().cloned());
///     assert_eq!(signal.next(), [0, 1]);
///     assert_eq!(signal.next(), [0, 0]);
/// }
/// ```
pub fn from_interleaved_samples_iter<I, F>(
    samples: I,
) -> FromInterleavedSamplesIterator<I::IntoIter, F>
where
    I: IntoIterator,
    I::Item: Sample,
    F: Frame<Sample = I::Item>,
{
    let mut samples = samples.into_iter();
    let next = Frame::from_samples(&mut samples);
    FromInterleavedSamplesIterator {
        samples: samples,
        next: next,
    }
}

/// Creates a `Phase` that continuously steps forward by the given `step` size yielder.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let step = signal::rate(4.0).const_hz(1.0);
///     // Note that this is the same as `step.phase()`, a composable alternative.
///     let mut phase = signal::phase(step);
///     assert_eq!(phase.next(), 0.0);
///     assert_eq!(phase.next(), 0.25);
///     assert_eq!(phase.next(), 0.5);
///     assert_eq!(phase.next(), 0.75);
///     assert_eq!(phase.next(), 0.0);
///     assert_eq!(phase.next(), 0.25);
/// }
/// ```
pub fn phase<S>(step: S) -> Phase<S>
where
    S: Step,
{
    Phase {
        step: step,
        next: 0.0,
    }
}

/// Creates a frame `Rate` (aka sample rate) representing the rate at which a signal may be
/// sampled.
///
/// This is necessary for composing `Hz` or `ConstHz`, both of which may be used to step forward
/// the `Phase` for some kind of oscillator (i.e. `Sine`, `Saw`, `Square` or `NoiseSimplex`).
pub fn rate(hz: f64) -> Rate {
    Rate { hz: hz }
}

/// Produces a `Signal` that yields a sine wave oscillating at the given hz.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     // Generates a sine wave signal at 1hz to be sampled 4 times per second.
///     let mut signal = signal::rate(4.0).const_hz(1.0).sine();
///     assert_eq!(signal.next(), 0.0);
///     assert_eq!(signal.next(), 1.0);
///     signal.next();
///     assert_eq!(signal.next(), -1.0);
/// }
/// ```
pub fn sine<S>(phase: Phase<S>) -> Sine<S> {
    Sine { phase: phase }
}

/// Produces a `Signal` that yields a saw wave oscillating at the given hz.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     // Generates a saw wave signal at 1hz to be sampled 4 times per second.
///     let mut signal = signal::rate(4.0).const_hz(1.0).saw();
///     assert_eq!(signal.next(), 1.0);
///     assert_eq!(signal.next(), 0.5);
///     assert_eq!(signal.next(), 0.0);
///     assert_eq!(signal.next(), -0.5);
/// }
/// ```
pub fn saw<S>(phase: Phase<S>) -> Saw<S> {
    Saw { phase: phase }
}

/// Produces a `Signal` that yields a square wave oscillating at the given hz.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     // Generates a square wave signal at 1hz to be sampled 4 times per second.
///     let mut signal = signal::rate(4.0).const_hz(1.0).square();
///     assert_eq!(signal.next(), 1.0);
///     assert_eq!(signal.next(), 1.0);
///     assert_eq!(signal.next(), -1.0);
///     assert_eq!(signal.next(), -1.0);
/// }
/// ```
pub fn square<S>(phase: Phase<S>) -> Square<S> {
    Square { phase: phase }
}

/// Produces a `Signal` that yields random values between -1.0..1.0.
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     let mut noise = signal::noise(0);
///     for n in noise.take(1_000_000) {
///         assert!(-1.0 <= n && n < 1.0);
///     }
/// }
/// ```
pub fn noise(seed: u64) -> Noise {
    Noise { seed: seed }
}

/// Produces a 1-dimensional simplex noise `Signal`.
///
/// This is sometimes known as the "drunken walk" or "noise walk".
///
/// # Example
///
/// ```rust
/// use dasp_signal::{self as signal, Signal};
///
/// fn main() {
///     // Creates a simplex noise signal oscillating at 440hz sampled 44_100 times per second.
///     let mut signal = signal::rate(44_100.0).const_hz(440.0).noise_simplex();
///     for n in signal.take(1_000_000) {
///         assert!(-1.0 <= n && n < 1.0);
///     }
/// }
/// ```
pub fn noise_simplex<S>(phase: Phase<S>) -> NoiseSimplex<S> {
    NoiseSimplex { phase: phase }
}

//// Trait Implementations for Signal Types.

impl<'a, S> Signal for &'a mut S
where
    S: Signal + ?Sized,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        (**self).next()
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        (**self).is_exhausted()
    }
}

impl<I> Signal for FromIterator<I>
where
    I: Iterator,
    I::Item: Frame,
{
    type Frame = I::Item;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        match self.next.take() {
            Some(frame) => {
                self.next = self.iter.next();
                frame
            }
            None => Frame::EQUILIBRIUM,
        }
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.next.is_none()
    }
}

impl<I, F> Signal for FromInterleavedSamplesIterator<I, F>
where
    I: Iterator,
    I::Item: Sample,
    F: Frame<Sample = I::Item>,
{
    type Frame = F;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        match self.next.take() {
            Some(frame) => {
                self.next = F::from_samples(&mut self.samples);
                frame
            }
            None => F::EQUILIBRIUM,
        }
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.next.is_none()
    }
}

impl<F> Signal for Equilibrium<F>
where
    F: Frame,
{
    type Frame = F;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        F::EQUILIBRIUM
    }
}

impl<G, F> Signal for Gen<G, F>
where
    G: Fn() -> F,
    F: Frame,
{
    type Frame = F;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        (self.gen)()
    }
}

impl<G, F> Signal for GenMut<G, F>
where
    G: FnMut() -> F,
    F: Frame,
{
    type Frame = F;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        (self.gen_mut)()
    }
}

impl<S, M, F> Signal for Map<S, M, F>
where
    S: Signal,
    M: FnMut(S::Frame) -> F,
    F: Frame,
{
    type Frame = F;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        (self.map)(self.signal.next())
    }

    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted()
    }
}

impl<S, O, M, F> Signal for ZipMap<S, O, M, F>
where
    S: Signal,
    O: Signal,
    M: FnMut(S::Frame, O::Frame) -> F,
    F: Frame,
{
    type Frame = F;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        (self.map)(self.this.next(), self.other.next())
    }

    fn is_exhausted(&self) -> bool {
        self.this.is_exhausted() || self.other.is_exhausted()
    }
}

impl<S> Signal for Hz<S>
where
    S: Signal<Frame = f64>,
{
    type Frame = f64;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.step()
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.hz.is_exhausted()
    }
}

impl Signal for ConstHz {
    type Frame = f64;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.step()
    }
}

impl<S> Signal for Phase<S>
where
    S: Step,
{
    type Frame = f64;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.next_phase()
    }
}

impl<S> Signal for Sine<S>
where
    S: Step,
{
    type Frame = f64;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        const PI_2: f64 = core::f64::consts::PI * 2.0;
        let phase = self.phase.next_phase();
        ops::f64::sin(PI_2 * phase)
    }
}

impl<S> Signal for Saw<S>
where
    S: Step,
{
    type Frame = f64;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        let phase = self.phase.next_phase();
        phase * -2.0 + 1.0
    }
}

impl<S> Signal for Square<S>
where
    S: Step,
{
    type Frame = f64;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        let phase = self.phase.next_phase();
        if phase < 0.5 {
            1.0
        } else {
            -1.0
        }
    }
}

impl Rate {
    /// Create a `ConstHz` signal which consistently yields `hz / rate`.
    pub fn const_hz(self, hz: f64) -> ConstHz {
        ConstHz { step: hz / self.hz }
    }

    /// Create a `Hz` signal which yields phase step sizes controlled by an input
    /// signal `hz`.
    ///
    /// # Example
    ///
    /// ``` rust
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let step = signal::rate(4.0).hz(signal::gen(|| 1.0));
    ///     let mut phase = signal::phase(step);
    ///     assert_eq!(phase.next(), 0.0);
    ///     assert_eq!(phase.next(), 0.25);
    ///     assert_eq!(phase.next(), 0.5);
    ///     assert_eq!(phase.next(), 0.75);
    ///     assert_eq!(phase.next(), 0.0);
    ///     assert_eq!(phase.next(), 0.25);
    /// }
    /// ```
    pub fn hz<S>(self, hz: S) -> Hz<S>
    where
        S: Signal<Frame = f64>,
    {
        Hz { hz: hz, rate: self }
    }
}

impl<S> Hz<S>
where
    S: Signal<Frame = f64>,
{
    /// Construct a `Phase` iterator that, for every `hz` yielded by `self`, yields a phase that is
    /// stepped by `hz / self.rate.hz`.
    #[inline]
    pub fn phase(self) -> Phase<Self> {
        phase(self)
    }

    /// A composable alternative to the `signal::sine` function.
    #[inline]
    pub fn sine(self) -> Sine<Self> {
        self.phase().sine()
    }

    /// A composable alternative to the `signal::saw` function.
    #[inline]
    pub fn saw(self) -> Saw<Self> {
        self.phase().saw()
    }

    /// A composable alternative to the `signal::square` function.
    #[inline]
    pub fn square(self) -> Square<Self> {
        self.phase().square()
    }

    /// A composable alternative to the `signal::noise_simplex` function.
    #[inline]
    pub fn noise_simplex(self) -> NoiseSimplex<Self> {
        self.phase().noise_simplex()
    }
}

impl ConstHz {
    /// Construct a `Phase` iterator that is incremented via the constant step size, `self.step`.
    #[inline]
    pub fn phase(self) -> Phase<Self> {
        phase(self)
    }

    /// A composable alternative to the `signal::sine` function.
    #[inline]
    pub fn sine(self) -> Sine<Self> {
        self.phase().sine()
    }

    /// A composable alternative to the `signal::saw` function.
    #[inline]
    pub fn saw(self) -> Saw<Self> {
        self.phase().saw()
    }

    /// A composable alternative to the `signal::square` function.
    #[inline]
    pub fn square(self) -> Square<Self> {
        self.phase().square()
    }

    /// A composable alternative to the `signal::noise_simplex` function.
    #[inline]
    pub fn noise_simplex(self) -> NoiseSimplex<Self> {
        self.phase().noise_simplex()
    }
}

/// Types that may be used to give a phase step size based on some `hz / sample rate`.
///
/// This allows the `Phase` to be generic over either `ConstHz` and `Hz<I>`.
///
/// Generally, users need not be concerned with this trait unless writing code that must remain
/// generic over phase stepping types like oscillators.
pub trait Step {
    /// Yield the phase step size (normally `hz / sampling rate`).
    ///
    /// The `Phase` calls this and uses the returned value to step forward its internal `phase`.
    fn step(&mut self) -> f64;
}

impl Step for ConstHz {
    #[inline]
    fn step(&mut self) -> f64 {
        self.step
    }
}

impl<S> Step for Hz<S>
where
    S: Signal<Frame = f64>,
{
    #[inline]
    fn step(&mut self) -> f64 {
        let hz = self.hz.next();
        hz / self.rate.hz
    }
}

impl<S> Phase<S>
where
    S: Step,
{
    /// Before yielding the current phase, the internal phase is stepped forward and wrapped via
    /// the given value.
    #[inline]
    pub fn next_phase_wrapped_to(&mut self, rem: f64) -> f64 {
        let phase = self.next;
        self.next = (self.next + self.step.step()) % rem;
        phase
    }

    /// Calls `next_phase_wrapped_to`, with a wrapping value of `1.0`.
    #[inline]
    pub fn next_phase(&mut self) -> f64 {
        self.next_phase_wrapped_to(1.0)
    }

    /// A composable version of the `signal::sine` function.
    #[inline]
    pub fn sine(self) -> Sine<S> {
        sine(self)
    }

    /// A composable version of the `signal::saw` function.
    #[inline]
    pub fn saw(self) -> Saw<S> {
        saw(self)
    }

    /// A composable version of the `signal::square` function.
    #[inline]
    pub fn square(self) -> Square<S> {
        square(self)
    }

    /// A composable version of the `signal::noise_simplex` function.
    #[inline]
    pub fn noise_simplex(self) -> NoiseSimplex<S> {
        noise_simplex(self)
    }
}

impl Noise {
    #[inline]
    pub fn next_sample(&mut self) -> f64 {
        // A simple one-dimensional noise generator.
        //
        // Credit for the pseudo code from which this was translated goes to Hugo Elias and his
        // excellent primer on perlin noise at
        // http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
        fn noise_1(seed: u64) -> f64 {
            const PRIME_1: u64 = 15_731;
            const PRIME_2: u64 = 789_221;
            const PRIME_3: u64 = 1_376_312_589;
            let x = (seed << 13) ^ seed;
            1.0 - (x
                .wrapping_mul(
                    x.wrapping_mul(x)
                        .wrapping_mul(PRIME_1)
                        .wrapping_add(PRIME_2),
                )
                .wrapping_add(PRIME_3)
                & 0x7fffffff) as f64
                / 1_073_741_824.0
        }

        let noise = noise_1(self.seed);
        self.seed += 1;
        noise
    }
}

impl Signal for Noise {
    type Frame = f64;
    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.next_sample()
    }
}

impl<S> NoiseSimplex<S>
where
    S: Step,
{
    #[inline]
    pub fn next_sample(&mut self) -> f64 {
        // The constant remainder used to wrap the phase back to 0.0.
        //
        // This is the first power of two that is over double the human hearing range. This should
        // allow for simplex noise to be generated at a frequency matching the extent of the human
        // hearing range while never repeating more than once per second; the repetition would
        // likely be indistinguishable at such a high frequency, and in this should be practical
        // for audio simplex noise.
        const TWO_POW_SIXTEEN: f64 = 65_536.0;
        let phase = self.phase.next_phase_wrapped_to(TWO_POW_SIXTEEN);

        // 1D Perlin simplex noise.
        //
        // Takes a floating point x coordinate and yields a noise value in the range of -1..1, with
        // value of 0.0 on all integer coordinates.
        //
        // This function and the enclosing functions have been adapted from SRombauts' MIT licensed
        // C++ implementation at the following link: https://github.com/SRombauts/SimplexNoise
        fn simplex_noise_1d(x: f64) -> f64 {
            // Permutation table. This is a random jumble of all numbers 0...255.
            const PERM: [u8; 256] = [
                151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36,
                103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0,
                26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87,
                174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146,
                158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40,
                244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18,
                169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
                52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206,
                59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2,
                44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98,
                108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242,
                193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107,
                49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4,
                150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66,
                215, 61, 156, 180,
            ];

            // Hashes the given integer with the above permutation table.
            fn hash(i: i64) -> u8 {
                PERM[(i as u8) as usize]
            }

            // Computes the gradients-dot-residual vectors (1D).
            fn grad(hash: i64, x: f64) -> f64 {
                // Convert low 4 bits of hash code.
                let h = hash & 0x0F;
                // Gradien value 1.0, 2.0, ..., 8.0.
                let mut grad = 1.0 + (h & 7) as f64;
                // Set a random sign for the gradient.
                if (h & 8) != 0 {
                    grad = -grad;
                }
                // Multiply the gradient with the distance.
                grad * x
            }

            // Corners coordinates (nearest integer values).
            let i0 = ops::f64::floor(x) as i64;
            let i1 = i0 + 1;

            // Distances to corners (between 0 and 1);
            let x0 = x - i0 as f64;
            let x1 = x0 - 1.0;

            // Calculate the contribution from the first corner.
            let mut t0 = 1.0 - x0 * x0;
            t0 *= t0;
            let n0 = t0 * t0 * grad(hash(i0) as i64, x0);

            // Calculate the contribution rom the second corner.
            let mut t1 = 1.0 - x1 * x1;
            t1 *= t1;
            let n1 = t1 * t1 * grad(hash(i1) as i64, x1);

            // The max value of this noise is 2.53125. 0.395 scales to fit exactly within -1..1.
            0.395 * (n0 + n1)
        }

        simplex_noise_1d(phase)
    }
}

impl<S> Signal for NoiseSimplex<S>
where
    S: Step,
{
    type Frame = f64;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.next_sample()
    }
}

impl<A, B> Signal for AddAmp<A, B>
where
    A: Signal,
    B: Signal,
    B::Frame: Frame<
        Sample = <<A::Frame as Frame>::Sample as Sample>::Signed,
        NumChannels = <A::Frame as Frame>::NumChannels,
    >,
{
    type Frame = A::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.a.next().add_amp(self.b.next())
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.a.is_exhausted() || self.b.is_exhausted()
    }
}

impl<A, B> Signal for MulAmp<A, B>
where
    A: Signal,
    B: Signal,
    B::Frame: Frame<
        Sample = <<A::Frame as Frame>::Sample as Sample>::Float,
        NumChannels = <A::Frame as Frame>::NumChannels,
    >,
{
    type Frame = A::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.a.next().mul_amp(self.b.next())
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.a.is_exhausted() || self.b.is_exhausted()
    }
}

impl<S> Signal for ScaleAmp<S>
where
    S: Signal,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.signal.next().scale_amp(self.amp)
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted()
    }
}

impl<S, F> Signal for ScaleAmpPerChannel<S, F>
where
    S: Signal,
    F: Frame<
        Sample = <<S::Frame as Frame>::Sample as Sample>::Float,
        NumChannels = <S::Frame as Frame>::NumChannels,
    >,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.signal.next().mul_amp(self.amp_frame)
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted()
    }
}

impl<S> Signal for OffsetAmp<S>
where
    S: Signal,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.signal.next().offset_amp(self.offset)
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted()
    }
}

impl<S, F> Signal for OffsetAmpPerChannel<S, F>
where
    S: Signal,
    F: Frame<
        Sample = <<S::Frame as Frame>::Sample as Sample>::Signed,
        NumChannels = <S::Frame as Frame>::NumChannels,
    >,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        self.signal.next().add_amp(self.amp_frame)
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted()
    }
}

impl<S, M, I> Signal for MulHz<S, M, I>
where
    S: Signal,
    <S::Frame as Frame>::Sample: Duplex<f64>,
    M: Signal<Frame = f64>,
    I: Interpolator<Frame = S::Frame>,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        let mul = self.mul_per_frame.next();
        self.signal.set_playback_hz_scale(mul);
        self.signal.next()
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted() || self.mul_per_frame.is_exhausted()
    }
}

impl<S> Signal for Delay<S>
where
    S: Signal,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        if self.n_frames > 0 {
            self.n_frames -= 1;
            Self::Frame::EQUILIBRIUM
        } else {
            self.signal.next()
        }
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.n_frames == 0 && self.signal.is_exhausted()
    }
}

impl<S, F> Signal for Inspect<S, F>
where
    S: Signal,
    F: FnMut(&S::Frame),
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        let out = self.signal.next();
        (self.inspect)(&out);
        out
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted()
    }
}

impl<S> IntoInterleavedSamples<S>
where
    S: Signal,
{
    /// Yield the next interleaved sample from the inner `Signal`.
    #[inline]
    pub fn next_sample(&mut self) -> <S::Frame as Frame>::Sample {
        loop {
            match self.current_frame.next() {
                Some(channel) => return channel,
                None => self.current_frame = self.signal.next().channels(),
            }
        }
    }

    /// Convert the `ToInterleavedSamples` into an `Iterator`.
    #[inline]
    pub fn into_iter(self) -> IntoInterleavedSamplesIterator<S> {
        IntoInterleavedSamplesIterator { samples: self }
    }
}

impl<S> Iterator for IntoInterleavedSamplesIterator<S>
where
    S: Signal,
{
    type Item = <S::Frame as Frame>::Sample;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        Some(self.samples.next_sample())
    }
}

impl<S> Iterator for UntilExhausted<S>
where
    S: Signal,
{
    type Item = S::Frame;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.signal.is_exhausted() {
            return None;
        }
        Some(self.signal.next())
    }
}

impl<S> Clone for IntoInterleavedSamples<S>
where
    S: Signal + Clone,
    <S::Frame as Frame>::Channels: Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        IntoInterleavedSamples {
            signal: self.signal.clone(),
            current_frame: self.current_frame.clone(),
        }
    }
}

impl<S> Clone for IntoInterleavedSamplesIterator<S>
where
    S: Signal,
    IntoInterleavedSamples<S>: Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        IntoInterleavedSamplesIterator {
            samples: self.samples.clone(),
        }
    }
}

impl<S> Signal for ClipAmp<S>
where
    S: Signal,
{
    type Frame = S::Frame;

    #[inline]
    fn next(&mut self) -> Self::Frame {
        let f = self.signal.next();
        f.map(|s| {
            let s: <<S::Frame as Frame>::Sample as Sample>::Signed = s.to_sample();
            if s > self.thresh {
                self.thresh
            } else if s < -self.thresh {
                -self.thresh
            } else {
                s
            }
            .to_sample()
        })
    }

    #[inline]
    fn is_exhausted(&self) -> bool {
        self.signal.is_exhausted()
    }
}

impl<S> Iterator for Take<S>
where
    S: Signal,
{
    type Item = S::Frame;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.n == 0 {
            return None;
        }
        self.n -= 1;
        Some(self.signal.next())
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.n, Some(self.n))
    }
}

impl<S> ExactSizeIterator for Take<S>
where
    S: Signal,
{
    #[inline]
    fn len(&self) -> usize {
        self.n
    }
}

impl<S, D> Buffered<S, D>
where
    S: Signal,
    D: ring_buffer::Slice<Element = S::Frame> + ring_buffer::SliceMut,
{
    /// Produces an iterator yielding the next batch of buffered frames.
    ///
    /// The returned iterator returns `None` once the inner ring buffer becomes exhausted.
    ///
    /// If the inner ring buffer is empty when this method is called, the ring buffer will first be
    /// filled using `Buffered`'s inner `signal` before `BufferedFrames` is returned.
    ///
    /// ```
    /// use dasp_ring_buffer as ring_buffer;
    /// use dasp_signal::{self as signal, Signal};
    ///
    /// fn main() {
    ///     let frames = [0.1, 0.2, 0.3, 0.4];
    ///     let signal = signal::from_iter(frames.iter().cloned());
    ///     let ring_buffer = ring_buffer::Bounded::from([0f32; 2]);
    ///     let mut buffered_signal = signal.buffered(ring_buffer);
    ///     assert_eq!(buffered_signal.next_frames().collect::<Vec<_>>(), vec![0.1, 0.2]);
    ///     assert_eq!(buffered_signal.next_frames().collect::<Vec<_>>(), vec![0.3, 0.4]);
    ///     assert_eq!(buffered_signal.next_frames().collect::<Vec<_>>(), vec![0.0, 0.0]);
    /// }
    /// ```
    pub fn next_frames(&mut self) -> BufferedFrames<D> {
        let Buffered {
            ref mut signal,
            ref mut ring_buffer,
        } = *self;
        if ring_buffer.len() == 0 {
            for _ in 0..ring_buffer.max_len() {
                ring_buffer.push(signal.next());
            }
        }
        BufferedFrames {
            ring_buffer: ring_buffer,
        }
    }

    /// Consumes the `Buffered` signal and returns its inner signal `S` and bounded ring buffer.
    pub fn into_parts(self) -> (S, ring_buffer::Bounded<D>) {
        let Buffered {
            signal,
            ring_buffer,
        } = self;
        (signal, ring_buffer)
    }
}

impl<S, D> Signal for Buffered<S, D>
where
    S: Signal,
    D: ring_buffer::Slice<Element = S::Frame> + ring_buffer::SliceMut,
{
    type Frame = S::Frame;

    fn next(&mut self) -> Self::Frame {
        let Buffered {
            ref mut signal,
            ref mut ring_buffer,
        } = *self;
        loop {
            match ring_buffer.pop() {
                Some(frame) => return frame,
                None => {
                    for _ in 0..ring_buffer.max_len() {
                        ring_buffer.push(signal.next());
                    }
                }
            }
        }
    }

    fn is_exhausted(&self) -> bool {
        self.ring_buffer.len() == 0 && self.signal.is_exhausted()
    }
}

impl<'a, D> Iterator for BufferedFrames<'a, D>
where
    D: ring_buffer::SliceMut,
    D::Element: Copy,
{
    type Item = D::Element;
    fn next(&mut self) -> Option<Self::Item> {
        self.ring_buffer.pop()
    }
}