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
/// Construct an audio buffer. /// /// This is useful when testing. /// /// # Examples /// /// ```rust /// let buf = rotary::dynamic![[0.0; 64]; 2]; /// /// let mut expected = vec![0.0; 64]; /// /// assert_eq!(&buf[0], &expected[..]); /// assert_eq!(&buf[1], &expected[..]); /// ``` /// /// Calling the macro with a template channel. /// /// ```rust /// let buf = rotary::dynamic![[0.0, 1.0, 2.0, 3.0]; 2]; /// /// assert_eq!(&buf[0], &[0.0, 1.0, 2.0, 3.0]); /// assert_eq!(&buf[1], &[0.0, 1.0, 2.0, 3.0]); /// ``` #[macro_export] macro_rules! dynamic { // Branch of the macro used when we can perform a literal instantiation of // the audio buffer. // // This is typically more performant, since it doesn't require looping and // writing through the buffer. ([$sample:expr; $frames:literal]; $channels:literal) => { $crate::Dynamic::from_array([[$sample; $frames]; $channels]); }; // Branch of the macro used when we can evaluate an expression that is // built into an audio buffer. // // `$sample`, `$frames`, and `$channels` are all expected to implement // `Copy`. `$frames` and `$channels` should evaluate to `usize`. ([$sample:expr; $frames:expr]; $channels:expr) => {{ let value = $sample; let mut buffer = $crate::Dynamic::with_topology($channels, $frames); for chan in &mut buffer { for f in chan { *f = value; } } buffer }}; // Build a dynamic audio buffer with a template channel. ([$($value:expr),* $(,)?]; $channels:expr) => { $crate::Dynamic::from_frames([$($value),*], $channels) }; } /// Construct a sequential audio buffer. /// /// This is useful for testing. /// /// # Examples /// /// ```rust /// let buf = rotary::sequential![[0.0; 64]; 2]; /// /// let mut expected = vec![0.0; 64]; /// /// assert_eq!(&buf[0], &expected[..]); /// assert_eq!(&buf[1], &expected[..]); /// ``` /// /// Calling the macro with a template channel. /// /// ```rust /// let buf = rotary::sequential![[0.0, 1.0, 2.0, 3.0]; 2]; /// /// assert_eq!(buf.as_slice(), &[0.0, 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 3.0]) /// ``` #[macro_export] macro_rules! sequential { // Branch of the macro used when we can evaluate an expression that is // built into a sequential audio buffer. // // `$sample`, `$frames`, and `$channels` are all expected to implement // `Copy`. `$frames` and `$channels` should evaluate to `usize`. ([$sample:expr; $frames:expr]; $channels:expr) => { $crate::Sequential::from_vec(vec![$sample; $channels * $frames], $channels, $frames) }; // Build a sequential audio buffer with a template channel. ([$($value:expr),* $(,)?]; $channels:expr) => { $crate::Sequential::from_frames([$($value),*], $channels) }; } /// Construct an interleaved audio buffer. /// /// This is useful for testing. /// /// # Examples /// /// ```rust /// let buf = rotary::interleaved![[0.0; 64]; 2]; /// /// let mut expected = vec![0.0; 64]; /// /// assert!(buf.get(0).unwrap().iter().eq(&expected[..])); /// assert!(buf.get(1).unwrap().iter().eq(&expected[..])); /// ``` /// /// Calling the macro with a template channel. /// /// ```rust /// let buf = rotary::interleaved![[0.0, 1.0, 2.0, 3.0]; 2]; /// /// assert_eq!(buf.as_slice(), &[0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0]) /// ``` #[macro_export] macro_rules! interleaved { // Branch of the macro used when we can evaluate an expression that is // built into a interleaved audio buffer. // // `$sample`, `$frames`, and `$channels` are all expected to implement // `Copy`. `$frames` and `$channels` should evaluate to `usize`. ([$sample:expr; $frames:expr]; $channels:expr) => { $crate::Interleaved::from_vec(vec![$sample; $channels * $frames], $channels, $frames) }; // Build an interleaved audio buffer with a template channel. ([$($value:expr),* $(,)?]; $channels:expr) => { $crate::Interleaved::from_frames([$($value),*], $channels) }; }