Skip to content

Commit

Permalink
Add PcmReader::reload() for reload PCM byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
AkiyukiOkayasu committed Aug 21, 2024
1 parent 01299d7 commit d2e9e23
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ impl<'a> PcmReader<'a> {
Err(LinearPcmError::UnsupportedAudioFormat)
}

/// Reload a new PCM byte array.
pub fn reload(&mut self, input: &'a [u8]) -> Result<(), LinearPcmError> {
let file_length = input.len();
self.data = &[];
self.specs = PcmSpecs::default();

// Parse WAVE format
if let Ok((input, riff)) = wav::parse_riff_header(input) {
if (file_length - 8) != riff.size as usize {
return Err(LinearPcmError::HeaderSizeMismatch);
}

if let Ok((_, _)) = self.parse_wav(input) {
return Ok(());
}
}

// Parse AIFF format
if let Ok((input, aiff)) = aiff::parse_aiff_header(input) {
if (file_length - 8) != aiff.size as usize {
return Err(LinearPcmError::HeaderSizeMismatch);
}

if let Ok((_, _)) = self.parse_aiff(input) {
return Ok(());
}
}

Err(LinearPcmError::UnsupportedAudioFormat)
}

fn parse_aiff(&mut self, input: &'a [u8]) -> IResult<&[u8], &[u8]> {
let (input, v) = fold_many1(
aiff::parse_chunk,
Expand Down

0 comments on commit d2e9e23

Please sign in to comment.