From d2e9e232c2451c3b68ece7b5b8bc215a6d4a318a Mon Sep 17 00:00:00 2001 From: AkiyukiOkayasu Date: Wed, 21 Aug 2024 22:03:18 +0900 Subject: [PATCH] Add PcmReader::reload() for reload PCM byte array --- src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f772621..07c2ff5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,