oximedia-audio 0.1.4

Audio codec implementations for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Waveform display and rendering.

use crate::frame::AudioFrame;
use crate::AudioBuffer;
use oximedia_core::SampleFormat;

/// Waveform rendering mode.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WaveformMode {
    /// Line waveform (connect samples).
    Line,
    /// Filled waveform (fill area under curve).
    Filled,
    /// Min/Max bars (show min and max in each column).
    MinMax,
    /// RMS bars (show RMS level in each column).
    Rms,
}

/// Waveform configuration.
#[derive(Clone, Debug)]
pub struct WaveformConfig {
    /// Width in pixels.
    pub width: usize,
    /// Height in pixels.
    pub height: usize,
    /// Rendering mode.
    pub mode: WaveformMode,
    /// Background color (RGB).
    pub background_color: [u8; 3],
    /// Waveform color (RGB).
    pub waveform_color: [u8; 3],
    /// Grid color (RGB), None for no grid.
    pub grid_color: Option<[u8; 3]>,
    /// Zero-line color (RGB), None for no zero-line.
    pub zero_line_color: Option<[u8; 3]>,
    /// Number of grid divisions.
    pub grid_divisions: usize,
}

impl WaveformConfig {
    /// Create a new waveform configuration.
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            width,
            height,
            mode: WaveformMode::MinMax,
            background_color: [0, 0, 0],
            waveform_color: [0, 255, 0],
            grid_color: Some([64, 64, 64]),
            zero_line_color: Some([128, 128, 128]),
            grid_divisions: 4,
        }
    }
}

impl Default for WaveformConfig {
    fn default() -> Self {
        Self::new(800, 200)
    }
}

/// Waveform image.
#[derive(Clone, Debug)]
pub struct WaveformImage {
    /// Image width.
    pub width: usize,
    /// Image height.
    pub height: usize,
    /// RGB pixel data (row-major order).
    pub data: Vec<u8>,
}

impl WaveformImage {
    /// Create a new waveform image with background color.
    #[must_use]
    pub fn new(width: usize, height: usize, background_color: [u8; 3]) -> Self {
        let mut data = vec![0; width * height * 3];

        // Fill with background color
        for i in 0..width * height {
            data[i * 3] = background_color[0];
            data[i * 3 + 1] = background_color[1];
            data[i * 3 + 2] = background_color[2];
        }

        Self {
            width,
            height,
            data,
        }
    }

    /// Set pixel at (x, y) to RGB color.
    pub fn set_pixel(&mut self, x: usize, y: usize, color: [u8; 3]) {
        if x >= self.width || y >= self.height {
            return;
        }

        let idx = (y * self.width + x) * 3;
        self.data[idx] = color[0];
        self.data[idx + 1] = color[1];
        self.data[idx + 2] = color[2];
    }

    /// Get pixel at (x, y).
    #[must_use]
    pub fn get_pixel(&self, x: usize, y: usize) -> Option<[u8; 3]> {
        if x >= self.width || y >= self.height {
            return None;
        }

        let idx = (y * self.width + x) * 3;
        Some([self.data[idx], self.data[idx + 1], self.data[idx + 2]])
    }

    /// Draw a horizontal line.
    pub fn draw_horizontal_line(&mut self, y: usize, color: [u8; 3]) {
        for x in 0..self.width {
            self.set_pixel(x, y, color);
        }
    }

    /// Draw a vertical line.
    pub fn draw_vertical_line(&mut self, x: usize, color: [u8; 3]) {
        for y in 0..self.height {
            self.set_pixel(x, y, color);
        }
    }

    /// Draw a line between two points using Bresenham's algorithm.
    #[allow(clippy::cast_possible_wrap)]
    #[allow(clippy::cast_sign_loss)]
    pub fn draw_line(&mut self, x0: usize, y0: usize, x1: usize, y1: usize, color: [u8; 3]) {
        let dx = (x1 as i32 - x0 as i32).abs();
        let dy = -(y1 as i32 - y0 as i32).abs();
        let sx = if x0 < x1 { 1 } else { -1 };
        let sy = if y0 < y1 { 1 } else { -1 };
        let mut err = dx + dy;

        let mut x = x0 as i32;
        let mut y = y0 as i32;

        loop {
            if x >= 0 && y >= 0 && (x as usize) < self.width && (y as usize) < self.height {
                self.set_pixel(x as usize, y as usize, color);
            }

            if x == x1 as i32 && y == y1 as i32 {
                break;
            }

            let e2 = 2 * err;
            if e2 >= dy {
                if x == x1 as i32 {
                    break;
                }
                err += dy;
                x += sx;
            }
            if e2 <= dx {
                if y == y1 as i32 {
                    break;
                }
                err += dx;
                y += sy;
            }
        }
    }

    /// Fill a vertical line (for filled waveform mode).
    pub fn fill_vertical(&mut self, x: usize, y_start: usize, y_end: usize, color: [u8; 3]) {
        let start = y_start.min(y_end);
        let end = y_start.max(y_end);

        for y in start..=end {
            if y < self.height {
                self.set_pixel(x, y, color);
            }
        }
    }

    /// Save as PPM format.
    pub fn save_ppm(&self, path: &str) -> std::io::Result<()> {
        use std::fs::File;
        use std::io::Write;

        let mut file = File::create(path)?;
        writeln!(file, "P6")?;
        writeln!(file, "{} {}", self.width, self.height)?;
        writeln!(file, "255")?;
        file.write_all(&self.data)?;

        Ok(())
    }
}

/// Waveform renderer.
pub struct WaveformRenderer {
    config: WaveformConfig,
}

impl WaveformRenderer {
    /// Create a new waveform renderer.
    #[must_use]
    pub const fn new(config: WaveformConfig) -> Self {
        Self { config }
    }

    /// Render a waveform from audio frame.
    pub fn render(&self, frame: &AudioFrame) -> Result<WaveformImage, String> {
        let samples = self.extract_samples(frame)?;
        self.render_samples(&samples)
    }

    /// Render a waveform from raw samples.
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    pub fn render_samples(&self, samples: &[f64]) -> Result<WaveformImage, String> {
        let mut image = WaveformImage::new(
            self.config.width,
            self.config.height,
            self.config.background_color,
        );

        if samples.is_empty() {
            return Ok(image);
        }

        // Draw grid
        if let Some(grid_color) = self.config.grid_color {
            self.draw_grid(&mut image, grid_color);
        }

        // Draw zero line
        if let Some(zero_color) = self.config.zero_line_color {
            let center_y = self.config.height / 2;
            image.draw_horizontal_line(center_y, zero_color);
        }

        // Calculate samples per pixel
        let samples_per_pixel = samples.len() as f64 / self.config.width as f64;

        match self.config.mode {
            WaveformMode::Line => {
                self.render_line(&mut image, samples, samples_per_pixel)?;
            }
            WaveformMode::Filled => {
                self.render_filled(&mut image, samples, samples_per_pixel)?;
            }
            WaveformMode::MinMax => {
                self.render_minmax(&mut image, samples, samples_per_pixel)?;
            }
            WaveformMode::Rms => {
                self.render_rms(&mut image, samples, samples_per_pixel)?;
            }
        }

        Ok(image)
    }

    /// Draw grid lines.
    fn draw_grid(&self, image: &mut WaveformImage, color: [u8; 3]) {
        let h_step = self.config.height / self.config.grid_divisions;
        let v_step = self.config.width / self.config.grid_divisions;

        // Horizontal lines
        for i in 0..=self.config.grid_divisions {
            let y = i * h_step;
            if y < self.config.height {
                image.draw_horizontal_line(y, color);
            }
        }

        // Vertical lines
        for i in 0..=self.config.grid_divisions {
            let x = i * v_step;
            if x < self.config.width {
                image.draw_vertical_line(x, color);
            }
        }
    }

    /// Render line waveform.
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    fn render_line(
        &self,
        image: &mut WaveformImage,
        samples: &[f64],
        samples_per_pixel: f64,
    ) -> Result<(), String> {
        let center_y = self.config.height / 2;

        let mut prev_y = center_y;

        for x in 0..self.config.width {
            let sample_idx = (x as f64 * samples_per_pixel) as usize;
            if sample_idx >= samples.len() {
                break;
            }

            let sample = samples[sample_idx].clamp(-1.0, 1.0);
            let y = center_y as f64 - sample * (self.config.height as f64 / 2.0);
            let y = y.clamp(0.0, (self.config.height - 1) as f64) as usize;

            if x > 0 {
                image.draw_line(x - 1, prev_y, x, y, self.config.waveform_color);
            }

            prev_y = y;
        }

        Ok(())
    }

    /// Render filled waveform.
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    fn render_filled(
        &self,
        image: &mut WaveformImage,
        samples: &[f64],
        samples_per_pixel: f64,
    ) -> Result<(), String> {
        let center_y = self.config.height / 2;

        for x in 0..self.config.width {
            let sample_idx = (x as f64 * samples_per_pixel) as usize;
            if sample_idx >= samples.len() {
                break;
            }

            let sample = samples[sample_idx].clamp(-1.0, 1.0);
            let y = center_y as f64 - sample * (self.config.height as f64 / 2.0);
            let y = y.clamp(0.0, (self.config.height - 1) as f64) as usize;

            image.fill_vertical(x, center_y, y, self.config.waveform_color);
        }

        Ok(())
    }

    /// Render min/max waveform.
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    fn render_minmax(
        &self,
        image: &mut WaveformImage,
        samples: &[f64],
        samples_per_pixel: f64,
    ) -> Result<(), String> {
        let center_y = self.config.height / 2;

        for x in 0..self.config.width {
            let start_idx = (x as f64 * samples_per_pixel) as usize;
            let end_idx = ((x + 1) as f64 * samples_per_pixel) as usize;

            if start_idx >= samples.len() {
                break;
            }

            let end_idx = end_idx.min(samples.len());

            // Find min and max in this range
            let mut min_sample = samples[start_idx];
            let mut max_sample = samples[start_idx];

            for &sample in &samples[start_idx..end_idx] {
                min_sample = min_sample.min(sample);
                max_sample = max_sample.max(sample);
            }

            min_sample = min_sample.clamp(-1.0, 1.0);
            max_sample = max_sample.clamp(-1.0, 1.0);

            let min_y = center_y as f64 - min_sample * (self.config.height as f64 / 2.0);
            let max_y = center_y as f64 - max_sample * (self.config.height as f64 / 2.0);

            let min_y = min_y.clamp(0.0, (self.config.height - 1) as f64) as usize;
            let max_y = max_y.clamp(0.0, (self.config.height - 1) as f64) as usize;

            image.fill_vertical(x, min_y, max_y, self.config.waveform_color);
        }

        Ok(())
    }

    /// Render RMS waveform.
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    fn render_rms(
        &self,
        image: &mut WaveformImage,
        samples: &[f64],
        samples_per_pixel: f64,
    ) -> Result<(), String> {
        let center_y = self.config.height / 2;

        for x in 0..self.config.width {
            let start_idx = (x as f64 * samples_per_pixel) as usize;
            let end_idx = ((x + 1) as f64 * samples_per_pixel) as usize;

            if start_idx >= samples.len() {
                break;
            }

            let end_idx = end_idx.min(samples.len());

            // Calculate RMS for this range
            let sum_squares: f64 = samples[start_idx..end_idx].iter().map(|&s| s * s).sum();

            let rms = (sum_squares / (end_idx - start_idx) as f64).sqrt();
            let rms = rms.clamp(0.0, 1.0);

            let pos_y = center_y as f64 - rms * (self.config.height as f64 / 2.0);
            let neg_y = center_y as f64 + rms * (self.config.height as f64 / 2.0);

            let pos_y = pos_y.clamp(0.0, (self.config.height - 1) as f64) as usize;
            let neg_y = neg_y.clamp(0.0, (self.config.height - 1) as f64) as usize;

            image.fill_vertical(x, pos_y, neg_y, self.config.waveform_color);
        }

        Ok(())
    }

    /// Extract samples from audio frame.
    fn extract_samples(&self, frame: &AudioFrame) -> Result<Vec<f64>, String> {
        let channel_count = frame.channels.count();
        if channel_count == 0 {
            return Err("No channels in audio frame".to_string());
        }

        match &frame.samples {
            AudioBuffer::Interleaved(data) => {
                self.extract_interleaved_samples(data, frame.format, channel_count)
            }
            AudioBuffer::Planar(planes) => {
                if planes.is_empty() {
                    return Err("Empty planar buffer".to_string());
                }
                self.extract_planar_samples(&planes[0], frame.format)
            }
        }
    }

    /// Extract samples from interleaved buffer.
    #[allow(clippy::cast_precision_loss)]
    fn extract_interleaved_samples(
        &self,
        data: &[u8],
        format: SampleFormat,
        channel_count: usize,
    ) -> Result<Vec<f64>, String> {
        let bytes_per_sample = format.bytes_per_sample();
        let sample_count = data.len() / (bytes_per_sample * channel_count);

        let mut samples = Vec::with_capacity(sample_count);

        for i in 0..sample_count {
            let offset = i * channel_count * bytes_per_sample;
            let sample = self.bytes_to_f64(&data[offset..offset + bytes_per_sample], format)?;
            samples.push(sample);
        }

        Ok(samples)
    }

    /// Extract samples from planar buffer.
    fn extract_planar_samples(
        &self,
        data: &[u8],
        format: SampleFormat,
    ) -> Result<Vec<f64>, String> {
        let bytes_per_sample = format.bytes_per_sample();
        let sample_count = data.len() / bytes_per_sample;

        let mut samples = Vec::with_capacity(sample_count);

        for i in 0..sample_count {
            let offset = i * bytes_per_sample;
            let sample = self.bytes_to_f64(&data[offset..offset + bytes_per_sample], format)?;
            samples.push(sample);
        }

        Ok(samples)
    }

    /// Convert bytes to f64 based on sample format.
    #[allow(clippy::cast_precision_loss)]
    fn bytes_to_f64(&self, bytes: &[u8], format: SampleFormat) -> Result<f64, String> {
        match format {
            SampleFormat::U8 => Ok(f64::from(bytes[0]) / 128.0 - 1.0),
            SampleFormat::S16 | SampleFormat::S16p => {
                if bytes.len() < 2 {
                    return Err("Insufficient bytes for S16".to_string());
                }
                let sample = i16::from_le_bytes([bytes[0], bytes[1]]);
                Ok(f64::from(sample) / 32768.0)
            }
            SampleFormat::S32 | SampleFormat::S32p => {
                if bytes.len() < 4 {
                    return Err("Insufficient bytes for S32".to_string());
                }
                let sample = i32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
                Ok(sample as f64 / 2_147_483_648.0)
            }
            SampleFormat::F32 | SampleFormat::F32p => {
                if bytes.len() < 4 {
                    return Err("Insufficient bytes for F32".to_string());
                }
                let sample = f32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
                Ok(f64::from(sample))
            }
            SampleFormat::F64 | SampleFormat::F64p => {
                if bytes.len() < 8 {
                    return Err("Insufficient bytes for F64".to_string());
                }
                Ok(f64::from_le_bytes([
                    bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
                ]))
            }
            _ => Err("Unsupported sample format".to_string()),
        }
    }
}

/// Phase scope for stereo correlation visualization.
pub struct PhaseScope {
    width: usize,
    height: usize,
    background_color: [u8; 3],
    trace_color: [u8; 3],
    grid_color: Option<[u8; 3]>,
}

impl PhaseScope {
    /// Create a new phase scope.
    #[must_use]
    pub const fn new(
        width: usize,
        height: usize,
        background_color: [u8; 3],
        trace_color: [u8; 3],
    ) -> Self {
        Self {
            width,
            height,
            background_color,
            trace_color,
            grid_color: Some([64, 64, 64]),
        }
    }

    /// Render phase scope from stereo samples.
    #[allow(clippy::cast_precision_loss)]
    #[allow(clippy::cast_possible_truncation)]
    #[allow(clippy::cast_sign_loss)]
    pub fn render(&self, left: &[f64], right: &[f64]) -> WaveformImage {
        let mut image = WaveformImage::new(self.width, self.height, self.background_color);

        // Draw grid
        if let Some(grid_color) = self.grid_color {
            let center_x = self.width / 2;
            let center_y = self.height / 2;

            image.draw_horizontal_line(center_y, grid_color);
            image.draw_vertical_line(center_x, grid_color);

            // Draw diagonals for correlation reference
            for i in 0..self.width.min(self.height) {
                image.set_pixel(i, i, grid_color);
                if i < self.width && (self.height - 1 - i) < self.height {
                    image.set_pixel(i, self.height - 1 - i, grid_color);
                }
            }
        }

        let center_x = self.width / 2;
        let center_y = self.height / 2;

        let sample_count = left.len().min(right.len());

        for i in 0..sample_count {
            let l = left[i].clamp(-1.0, 1.0);
            let r = right[i].clamp(-1.0, 1.0);

            let x = center_x as f64 + l * (self.width as f64 / 2.0);
            let y = center_y as f64 - r * (self.height as f64 / 2.0);

            let x = x.clamp(0.0, (self.width - 1) as f64) as usize;
            let y = y.clamp(0.0, (self.height - 1) as f64) as usize;

            image.set_pixel(x, y, self.trace_color);
        }

        image
    }
}