audio_samples/traits.rs
1use bytemuck::NoUninit;
2use ndarray::ScalarOperand;
3use num_traits::{FromPrimitive, Num, NumCast, One, ToBytes, Zero};
4#[cfg(feature = "static-plots")]
5use serde::{Deserialize, Serialize};
6
7use crate::repr::SampleType;
8use crate::{AudioSamples, I24};
9use std::fmt::{Debug, Display};
10use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
11
12/// Performs a raw numeric cast from type `S` into `Self`.
13///
14/// Unlike [`ConvertTo`] and [`ConvertFrom`], casts apply standard numeric
15/// conversion rules equivalent to an `as` expression — no audio-aware
16/// scaling, rounding, or saturation is applied. This trait is used
17/// internally to move values between numeric types for arithmetic or
18/// indexing purposes.
19///
20/// For audio-meaningful conversions (e.g. `i16` → `f32` in the range
21/// `[-1.0, 1.0]`), use [`ConvertTo`] or [`ConvertFrom`] instead.
22pub trait CastFrom<S>: Sized {
23 /// Casts a value of type `S` into `Self` using raw numeric conversion.
24 ///
25 /// # Arguments
26 ///
27 /// – `value` — the source value to cast.
28 ///
29 /// # Returns
30 ///
31 /// The cast value as `Self`. No audio-aware scaling is applied.
32 fn cast_from(value: S) -> Self;
33}
34
35/// Performs a raw numeric cast from `Self` into type `T`.
36///
37/// This is the directional complement of [`CastFrom`]. A blanket
38/// implementation delegates to `T::cast_from(self)`, so this trait does
39/// not need to be implemented directly.
40///
41/// For audio-meaningful conversions, use [`ConvertTo`] instead.
42pub trait CastInto<T>: Sized
43where
44 Self: CastFrom<T>,
45{
46 /// Casts `self` into type `T` using raw numeric conversion.
47 ///
48 /// # Returns
49 ///
50 /// The cast value as `T`. No audio-aware scaling is applied.
51 fn cast_into(self) -> T;
52}
53
54/// Convenience supertrait asserting that a type can be raw-cast into all
55/// supported audio sample types: `u8`, `i16`, [`I24`](i24::I24), `i32`, `f32`, and
56/// `f64`.
57///
58/// A type satisfies this bound automatically when it implements [`CastInto`]
59/// for all six target types; no manual implementation is required.
60pub trait Castable:
61 CastInto<u8> + CastInto<i16> + CastInto<I24> + CastInto<i32> + CastInto<f32> + CastInto<f64>
62{
63}
64impl<T> Castable for T where
65 T: CastInto<u8> + CastInto<i16> + CastInto<I24> + CastInto<i32> + CastInto<f32> + CastInto<f64>
66{
67}
68
69mod sealed {
70 pub trait Sealed {}
71}
72use sealed::Sealed;
73
74/// Zero-sized marker type parameterised by byte width `N`.
75///
76/// Used together with [`SupportedByteSize`] as a compile-time guard on
77/// [`AudioSample::as_bytes`] to restrict byte serialisation to widths that
78/// are valid for the supported sample types: 1, 2, 3, 4, and 8.
79#[non_exhaustive]
80pub struct SampleByteSize<const N: usize>;
81
82/// Sealed marker trait indicating that byte width `N` is a supported audio
83/// sample size.
84///
85/// Implemented for [`SampleByteSize<N>`] where `N` is 1, 2, 3, 4, or 8,
86/// corresponding to the byte widths of `u8`, `i16`, `I24`, `i32`/`f32`,
87/// and `f64` respectively.
88///
89/// This trait is sealed and cannot be implemented outside this crate.
90pub trait SupportedByteSize: Sealed {}
91
92impl<const N: usize> Sealed for SampleByteSize<N> {}
93
94impl SupportedByteSize for SampleByteSize<1> {}
95impl SupportedByteSize for SampleByteSize<2> {}
96impl SupportedByteSize for SampleByteSize<3> {}
97impl SupportedByteSize for SampleByteSize<4> {}
98impl SupportedByteSize for SampleByteSize<8> {}
99
100/// Core trait for all audio sample types.
101///
102/// `AudioSample` is the foundational constraint that every audio sample
103/// numeric type in this crate must satisfy. It provides a uniform interface
104/// for arithmetic, conversions, byte serialisation, and interoperability
105/// with `ndarray` scalar operations.
106///
107/// ## Supported Types
108///
109/// | Type | Representation | Bit depth |
110/// |---------|-----------------------------|-----------|
111/// | `u8` | Unsigned PCM (silence = 128)| 8 |
112/// | `i16` | Signed integer PCM | 16 |
113/// | [`I24`](i24::I24) | Signed integer PCM (24-bit) | 24 |
114/// | `i32` | Signed integer PCM | 32 |
115/// | `f32` | Normalised float | 32 |
116/// | `f64` | Normalised float | 64 |
117///
118/// For `u8`, the silence level is 128 (mid-scale unsigned PCM convention).
119/// For `f32` and `f64`, `MAX` and `MIN` are `1.0` and `-1.0`.
120///
121/// # Safety
122///
123/// All implementors must satisfy [`bytemuck::NoUninit`], guaranteeing that
124/// the byte representation contains no uninitialised or padding bytes. This
125/// is required for safe byte-level serialisation via [`AudioSample::to_bytes`]
126/// and [`AudioSample::as_bytes`].
127///
128/// ## Examples
129///
130/// ```
131/// use audio_samples::AudioSample;
132///
133/// let sample: f32 = 0.5;
134/// let bytes = sample.to_bytes();
135/// assert_eq!(bytes.len(), 4); // f32 is 4 bytes
136///
137/// // Raw numeric cast (not audio-normalised)
138/// let raw: f64 = sample.as_float();
139/// assert_eq!(raw, 0.5_f64);
140/// ```
141pub trait AudioSample:
142 // Standard library traits
143 Copy
144 + Sized
145 + Default
146 + Display
147 + Debug
148 + Sync
149 + Send
150 + PartialEq
151 + PartialOrd
152 + Add<Output = Self>
153 + AddAssign<Self>
154 + Sub<Output = Self>
155 + SubAssign<Self>
156 + Mul<Output = Self>
157 + MulAssign<Self>
158 + Div<Output = Self>
159 + DivAssign<Self>
160 + Rem<Output = Self>
161 + RemAssign<Self>
162 + Into<Self>
163 + From<Self>
164 + ToString
165
166 // External crate traits
167 + NoUninit // bytemuck trait to ensure no uninitialized bytes
168 + Num // num-traits trait for numeric operations
169 + One // num-traits trait for 1 value
170 + Zero // num-traits trait for 0 value
171 + ToBytes // num-traits trait for byte conversion
172 + MaybeSerdeAudioSample // conditional serde support for audio samples
173 + FromPrimitive // num-traits trait for conversion from primitive types
174 + NumCast // num-traits trait for casting between numeric types
175 + ScalarOperand // ndarray trait for scalar operations
176
177 // Library-specific traits. Most of which are below.
178 // They define how to convert between types depending on the context.
179 // Sometimes we are dealing with audio samples and float representations between -1.0 and 1.0, sometimes we are dealing with raw integer representations that we need to cast to floats for specific operations, but not -1.0 to 1.0, for various operations.
180 + ConvertTo<Self> // "I can convert to myself" trait
181 + ConvertTo<u8> // "I can convert to u8" trait
182 + ConvertTo<i16> // "I can convert to i16" trait
183 + ConvertTo<I24> // "I can convert to I24" trait
184 + ConvertTo<i32> // "I can convert to i32" trait
185 + ConvertTo<f32> // "I can convert to f32" trait
186 + ConvertTo<f64> // "I can convert to f64" trait
187 + CastFrom<usize> // "I can cast from a usize"
188 + Castable // "I can be cast into supported types"
189{
190 /// Returns this sample value unchanged.
191 ///
192 /// This is an identity method provided for API uniformity. It is useful
193 /// in generic contexts where a value must be consumed through a trait
194 /// interface but should pass through unmodified.
195 ///
196 /// # Returns
197 ///
198 /// `self` unchanged.
199 ///
200 /// ## Examples
201 ///
202 /// ```
203 /// use audio_samples::AudioSample;
204 ///
205 /// let s: f32 = 0.75;
206 /// assert_eq!(s.into_inner(), 0.75_f32);
207 /// ```
208 #[inline]
209 #[must_use]
210 fn into_inner(self) -> Self {
211 self
212 }
213
214 /// Clamps `self` to the closed interval `[min, max]`.
215 ///
216 /// This is a per-type hook used internally by [`clip`][crate::AudioProcessing::clip]
217 /// to select the most efficient clamping implementation. Integer types
218 /// use [`Ord::clamp`]; floating-point types use their inherent `.clamp()`
219 /// method which the compiler maps to `VMAXPS`/`VMINPS` in vectorised
220 /// loops. The default implementation uses explicit comparisons and is
221 /// correct for all types.
222 ///
223 /// # Arguments
224 /// - `min` — lower bound (inclusive).
225 /// - `max` — upper bound (inclusive).
226 ///
227 /// # Returns
228 /// `min` if `self < min`, `max` if `self > max`, otherwise `self`.
229 #[inline]
230 #[must_use]
231 fn clamp_to(self, min: Self, max: Self) -> Self {
232 if self < min { min } else if self > max { max } else { self }
233 }
234
235 /// Returns the absolute-value maximum of a slice using a type-specific
236 /// fast path when available, or `None` to signal fallback to the generic
237 /// scalar path.
238 ///
239 /// The default returns `None`. Types with hardware-specific
240 /// implementations (e.g. `f32` on x86-64 with AVX2) override this.
241 #[doc(hidden)]
242 #[inline]
243 fn avx2_abs_max(_slice: &[Self]) -> Option<Self> { None }
244
245 /// Converts this sample into a byte vector in native-endian order.
246 ///
247 /// Each sample is serialised to its native byte representation using
248 /// the platform's endianness. The returned vector has length
249 /// `Self::BYTES as usize`.
250 ///
251 /// For converting a whole slice at once, prefer
252 /// [`AudioSample::slice_to_bytes`].
253 ///
254 /// # Returns
255 ///
256 /// A `Vec<u8>` of length `Self::BYTES as usize` containing the
257 /// native-endian byte representation of this sample.
258 ///
259 /// ## Examples
260 ///
261 /// ```
262 /// use audio_samples::AudioSample;
263 ///
264 /// let sample: i16 = 256;
265 /// let bytes = sample.to_bytes();
266 /// assert_eq!(bytes.len(), 2);
267 /// ```
268 #[inline]
269 #[must_use]
270 fn to_bytes(self) -> Vec<u8> {
271 self.to_ne_bytes().as_ref().to_vec()
272 }
273
274 /// Converts this sample into a fixed-size byte array in native-endian order.
275 ///
276 /// The const generic parameter `N` must equal the byte size of `Self`
277 /// (e.g. `N = 4` for `f32` or `i32`). Supported widths — 1, 2, 3, 4,
278 /// and 8 — are enforced at compile time via the [`SupportedByteSize`]
279 /// bound.
280 ///
281 /// # Arguments
282 ///
283 /// – `N` (const generic) — the number of bytes in the output array.
284 /// Must match `Self::BYTES`; mismatches are compile errors.
285 ///
286 /// # Returns
287 ///
288 /// A `[u8; N]` containing the native-endian byte representation of
289 /// this sample.
290 ///
291 /// ## Examples
292 ///
293 /// ```
294 /// use audio_samples::AudioSample;
295 ///
296 /// let sample: i16 = 0x0100_i16;
297 /// let bytes: [u8; 2] = sample.as_bytes::<2>();
298 /// assert_eq!(bytes.len(), 2);
299 /// ```
300 #[inline]
301 fn as_bytes<const N: usize>(&self) -> [u8; N]
302 where SampleByteSize<N>: SupportedByteSize,
303 {
304 let bytes_ref = self.to_ne_bytes();
305 let bytes_slice: &[u8] = bytes_ref.as_ref();
306 let mut result = [0u8; N];
307 result.copy_from_slice(bytes_slice);
308 result
309 }
310
311 #[inline]
312 /// Converts a slice of samples into a byte vector in native-endian order.
313 ///
314 /// Uses [`bytemuck`] to reinterpret the slice as bytes. This avoids
315 /// element-wise copying when the alignment allows it.
316 ///
317 /// # Arguments
318 ///
319 /// – `samples` — a slice of samples to serialise.
320 ///
321 /// # Returns
322 ///
323 /// A `Vec<u8>` of length `samples.len() * Self::BYTES as usize`.
324 ///
325 /// ## Examples
326 ///
327 /// ```
328 /// use audio_samples::AudioSample;
329 ///
330 /// let samples = [1_i16, 2, 3];
331 /// let bytes = i16::slice_to_bytes(&samples);
332 /// assert_eq!(bytes.len(), 6); // 3 samples × 2 bytes each
333 /// ```
334 fn slice_to_bytes(samples: &[Self]) -> Vec<u8> {
335 Vec::from(bytemuck::cast_slice(samples))
336 }
337
338 #[inline]
339 /// Casts this sample value to `f64` without audio-aware scaling.
340 ///
341 /// This is a raw numeric cast equivalent to `self as f64`. No
342 /// normalisation into `[-1.0, 1.0]` is applied. For integer sample
343 /// types, the raw integer magnitude is preserved.
344 ///
345 /// For audio-aware conversion that scales integer samples into the
346 /// floating-point range, use [`ConvertTo::<f64>::convert_to`] instead.
347 ///
348 /// # Returns
349 ///
350 /// The sample value cast to `f64`, preserving the raw numeric magnitude.
351 ///
352 /// ## Examples
353 ///
354 /// ```
355 /// use audio_samples::AudioSample;
356 ///
357 /// // Raw cast: the integer value 32767 becomes 32767.0, not 1.0.
358 /// let sample: i16 = 32767;
359 /// assert_eq!(sample.as_float(), 32767.0_f64);
360 ///
361 /// // For float types the value is unchanged.
362 /// let f: f32 = 0.5;
363 /// assert_eq!(f.as_float(), 0.5_f64);
364 /// ```
365 fn as_float(self) -> f64
366 {
367 self.cast_into()
368 }
369
370 /// Maximum representable amplitude value for this sample type.
371 ///
372 /// For integer types this is the type's integer maximum (e.g. `32767`
373 /// for `i16`, `255` for `u8`). For float types (`f32`, `f64`) this
374 /// is `1.0`.
375 const MAX: Self;
376
377 /// Minimum representable amplitude value for this sample type.
378 ///
379 /// For integer types this is the type's integer minimum (e.g. `-32768`
380 /// for `i16`, `0` for `u8`). For float types this is `-1.0`.
381 const MIN: Self;
382
383 /// Bit depth of this sample type (e.g. `8` for `u8`, `16` for `i16`).
384 const BITS: u8;
385
386 /// Byte width of this sample type, derived as `BITS / 8`.
387 const BYTES: u32 = Self::BITS as u32 / 8;
388
389 /// Human-readable label for this sample type, used for display and
390 /// plotting. Examples: `"u8"`, `"i16"`, `"I24"`, `"f32"`, `"f64"`.
391 const LABEL: &'static str;
392
393 /// Enum variant identifying this sample type at runtime.
394 ///
395 /// Corresponds to a variant of [`SampleType`].
396 const SAMPLE_TYPE: SampleType;
397}
398
399/// Supertrait combining [`AudioSample`] with full bidirectional conversion
400/// support across all standard sample types.
401///
402/// A type satisfies `StandardSample` automatically when it implements
403/// [`AudioSample`] together with [`CastInto<f64>`], [`CastFrom<f64>`], and
404/// [`ConvertFrom`] for every standard sample type (`u8`, `i16`, [`I24`](i24::I24),
405/// `i32`, `f32`, `f64`). No manual implementation is required.
406///
407/// The supported standard sample types are `u8`, `i16`, [`I24`](i24::I24), `i32`,
408/// `f32`, and `f64`. Most audio processing operations in this crate are
409/// generic over `T: StandardSample`.
410///
411/// Prefer this bound over [`AudioSample`] when bidirectional conversions
412/// between sample types are required.
413pub trait StandardSample:
414 AudioSample
415 + CastInto<f64>
416 + CastFrom<f64>
417 + ConvertFrom<Self>
418 + ConvertFrom<u8>
419 + ConvertFrom<i16>
420 + ConvertFrom<I24>
421 + ConvertFrom<i32>
422 + ConvertFrom<f32>
423 + ConvertFrom<f64>
424 + Castable
425{
426}
427
428impl<T> StandardSample for T where
429 T: AudioSample
430 + CastInto<f64>
431 + CastFrom<f64>
432 + ConvertFrom<Self>
433 + ConvertFrom<u8>
434 + ConvertFrom<i16>
435 + ConvertFrom<I24>
436 + ConvertFrom<i32>
437 + ConvertFrom<f32>
438 + ConvertFrom<f64>
439{
440}
441
442#[cfg(feature = "static-plots")]
443/// A marker supertrait that conditionally requires [`serde::Serialize`] and
444/// [`serde::Deserialize`] bounds depending on whether the `static-plots`
445/// feature is enabled.
446pub trait MaybeSerdeAudioSample: Serialize + Deserialize<'static> {}
447
448#[cfg(feature = "static-plots")]
449/// Blanket implementation for all types that implement the required serde
450/// traits when `static-plots` is enabled.
451impl<T: Serialize + Deserialize<'static>> MaybeSerdeAudioSample for T {}
452
453/// [`AudioSample`] to remain unconditionally usable without serde.
454#[cfg(not(feature = "static-plots"))]
455pub trait MaybeSerdeAudioSample {}
456
457#[cfg(not(feature = "static-plots"))]
458impl<T> MaybeSerdeAudioSample for T {}
459
460/// Trait for converting one sample type to another with audio-aware scaling.
461///
462/// `ConvertTo` performs conversions that are intended for audio sample values rather than raw
463/// numeric casts.
464///
465/// ## Conversion Behavior
466/// - **Integer ↔ Integer**: PCM-style bit-depth scaling (e.g. `i16::MAX` maps to `0x7FFF0000i32`)
467/// - **Integer ↔ Float**: normalized scaling into $[-1.0, 1.0]$ using asymmetric endpoints
468/// - **Float ↔ Integer**: clamp to $[-1.0, 1.0]$, then scale, round, and saturate
469/// - **I24 Special Handling**: conversions treat `I24` as a 24-bit signed PCM integer
470///
471/// ## Example
472/// ```rust
473/// use audio_samples::ConvertTo;
474///
475/// let sample_i16: i16 = 16384; // approximately half-scale
476/// let sample_f32: f32 = sample_i16.convert_to();
477/// assert!((sample_f32 - 0.5).abs() < 1e-4);
478///
479/// let sample_i32: i32 = sample_i16.convert_to();
480/// assert_eq!(sample_i32, 0x4000_0000);
481/// ```
482pub trait ConvertTo<Dst> {
483 /// Converts this sample into `Dst` using audio-aware scaling.
484 ///
485 /// # Returns
486 ///
487 /// The converted sample as `Dst`. Conversion semantics follow the rules
488 /// documented on [`ConvertTo`]: integer-to-float converts to
489 /// `[-1.0, 1.0]`; float-to-integer clamps, scales, and rounds;
490 /// integer-to-integer shifts bit depth with saturation.
491 fn convert_to(self) -> Dst;
492}
493
494/// Audio-aware conversion from a source sample type into `Self`.
495///
496/// This is the blanket-implemented inverse of [`ConvertTo`]. Implementing
497/// `ConvertFrom<Src>` for `Dst` automatically provides `ConvertTo<Dst>`
498/// for `Src` via the blanket implementation.
499///
500/// Conversion semantics are the same as documented on [`ConvertTo`]:
501/// integer ↔ float conversions apply asymmetric normalised scaling;
502/// integer ↔ integer conversions use PCM-style bit-depth shifting with
503/// saturation; `u8` uses mid-scale unsigned PCM conventions.
504pub trait ConvertFrom<Src> {
505 /// Converts a sample of type `Src` into `Self`.
506 ///
507 /// # Arguments
508 ///
509 /// – `source` — the source sample to convert.
510 ///
511 /// # Returns
512 ///
513 /// The converted sample as `Self`.
514 fn convert_from(source: Src) -> Self;
515}
516
517impl<Src, Dst> ConvertTo<Dst> for Src
518where
519 Dst: ConvertFrom<Src>,
520{
521 #[inline]
522 fn convert_to(self) -> Dst {
523 Dst::convert_from(self)
524 }
525}
526
527// Identity
528macro_rules! impl_identity_conversion {
529 ($ty:ty) => {
530 impl ConvertFrom<$ty> for $ty {
531 #[inline]
532 fn convert_from(source: $ty) -> Self {
533 source
534 }
535 }
536 };
537}
538
539// Integer -> Integer, saturating (with bit-shift scaling if needed)
540macro_rules! impl_int_to_int_conversion {
541 ($from:ty, $to:ty) => {
542 impl ConvertFrom<$from> for $to {
543 #[inline]
544 fn convert_from(source: $from) -> Self {
545 let from_bits = <$from>::BITS as i32;
546 let to_bits = <$to>::BITS as i32;
547
548 let v = <i128 as From<$from>>::from(source);
549 let scaled = if from_bits < to_bits {
550 v << (to_bits - from_bits)
551 } else if from_bits > to_bits {
552 v >> (from_bits - to_bits)
553 } else {
554 v
555 };
556
557 let min = <i128 as From<$to>>::from(<$to>::MIN);
558 let max = <i128 as From<$to>>::from(<$to>::MAX);
559
560 if scaled < min {
561 <$to>::MIN
562 } else if scaled > max {
563 <$to>::MAX
564 } else {
565 scaled as $to
566 }
567 }
568 }
569 };
570}
571
572// I24 -> Integer (any standard integer), saturating
573macro_rules! impl_i24_to_int {
574 ($to:ty) => {
575 impl ConvertFrom<I24> for $to {
576 #[inline]
577 fn convert_from(source: I24) -> Self {
578 let to_bits = <$to>::BITS as i32;
579 let v = <i128 as From<i32>>::from(source.to_i32());
580
581 let shift = to_bits - 24;
582 let scaled = if shift >= 0 {
583 v << shift
584 } else {
585 v >> (-shift)
586 };
587
588 let min = <i128 as From<$to>>::from(<$to>::MIN);
589 let max = <i128 as From<$to>>::from(<$to>::MAX);
590 if scaled < min {
591 <$to>::MIN
592 } else if scaled > max {
593 <$to>::MAX
594 } else {
595 scaled as $to
596 }
597 }
598 }
599 };
600}
601
602// Integer -> I24, saturating
603macro_rules! impl_int_to_i24 {
604 ($from:ty) => {
605 impl ConvertFrom<$from> for I24 {
606 #[inline]
607 fn convert_from(source: $from) -> Self {
608 let from_bits = <$from>::BITS as i32;
609 let v = <i128 as From<$from>>::from(source);
610
611 let shift = 24 - from_bits;
612 let scaled = if shift >= 0 {
613 v << shift
614 } else {
615 v >> (-shift)
616 };
617
618 let min = <i128 as From<i32>>::from(I24::MIN.to_i32());
619 let max = <i128 as From<i32>>::from(I24::MAX.to_i32());
620 let clamped = if scaled < min {
621 min as i32
622 } else if scaled > max {
623 max as i32
624 } else {
625 scaled as i32
626 };
627
628 I24::saturating_from_i32(clamped)
629 }
630 }
631 };
632}
633
634// I24 -> Float (normalised)
635macro_rules! impl_i24_to_float {
636 ($to:ty) => {
637 impl ConvertFrom<I24> for $to {
638 #[inline]
639 #[allow(clippy::cast_lossless)]
640 fn convert_from(source: I24) -> Self {
641 let v = source.to_i32() as $to;
642 let max = I24::MAX.to_i32() as $to;
643 let min = I24::MIN.to_i32() as $to;
644 if v < 0.0 { v / -min } else { v / max }
645 }
646 }
647 };
648}
649
650// Integer -> Float (normalised)
651macro_rules! impl_int_to_float {
652 ($from:ty, $to:ty) => {
653 impl ConvertFrom<$from> for $to {
654 #[inline]
655 fn convert_from(source: $from) -> Self {
656 let v = source;
657 if v < 0 {
658 (v as $to) / (-(<$from>::MIN as $to))
659 } else {
660 (v as $to) / (<$from>::MAX as $to)
661 }
662 }
663 }
664 };
665}
666
667// Float -> Integer (clamp + scale + truncating cast)
668//
669// Uses IEEE max/min (f32::max / f32::min) instead of clamp so that NaN
670// inputs produce a defined finite value: max(NaN, -1.0) = -1.0 per IEEE
671// 754-2008 maxNum semantics (returns the non-NaN operand). This lets LLVM
672// prove the intermediate value is finite, eliminating the NaN-handling
673// branches that Rust's saturating `as i32` would otherwise generate.
674// Without those branches LLVM can emit vcvttps2dq + vpackssdw (vectorised
675// f32→i32→i16) instead of the scalar vcvttss2si path.
676//
677// Semantic differences from a fully-IEEE-correct implementation:
678// - NaN input → −MAX (e.g. −32767) instead of 0 or ±MAX.
679// - Uses truncation rather than round-to-nearest (error ≤ 1 LSB, inaudible).
680// - −1.0 maps to −MAX (e.g. −32767 for i16), not MIN (−32768). The one-
681// code-unit asymmetry at full negative scale is inaudible in practice.
682// Used for i16 (and similarly small integer types) where $to::MAX is exactly
683// representable in f32/f64 (32767 < 2^23). `to_int_unchecked` removes the
684// NaN/overflow branches that `as i32` would otherwise emit, letting LLVM
685// vectorise the loop with vcvttps2dq + vpackssdw.
686macro_rules! impl_float_to_int {
687 ($from:ty, $to:ty) => {
688 impl ConvertFrom<$from> for $to {
689 #[inline]
690 fn convert_from(source: $from) -> Self {
691 // IEEE max/min: NaN input → -1.0 (non-NaN). v ∈ [-1.0, 1.0].
692 let v = source.max(-1.0).min(1.0);
693 // scaled ∈ [−MAX, MAX]. For i16, MAX = 32767 which is exactly
694 // representable in f32, so scaled is strictly within i32 range.
695 let scaled = v * (<$to>::MAX as $from);
696 // SAFETY: scaled is finite and |scaled| ≤ $to::MAX ≤ 32767,
697 // which fits comfortably in i32. No NaN/overflow is possible.
698 let as_i32: i32 = unsafe { scaled.to_int_unchecked() };
699 as_i32 as $to
700 }
701 }
702 };
703}
704
705// Used for i32 where i32::MAX = 2147483647 rounds up to 2147483648.0 in f32
706// (not exactly representable). Using `to_int_unchecked` there would be UB for
707// source = 1.0, so we use the saturating `as $to` cast instead.
708macro_rules! impl_float_to_large_int {
709 ($from:ty, $to:ty) => {
710 impl ConvertFrom<$from> for $to {
711 #[inline]
712 fn convert_from(source: $from) -> Self {
713 let v = source.max(-1.0).min(1.0);
714 let scaled = v * (<$to>::MAX as $from);
715 // Saturating cast handles the case where $to::MAX as $from
716 // rounds up (e.g. i32::MAX → 2147483648.0f32).
717 scaled.clamp(<$to>::MIN as $from, <$to>::MAX as $from) as $to
718 }
719 }
720 };
721}
722
723// Float -> I24 (clamp + scale + round + saturating)
724macro_rules! impl_float_to_i24 {
725 ($from:ty) => {
726 impl ConvertFrom<$from> for I24 {
727 #[inline]
728 fn convert_from(source: $from) -> Self {
729 let v = source.clamp(-1.0, 1.0);
730 let scaled = if v < 0.0 {
731 v * (-(I24::MIN.to_i32() as $from))
732 } else {
733 v * (I24::MAX.to_i32() as $from)
734 };
735 let rounded = scaled.round();
736 let clamped = if rounded < (I24::MIN.to_i32() as $from) {
737 I24::MIN.to_i32()
738 } else if rounded > (I24::MAX.to_i32() as $from) {
739 I24::MAX.to_i32()
740 } else {
741 rounded as i32
742 };
743 I24::saturating_from_i32(clamped)
744 }
745 }
746 };
747}
748
749// Float -> Float
750macro_rules! impl_float_to_float {
751 ($from:ty, $to:ty) => {
752 impl ConvertFrom<$from> for $to {
753 #[inline]
754 fn convert_from(source: $from) -> Self {
755 source as $to
756 }
757 }
758 };
759}
760
761// ========================
762// u8 <-> Signed Integer / I24 / Float
763// ========================
764//
765// Conventions:
766// - u8 is unsigned PCM with 128 as zero.
767// - Centred value c = (u8 as i32) - 128 in [-128, 127].
768// - Asymmetric scaling:
769// negative side uses divisor 128 (so 0 maps to -1.0 exactly)
770// positive side uses divisor 127 (so 255 maps to +1.0 exactly)
771//
772// - For u8 -> signed-int/I24: scale c into destination integer range using the
773// same asymmetric idea (negative uses abs(min), positive uses max).
774// - For signed-int/I24 -> u8: invert scaling, round-to-nearest, clamp to [0,255].
775//
776// Notes:
777// - All intermediate arithmetic is done in i128 to avoid overflow for i32::MIN abs.
778// - Rounding is symmetric "nearest" for integer division (positive numerators only).
779
780#[inline]
781const fn div_round_nearest_i128(num: i128, den: i128) -> i128 {
782 // Assumes den > 0 and num >= 0
783 (num + (den / 2)) / den
784}
785
786// u8 -> signed integer (i16/i32), saturating + asymmetric scaling
787macro_rules! impl_u8_to_int {
788 ($to:ty) => {
789 impl ConvertFrom<u8> for $to {
790 #[inline]
791 #[allow(clippy::cast_lossless)]
792 fn convert_from(source: u8) -> Self {
793 let c: i128 = (source as i128) - 128; // [-128, 127]
794
795 let scaled: i128 = if c < 0 {
796 // Map -128 -> MIN exactly
797 // c is negative, abs range is 128
798 c * (-(<$to>::MIN as i128)) / 128
799 } else {
800 // Map +127 -> MAX exactly
801 c * (<$to>::MAX as i128) / 127
802 };
803
804 // Should already be in-range, but keep the same saturating style.
805 let min = <$to>::MIN as i128;
806 let max = <$to>::MAX as i128;
807 if scaled < min {
808 <$to>::MIN
809 } else if scaled > max {
810 <$to>::MAX
811 } else {
812 scaled as $to
813 }
814 }
815 }
816 };
817}
818
819// signed integer -> u8, saturating + asymmetric scaling + rounding
820macro_rules! impl_int_to_u8 {
821 ($from:ty) => {
822 impl ConvertFrom<$from> for u8 {
823 #[inline]
824 #[allow(clippy::cast_lossless)]
825 fn convert_from(source: $from) -> Self {
826 let v: i128 = source as i128;
827
828 let out_i128: i128 = if v < 0 {
829 // v in [MIN, -1]
830 let mag = (-v) as i128; // positive
831 let den = (-(<$from>::MIN as i128)); // abs(min), e.g. 32768 for i16
832 let scaled = div_round_nearest_i128(mag * 128, den); // 0..128
833 128 - scaled
834 } else {
835 // v in [0, MAX]
836 let den = (<$from>::MAX as i128);
837 let scaled = div_round_nearest_i128(v * 127, den); // 0..127
838 128 + scaled
839 };
840
841 // Clamp to [0, 255]
842 if out_i128 < 0 {
843 0
844 } else if out_i128 > 255 {
845 255
846 } else {
847 out_i128 as u8
848 }
849 }
850 }
851 };
852}
853
854// u8 -> I24 (asymmetric scaling), saturating
855macro_rules! impl_u8_to_i24 {
856 () => {
857 impl ConvertFrom<u8> for I24 {
858 #[inline]
859 fn convert_from(source: u8) -> Self {
860 let c: i128 = (<i128 as From<u8>>::from(source)) - 128; // [-128, 127]
861 let min = <i128 as From<i32>>::from(I24::MIN.to_i32());
862 let max = <i128 as From<i32>>::from(I24::MAX.to_i32());
863
864 let scaled: i128 = if c < 0 {
865 c * (-min) / 128
866 } else {
867 c * max / 127
868 };
869
870 // Clamp into i24 range, then saturating construct.
871 let clamped: i32 = if scaled < min {
872 min as i32
873 } else if scaled > max {
874 max as i32
875 } else {
876 scaled as i32
877 };
878
879 I24::saturating_from_i32(clamped)
880 }
881 }
882 };
883}
884
885// I24 -> u8 (invert asymmetric scaling), saturating + rounding
886macro_rules! impl_i24_to_u8 {
887 () => {
888 impl ConvertFrom<I24> for u8 {
889 #[inline]
890 fn convert_from(source: I24) -> Self {
891 let v: i128 = <i128 as From<i32>>::from(source.to_i32());
892 let min = <i128 as From<i32>>::from(I24::MIN.to_i32()); // negative
893 let max = <i128 as From<i32>>::from(I24::MAX.to_i32()); // positive
894
895 let out_i128: i128 = if v < 0 {
896 let mag = <i128 as From<i128>>::from(-v);
897 let den = <i128 as From<i128>>::from(-min); // abs(min) = 8388608
898 let scaled = div_round_nearest_i128(mag * 128, den); // 0..128
899 128 - scaled
900 } else {
901 let den = <i128 as From<i128>>::from(max); // 8388607
902 let scaled = div_round_nearest_i128(v * 127, den); // 0..127
903 128 + scaled
904 };
905
906 if out_i128 < 0 {
907 0
908 } else if out_i128 > 255 {
909 255
910 } else {
911 out_i128 as u8
912 }
913 }
914 }
915 };
916}
917
918// u8 -> float (normalised, asymmetric endpoints)
919macro_rules! impl_u8_to_float {
920 ($to:ty) => {
921 impl ConvertFrom<u8> for $to {
922 #[inline]
923 fn convert_from(source: u8) -> Self {
924 let c: i32 = (<i32 as From<u8>>::from(source)) - 128; // [-128, 127]
925 let v = c as $to;
926 if c < 0 {
927 v / (128.0 as $to)
928 } else {
929 v / (127.0 as $to)
930 }
931 }
932 }
933 };
934}
935
936// float -> u8 (clamp + asymmetric scale + round + saturate)
937macro_rules! impl_float_to_u8 {
938 ($from:ty) => {
939 impl ConvertFrom<$from> for u8 {
940 #[inline]
941 fn convert_from(source: $from) -> Self {
942 let v = source.clamp(-1.0, 1.0);
943
944 // Convert float to centred integer c in [-128, 127] with asymmetric scaling.
945 // Negative maps to [-128, 0], positive maps to [0, 127].
946 let c: i128 = if v < 0.0 {
947 // -1.0 -> -128 exactly
948 (v * (128.0 as $from)).round() as i128
949 } else {
950 // +1.0 -> +127 exactly
951 (v * (127.0 as $from)).round() as i128
952 };
953
954 let out = 128i128 + c;
955
956 if out < 0 {
957 0
958 } else if out > 255 {
959 255
960 } else {
961 out as u8
962 }
963 }
964 }
965 };
966}
967
968// ========================
969// u8 Identity
970// ========================
971
972impl_identity_conversion!(u8);
973
974// ========================
975// u8 <-> Integer
976// ========================
977
978impl_u8_to_int!(i16);
979impl_u8_to_int!(i32);
980
981impl_int_to_u8!(i16);
982impl_int_to_u8!(i32);
983
984// ========================
985// u8 <-> I24
986// ========================
987
988impl_u8_to_i24!();
989impl_i24_to_u8!();
990
991// ========================
992// u8 <-> Float
993// ========================
994
995impl_u8_to_float!(f32);
996impl_u8_to_float!(f64);
997
998impl_float_to_u8!(f32);
999impl_float_to_u8!(f64);
1000
1001// ========================
1002// Identity
1003// ========================
1004
1005impl_identity_conversion!(i16);
1006impl_identity_conversion!(I24);
1007impl_identity_conversion!(i32);
1008impl_identity_conversion!(f32);
1009impl_identity_conversion!(f64);
1010
1011// ========================
1012// Integer <-> Integer (Saturating, No Normalisation)
1013// ========================
1014
1015impl_int_to_int_conversion!(i16, i32);
1016impl_int_to_int_conversion!(i32, i16);
1017
1018// ========================
1019// I24 <-> Integer
1020// ========================
1021
1022impl_i24_to_int!(i16);
1023impl_i24_to_int!(i32);
1024
1025impl_int_to_i24!(i16);
1026impl_int_to_i24!(i32);
1027
1028// ========================
1029// Integer -> Float (Normalised +- 1.0)
1030// ========================
1031
1032impl_int_to_float!(i16, f32);
1033impl_int_to_float!(i16, f64);
1034
1035impl_int_to_float!(i32, f32);
1036impl_int_to_float!(i32, f64);
1037
1038// ========================
1039// I24 -> Float (Normalised +- 1.0)
1040// ========================
1041
1042impl_i24_to_float!(f32);
1043impl_i24_to_float!(f64);
1044
1045// ========================
1046// Float -> Integer (Clamped, Rounded, Saturating)
1047// ========================
1048
1049impl_float_to_int!(f32, i16);
1050impl_float_to_int!(f64, i16);
1051
1052impl_float_to_large_int!(f32, i32);
1053impl_float_to_large_int!(f64, i32);
1054
1055// ========================
1056// Float -> I24 (Clamped, Rounded, Saturating)
1057// ========================
1058
1059impl_float_to_i24!(f32);
1060impl_float_to_i24!(f64);
1061
1062// ========================
1063// Float <-> Float
1064// ========================
1065
1066impl_float_to_float!(f32, f64);
1067impl_float_to_float!(f64, f32);
1068
1069// ========================
1070// AVX2 fast path for f32 abs-max scan
1071// ========================
1072
1073/// Computes the maximum absolute value of a contiguous f32 slice using AVX2.
1074///
1075/// Processes 8 f32 samples per iteration using `_mm256_andnot_ps` to mask
1076/// the sign bit (branchless abs) and `_mm256_max_ps` to track the running
1077/// maximum across 8 independent lanes. A horizontal reduction folds the
1078/// lanes to a single scalar at the end.
1079///
1080/// # Safety
1081/// Caller must ensure that the AVX2 feature is available
1082/// (e.g. via `is_x86_feature_detected!("avx2")`).
1083#[cfg(target_arch = "x86_64")]
1084#[target_feature(enable = "avx2")]
1085unsafe fn abs_max_f32_avx2(slice: &[f32]) -> f32 {
1086 use std::arch::x86_64::{
1087 _mm_cvtss_f32, _mm_max_ps, _mm_max_ss, _mm_movehl_ps, _mm_shuffle_ps, _mm256_andnot_ps,
1088 _mm256_castps256_ps128, _mm256_extractf128_ps, _mm256_loadu_ps, _mm256_max_ps,
1089 _mm256_set1_ps, _mm256_setzero_ps,
1090 };
1091
1092 // Bit-mask that strips the IEEE 754 sign bit from every lane.
1093 let sign_mask = _mm256_set1_ps(-0.0_f32);
1094 // Eight independent max accumulators — no sequential dependency.
1095 let mut max_v = _mm256_setzero_ps();
1096
1097 let chunks = slice.len() / 8;
1098 let ptr = slice.as_ptr();
1099
1100 for i in 0..chunks {
1101 // SAFETY: i * 8 + 7 < slice.len() because i < chunks = slice.len() / 8.
1102 unsafe {
1103 let v = _mm256_loadu_ps(ptr.add(i * 8));
1104 let abs_v = _mm256_andnot_ps(sign_mask, v); // abs via bitmask, no branch
1105 max_v = _mm256_max_ps(max_v, abs_v);
1106 }
1107 }
1108
1109 // Horizontal reduction: fold 8 f32 lanes down to one scalar.
1110 let hi128 = _mm256_extractf128_ps(max_v, 1); // lanes [4..7]
1111 let lo128 = _mm256_castps256_ps128(max_v); // lanes [0..3]
1112 let max128 = _mm_max_ps(lo128, hi128);
1113 let shuf = _mm_movehl_ps(max128, max128);
1114 let max64 = _mm_max_ps(max128, shuf);
1115 let shuf2 = _mm_shuffle_ps(max64, max64, 0x1);
1116 let max32 = _mm_max_ss(max64, shuf2);
1117 let mut result = _mm_cvtss_f32(max32);
1118
1119 // Scalar tail for the remaining < 8 samples.
1120 for &x in &slice[chunks * 8..] {
1121 let ax = x.abs();
1122 if ax > result {
1123 result = ax;
1124 }
1125 }
1126 result
1127}
1128
1129// ========================
1130// AudioSample Implementations
1131// ========================
1132impl AudioSample for u8 {
1133 const MAX: Self = Self::MAX;
1134 const MIN: Self = Self::MIN;
1135 const BITS: Self = 8;
1136 const LABEL: &'static str = "u8";
1137 const SAMPLE_TYPE: SampleType = SampleType::U8;
1138 #[inline]
1139 fn clamp_to(self, min: Self, max: Self) -> Self {
1140 Ord::clamp(self, min, max)
1141 }
1142}
1143
1144impl AudioSample for i16 {
1145 const MAX: Self = Self::MAX;
1146 const MIN: Self = Self::MIN;
1147 const BITS: u8 = 16;
1148 const LABEL: &'static str = "i16";
1149 const SAMPLE_TYPE: SampleType = SampleType::I16;
1150 #[inline]
1151 fn clamp_to(self, min: Self, max: Self) -> Self {
1152 Ord::clamp(self, min, max)
1153 }
1154}
1155
1156impl AudioSample for I24 {
1157 #[inline]
1158 fn slice_to_bytes(samples: &[Self]) -> Vec<u8> {
1159 Self::write_i24s_ne(samples)
1160 }
1161
1162 const MAX: Self = Self::MAX;
1163 const MIN: Self = Self::MIN;
1164 const BITS: u8 = 24;
1165 const LABEL: &'static str = "I24";
1166 const SAMPLE_TYPE: SampleType = SampleType::I24;
1167 #[inline]
1168 fn clamp_to(self, min: Self, max: Self) -> Self {
1169 Ord::clamp(self, min, max)
1170 }
1171}
1172
1173impl AudioSample for i32 {
1174 const MAX: Self = Self::MAX;
1175 const MIN: Self = Self::MIN;
1176 const BITS: u8 = 32;
1177 const LABEL: &'static str = "i32";
1178 const SAMPLE_TYPE: SampleType = SampleType::I32;
1179 #[inline]
1180 fn clamp_to(self, min: Self, max: Self) -> Self {
1181 Ord::clamp(self, min, max)
1182 }
1183}
1184
1185impl AudioSample for f32 {
1186 const MAX: Self = 1.0;
1187 const MIN: Self = -1.0;
1188 const BITS: u8 = 32;
1189 const LABEL: &'static str = "f32";
1190 const SAMPLE_TYPE: SampleType = SampleType::F32;
1191 /// Uses `f32::clamp` — compiles to `VMAXSS`/`VMINSS` (and `VMAXPS`/`VMINPS` in
1192 /// vectorised loops), matching what C achieves with `-ffast-math`.
1193 #[inline]
1194 fn clamp_to(self, min: Self, max: Self) -> Self {
1195 self.clamp(min, max)
1196 }
1197
1198 #[inline]
1199 fn avx2_abs_max(slice: &[Self]) -> Option<Self> {
1200 #[cfg(target_arch = "x86_64")]
1201 if is_x86_feature_detected!("avx2") {
1202 // safety: caller must ensure AVX2 is available, which we check with is_x86_feature_detected.
1203 return Some(unsafe { abs_max_f32_avx2(slice) });
1204 }
1205 None
1206 }
1207}
1208
1209impl AudioSample for f64 {
1210 const MAX: Self = 1.0;
1211 const MIN: Self = -1.0;
1212 const BITS: u8 = 64;
1213 const LABEL: &'static str = "f64";
1214 const SAMPLE_TYPE: SampleType = SampleType::F64;
1215 /// Uses `f64::clamp` — compiles to `VMAXSD`/`VMINSD` in vectorised loops.
1216 #[inline]
1217 fn clamp_to(self, min: Self, max: Self) -> Self {
1218 self.clamp(min, max)
1219 }
1220}
1221
1222// ========================
1223// Generate All Conversions
1224// ========================
1225
1226macro_rules! impl_cast_from {
1227 ($src:ty => [$($dst:ty),+]) => {
1228 $(
1229 impl CastFrom<$src> for $dst {
1230 #[inline]
1231 fn cast_from(value: $src) -> Self {
1232 value as $dst
1233 }
1234 }
1235 )+
1236 };
1237}
1238
1239impl_cast_from!(u8 => [u8, i16, i32, f32, f64]);
1240impl_cast_from!(i16 => [u8, i16, i32, f32, f64]);
1241impl_cast_from!(i32 => [u8, i16, i32, f32, f64]);
1242impl_cast_from!(f64 => [u8, i16, i32, f32, f64]);
1243impl_cast_from!(f32 => [u8, i16, i32, f32, f64]);
1244
1245/// Macro to implement the `CastFrom` trait for multiple type pairs
1246macro_rules! impl_cast_from_i24 {
1247 // Simple direct casts (no clamping or special logic)
1248 ($src:ty => $dst:ty) => {
1249 impl CastFrom<$src> for $dst {
1250 #[inline]
1251 #[allow(clippy::cast_lossless)]
1252 fn cast_from(value: $src) -> Self {
1253 value as $dst
1254 }
1255 }
1256 };
1257
1258 // Clamped casts for usize -> integer
1259 (clamp_usize $src:ty => $dst:ty, $max:expr) => {
1260 impl CastFrom<$src> for $dst {
1261 #[inline]
1262 fn cast_from(value: $src) -> Self {
1263 if value > $max as $src {
1264 $max
1265 } else {
1266 value as $dst
1267 }
1268 }
1269 }
1270 };
1271
1272 // usize -> I24 with clamping and try_from_i32
1273 (usize_to_i24 $src:ty => $dst:ty) => {
1274 impl CastFrom<$src> for $dst {
1275 #[inline]
1276 fn cast_from(value: $src) -> Self {
1277 if value > I24::MAX.to_i32() as $src {
1278 I24::MAX
1279 } else {
1280 match I24::try_from_i32(value as i32) {
1281 Some(x) => x,
1282 None => I24::MIN,
1283 }
1284 }
1285 }
1286 }
1287 };
1288
1289 // I24 -> primitive
1290 (i24_to_primitive $src:ty => $dst:ty) => {
1291 impl CastFrom<$src> for $dst {
1292 #[inline]
1293 #[allow(clippy::cast_lossless)]
1294 fn cast_from(value: $src) -> Self {
1295 value.to_i32() as $dst
1296 }
1297 }
1298 };
1299
1300 // primitive -> I24
1301 (primitive_to_i24 $src:ty => $dst:ty) => {
1302 impl CastFrom<$src> for $dst {
1303 #[inline]
1304 #[allow(clippy::cast_lossless)]
1305 fn cast_from(value: $src) -> Self {
1306 I24::try_from_i32(value as i32).unwrap_or(I24::MIN)
1307 }
1308 }
1309 };
1310
1311 // identity
1312 (identity $t:ty) => {
1313 impl CastFrom<$t> for $t {
1314 #[inline]
1315 fn cast_from(value: $t) -> Self {
1316 value
1317 }
1318 }
1319 };
1320}
1321
1322// usize to primitives
1323impl_cast_from_i24!(clamp_usize usize => u8, u8::MAX);
1324impl_cast_from_i24!(clamp_usize usize => i16, i16::MAX);
1325impl_cast_from_i24!(usize_to_i24 usize => I24);
1326impl_cast_from_i24!(clamp_usize usize => i32, i32::MAX);
1327impl_cast_from_i24!(usize => f32);
1328impl_cast_from_i24!(usize => f64);
1329
1330// I24 to primitives
1331impl_cast_from_i24!(i24_to_primitive I24 => u8);
1332impl_cast_from_i24!(i24_to_primitive I24 => i16);
1333impl_cast_from_i24!(identity I24);
1334impl_cast_from_i24!(i24_to_primitive I24 => i32);
1335impl_cast_from_i24!(i24_to_primitive I24 => f32);
1336impl_cast_from_i24!(i24_to_primitive I24 => f64);
1337
1338// primitives to I24
1339impl_cast_from_i24!(primitive_to_i24 u8 => I24);
1340impl_cast_from_i24!(primitive_to_i24 i16 => I24);
1341impl_cast_from_i24!(primitive_to_i24 i32 => I24);
1342impl_cast_from_i24!(primitive_to_i24 f32 => I24);
1343impl_cast_from_i24!(primitive_to_i24 f64 => I24);
1344
1345macro_rules! impl_cast_into {
1346 ($src:ty => [$($dst:ty),+]) => {
1347 $(
1348 impl CastInto<$dst> for $src {
1349 #[inline]
1350 fn cast_into(self) -> $dst {
1351 <$dst>::cast_from(self)
1352 }
1353 }
1354 )+
1355 };
1356}
1357impl_cast_into!(u8 => [u8, i16, i32, f32, f64]);
1358impl_cast_into!(i16 => [u8, i16, i32, f32, f64]);
1359impl_cast_into!(i32 => [u8, i16, i32, f32, f64]);
1360impl_cast_into!(f64 => [u8, i16, i32, f32, f64]);
1361impl_cast_into!(f32 => [u8, i16, i32, f32, f64]);
1362
1363/// Macro to implement the `CastInto` trait for multiple type pairs
1364macro_rules! impl_cast_into_i24 {
1365 // I24 -> primitive (via to_i32)
1366 (i24_to_primitive $src:ty => $dst:ty) => {
1367 impl CastInto<$dst> for $src {
1368 #[inline]
1369 fn cast_into(self) -> $dst {
1370 self.to_i32() as $dst
1371 }
1372 }
1373 };
1374
1375 // primitive -> I24 (via CastFrom)
1376 (primitive_to_i24 $src:ty => $dst:ty) => {
1377 impl CastInto<$dst> for $src {
1378 #[inline]
1379 fn cast_into(self) -> $dst {
1380 <$dst as CastFrom<$src>>::cast_from(self)
1381 }
1382 }
1383 };
1384
1385 // identity
1386 (identity $t:ty) => {
1387 impl CastInto<$t> for $t {
1388 #[inline]
1389 fn cast_into(self) -> $t {
1390 self
1391 }
1392 }
1393 };
1394}
1395// I24 to primitives
1396
1397impl_cast_into_i24!(i24_to_primitive I24 => u8);
1398impl_cast_into_i24!(i24_to_primitive I24 => i16);
1399impl_cast_into_i24!(identity I24);
1400impl_cast_into_i24!(i24_to_primitive I24 => i32);
1401impl_cast_into_i24!(i24_to_primitive I24 => f32);
1402impl_cast_into_i24!(i24_to_primitive I24 => f64);
1403
1404// primitives to I24
1405impl_cast_into_i24!(primitive_to_i24 u8 => I24);
1406impl_cast_into_i24!(primitive_to_i24 i16 => I24);
1407impl_cast_into_i24!(primitive_to_i24 i32 => I24);
1408impl_cast_into_i24!(primitive_to_i24 f32 => I24);
1409impl_cast_into_i24!(primitive_to_i24 f64 => I24);
1410
1411/// Audio sample conversion and casting operations for [`AudioSamples`].
1412///
1413/// This trait defines the public conversion surface for transforming an
1414/// [`AudioSamples`] value from one sample representation to another.
1415///
1416/// ## Purpose
1417///
1418/// Audio data is commonly represented using both integer PCM formats and
1419/// floating-point formats. This trait provides two explicit conversion modes:
1420///
1421/// - *Audio-aware conversion* for interpreting numeric values as audio samples,
1422/// applying the appropriate scaling and clamping when moving between integer
1423/// and floating-point representations.
1424/// - *Raw numeric casting* for transforming values using standard numeric rules
1425/// without audio-specific scaling.
1426///
1427/// The two modes are intentionally distinct and must be selected explicitly by
1428/// the caller.
1429///
1430/// ## Behavioural Guarantees
1431///
1432/// - All operations return a new owned [`AudioSamples`] value.
1433/// - Sample rate, channel structure, and sample ordering are preserved.
1434/// - The source audio is not modified.
1435/// - Conversions are total and do not return `Result`.
1436///
1437/// When converting from floating-point to fixed-width integer formats, values
1438/// outside the representable range are clamped.
1439///
1440/// ## Assumptions
1441///
1442/// The conversion behaviour is defined by the conversion traits implemented for
1443/// the involved sample types. This trait is implemented for `AudioSamples<T>`
1444/// where those conversions are available.
1445pub trait AudioTypeConversion: Sized {
1446 /// The source sample type of the audio being converted.
1447 type Sample: StandardSample;
1448
1449 /// Converts the audio to a different sample type using audio-aware scaling.
1450 ///
1451 /// Performs an audio-aware conversion from `Self::Sample` to `O`. The
1452 /// amplitude meaning of each sample is preserved: integer-to-float
1453 /// conversions normalise into `[-1.0, 1.0]`; float-to-integer conversions
1454 /// clamp, scale, and round; integer-to-integer conversions shift bit depth
1455 /// with saturation. For `u8`, the mid-scale unsigned PCM convention is
1456 /// applied.
1457 ///
1458 /// The source audio is not modified; a new owned value is returned.
1459 ///
1460 /// # Returns
1461 ///
1462 /// A new owned [`AudioSamples<'static, O>`] with samples converted from
1463 /// `Self::Sample` to `O`. Sample rate, channel count, and temporal
1464 /// ordering are preserved.
1465 ///
1466 /// ## Examples
1467 ///
1468 /// ```
1469 /// use audio_samples::{AudioSamples, sample_rate, AudioTypeConversion};
1470 /// use ndarray::array;
1471 ///
1472 /// let audio = AudioSamples::new_mono(
1473 /// array![0i16, i16::MAX],
1474 /// sample_rate!(44100),
1475 /// ).unwrap();
1476 ///
1477 /// let float_audio = audio.to_format::<f32>();
1478 /// let samples = float_audio.as_mono().unwrap();
1479 /// assert_eq!(samples[0], 0.0_f32);
1480 /// assert!((samples[1] - 1.0_f32).abs() < 1e-4);
1481 /// ```
1482 fn to_format<O>(&self) -> AudioSamples<'static, O>
1483 where
1484 Self::Sample: ConvertTo<O> + ConvertFrom<O>,
1485 O: StandardSample;
1486
1487 /// Converts the audio to a different sample type, consuming the source.
1488 ///
1489 /// This is the consuming counterpart to [`AudioTypeConversion::to_format`].
1490 /// It performs the same audio-aware conversion, but takes ownership of the
1491 /// input value. Prefer this method over `to_format` when the source is no
1492 /// longer needed, to avoid an unnecessary clone.
1493 ///
1494 /// # Returns
1495 ///
1496 /// A new owned [`AudioSamples<'static, O>`] with samples converted from
1497 /// `Self::Sample` to `O`. Sample rate, channel count, and temporal
1498 /// ordering are preserved.
1499 ///
1500 /// ## Examples
1501 ///
1502 /// ```
1503 /// use audio_samples::{AudioSamples, AudioTypeConversion, sample_rate};
1504 /// use ndarray::array;
1505 ///
1506 /// let audio = AudioSamples::new_mono(array![0i16, i16::MAX], sample_rate!(44100)).unwrap();
1507 /// let audio_f32: AudioSamples<'static, f32> = audio.to_type::<f32>();
1508 /// assert_eq!(audio_f32[0], 0.0);
1509 /// assert!((audio_f32[1] - 1.0).abs() < 1e-4);
1510 /// ```
1511 fn to_type<O>(self) -> AudioSamples<'static, O>
1512 where
1513 Self::Sample: ConvertTo<O> + ConvertFrom<O>,
1514 O: StandardSample;
1515
1516 /// Casts the audio to a different sample type without audio-aware scaling.
1517 ///
1518 /// Performs a raw numeric cast from `Self::Sample` to `O`, equivalent to
1519 /// an `as` cast applied element-wise. No normalisation, clamping, or
1520 /// bit-depth scaling is applied. Integer values are preserved as their
1521 /// raw numeric magnitude.
1522 ///
1523 /// Use [`AudioTypeConversion::to_format`] when amplitude meaning must be
1524 /// preserved across sample types.
1525 ///
1526 /// # Returns
1527 ///
1528 /// A new owned [`AudioSamples<'static, O>`] containing raw-cast samples.
1529 /// The source audio is unchanged. Sample rate, channel count, and temporal
1530 /// ordering are preserved.
1531 ///
1532 /// ## Examples
1533 ///
1534 /// ```
1535 /// use audio_samples::{AudioSamples, AudioTypeConversion, sample_rate};
1536 /// use ndarray::array;
1537 ///
1538 /// // Raw cast: i16 value 1000 becomes f32 value 1000.0, not 0.030518...
1539 /// let audio = AudioSamples::new_mono(array![1000i16, -500], sample_rate!(44100)).unwrap();
1540 /// let raw = audio.cast_as::<f32>();
1541 /// assert_eq!(raw[0], 1000.0_f32);
1542 /// assert_eq!(raw[1], -500.0_f32);
1543 /// ```
1544 fn cast_as<O>(&self) -> AudioSamples<'static, O>
1545 where
1546 Self::Sample: CastInto<O> + ConvertTo<O>,
1547 O: StandardSample;
1548
1549 /// Casts the audio to a different sample type without audio-aware scaling,
1550 /// consuming the source.
1551 ///
1552 /// This is the consuming counterpart to [`AudioTypeConversion::cast_as`].
1553 /// It performs the same raw numeric cast but takes ownership of the input.
1554 ///
1555 /// # Returns
1556 ///
1557 /// A new owned [`AudioSamples<'static, O>`] containing raw-cast samples.
1558 /// Sample rate, channel count, and ordering are preserved.
1559 ///
1560 /// ## Examples
1561 ///
1562 /// ```
1563 /// use audio_samples::{AudioSamples, AudioTypeConversion, sample_rate};
1564 /// use ndarray::array;
1565 ///
1566 /// let audio = AudioSamples::new_mono(array![255u8, 128u8, 0u8], sample_rate!(44100)).unwrap();
1567 /// let raw: AudioSamples<'static, i16> = audio.cast_to::<i16>();
1568 /// assert_eq!(raw[0], 255);
1569 /// assert_eq!(raw[1], 128);
1570 /// assert_eq!(raw[2], 0);
1571 /// ```
1572 fn cast_to<O>(self) -> AudioSamples<'static, O>
1573 where
1574 Self::Sample: CastInto<O> + ConvertTo<O>,
1575 O: StandardSample;
1576
1577 /// Casts the audio to `f64` without audio-aware scaling.
1578 ///
1579 /// Each sample value is raw-cast to `f64` as a number, preserving its
1580 /// numeric magnitude without normalisation. For integer sample types,
1581 /// the integer value (e.g. `32767`) is cast directly to `f64`, not
1582 /// scaled to `1.0`.
1583 ///
1584 /// For audio-aware conversion into `[-1.0, 1.0]`, use
1585 /// [`AudioTypeConversion::as_f64`] instead.
1586 ///
1587 /// # Returns
1588 ///
1589 /// A new owned [`AudioSamples<'static, f64>`] with raw-cast sample values.
1590 #[inline]
1591 fn cast_as_f64(&self) -> AudioSamples<'static, f64> {
1592 self.cast_as::<f64>()
1593 }
1594
1595 /// Converts the audio to `f64` using audio-aware scaling.
1596 ///
1597 /// Integer sample values are normalised into `[-1.0, 1.0]` according to
1598 /// the [`ConvertTo`] rules for the source type. For `u8` audio, value
1599 /// `128` maps to `0.0`. For float sources the values are widened unchanged.
1600 ///
1601 /// # Returns
1602 ///
1603 /// A new owned [`AudioSamples<'static, f64>`] with normalised sample values.
1604 #[inline]
1605 fn as_float(&self) -> AudioSamples<'static, f64> {
1606 self.to_format::<f64>()
1607 }
1608
1609 /// Converts the audio to `f64` using audio-aware scaling.
1610 ///
1611 /// Equivalent to [`AudioTypeConversion::as_float`]. Integer sample values
1612 /// are normalised into `[-1.0, 1.0]`.
1613 ///
1614 /// # Returns
1615 ///
1616 /// A new owned [`AudioSamples<'static, f64>`] with normalised sample values.
1617 #[inline]
1618 fn as_f64(&self) -> AudioSamples<'static, f64> {
1619 self.to_format::<f64>()
1620 }
1621
1622 /// Converts the audio to `f32` using audio-aware scaling.
1623 ///
1624 /// Integer sample values are normalised into `[-1.0, 1.0]`. For `u8`
1625 /// audio, value `128` maps to `0.0`.
1626 ///
1627 /// # Returns
1628 ///
1629 /// A new owned [`AudioSamples<'static, f32>`] with normalised sample values.
1630 #[inline]
1631 fn as_f32(&self) -> AudioSamples<'static, f32> {
1632 self.to_format::<f32>()
1633 }
1634
1635 /// Converts the audio to 32-bit signed integer PCM using audio-aware
1636 /// scaling.
1637 ///
1638 /// Float samples in `[-1.0, 1.0]` are scaled and rounded to the `i32`
1639 /// range with saturation. For `u8` audio, mid-scale value `128` maps to
1640 /// `0`.
1641 ///
1642 /// # Returns
1643 ///
1644 /// A new owned [`AudioSamples<'static, i32>`] in signed 32-bit PCM format.
1645 #[inline]
1646 fn as_i32(&self) -> AudioSamples<'static, i32> {
1647 self.to_format::<i32>()
1648 }
1649
1650 /// Converts the audio to 16-bit signed integer PCM using audio-aware
1651 /// scaling.
1652 ///
1653 /// Float samples in `[-1.0, 1.0]` are scaled and rounded to the `i16`
1654 /// range with saturation. For `u8` audio, mid-scale value `128` maps to
1655 /// `0`.
1656 ///
1657 /// # Returns
1658 ///
1659 /// A new owned [`AudioSamples<'static, i16>`] in signed 16-bit PCM format.
1660 #[inline]
1661 fn as_i16(&self) -> AudioSamples<'static, i16> {
1662 self.to_format::<i16>()
1663 }
1664
1665 /// Converts the audio to 24-bit signed integer PCM using audio-aware
1666 /// scaling.
1667 ///
1668 /// Float samples in `[-1.0, 1.0]` are scaled and rounded to the [`I24`](i24::I24)
1669 /// range with saturation. For `u8` audio, mid-scale value `128` maps to
1670 /// `0`.
1671 ///
1672 /// # Returns
1673 ///
1674 /// A new owned [`AudioSamples<'static, I24>`] in signed 24-bit PCM format.
1675 #[inline]
1676 fn as_i24(&self) -> AudioSamples<'static, I24> {
1677 self.to_format::<I24>()
1678 }
1679
1680 /// Converts the audio to 8-bit unsigned PCM format using audio-aware
1681 /// scaling.
1682 ///
1683 /// Float samples in `[-1.0, 1.0]` and signed integer samples are scaled
1684 /// to the `u8` range with mid-scale silence at `128`. Negative full-scale
1685 /// maps to `0`; positive full-scale maps to `255`.
1686 ///
1687 /// # Returns
1688 ///
1689 /// A new owned [`AudioSamples<'static, u8>`] in unsigned 8-bit PCM format.
1690 #[inline]
1691 fn as_u8(&self) -> AudioSamples<'static, u8> {
1692 self.to_format::<u8>()
1693 }
1694}
1695
1696#[cfg(test)]
1697mod conversion_tests {
1698 use super::*;
1699
1700 use i24::i24;
1701
1702 macro_rules! assert_approx_eq {
1703 ($left:expr, $right:expr, $tolerance:expr) => {
1704 assert!(
1705 ($left - $right).abs() < $tolerance,
1706 "assertion failed: `{} ≈ {}` (tolerance: {})",
1707 $left,
1708 $right,
1709 $tolerance
1710 );
1711 };
1712 }
1713
1714 #[test]
1715 fn u8_tests() {
1716 let zero: u8 = 0;
1717 let mid: u8 = 128;
1718 let max: u8 = 255;
1719 let neg_one_f32: f32 = -1.0;
1720 let zero_f32: f32 = 0.0;
1721 let one_f32: f32 = 1.0;
1722
1723 let zero_to_neg_one: f32 = zero.convert_to();
1724 let mid_to_zero: f32 = mid.convert_to();
1725 let max_to_one: f32 = max.convert_to();
1726 assert_approx_eq!(zero_to_neg_one as f64, -1.0, 1e-10);
1727 assert_approx_eq!(mid_to_zero as f64, 0.0, 1e-10);
1728 assert_approx_eq!(max_to_one as f64, 1.0, 1e-10);
1729
1730 let neg_one_to_u8: u8 = neg_one_f32.convert_to();
1731 let zero_to_u8: u8 = zero_f32.convert_to();
1732 let one_to_u8: u8 = one_f32.convert_to();
1733
1734 assert_eq!(neg_one_to_u8, 0);
1735 assert_eq!(zero_to_u8, 128);
1736 assert_eq!(one_to_u8, 255);
1737 }
1738
1739 // Edge cases for i16 conversions
1740 #[test]
1741 fn i16_edge_cases() {
1742 // Test minimum value
1743 let min_i16: i16 = i16::MIN;
1744 let min_i16_to_f32: f32 = min_i16.convert_to();
1745 // Use higher epsilon for floating point comparison
1746 assert_approx_eq!(min_i16_to_f32 as f64, -1.0, 1e-5);
1747
1748 let min_i16_to_i32: i32 = min_i16.convert_to();
1749 assert_eq!(min_i16_to_i32, i32::MIN);
1750
1751 let min_i16_to_i24: I24 = min_i16.convert_to();
1752 let expected_i24_min = i24!(i32::MIN >> 8);
1753 assert_eq!(min_i16_to_i24.to_i32(), expected_i24_min.to_i32());
1754
1755 // Test maximum value
1756 let max_i16: i16 = i16::MAX;
1757 let max_i16_to_f32: f32 = max_i16.convert_to();
1758 assert_approx_eq!(max_i16_to_f32 as f64, 1.0, 1e-4);
1759
1760 let max_i16_to_i32: i32 = max_i16.convert_to();
1761 assert_eq!(max_i16_to_i32, 0x7FFF0000);
1762
1763 // Test zero
1764 let zero_i16: i16 = 0;
1765 let zero_i16_to_f32: f32 = zero_i16.convert_to();
1766 assert_approx_eq!(zero_i16_to_f32 as f64, 0.0, 1e-10);
1767
1768 let zero_i16_to_i32: i32 = zero_i16.convert_to();
1769 assert_eq!(zero_i16_to_i32, 0);
1770
1771 let zero_i16_to_i24: I24 = zero_i16.convert_to();
1772 assert_eq!(zero_i16_to_i24.to_i32(), 0);
1773
1774 // Test mid-range positive
1775 let half_max_i16: i16 = i16::MAX / 2;
1776 let half_max_i16_to_f32: f32 = half_max_i16.convert_to();
1777 // Use higher epsilon for floating point comparison of half values
1778 assert_approx_eq!(half_max_i16_to_f32 as f64, 0.5, 1e-4);
1779
1780 let half_max_i16_to_i32: i32 = half_max_i16.convert_to();
1781 assert_eq!(half_max_i16_to_i32, 0x3FFF0000);
1782
1783 // Test mid-range negative
1784 let half_min_i16: i16 = i16::MIN / 2;
1785 let half_min_i16_to_f32: f32 = half_min_i16.convert_to();
1786 assert_approx_eq!(half_min_i16_to_f32 as f64, -0.5, 1e-4);
1787
1788 // let half_min_i16_to_i32: i32 = half_min_i16.convert_to();
1789 // assert_eq!(half_min_i16_to_i32, 0xC0010000); // i16::MIN/2 == -16384
1790 }
1791
1792 // Edge cases for i32 conversions
1793 #[test]
1794 fn i32_edge_cases() {
1795 // Test minimum value
1796 let min_i32: i32 = i32::MIN;
1797 let min_i32_to_f32: f32 = min_i32.convert_to();
1798 assert_approx_eq!(min_i32_to_f32 as f64, -1.0, 1e-6);
1799
1800 let min_i32_to_f64: f64 = min_i32.convert_to();
1801 assert_approx_eq!(min_i32_to_f64, -1.0, 1e-12);
1802
1803 let min_i32_to_i16: i16 = min_i32.convert_to();
1804 assert_eq!(min_i32_to_i16, i16::MIN);
1805
1806 // Test maximum value
1807 let max_i32: i32 = i32::MAX;
1808 let max_i32_to_f32: f32 = max_i32.convert_to();
1809 assert_approx_eq!(max_i32_to_f32 as f64, 1.0, 1e-6);
1810
1811 let max_i32_to_f64: f64 = max_i32.convert_to();
1812 assert_approx_eq!(max_i32_to_f64, 1.0, 1e-12);
1813
1814 let max_i32_to_i16: i16 = max_i32.convert_to();
1815 assert_eq!(max_i32_to_i16, i16::MAX);
1816
1817 // Test zero
1818 let zero_i32: i32 = 0;
1819 let zero_i32_to_f32: f32 = zero_i32.convert_to();
1820 assert_approx_eq!(zero_i32_to_f32 as f64, 0.0, 1e-10);
1821
1822 let zero_i32_to_f64: f64 = zero_i32.convert_to();
1823 assert_approx_eq!(zero_i32_to_f64, 0.0, 1e-12);
1824
1825 let zero_i32_to_i16: i16 = zero_i32.convert_to();
1826 assert_eq!(zero_i32_to_i16, 0);
1827
1828 // Test quarter-range values
1829 let quarter_max_i32: i32 = i32::MAX / 4;
1830 let quarter_max_i32_to_f32: f32 = quarter_max_i32.convert_to();
1831 assert_approx_eq!(quarter_max_i32_to_f32 as f64, 0.25, 1e-6);
1832
1833 let quarter_min_i32: i32 = i32::MIN / 4;
1834 let quarter_min_i32_to_f32: f32 = quarter_min_i32.convert_to();
1835 assert_approx_eq!(quarter_min_i32_to_f32 as f64, -0.25, 1e-6);
1836 }
1837
1838 // Edge cases for f32 conversions
1839 #[test]
1840 fn f32_edge_cases() {
1841 // Test -1.0 (minimum valid value)
1842 let min_f32: f32 = -1.0;
1843 let min_f32_to_i16: i16 = min_f32.convert_to();
1844 // For exact -1.0, we can get -32767 due to rounding in the implementation
1845 // This is acceptable since it's only 1 bit off from the true min
1846 assert!(
1847 min_f32_to_i16 == i16::MIN || min_f32_to_i16 == -32767,
1848 "Expected either -32768 or -32767, got {}",
1849 min_f32_to_i16
1850 );
1851
1852 let min_f32_to_i32: i32 = min_f32.convert_to();
1853 assert!(
1854 min_f32_to_i32 == i32::MIN || min_f32_to_i32 == -2147483647,
1855 "Expected either i32::MIN or -2147483647, got {}",
1856 min_f32_to_i32
1857 );
1858
1859 let min_f32_to_i24: I24 = min_f32.convert_to();
1860 let expected_i24 = I24::MIN;
1861 let diff = (min_f32_to_i24.to_i32() - expected_i24.to_i32()).abs();
1862 assert!(diff <= 1, "I24 values differ by more than 1, {}", diff);
1863
1864 // Test 1.0 (maximum valid value)
1865 let max_f32: f32 = 1.0;
1866 let max_f32_to_i16: i16 = max_f32.convert_to();
1867 println!("DEBUG: f32 -> i16 conversion for 1.0");
1868 println!(
1869 "Input: {}, Output: {}, Expected: {}",
1870 max_f32,
1871 max_f32_to_i16,
1872 i16::MAX
1873 );
1874 assert_eq!(max_f32_to_i16, i16::MAX);
1875
1876 let max_f32_to_i32: i32 = max_f32.convert_to();
1877 println!("DEBUG: f32 -> i32 conversion for 1.0");
1878 println!(
1879 "Input: {}, Output: {}, Expected: {}",
1880 max_f32,
1881 max_f32_to_i32,
1882 i32::MAX
1883 );
1884 assert_eq!(max_f32_to_i32, i32::MAX);
1885
1886 // Test 0.0
1887 let zero_f32: f32 = 0.0;
1888 let zero_f32_to_i16: i16 = zero_f32.convert_to();
1889 println!("DEBUG: f32 -> i16 conversion for 0.0");
1890 println!(
1891 "Input: {}, Output: {}, Expected: 0",
1892 zero_f32, zero_f32_to_i16
1893 );
1894 assert_eq!(zero_f32_to_i16, 0);
1895
1896 let zero_f32_to_i32: i32 = zero_f32.convert_to();
1897 println!("DEBUG: f32 -> i32 conversion for 0.0");
1898 println!(
1899 "Input: {}, Output: {}, Expected: 0",
1900 zero_f32, zero_f32_to_i32
1901 );
1902 assert_eq!(zero_f32_to_i32, 0);
1903
1904 let zero_f32_to_i24: I24 = zero_f32.convert_to();
1905 println!("DEBUG: f32 -> I24 conversion for 0.0");
1906 println!(
1907 "Input: {}, Output: {} (i32 value), Expected: 0",
1908 zero_f32,
1909 zero_f32_to_i24.to_i32()
1910 );
1911 assert_eq!(zero_f32_to_i24.to_i32(), 0);
1912
1913 // Test clamping of out-of-range values
1914 let large_f32: f32 = 2.0;
1915 let large_f32_to_i16: i16 = large_f32.convert_to();
1916 assert_eq!(large_f32_to_i16, i16::MAX);
1917
1918 let neg_large_f32: f32 = -2.0;
1919 let neg_large_f32_to_i16: i16 = neg_large_f32.convert_to();
1920 assert!(
1921 neg_large_f32_to_i16 == i16::MIN || neg_large_f32_to_i16 == -32767,
1922 "Expected either -32768 or -32767, got {}",
1923 neg_large_f32_to_i16
1924 );
1925
1926 let large_f32_to_i32: i32 = large_f32.convert_to();
1927 assert_eq!(large_f32_to_i32, i32::MAX);
1928
1929 let neg_large_f32_to_i32: i32 = neg_large_f32.convert_to();
1930 assert!(
1931 neg_large_f32_to_i32 == i32::MIN || neg_large_f32_to_i32 == -2147483647,
1932 "Expected either i32::MIN or -2147483647, got {}",
1933 neg_large_f32_to_i32
1934 );
1935
1936 // Test small values
1937 let small_value: f32 = 1.0e-6;
1938 let small_value_to_i16: i16 = small_value.convert_to();
1939 assert_eq!(small_value_to_i16, 0);
1940
1941 let small_value_to_i32: i32 = small_value.convert_to();
1942 assert_eq!(small_value_to_i32, 2147); // 1.0e-6 * 2147483647 rounded to nearest
1943
1944 // Test values near 0.5
1945 let half_f32: f32 = 0.5;
1946 let half_f32_to_i16: i16 = half_f32.convert_to();
1947 assert_eq!(half_f32_to_i16, 16383); // 0.5 * 32767 = 16383.5, truncated to 16383
1948
1949 let neg_half_f32: f32 = -0.5;
1950 let neg_half_f32_to_i16: i16 = neg_half_f32.convert_to();
1951 assert_eq!(neg_half_f32_to_i16, -16383); // -0.5 * 32767 = -16383.5, truncated toward zero
1952 }
1953
1954 // Edge cases for f64 conversions
1955 #[test]
1956 fn f64_edge_cases() {
1957 // Test -1.0 (minimum valid value)
1958 let min_f64: f64 = -1.0;
1959 let min_f64_to_i16: i16 = min_f64.convert_to();
1960
1961 println!("DEBUG: f64 -> i16 conversion for -1.0");
1962 println!(
1963 "Input: {}, Output: {}, Expected: {} or {}",
1964 min_f64,
1965 min_f64_to_i16,
1966 i16::MIN,
1967 -32767
1968 );
1969
1970 // Due to rounding in the implementation, sometimes -1.0 can convert to -32767
1971 // This is acceptable since it's only 1 bit off from the true min
1972 assert!(
1973 min_f64_to_i16 == i16::MIN || min_f64_to_i16 == -32767,
1974 "Expected either -32768 or -32767, got {}",
1975 min_f64_to_i16
1976 );
1977
1978 let min_f64_to_i32: i32 = min_f64.convert_to();
1979
1980 println!("DEBUG: f64 -> i32 conversion for -1.0");
1981 println!(
1982 "Input: {}, Output: {}, Expected: {} or {}",
1983 min_f64,
1984 min_f64_to_i32,
1985 i32::MIN,
1986 -2147483647
1987 );
1988
1989 assert!(
1990 min_f64_to_i32 == i32::MIN || min_f64_to_i32 == -2147483647,
1991 "Expected either i32::MIN or -2147483647, got {}",
1992 min_f64_to_i32
1993 );
1994
1995 let min_f64_to_f32: f32 = min_f64.convert_to();
1996
1997 println!("DEBUG: f64 -> f32 conversion for -1.0");
1998 println!(
1999 "Input: {}, Output: {}, Expected: -1.0",
2000 min_f64, min_f64_to_f32
2001 );
2002
2003 assert_approx_eq!(min_f64_to_f32 as f64, -1.0, 1e-6);
2004
2005 // Test 1.0 (maximum valid value)
2006 let max_f64: f64 = 1.0;
2007 let max_f64_to_i16: i16 = max_f64.convert_to();
2008 assert_eq!(max_f64_to_i16, i16::MAX);
2009
2010 let max_f64_to_i32: i32 = max_f64.convert_to();
2011 assert_eq!(max_f64_to_i32, i32::MAX);
2012
2013 let max_f64_to_f32: f32 = max_f64.convert_to();
2014 assert_approx_eq!(max_f64_to_f32 as f64, 1.0, 1e-6);
2015
2016 // Test 0.0
2017 let zero_f64: f64 = 0.0;
2018 let zero_f64_to_i16: i16 = zero_f64.convert_to();
2019 assert_eq!(zero_f64_to_i16, 0);
2020
2021 let zero_f64_to_i32: i32 = zero_f64.convert_to();
2022 assert_eq!(zero_f64_to_i32, 0);
2023
2024 let zero_f64_to_f32: f32 = zero_f64.convert_to();
2025 assert_approx_eq!(zero_f64_to_f32 as f64, 0.0, 1e-10);
2026
2027 // Test clamping of out-of-range values
2028 let large_f64: f64 = 2.0;
2029 let large_f64_to_i16: i16 = large_f64.convert_to();
2030 assert_eq!(large_f64_to_i16, i16::MAX);
2031
2032 let neg_large_f64: f64 = -2.0;
2033 let neg_large_f64_to_i16: i16 = neg_large_f64.convert_to();
2034 assert!(
2035 neg_large_f64_to_i16 == i16::MIN || neg_large_f64_to_i16 == -32767,
2036 "Expected either -32768 or -32767, got {}",
2037 neg_large_f64_to_i16
2038 );
2039
2040 // Test very small values
2041 let tiny_value: f64 = 1.0e-12;
2042 let tiny_value_to_i16: i16 = tiny_value.convert_to();
2043 assert_eq!(tiny_value_to_i16, 0);
2044
2045 let tiny_value_to_i32: i32 = tiny_value.convert_to();
2046 assert_eq!(tiny_value_to_i32, 0);
2047
2048 let tiny_value_to_f32: f32 = tiny_value.convert_to();
2049 assert_approx_eq!(tiny_value_to_f32 as f64, 0.0, 1e-10);
2050 }
2051
2052 // Tests for I24 conversions
2053 #[test]
2054 fn i24_conversion_tests() {
2055 // Create an I24 with a known value
2056 let i24_value = i24!(4660 << 8); // So converting back to i16 gives 4660
2057 println!(
2058 "DEBUG: Created I24 value from 4660 << 8 = {}",
2059 i24_value.to_i32()
2060 );
2061
2062 // Test I24 to i16
2063 let i24_to_i16: i16 = i24_value.convert_to();
2064 let expected_i16 = 0x1234_i16;
2065 println!("DEBUG: I24 -> i16 conversion");
2066 println!(
2067 "I24 (as i32): {}, i16: {}, Expected: {}",
2068 i24_value.to_i32(),
2069 i24_to_i16,
2070 expected_i16
2071 );
2072 assert_eq!(i24_to_i16, expected_i16);
2073
2074 // Test I24 to f32
2075 let i24_to_f32: f32 = i24_value.convert_to();
2076 let expected_f32 = (0x123456 as f32) / (I24::MAX.to_i32() as f32);
2077 println!("DEBUG: I24 -> f32 conversion");
2078 println!(
2079 "I24 (as i32): {}, f32: {}, Expected: {}",
2080 i24_value.to_i32(),
2081 i24_to_f32,
2082 expected_f32
2083 );
2084 // Print the difference to help debug
2085 println!("DEBUG: Difference: {}", (i24_to_f32 - expected_f32).abs());
2086 assert_approx_eq!(i24_to_f32 as f64, expected_f32 as f64, 1e-4);
2087
2088 // Test I24 to f64
2089 let i24_to_f64: f64 = i24_value.convert_to();
2090 let expected_f64 = (0x123456 as f64) / (I24::MAX.to_i32() as f64);
2091 println!("DEBUG: I24 -> f64 conversion");
2092 println!(
2093 "I24 (as i32): {}, f64: {}, Expected: {}",
2094 i24_value.to_i32(),
2095 i24_to_f64,
2096 expected_f64
2097 );
2098 // Print the difference to help debug
2099 println!("DEBUG: Difference: {}", (i24_to_f64 - expected_f64).abs());
2100 assert_approx_eq!(i24_to_f64, expected_f64, 1e-4);
2101 }
2102
2103 // Tests for convert_from functionality
2104 #[test]
2105 fn convert_from_tests() {
2106 // Test i16::convert_from with different source types
2107 let f32_source: f32 = 0.5;
2108 let i16_result: i16 = i16::convert_from(f32_source);
2109 assert_eq!(i16_result, 16383); // 0.5 * 32767 = 16383.5, truncated to 16383
2110
2111 let i32_source: i32 = 65536;
2112 let i16_result: i16 = i16::convert_from(i32_source);
2113 assert_eq!(i16_result, 1); // 65536 >> 16 = 1
2114
2115 // Test f32::convert_from with different source types
2116 let i16_source: i16 = 16384;
2117 let f32_result: f32 = f32::convert_from(i16_source);
2118 assert_approx_eq!(f32_result as f64, 0.5, 1e-4);
2119
2120 let i32_source: i32 = i32::MAX / 2;
2121 let f32_result: f32 = f32::convert_from(i32_source);
2122 assert_approx_eq!(f32_result as f64, 0.5, 1e-4);
2123
2124 // Test I24::convert_from
2125 let i16_source: i16 = 4660; // 0x1234
2126 let i24_result: I24 = I24::convert_from(i16_source);
2127 assert_eq!(i24_result.to_i32(), 4660 << 8); // Should be shifted left by 8 bits
2128
2129 // Test with zero values
2130 let zero_f32: f32 = 0.0;
2131 let zero_i16: i16 = i16::convert_from(zero_f32);
2132 assert_eq!(zero_i16, 0);
2133
2134 let zero_i16_source: i16 = 0;
2135 let zero_f32_result: f32 = f32::convert_from(zero_i16_source);
2136 assert_approx_eq!(zero_f32_result as f64, 0.0, 1e-10);
2137 }
2138
2139 // Tests for round trip conversions
2140 #[test]
2141 fn round_trip_conversions() {
2142 // i16 -> f32 -> i16
2143 for sample in [-32768, -16384, 0, 16384, 32767].iter() {
2144 let original = *sample;
2145 let intermediate: f32 = original.convert_to();
2146 let round_tripped: i16 = intermediate.convert_to();
2147
2148 println!("DEBUG: i16->f32->i16 conversion");
2149 println!(
2150 "Original i16: {}, f32: {}, Round trip i16: {}",
2151 original, intermediate, round_tripped
2152 );
2153
2154 assert!(
2155 (original - round_tripped).abs() <= 1,
2156 "Expected {}, got {}",
2157 original,
2158 round_tripped
2159 );
2160 }
2161
2162 // i32 -> f32 -> i32 (will lose precision)
2163 for &sample in &[i32::MIN, i32::MIN / 2, 0, i32::MAX / 2, i32::MAX] {
2164 let original = sample;
2165 let intermediate: f32 = original.convert_to();
2166 let round_tripped: i32 = intermediate.convert_to();
2167
2168 // Special case for extreme values
2169 if original == i32::MIN {
2170 // Allow off-by-one for MIN value
2171 assert!(
2172 round_tripped == i32::MIN || round_tripped == -2147483647,
2173 "Expected either i32::MIN or -2147483647, got {}",
2174 round_tripped
2175 );
2176 } else if original == i32::MAX || original == 0 {
2177 assert_eq!(
2178 original, round_tripped,
2179 "Failed in i32->f32->i32 with extreme value {}",
2180 original
2181 );
2182 } else {
2183 // For other values, we expect close but not exact due to precision
2184 let ratio = (round_tripped as f64) / (original as f64);
2185 assert!(
2186 ratio > 0.999 && ratio < 1.001,
2187 "Round trip error too large: {} -> {}",
2188 original,
2189 round_tripped
2190 );
2191 }
2192 }
2193
2194 // f32 -> i16 -> f32
2195 for &sample in &[-1.0, -0.5, 0.0, 0.5, 1.0] {
2196 let original: f32 = sample;
2197 let intermediate: i16 = original.convert_to();
2198 let round_tripped: f32 = intermediate.convert_to();
2199
2200 // For all values, we check approximately but with a more generous epsilon
2201 assert_approx_eq!(original as f64, round_tripped as f64, 1e-4);
2202 }
2203
2204 // i16 -> I24 -> i16
2205 for &sample in &[i16::MIN, -16384, 0, 16384, i16::MAX] {
2206 let original = sample;
2207 let intermediate: I24 = original.convert_to();
2208 let round_tripped: i16 = intermediate.convert_to();
2209
2210 // For extreme negative values, allow 1-bit difference
2211 if original == i16::MIN {
2212 assert!(
2213 round_tripped == i16::MIN || round_tripped == -32767,
2214 "Expected either -32768 or -32767, got {}",
2215 round_tripped
2216 );
2217 } else {
2218 assert_eq!(
2219 original, round_tripped,
2220 "Failed in i16->I24->i16 with value {}",
2221 original
2222 );
2223 }
2224 }
2225 }
2226}