1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
#![warn(clippy::pedantic)]
mod id3_display;
use crate::id3_display::id3_tag_to_string;
use id3::Tag;
use sampled_data_duration::ConstantRateDuration;
use std::convert::TryFrom;
use std::fmt;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::Read;
use std::io::SeekFrom;
use std::path::Path;
use std::u64;
pub struct DsfFile {
file: File,
dsd_chunk: DsdChunk,
fmt_chunk: FmtChunk,
data_chunk: DataChunk,
id3_tag: Option<Tag>,
}
impl DsfFile {
pub fn open(path: &Path) -> Result<DsfFile, Error> {
let mut file = File::open(path)?;
let mut dsd_chunk_buffer: [u8; 28] = [0; 28];
file.read_exact(&mut dsd_chunk_buffer)?;
let dsd_chunk = DsdChunk::try_from(dsd_chunk_buffer)?;
let mut fmt_chunk_buffer: [u8; 52] = [0; 52];
file.read_exact(&mut fmt_chunk_buffer)?;
let fmt_chunk = FmtChunk::try_from(fmt_chunk_buffer)?;
let mut data_chunk_buffer: [u8; 12] = [0; 12];
file.read_exact(&mut data_chunk_buffer)?;
let data_chunk = DataChunk::try_from(data_chunk_buffer)?;
let id3_tag: Option<Tag> = if dsd_chunk.metadata_offset == 0 {
None
} else {
file.seek(SeekFrom::Start(dsd_chunk.metadata_offset))?;
Some(Tag::read_from(&file)?)
};
Ok(DsfFile {
file,
dsd_chunk,
fmt_chunk,
data_chunk,
id3_tag,
})
}
#[must_use]
pub fn file(&self) -> &File {
&self.file
}
#[must_use]
pub fn dsd_chunk(&self) -> &DsdChunk {
&self.dsd_chunk
}
#[must_use]
pub fn fmt_chunk(&self) -> &FmtChunk {
&self.fmt_chunk
}
#[must_use]
pub fn data_chunk(&self) -> &DataChunk {
&self.data_chunk
}
#[must_use]
pub fn id3_tag(&self) -> &Option<Tag> {
&self.id3_tag
}
pub fn frames(&mut self) -> Result<Frames, Error> {
Frames::new(self)
}
pub fn interleaved_u32_samples_iter(&mut self) -> Result<InterleavedU32SamplesIter, Error> {
InterleavedU32SamplesIter::new(self)
}
}
impl fmt::Display for DsfFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let id3_tag_as_string = match &self.id3_tag {
Some(tag) => id3_tag_to_string(tag),
None => String::from("None"),
};
write!(
f,
"DSD chunk:\n{}\n\nFmt chunk:\n{}\n\nData chunk:\n{}\n\nID3Tag:\n{}",
self.dsd_chunk, self.fmt_chunk, self.data_chunk, &id3_tag_as_string,
)
}
}
fn u64_from_byte_buffer(buffer: &[u8], index: usize) -> u64 {
let mut byte_array: [u8; 8] = [0; 8];
byte_array.copy_from_slice(&buffer[index..index + 8]);
u64::from_le_bytes(byte_array)
}
fn u32_from_byte_buffer(buffer: &[u8], index: usize) -> u32 {
let mut byte_array: [u8; 4] = [0; 4];
byte_array.copy_from_slice(&buffer[index..index + 4]);
u32::from_le_bytes(byte_array)
}
const DSD_CHUNK_HEADER: [u8; 4] = [b'D', b'S', b'D', b' '];
pub struct DsdChunk {
file_size: u64,
metadata_offset: u64,
}
impl DsdChunk {
fn new(file_size: u64, metadata_offset: u64) -> DsdChunk {
DsdChunk {
file_size,
metadata_offset,
}
}
#[must_use]
pub fn file_size(&self) -> u64 {
self.file_size
}
}
impl fmt::Display for DsdChunk {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"File size = {} bytes\nMetadata offset = {} bytes",
self.file_size, self.metadata_offset
)
}
}
impl TryFrom<[u8; 28]> for DsdChunk {
type Error = Error;
fn try_from(buffer: [u8; 28]) -> Result<Self, Self::Error> {
if buffer[0..4] != DSD_CHUNK_HEADER {
return Err(Error::DsdChunkHeader);
}
let chunk_size = u64_from_byte_buffer(&buffer, 4);
if chunk_size != 28 {
return Err(Error::DsdChunkSize);
}
let file_size = u64_from_byte_buffer(&buffer, 12);
let metadata_offset = u64_from_byte_buffer(&buffer, 20);
Ok(DsdChunk::new(file_size, metadata_offset))
}
}
const FMT_CHUNK_HEADER: [u8; 4] = [b'f', b'm', b't', b' '];
pub struct FmtChunk {
channel_type: ChannelType,
channel_num: u32,
sampling_frequency: u32,
bits_per_sample: u32,
sample_count: u64,
block_size_per_channel: u32,
}
impl FmtChunk {
fn new(
channel_type: ChannelType,
channel_num: u32,
sampling_frequency: u32,
bits_per_sample: u32,
sample_count: u64,
block_size_per_channel: u32,
) -> FmtChunk {
FmtChunk {
channel_type,
channel_num,
sampling_frequency,
bits_per_sample,
sample_count,
block_size_per_channel,
}
}
#[must_use]
pub fn channel_type(&self) -> &ChannelType {
&self.channel_type
}
#[must_use]
pub fn channel_num(&self) -> u32 {
self.channel_num
}
#[must_use]
pub fn sampling_frequency(&self) -> u32 {
self.sampling_frequency
}
#[must_use]
pub fn bits_per_sample(&self) -> u32 {
self.bits_per_sample
}
#[must_use]
pub fn sample_count(&self) -> u64 {
self.sample_count
}
#[must_use]
pub fn block_size_per_channel(&self) -> u32 {
self.block_size_per_channel
}
fn duration(&self) -> ConstantRateDuration {
ConstantRateDuration::new(self.sample_count, u64::from(self.sampling_frequency))
}
}
impl fmt::Display for FmtChunk {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Channel type = {}
Channel number = {}
Sampling frequency = {} Hz
Bits per sample = {}
Sample count per channel = {}
Block size per channel = {} bytes
Calculated duration = {} h:min:s;samples",
self.channel_type,
self.channel_num,
self.sampling_frequency,
self.bits_per_sample,
self.sample_count,
self.block_size_per_channel,
self.duration()
)
}
}
impl TryFrom<[u8; 52]> for FmtChunk {
type Error = Error;
fn try_from(buffer: [u8; 52]) -> Result<Self, Self::Error> {
if buffer[0..4] != FMT_CHUNK_HEADER {
return Err(Error::FmtChunkHeader);
}
let chunk_size = u64_from_byte_buffer(&buffer, 4);
if chunk_size != 52 {
return Err(Error::FmtChunkSize);
}
let format_version = u32_from_byte_buffer(&buffer, 12);
if format_version != 1 {
return Err(Error::FormatVersion);
}
let format_id = u32_from_byte_buffer(&buffer, 16);
if format_id != 0 {
return Err(Error::FormatId);
}
let channel_type = ChannelType::try_from(u32_from_byte_buffer(&buffer, 20))?;
let channel_num = u32_from_byte_buffer(&buffer, 24);
match channel_num {
1 | 2 | 3 | 4 | 5 | 6 => (),
_ => return Err(Error::ChannelNum),
}
let sampling_frequency = u32_from_byte_buffer(&buffer, 28);
let bits_per_sample = u32_from_byte_buffer(&buffer, 32);
let sample_count = u64_from_byte_buffer(&buffer, 36);
let block_size_per_channel = u32_from_byte_buffer(&buffer, 44);
if block_size_per_channel != BLOCK_SIZE_AS_U32 {
return Err(Error::BlockSizePerChannelNonStandard);
}
let reserved = u32_from_byte_buffer(&buffer, 48);
if reserved != 0 {
return Err(Error::ReservedNotZero);
}
Ok(FmtChunk::new(
channel_type,
channel_num,
sampling_frequency,
bits_per_sample,
sample_count,
block_size_per_channel,
))
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum ChannelType {
Mono,
Stereo,
ThreeChannels,
Quad,
FourChannels,
FiveChannels,
FivePointOneChannels,
}
impl fmt::Display for ChannelType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let channel_type_as_str = match self {
ChannelType::Mono => "Mono",
ChannelType::Stereo => "Stereo: FL, FR.",
ChannelType::ThreeChannels => "3 channels: FL, FR, C.",
ChannelType::Quad => "Quad: FL, FR, BL, BR.",
ChannelType::FourChannels => "4 channels: FL, FR, C, LFE.",
ChannelType::FiveChannels => "5 channels: FL, FR, C, BL, BR.",
ChannelType::FivePointOneChannels => "5.1 channels: FL, FR, C, LFE, BL, BR.",
};
write!(f, "{}", channel_type_as_str)
}
}
impl TryFrom<u32> for ChannelType {
type Error = Error;
fn try_from(channel_type_as_u32: u32) -> Result<Self, Self::Error> {
match channel_type_as_u32 {
1 => Ok(ChannelType::Mono),
2 => Ok(ChannelType::Stereo),
3 => Ok(ChannelType::ThreeChannels),
4 => Ok(ChannelType::Quad),
5 => Ok(ChannelType::FourChannels),
6 => Ok(ChannelType::FiveChannels),
7 => Ok(ChannelType::FivePointOneChannels),
_ => Err(Error::ChannelType),
}
}
}
const DATA_CHUNK_HEADER: [u8; 4] = [b'd', b'a', b't', b'a'];
pub struct DataChunk {
chunk_size: u64,
}
impl DataChunk {
fn new(chunk_size: u64) -> DataChunk {
DataChunk { chunk_size }
}
#[must_use]
pub fn chunk_size(&self) -> u64 {
self.chunk_size
}
}
impl fmt::Display for DataChunk {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Data chunk size = {} bytes", self.chunk_size)
}
}
impl TryFrom<[u8; 12]> for DataChunk {
type Error = Error;
fn try_from(buffer: [u8; 12]) -> Result<Self, Self::Error> {
if buffer[0..4] != DATA_CHUNK_HEADER {
return Err(Error::DataChunkHeader);
}
let chunk_size = u64_from_byte_buffer(&buffer, 4);
Ok(DataChunk::new(chunk_size))
}
}
const BLOCK_SIZE: usize = 4096;
const BLOCK_SIZE_AS_U32: u32 = 4096;
const SAMPLES_PER_BLOCK: u64 = 8 * BLOCK_SIZE as u64;
const SAMPLE_DATA_OFFSET: u64 = 92;
pub struct Frames<'a> {
channels: usize,
dsf_file: &'a mut DsfFile,
frame: Vec<[u8; BLOCK_SIZE]>,
frame_index: u64,
frame_count: u64,
reverse_bits: bool,
}
impl<'a> Frames<'a> {
fn new(dsf_file: &mut DsfFile) -> Result<Frames, Error> {
let channels = dsf_file.fmt_chunk.channel_num as usize;
let frame_count = dsf_file.fmt_chunk.sample_count / SAMPLES_PER_BLOCK;
let mut frame: Vec<[u8; BLOCK_SIZE]> = Vec::with_capacity(channels);
for _ in 0..channels {
frame.push([0; BLOCK_SIZE]);
}
let reverse_bits = match dsf_file.fmt_chunk.bits_per_sample {
1 => true,
8 => false,
_ => panic!("Illegal value for bits_per_sample"),
};
let mut frames = Frames {
channels,
dsf_file,
frame,
frame_index: 0,
frame_count,
reverse_bits,
};
frames.load_frame(0)?;
Ok(frames)
}
pub fn offset(&self, frame_index: u64) -> Result<u64, Error> {
if frame_index >= self.frame_count {
return Err(Error::FrameIndexOutOfRange);
}
Ok(self.offset_unchecked(frame_index))
}
fn offset_unchecked(&self, frame_index: u64) -> u64 {
debug_assert!(frame_index <= self.frame_count);
SAMPLE_DATA_OFFSET + frame_index * (BLOCK_SIZE * self.channels) as u64
}
pub fn frame_and_block_index(&self, sample_index: u64) -> Result<(u64, usize), Error> {
if sample_index >= self.dsf_file.fmt_chunk.sample_count {
return Err(Error::SampleIndexOutOfRange);
}
Ok(self.frame_and_block_index_unchecked(sample_index))
}
fn frame_and_block_index_unchecked(&self, sample_index: u64) -> (u64, usize) {
debug_assert!(sample_index < self.dsf_file.fmt_chunk.sample_count);
let frame_index = sample_index / SAMPLES_PER_BLOCK;
let block_index = ((sample_index % SAMPLES_PER_BLOCK) / 8) as usize;
(frame_index, block_index)
}
pub fn load_frame(&mut self, frame_index: u64) -> Result<(), Error> {
if frame_index >= self.frame_count {
return Err(Error::FrameIndexOutOfRange);
}
self.dsf_file
.file
.seek(SeekFrom::Start(self.offset_unchecked(frame_index)))?;
for i in 0..self.channels {
self.dsf_file.file.read_exact(&mut self.frame[i])?;
}
self.frame_index = frame_index;
Ok(())
}
fn load_frame_unchecked(&mut self, frame_index: u64) -> Result<(), Error> {
debug_assert!(frame_index < self.frame_count);
self.dsf_file
.file
.seek(SeekFrom::Start(self.offset_unchecked(frame_index)))?;
for i in 0..self.channels {
self.dsf_file.file.read_exact(&mut self.frame[i])?;
}
self.frame_index = frame_index;
Ok(())
}
pub fn samples_as_u32(
&mut self,
channel_index: usize,
sample_index: u64,
) -> Result<u32, Error> {
if channel_index >= self.channels {
return Err(Error::ChannelIndexOutOfRange);
}
let (frame_index, block_index) = self.frame_and_block_index(sample_index)?;
if self.frame_index != frame_index {
self.load_frame(frame_index)?;
}
let samples_as_u8_array: [u8; 4] = if self.reverse_bits {
[
self.frame[channel_index][block_index].reverse_bits(),
self.frame[channel_index][block_index + 1].reverse_bits(),
self.frame[channel_index][block_index + 2].reverse_bits(),
self.frame[channel_index][block_index + 3].reverse_bits(),
]
} else {
[
self.frame[channel_index][block_index],
self.frame[channel_index][block_index + 1],
self.frame[channel_index][block_index + 2],
self.frame[channel_index][block_index + 3],
]
};
Ok(u32::from_le_bytes(samples_as_u8_array))
}
fn samples_as_u32_unchecked(
&mut self,
channel_index: usize,
sample_index: u64,
) -> Result<u32, Error> {
debug_assert!(channel_index < self.channels);
let (frame_index, block_index) = self.frame_and_block_index_unchecked(sample_index);
if self.frame_index != frame_index {
self.load_frame_unchecked(frame_index)?;
}
let samples_as_u8_array: [u8; 4] = if self.reverse_bits {
[
self.frame[channel_index][block_index].reverse_bits(),
self.frame[channel_index][block_index + 1].reverse_bits(),
self.frame[channel_index][block_index + 2].reverse_bits(),
self.frame[channel_index][block_index + 3].reverse_bits(),
]
} else {
[
self.frame[channel_index][block_index],
self.frame[channel_index][block_index + 1],
self.frame[channel_index][block_index + 2],
self.frame[channel_index][block_index + 3],
]
};
Ok(u32::from_le_bytes(samples_as_u8_array))
}
}
pub struct InterleavedU32SamplesIter<'a> {
channel_index: usize,
channels: usize,
frames: Frames<'a>,
sample_index: u64,
sample_count: u64,
}
impl<'a> InterleavedU32SamplesIter<'a> {
fn new(dsf_file: &mut DsfFile) -> Result<InterleavedU32SamplesIter, Error> {
let channels = dsf_file.fmt_chunk.channel_num as usize;
let sample_count = dsf_file.fmt_chunk.sample_count;
let frames = Frames::new(dsf_file)?;
Ok(InterleavedU32SamplesIter {
channel_index: 0,
channels,
frames,
sample_index: 0,
sample_count,
})
}
#[must_use]
pub fn sample_index(&self) -> u64 {
self.sample_index
}
#[must_use]
pub fn sample_count(&self) -> u64 {
self.sample_count
}
pub fn set_sample_index(&mut self, sample_index: u64) -> Result<u64, Error> {
if sample_index >= self.sample_count {
return Err(Error::SampleIndexOutOfRange);
}
let normalized_sample_index = 32 * (sample_index / 32);
self.sample_index = normalized_sample_index;
Ok(normalized_sample_index)
}
}
impl<'a> Iterator for InterleavedU32SamplesIter<'a> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
match self
.frames
.samples_as_u32_unchecked(self.channel_index, self.sample_index)
{
Ok(samples_as_u32) => {
self.channel_index += 1;
if self.channel_index >= self.channels {
self.channel_index = 0;
self.sample_index += 32;
}
Some(samples_as_u32)
}
Err(_) => None,
}
}
}
#[derive(Debug)]
pub enum Error {
BlockSizePerChannelNonStandard,
ChannelNum,
ChannelType,
DataChunkHeader,
DsdChunkHeader,
DsdChunkSize,
FmtChunkHeader,
FmtChunkSize,
FormatId,
FormatVersion,
Id3Error(id3::Error),
IoError(io::Error),
ReservedNotZero,
ChannelIndexOutOfRange,
SampleIndexOutOfRange,
FrameIndexOutOfRange,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::BlockSizePerChannelNonStandard => write!(
f,
"A fmt chunk is expected to specify its block size per channel as {}.",
BLOCK_SIZE
),
Error::ChannelNum => {
f.write_str("A fmt chunk’s channel num is expected to be in the range 1–6.")
}
Error::ChannelType => {
f.write_str("A fmt chunk’s channel type is expected to be in the range 1–7.")
}
Error::DataChunkHeader => f.write_str("A data chunk must start with the bytes 'data'."),
Error::DsdChunkHeader => f.write_str("A DSD chunk must start with the bytes 'DSD '."),
Error::DsdChunkSize => f.write_str("A DSD chunk must specify its size as 28 bytes."),
Error::FmtChunkHeader => f.write_str("A fmt chunk must start with the bytes 'fmt '."),
Error::FmtChunkSize => {
f.write_str("A fmt chunk is expected to specify its size as 52 bytes.")
}
Error::FormatId => f.write_str("A fmt chumk must specifiy a format ID of 0."),
Error::FormatVersion => f.write_str("A fmt chunk must specify version 1."),
Error::Id3Error(id3_error) => write!(f, "Id3 error: {}", id3_error),
Error::IoError(io_error) => write!(f, "IO error: {}", io_error),
Error::ReservedNotZero => {
f.write_str("A fmt chunk’s reserved space is expected to be zero filled.")
}
Error::ChannelIndexOutOfRange => f.write_str("Channel index is out of range."),
Error::SampleIndexOutOfRange => f.write_str("Sample index is out of range."),
Error::FrameIndexOutOfRange => f.write_str("Frame index is out of range."),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Id3Error(id3_error) => Some(id3_error),
Error::IoError(io_error) => Some(io_error),
_ => None,
}
}
}
impl From<id3::Error> for Error {
fn from(error: id3::Error) -> Self {
Error::Id3Error(error)
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::IoError(error)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::process::Command;
fn get_sweep_dsf_file() -> Result<DsfFile, Error> {
let sweep_filename = "sweep-176400hz-0-22050hz-20s-D64-2.8mhz.dsf";
let path = Path::new(sweep_filename);
if !path.is_file() {
let sweep_url =
"http://samplerateconverter.com/free-files/samples/dsf/sweep-176400hz-0-22050hz-20s-D64-2.8mhz.zip";
Command::new("wget")
.arg(sweep_url)
.status()
.unwrap_or_else(|_| panic!("Failed to download {}", sweep_url));
let sweep_zip_filename = "sweep-176400hz-0-22050hz-20s-D64-2.8mhz.zip";
Command::new("unzip")
.arg(sweep_zip_filename)
.status()
.unwrap_or_else(|_| panic!("Failed to unzip {}", sweep_zip_filename));
}
DsfFile::open(path)
}
#[test]
fn sweep_file() {
let dsf_file = get_sweep_dsf_file().unwrap();
assert_eq!(dsf_file.dsd_chunk.file_size, 14_114_908);
assert_eq!(dsf_file.dsd_chunk.metadata_offset, 0);
assert_eq!(dsf_file.fmt_chunk.channel_type, ChannelType::Stereo);
assert_eq!(dsf_file.fmt_chunk.channel_num, 2);
assert_eq!(dsf_file.fmt_chunk.sampling_frequency, 2_822_400);
assert_eq!(dsf_file.fmt_chunk.bits_per_sample, 1);
assert_eq!(dsf_file.fmt_chunk.sample_count, 56_459_264);
assert_eq!(dsf_file.fmt_chunk.block_size_per_channel, 4096);
assert_eq!(dsf_file.data_chunk.chunk_size, 14_114_828);
}
}