Skip to content

Commit 23402d6

Browse files
author
Kevin Brothaler
committed
Start adding a writer.
1 parent 03bf13d commit 23402d6

File tree

3 files changed

+255
-45
lines changed

3 files changed

+255
-45
lines changed

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@
137137
extern crate byteorder;
138138

139139
mod reader;
140+
mod writer;
140141

141-
pub use self::reader::{FormatErrorKind, ReadError, ReadResult, WaveReader};
142+
pub use self::reader::{ReadError, ReadErrorKind, ReadResult, WaveReader};
143+
pub use self::writer::{WaveWriter, WriteError, WriteErrorKind, WriteResult};
142144

143145
const FORMAT_UNCOMPRESSED_PCM: u16 = 1;
144146
const FORMAT_EXTENDED: u16 = 65534;

src/reader.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use super::{FORMAT_UNCOMPRESSED_PCM, FORMAT_EXTENDED};
2727
#[derive(Debug)]
2828
pub enum ReadError {
2929
/// The file format is incorrect or unsupported.
30-
Format(FormatErrorKind),
30+
Format(ReadErrorKind),
3131
/// An IO error occurred.
3232
Io(io::Error),
3333
}
@@ -46,7 +46,7 @@ impl fmt::Display for ReadError {
4646

4747
/// Represents a file format error, when the wave file is incorrect or unsupported.
4848
#[derive(Debug)]
49-
pub enum FormatErrorKind {
49+
pub enum ReadErrorKind {
5050
/// The file does not start with a "RIFF" tag and chunk size.
5151
NotARiffFile,
5252
/// The file doesn't continue with "WAVE" after the RIFF chunk header.
@@ -66,24 +66,24 @@ pub enum FormatErrorKind {
6666
InvalidBitsPerSample(u16, u16),
6767
}
6868

69-
impl FormatErrorKind {
69+
impl ReadErrorKind {
7070
fn to_string(&self) -> &str {
7171
match *self {
72-
FormatErrorKind::NotARiffFile => "not a RIFF file",
73-
FormatErrorKind::NotAWaveFile => "not a WAVE file",
74-
FormatErrorKind::NotAnUncompressedPcmWaveFile(_) => "Not an uncompressed wave file",
75-
FormatErrorKind::FmtChunkTooShort => "fmt_ chunk is too short",
76-
FormatErrorKind::NumChannelsIsZero => "Number of channels is zero",
77-
FormatErrorKind::SampleRateIsZero => "Sample rate is zero",
78-
FormatErrorKind::UnsupportedBitsPerSample(_) => "Unsupported bits per sample",
79-
FormatErrorKind::InvalidBitsPerSample(_, _) => {
72+
ReadErrorKind::NotARiffFile => "not a RIFF file",
73+
ReadErrorKind::NotAWaveFile => "not a WAVE file",
74+
ReadErrorKind::NotAnUncompressedPcmWaveFile(_) => "Not an uncompressed wave file",
75+
ReadErrorKind::FmtChunkTooShort => "fmt_ chunk is too short",
76+
ReadErrorKind::NumChannelsIsZero => "Number of channels is zero",
77+
ReadErrorKind::SampleRateIsZero => "Sample rate is zero",
78+
ReadErrorKind::UnsupportedBitsPerSample(_) => "Unsupported bits per sample",
79+
ReadErrorKind::InvalidBitsPerSample(_, _) => {
8080
"A bits per sample of less than the container size is not currently supported"
8181
}
8282
}
8383
}
8484
}
8585

86-
impl fmt::Display for FormatErrorKind {
86+
impl fmt::Display for ReadErrorKind {
8787
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8888
write!(f, "{}", self.to_string())
8989
}
@@ -117,20 +117,20 @@ fn validate_pcm_format(format: u16) -> ReadResult<Format> {
117117
match format {
118118
FORMAT_UNCOMPRESSED_PCM => Ok(Format::UncompressedPcm),
119119
FORMAT_EXTENDED => Ok(Format::Extended),
120-
_ => Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(format))),
120+
_ => Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(format))),
121121
}
122122
}
123123

124124
fn validate_pcm_subformat(sub_format: u16) -> ReadResult<()> {
125125
match sub_format {
126126
FORMAT_UNCOMPRESSED_PCM => Ok(()),
127-
_ => Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(sub_format))),
127+
_ => Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(sub_format))),
128128
}
129129
}
130130

131131
fn validate_fmt_header_is_large_enough(size: u32, min_size: u32) -> ReadResult<()> {
132132
if size < min_size {
133-
Err(ReadError::Format(FormatErrorKind::FmtChunkTooShort))
133+
Err(ReadError::Format(ReadErrorKind::FmtChunkTooShort))
134134
} else {
135135
Ok(())
136136
}
@@ -161,13 +161,13 @@ trait ReadWaveExt: Read + Seek {
161161
}
162162

163163
if num_channels == 0 {
164-
return Err(ReadError::Format(FormatErrorKind::NumChannelsIsZero));
164+
return Err(ReadError::Format(ReadErrorKind::NumChannelsIsZero));
165165
} else if sample_rate == 0 {
166-
return Err(ReadError::Format(FormatErrorKind::SampleRateIsZero));
166+
return Err(ReadError::Format(ReadErrorKind::SampleRateIsZero));
167167
} else if bits_per_sample != 8 && bits_per_sample != 16
168168
&& bits_per_sample != 24 && bits_per_sample != 32 {
169169
return Err(ReadError::Format(
170-
FormatErrorKind::UnsupportedBitsPerSample(bits_per_sample)));
170+
ReadErrorKind::UnsupportedBitsPerSample(bits_per_sample)));
171171
}
172172

173173
Ok(PcmFormat {
@@ -189,7 +189,7 @@ trait ReadWaveExt: Read + Seek {
189189
if sample_info != bits_per_sample {
190190
// We don't currently support wave files where the bits per sample
191191
// doesn't entirely fill the allocated bits per sample.
192-
return Err(ReadError::Format(FormatErrorKind::InvalidBitsPerSample(bits_per_sample,
192+
return Err(ReadError::Format(ReadErrorKind::InvalidBitsPerSample(bits_per_sample,
193193
sample_info)));
194194
}
195195

@@ -205,7 +205,7 @@ trait ReadWaveExt: Read + Seek {
205205
}
206206

207207
fn validate_is_riff_file(&mut self) -> ReadResult<()> {
208-
try!(self.validate_tag(b"RIFF", FormatErrorKind::NotARiffFile));
208+
try!(self.validate_tag(b"RIFF", ReadErrorKind::NotARiffFile));
209209
// The next four bytes represent the chunk size. We're not going to
210210
// validate it, so that we can still try to read files that might have
211211
// an incorrect chunk size, so let's skip over it.
@@ -214,13 +214,13 @@ trait ReadWaveExt: Read + Seek {
214214
}
215215

216216
fn validate_is_wave_file(&mut self) -> ReadResult<()> {
217-
try!(self.validate_tag(b"WAVE", FormatErrorKind::NotAWaveFile));
217+
try!(self.validate_tag(b"WAVE", ReadErrorKind::NotAWaveFile));
218218
Ok(())
219219
}
220220

221221
fn validate_tag(&mut self,
222222
expected_tag: &[u8; 4],
223-
err_kind: FormatErrorKind)
223+
err_kind: ReadErrorKind)
224224
-> ReadResult<()> {
225225
let tag = try!(self.read_tag());
226226
if &tag != expected_tag {
@@ -267,6 +267,8 @@ pub struct WaveReader<T>
267267
reader: T,
268268
}
269269

270+
// TODO what should we do if an incorrect read_* method is called? Return the error in the result?
271+
270272
impl<T> WaveReader<T>
271273
where T: Read + Seek
272274
{
@@ -321,7 +323,7 @@ mod tests {
321323

322324
use super::super::{FORMAT_UNCOMPRESSED_PCM, FORMAT_EXTENDED};
323325
use super::super::{Format, PcmFormat};
324-
use super::{FormatErrorKind, ReadError, ReadWaveExt, WaveReader};
326+
use super::{ReadError, ReadErrorKind, ReadWaveExt, WaveReader};
325327
use super::{validate_fmt_header_is_large_enough, validate_pcm_format, validate_pcm_subformat};
326328

327329
// This is a helper macro that helps us validate results in our tests.
@@ -349,14 +351,14 @@ mod tests {
349351
#[test]
350352
fn test_validate_is_riff_file_err_incomplete() {
351353
let mut data = Cursor::new(b"RIF ");
352-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotARiffFile)),
354+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotARiffFile)),
353355
data.validate_is_riff_file());
354356
}
355357

356358
#[test]
357359
fn test_validate_is_riff_file_err_something_else() {
358360
let mut data = Cursor::new(b"JPEG ");
359-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotARiffFile)),
361+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotARiffFile)),
360362
data.validate_is_riff_file());
361363
}
362364

@@ -371,14 +373,14 @@ mod tests {
371373
#[test]
372374
fn test_validate_is_wave_file_err_incomplete() {
373375
let mut data = Cursor::new(b"WAV ");
374-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAWaveFile)),
376+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAWaveFile)),
375377
data.validate_is_wave_file());
376378
}
377379

378380
#[test]
379381
fn test_validate_is_wave_file_err_something_else() {
380382
let mut data = Cursor::new(b"JPEG");
381-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAWaveFile)),
383+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAWaveFile)),
382384
data.validate_is_wave_file());
383385
}
384386

@@ -436,7 +438,7 @@ mod tests {
436438

437439
#[test]
438440
fn test_validate_pcm_format_err_not_uncompressed() {
439-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(_))),
441+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(_))),
440442
validate_pcm_format(12345));
441443
}
442444

@@ -449,13 +451,13 @@ mod tests {
449451

450452
#[test]
451453
fn test_validate_pcm_subformat_err_extended_format_value_not_valid_for_subformat() {
452-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(_))),
454+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(_))),
453455
validate_pcm_subformat(FORMAT_EXTENDED));
454456
}
455457

456458
#[test]
457459
fn test_validate_pcm_subformat_err_not_uncompressed() {
458-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(_))),
460+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(_))),
459461
validate_pcm_subformat(12345));
460462
}
461463

@@ -473,7 +475,7 @@ mod tests {
473475

474476
#[test]
475477
fn test_validate_fmt_header_is_large_enough_too_small() {
476-
assert_matches!(Err(ReadError::Format(FormatErrorKind::FmtChunkTooShort)),
478+
assert_matches!(Err(ReadError::Format(ReadErrorKind::FmtChunkTooShort)),
477479
validate_fmt_header_is_large_enough(14, 16));
478480
}
479481

@@ -490,7 +492,7 @@ mod tests {
490492
fn test_validate_pcm_header_fmt_chunk_too_small() {
491493
let mut data = Cursor::new(b"RIFF WAVE\
492494
fmt \x0C\x00\x00\x00");
493-
assert_matches!(Err(ReadError::Format(FormatErrorKind::FmtChunkTooShort)),
495+
assert_matches!(Err(ReadError::Format(ReadErrorKind::FmtChunkTooShort)),
494496
data.read_wave_header());
495497
}
496498

@@ -499,7 +501,7 @@ mod tests {
499501
let mut data = Cursor::new(b"RIFF WAVE\
500502
fmt \x0E\x00\x00\x00\
501503
\x01\x00");
502-
assert_matches!(Err(ReadError::Format(FormatErrorKind::FmtChunkTooShort)),
504+
assert_matches!(Err(ReadError::Format(ReadErrorKind::FmtChunkTooShort)),
503505
data.read_wave_header());
504506
}
505507

@@ -508,7 +510,7 @@ mod tests {
508510
let mut data = Cursor::new(b"RIFF WAVE\
509511
fmt \x0E\x00\x00\x00\
510512
\x02\x00");
511-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(_))),
513+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(_))),
512514
data.read_wave_header());
513515
}
514516

@@ -522,7 +524,7 @@ mod tests {
522524
\x00\x00\x00\x00\
523525
\x00\x00\
524526
\x00\x00" as &[u8]);
525-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NumChannelsIsZero)),
527+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NumChannelsIsZero)),
526528
data.read_wave_header());
527529
}
528530

@@ -536,7 +538,7 @@ mod tests {
536538
\x00\x00\x00\x00\
537539
\x00\x00\
538540
\x00\x00" as &[u8]);
539-
assert_matches!(Err(ReadError::Format(FormatErrorKind::SampleRateIsZero)),
541+
assert_matches!(Err(ReadError::Format(ReadErrorKind::SampleRateIsZero)),
540542
data.read_wave_header());
541543
}
542544

@@ -571,12 +573,12 @@ mod tests {
571573

572574
vec[34] = 48;
573575
let mut cursor = Cursor::new(vec.clone());
574-
assert_matches!(Err(ReadError::Format(FormatErrorKind::UnsupportedBitsPerSample(_))),
576+
assert_matches!(Err(ReadError::Format(ReadErrorKind::UnsupportedBitsPerSample(_))),
575577
cursor.read_wave_header());
576578

577579
vec[34] = 0;
578580
let mut cursor = Cursor::new(vec.clone());
579-
assert_matches!(Err(ReadError::Format(FormatErrorKind::UnsupportedBitsPerSample(_))),
581+
assert_matches!(Err(ReadError::Format(ReadErrorKind::UnsupportedBitsPerSample(_))),
580582
cursor.read_wave_header());
581583
}
582584

@@ -639,7 +641,7 @@ mod tests {
639641
\x02\x00\x00\x00");
640642
let mut cursor = Cursor::new(vec.clone());
641643

642-
assert_matches!(Err(ReadError::Format(FormatErrorKind::FmtChunkTooShort)),
644+
assert_matches!(Err(ReadError::Format(ReadErrorKind::FmtChunkTooShort)),
643645
cursor.read_wave_header());
644646
}
645647

@@ -660,7 +662,7 @@ mod tests {
660662
\x09\x00\x00\x00\x00\x00\x10\x00\x80\x00\x00\xAA\x00\x38\x9B\x71");
661663
let mut cursor = Cursor::new(vec.clone());
662664

663-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(_))),
665+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(_))),
664666
cursor.read_wave_header());
665667
}
666668

@@ -681,7 +683,7 @@ mod tests {
681683
\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00");
682684
let mut cursor = Cursor::new(vec.clone());
683685

684-
assert_matches!(Err(ReadError::Format(FormatErrorKind::InvalidBitsPerSample(_, _))),
686+
assert_matches!(Err(ReadError::Format(ReadErrorKind::InvalidBitsPerSample(_, _))),
685687
cursor.read_wave_header());
686688
}
687689

@@ -734,7 +736,7 @@ mod tests {
734736
fn test_validate_extended_format_too_short() {
735737
// Extended size is less than 22 -- should fail.
736738
let mut data = Cursor::new(b"\x0F\x00\x00\x00");
737-
assert_matches!(Err(ReadError::Format(FormatErrorKind::FmtChunkTooShort)),
739+
assert_matches!(Err(ReadError::Format(ReadErrorKind::FmtChunkTooShort)),
738740
data.validate_extended_format(16));
739741
}
740742

@@ -745,7 +747,7 @@ mod tests {
745747
\x00\x00\x00\x00\
746748
\xFF\xFF\x00\x00\x00\x00\x00\x00\
747749
\x00\x00\x00\x00\x00\x00\x00\x00");
748-
assert_matches!(Err(ReadError::Format(FormatErrorKind::NotAnUncompressedPcmWaveFile(_))),
750+
assert_matches!(Err(ReadError::Format(ReadErrorKind::NotAnUncompressedPcmWaveFile(_))),
749751
data.validate_extended_format(16));
750752
}
751753

@@ -756,7 +758,7 @@ mod tests {
756758
\x00\x00\x00\x00\
757759
\x01\x00\x00\x00\x00\x00\x00\x00\
758760
\x00\x00\x00\x00\x00\x00\x00\x00");
759-
assert_matches!(Err(ReadError::Format(FormatErrorKind::InvalidBitsPerSample(_, _))),
761+
assert_matches!(Err(ReadError::Format(ReadErrorKind::InvalidBitsPerSample(_, _))),
760762
data.validate_extended_format(16));
761763
}
762764

0 commit comments

Comments
 (0)