1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
//! Sound format conversion.
//!
//! This module implements the functionality for conversion between different sound formats: packed or planar audio, 8-/16-/24-/32-bit, integer or floating point, different number of channels.
//! Eventually this might support resampling as well.
pub use crate::formats::{NASoniton,NAChannelMap};
pub use crate::frame::{NAAudioBuffer,NAAudioInfo,NABufferType};
use crate::formats::NAChannelType;
use crate::frame::alloc_audio_buffer;
use crate::io::byteio::*;
use std::f32::consts::SQRT_2;
/// A list specifying general sound conversion errors.
#[derive(Clone,Copy,Debug,PartialEq)]
pub enum SoundConvertError {
/// Invalid input arguments.
InvalidInput,
/// Allocation failed.
AllocError,
/// Requested feature is not supported.
Unsupported,
}
enum ChannelOp {
Passthrough,
Reorder(Vec<usize>),
Remix(Vec<f32>),
DupMono(Vec<bool>),
}
impl ChannelOp {
fn is_remix(&self) -> bool {
match *self {
ChannelOp::Remix(_) => true,
ChannelOp::DupMono(_) => true,
_ => false,
}
}
}
fn apply_channel_op<T:Copy>(ch_op: &ChannelOp, src: &Vec<T>, dst: &mut Vec<T>) {
match *ch_op {
ChannelOp::Passthrough => {
dst.copy_from_slice(src.as_slice());
},
ChannelOp::Reorder(ref reorder) => {
for (out, idx) in dst.iter_mut().zip(reorder.iter()) {
*out = src[*idx];
}
},
_ => {},
};
}
fn remix_i32(ch_op: &ChannelOp, src: &Vec<i32>, dst: &mut Vec<i32>) {
if let ChannelOp::Remix(ref remix_mat) = ch_op {
let sch = src.len();
for (out, coeffs) in dst.iter_mut().zip(remix_mat.chunks(sch)) {
let mut sum = 0.0;
for (inval, coef) in src.iter().zip(coeffs.iter()) {
sum += (*inval as f32) * *coef;
}
*out = sum as i32;
}
}
if let ChannelOp::DupMono(ref dup_mat) = ch_op {
let src = src[0];
for (out, copy) in dst.iter_mut().zip(dup_mat.iter()) {
*out = if *copy { src } else { 0 };
}
}
}
fn remix_f32(ch_op: &ChannelOp, src: &Vec<f32>, dst: &mut Vec<f32>) {
if let ChannelOp::Remix(ref remix_mat) = ch_op {
let sch = src.len();
for (out, coeffs) in dst.iter_mut().zip(remix_mat.chunks(sch)) {
let mut sum = 0.0;
for (inval, coef) in src.iter().zip(coeffs.iter()) {
sum += *inval * *coef;
}
*out = sum;
}
}
if let ChannelOp::DupMono(ref dup_mat) = ch_op {
let src = src[0];
for (out, copy) in dst.iter_mut().zip(dup_mat.iter()) {
*out = if *copy { src } else { 0.0 };
}
}
}
trait FromFmt<T:Copy> {
fn cvt_from(val: T) -> Self;
}
impl FromFmt<u8> for u8 {
fn cvt_from(val: u8) -> u8 { val }
}
impl FromFmt<u8> for i16 {
fn cvt_from(val: u8) -> i16 { ((val as i16) - 128).wrapping_mul(0x101) }
}
impl FromFmt<u8> for i32 {
fn cvt_from(val: u8) -> i32 { ((val as i32) - 128).wrapping_mul(0x01010101) }
}
impl FromFmt<u8> for f32 {
fn cvt_from(val: u8) -> f32 { ((val as f32) - 128.0) / 128.0 }
}
impl FromFmt<i16> for u8 {
fn cvt_from(val: i16) -> u8 { ((val >> 8) + 128).min(255).max(0) as u8 }
}
impl FromFmt<i16> for i16 {
fn cvt_from(val: i16) -> i16 { val }
}
impl FromFmt<i16> for i32 {
fn cvt_from(val: i16) -> i32 { (val as i32).wrapping_mul(0x10001) }
}
impl FromFmt<i16> for f32 {
fn cvt_from(val: i16) -> f32 { (val as f32) / 32768.0 }
}
impl FromFmt<i32> for u8 {
fn cvt_from(val: i32) -> u8 { ((val >> 24) + 128).min(255).max(0) as u8 }
}
impl FromFmt<i32> for i16 {
fn cvt_from(val: i32) -> i16 { (val >> 16) as i16 }
}
impl FromFmt<i32> for i32 {
fn cvt_from(val: i32) -> i32 { val }
}
impl FromFmt<i32> for f32 {
fn cvt_from(val: i32) -> f32 { (val as f32) / 31.0f32.exp2() }
}
impl FromFmt<f32> for u8 {
fn cvt_from(val: f32) -> u8 { ((val * 128.0) + 128.0).min(255.0).max(0.0) as u8 }
}
impl FromFmt<f32> for i16 {
fn cvt_from(val: f32) -> i16 { (val * 32768.0).min(16383.0).max(-16384.0) as i16 }
}
impl FromFmt<f32> for i32 {
fn cvt_from(val: f32) -> i32 { (val * 31.0f32.exp2()) as i32 }
}
impl FromFmt<f32> for f32 {
fn cvt_from(val: f32) -> f32 { val }
}
trait IntoFmt<T:Copy> {
fn cvt_into(self) -> T;
}
impl<T:Copy, U:Copy> IntoFmt<U> for T where U: FromFmt<T> {
fn cvt_into(self) -> U { U::cvt_from(self) }
}
trait SampleReader {
fn get_samples_i32(&self, pos: usize, dst: &mut Vec<i32>);
fn get_samples_f32(&self, pos: usize, dst: &mut Vec<f32>);
}
struct GenericSampleReader<'a, T:Copy> {
data: &'a [T],
stride: usize,
}
impl<'a, T:Copy+IntoFmt<i32>+IntoFmt<f32>> SampleReader for GenericSampleReader<'a, T> {
fn get_samples_i32(&self, pos: usize, dst: &mut Vec<i32>) {
let mut off = pos;
for el in dst.iter_mut() {
*el = self.data[off].cvt_into();
off += self.stride;
}
}
fn get_samples_f32(&self, pos: usize, dst: &mut Vec<f32>) {
let mut off = pos;
for el in dst.iter_mut() {
*el = self.data[off].cvt_into();
off += self.stride;
}
}
}
struct PackedSampleReader<'a> {
data: &'a [u8],
fmt: NASoniton,
bpp: usize,
}
impl<'a> PackedSampleReader<'a> {
fn new(data: &'a [u8], fmt: NASoniton) -> Self {
if (fmt.bits & 7) != 0 { unimplemented!(); }
let bpp = (fmt.bits >> 3) as usize;
Self { data, fmt, bpp }
}
fn get_samples<T:Copy>(&self, pos: usize, dst: &mut Vec<T>) where u8: IntoFmt<T>, i16: IntoFmt<T>, i32: IntoFmt<T>, f32: IntoFmt<T> {
let mut offset = pos * self.bpp * dst.len();
for el in dst.iter_mut() {
let src = &self.data[offset..];
*el = if !self.fmt.float {
match (self.bpp, self.fmt.be) {
(1, _) => src[0].cvt_into(),
(2, true) => (read_u16be(src).unwrap() as i16).cvt_into(),
(2, false) => (read_u16le(src).unwrap() as i16).cvt_into(),
(3, true) => ((read_u24be(src).unwrap() << 8) as i32).cvt_into(),
(3, false) => ((read_u24be(src).unwrap() << 8) as i32).cvt_into(),
(4, true) => (read_u32be(src).unwrap() as i32).cvt_into(),
(4, false) => (read_u32be(src).unwrap() as i32).cvt_into(),
_ => unreachable!(),
}
} else {
match (self.bpp, self.fmt.be) {
(4, true) => read_f32be(src).unwrap().cvt_into(),
(4, false) => read_f32le(src).unwrap().cvt_into(),
(8, true) => (read_f64be(src).unwrap() as f32).cvt_into(),
(8, false) => (read_f64le(src).unwrap() as f32).cvt_into(),
(_, _) => unreachable!(),
}
};
offset += self.bpp;
}
}
}
impl SampleReader for PackedSampleReader<'_> {
fn get_samples_i32(&self, pos: usize, dst: &mut Vec<i32>) {
self.get_samples(pos, dst);
}
fn get_samples_f32(&self, pos: usize, dst: &mut Vec<f32>) {
self.get_samples(pos, dst);
}
}
trait SampleWriter {
fn store_samples_i32(&mut self, pos: usize, src: &Vec<i32>);
fn store_samples_f32(&mut self, pos: usize, src: &Vec<f32>);
}
struct GenericSampleWriter<'a, T:Copy> {
data: &'a mut [T],
stride: usize,
}
impl<'a, T:Copy+FromFmt<i32>+FromFmt<f32>> SampleWriter for GenericSampleWriter<'a, T> {
fn store_samples_i32(&mut self, pos: usize, src: &Vec<i32>) {
let mut off = pos;
for el in src.iter() {
self.data[off] = (*el).cvt_into();
off += self.stride;
}
}
fn store_samples_f32(&mut self, pos: usize, src: &Vec<f32>) {
let mut off = pos;
for el in src.iter() {
self.data[off] = (*el).cvt_into();
off += self.stride;
}
}
}
struct PackedSampleWriter<'a> {
data: &'a mut [u8],
fmt: NASoniton,
bpp: usize,
}
impl<'a> PackedSampleWriter<'a> {
fn new(data: &'a mut [u8], fmt: NASoniton) -> Self {
if (fmt.bits & 7) != 0 { unimplemented!(); }
let bpp = (fmt.bits >> 3) as usize;
Self { data, fmt, bpp }
}
fn store_samples<T:Copy>(&mut self, pos: usize, src: &Vec<T>) where u8: FromFmt<T>, i16: FromFmt<T>, i32: FromFmt<T>, f32: FromFmt<T> {
let mut offset = pos * self.bpp * src.len();
for el in src.iter() {
let dst = &mut self.data[offset..];
if !self.fmt.float {
match (self.bpp, self.fmt.be) {
(1, _) => {
dst[0] = u8::cvt_from(*el);
},
(2, true) => write_u16be(dst, i16::cvt_from(*el) as u16).unwrap(),
(2, false) => write_u16le(dst, i16::cvt_from(*el) as u16).unwrap(),
(3, true) => write_u24be(dst, (i32::cvt_from(*el) >> 8) as u32).unwrap(),
(3, false) => write_u24le(dst, (i32::cvt_from(*el) >> 8) as u32).unwrap(),
(4, true) => write_u32be(dst, i32::cvt_from(*el) as u32).unwrap(),
(4, false) => write_u32le(dst, i32::cvt_from(*el) as u32).unwrap(),
_ => unreachable!(),
};
} else {
match (self.bpp, self.fmt.be) {
(4, true) => write_f32be(dst, f32::cvt_from(*el)).unwrap(),
(4, false) => write_f32le(dst, f32::cvt_from(*el)).unwrap(),
(8, true) => write_f64be(dst, f32::cvt_from(*el) as f64).unwrap(),
(8, false) => write_f64le(dst, f32::cvt_from(*el) as f64).unwrap(),
(_, _) => unreachable!(),
};
}
offset += self.bpp;
}
}
}
impl SampleWriter for PackedSampleWriter<'_> {
fn store_samples_i32(&mut self, pos: usize, src: &Vec<i32>) {
self.store_samples(pos, src);
}
fn store_samples_f32(&mut self, pos: usize, src: &Vec<f32>) {
self.store_samples(pos, src);
}
}
/// Converts input audio buffer into desired format and returns a newly allocated buffer.
pub fn convert_audio_frame(src: &NABufferType, dst_info: &NAAudioInfo, dst_chmap: &NAChannelMap) ->
Result<NABufferType, SoundConvertError> {
let mut nsamples = src.get_audio_length();
if nsamples == 0 {
return Err(SoundConvertError::InvalidInput);
}
let src_chmap = src.get_chmap().unwrap();
let src_info = src.get_audio_info().unwrap();
if (src_chmap.num_channels() == 0) || (dst_chmap.num_channels() == 0) {
return Err(SoundConvertError::InvalidInput);
}
if let NABufferType::AudioPacked(_) = src {
nsamples = nsamples * 8 / (src_info.get_format().get_bits() as usize) / src_chmap.num_channels();
}
let needs_remix = src_chmap.num_channels() != dst_chmap.num_channels();
let no_channel_needs = !needs_remix && channel_maps_equal(src_chmap, dst_chmap);
let needs_reorder = !needs_remix && !no_channel_needs && channel_maps_reordered(src_chmap, dst_chmap);
let channel_op = if no_channel_needs {
ChannelOp::Passthrough
} else if needs_reorder {
let reorder_mat = calculate_reorder_matrix(src_chmap, dst_chmap);
ChannelOp::Reorder(reorder_mat)
} else if src_chmap.num_channels() > 1 {
let remix_mat = calculate_remix_matrix(src_chmap, dst_chmap);
ChannelOp::Remix(remix_mat)
} else {
let mut dup_mat: Vec<bool> = Vec::with_capacity(dst_chmap.num_channels());
for i in 0..dst_chmap.num_channels() {
let ch = dst_chmap.get_channel(i);
if ch.is_left() || ch.is_right() || ch == NAChannelType::C {
dup_mat.push(true);
} else {
dup_mat.push(false);
}
}
ChannelOp::DupMono(dup_mat)
};
let src_fmt = src_info.get_format();
let dst_fmt = dst_info.get_format();
let no_conversion = src_fmt == dst_fmt;
if no_conversion && no_channel_needs {
return Ok(src.clone());
}
let ret = alloc_audio_buffer(dst_info.clone(), nsamples, dst_chmap.clone());
if ret.is_err() {
return Err(SoundConvertError::AllocError);
}
let mut dst_buf = ret.unwrap();
let sstep = src.get_audio_step().max(1);
let dstep = dst_buf.get_audio_step().max(1);
let sr: Box<dyn SampleReader> = match src {
NABufferType::AudioU8(ref ab) => {
let stride = ab.get_stride();
let data = ab.get_data();
Box::new(GenericSampleReader { data, stride })
},
NABufferType::AudioI16(ref ab) => {
let data = ab.get_data();
let stride = ab.get_stride();
Box::new(GenericSampleReader { data, stride })
},
NABufferType::AudioI32(ref ab) => {
let data = ab.get_data();
let stride = ab.get_stride();
Box::new(GenericSampleReader { data, stride })
},
NABufferType::AudioF32(ref ab) => {
let data = ab.get_data();
let stride = ab.get_stride();
Box::new(GenericSampleReader { data, stride })
},
NABufferType::AudioPacked(ref ab) => {
let data = ab.get_data();
Box::new(PackedSampleReader::new(data, src_fmt))
},
_ => unimplemented!(),
};
let mut sw: Box<dyn SampleWriter> = match dst_buf {
NABufferType::AudioU8(ref mut ab) => {
let stride = ab.get_stride();
let data = ab.get_data_mut().unwrap();
Box::new(GenericSampleWriter { data, stride })
},
NABufferType::AudioI16(ref mut ab) => {
let stride = ab.get_stride();
let data = ab.get_data_mut().unwrap();
Box::new(GenericSampleWriter { data, stride })
},
NABufferType::AudioI32(ref mut ab) => {
let stride = ab.get_stride();
let data = ab.get_data_mut().unwrap();
Box::new(GenericSampleWriter { data, stride })
},
NABufferType::AudioF32(ref mut ab) => {
let stride = ab.get_stride();
let data = ab.get_data_mut().unwrap();
Box::new(GenericSampleWriter { data, stride })
},
NABufferType::AudioPacked(ref mut ab) => {
let data = ab.get_data_mut().unwrap();
Box::new(PackedSampleWriter::new(data, dst_fmt))
},
_ => unimplemented!(),
};
let into_float = dst_fmt.float;
if !into_float {
let mut svec = vec![0; src_chmap.num_channels()];
let mut dvec = vec![0; dst_chmap.num_channels()];
let mut spos = 0;
let mut dpos = 0;
for _ in 0..nsamples {
sr.get_samples_i32(spos, &mut svec);
if !channel_op.is_remix() {
apply_channel_op(&channel_op, &svec, &mut dvec);
} else {
remix_i32(&channel_op, &svec, &mut dvec);
}
sw.store_samples_i32(dpos, &dvec);
spos += sstep;
dpos += dstep;
}
} else {
let mut svec = vec![0.0; src_chmap.num_channels()];
let mut dvec = vec![0.0; dst_chmap.num_channels()];
let mut spos = 0;
let mut dpos = 0;
for _ in 0..nsamples {
sr.get_samples_f32(spos, &mut svec);
if !channel_op.is_remix() {
apply_channel_op(&channel_op, &svec, &mut dvec);
} else {
remix_f32(&channel_op, &svec, &mut dvec);
}
sw.store_samples_f32(dpos, &dvec);
spos += sstep;
dpos += dstep;
}
}
drop(sw);
Ok(dst_buf)
}
/// Checks whether two channel maps are identical.
pub fn channel_maps_equal(a: &NAChannelMap, b: &NAChannelMap) -> bool {
if a.num_channels() != b.num_channels() { return false; }
for i in 0..a.num_channels() {
if a.get_channel(i) != b.get_channel(i) {
return false;
}
}
true
}
/// Checks whether two channel maps have identical channels (but maybe in different order).
pub fn channel_maps_reordered(a: &NAChannelMap, b: &NAChannelMap) -> bool {
if a.num_channels() != b.num_channels() { return false; }
let mut count_a = [0u8; 32];
let mut count_b = [0u8; 32];
for i in 0..a.num_channels() {
count_a[a.get_channel(i) as usize] += 1;
count_b[b.get_channel(i) as usize] += 1;
}
for (c0, c1) in count_a.iter().zip(count_b.iter()) {
if *c0 != *c1 {
return false;
}
}
true
}
/// Calculates permutation matrix for reordering channels from source channel map into destination one.
pub fn calculate_reorder_matrix(src: &NAChannelMap, dst: &NAChannelMap) -> Vec<usize> {
if src.num_channels() != dst.num_channels() { return Vec::new(); }
let num_channels = src.num_channels();
let mut reorder: Vec<usize> = Vec::with_capacity(num_channels);
for i in 0..num_channels {
let dst_ch = dst.get_channel(i);
for j in 0..num_channels {
if src.get_channel(j) == dst_ch {
reorder.push(j);
break;
}
}
}
if reorder.len() != num_channels { reorder.clear(); }
reorder
}
fn is_stereo(chmap: &NAChannelMap) -> bool {
(chmap.num_channels() == 2) &&
(chmap.get_channel(0) == NAChannelType::L) &&
(chmap.get_channel(1) == NAChannelType::R)
}
/// Calculates matrix of remixing coefficients for converting input channel layout into destination one.
pub fn calculate_remix_matrix(src: &NAChannelMap, dst: &NAChannelMap) -> Vec<f32> {
if is_stereo(src) && dst.num_channels() == 1 &&
(dst.get_channel(0) == NAChannelType::L || dst.get_channel(0) == NAChannelType::C) {
return vec![0.5, 0.5];
}
if src.num_channels() >= 5 && is_stereo(dst) {
let src_nch = src.num_channels();
let mut mat = vec![0.0f32; src_nch * 2];
let (l_mat, r_mat) = mat.split_at_mut(src_nch);
for ch in 0..src_nch {
match src.get_channel(ch) {
NAChannelType::L => l_mat[ch] = 1.0,
NAChannelType::R => r_mat[ch] = 1.0,
NAChannelType::C => { l_mat[ch] = SQRT_2 / 2.0; r_mat[ch] = SQRT_2 / 2.0; },
NAChannelType::Ls => l_mat[ch] = SQRT_2 / 2.0,
NAChannelType::Rs => r_mat[ch] = SQRT_2 / 2.0,
_ => {},
};
}
return mat;
}
unimplemented!();
}
#[cfg(test)]
mod test {
use super::*;
use std::str::FromStr;
use crate::formats::*;
#[test]
fn test_matrices() {
let chcfg51 = NAChannelMap::from_str("L,R,C,LFE,Ls,Rs").unwrap();
let chcfg52 = NAChannelMap::from_str("C,L,R,Ls,Rs,LFE").unwrap();
let stereo = NAChannelMap::from_str("L,R").unwrap();
let reorder = calculate_reorder_matrix(&chcfg51, &chcfg52);
assert_eq!(reorder.as_slice(), [ 2, 0, 1, 4, 5, 3]);
let remix = calculate_remix_matrix(&chcfg51, &stereo);
assert_eq!(remix.as_slice(), [ 1.0, 0.0, SQRT_2 / 2.0, 0.0, SQRT_2 / 2.0, 0.0,
0.0, 1.0, SQRT_2 / 2.0, 0.0, 0.0, SQRT_2 / 2.0 ]);
}
#[test]
fn test_conversion() {
const CHANNEL_VALUES: [u8; 6] = [ 140, 90, 130, 128, 150, 70 ];
let chcfg51 = NAChannelMap::from_str("L,R,C,LFE,Ls,Rs").unwrap();
let stereo = NAChannelMap::from_str("L,R").unwrap();
let src_ainfo = NAAudioInfo {
sample_rate: 44100,
channels: chcfg51.num_channels() as u8,
format: SND_U8_FORMAT,
block_len: 512,
};
let mut dst_ainfo = NAAudioInfo {
sample_rate: 44100,
channels: stereo.num_channels() as u8,
format: SND_S16P_FORMAT,
block_len: 512,
};
let mut src_frm = alloc_audio_buffer(src_ainfo, 42, chcfg51.clone()).unwrap();
if let NABufferType::AudioU8(ref mut abuf) = src_frm {
let data = abuf.get_data_mut().unwrap();
let mut idx = 0;
for _ in 0..42 {
for ch in 0..chcfg51.num_channels() {
data[idx] = CHANNEL_VALUES[ch];
idx += 1;
}
}
} else {
panic!("wrong buffer type");
}
let out_frm = convert_audio_frame(&src_frm, &dst_ainfo, &stereo).unwrap();
if let NABufferType::AudioI16(ref abuf) = out_frm {
let off0 = abuf.get_offset(0);
let off1 = abuf.get_offset(1);
let data = abuf.get_data();
let l = data[off0];
let r = data[off1];
assert_eq!(l, 7445);
assert_eq!(r, -19943);
} else {
panic!("wrong buffer type");
}
dst_ainfo.format = SND_F32P_FORMAT;
let out_frm = convert_audio_frame(&src_frm, &dst_ainfo, &stereo).unwrap();
if let NABufferType::AudioF32(ref abuf) = out_frm {
let off0 = abuf.get_offset(0);
let off1 = abuf.get_offset(1);
let data = abuf.get_data();
let l = data[off0];
let r = data[off1];
assert_eq!(l, 0.22633252);
assert_eq!(r, -0.6062342);
} else {
panic!("wrong buffer type");
}
}
}
|