Skip to content

Commit

Permalink
Merge pull request #11 from AkiyukiOkayasu/develop
Browse files Browse the repository at this point in the history
Add rewind() to IMA-ADPCM player
  • Loading branch information
AkiyukiOkayasu authored Jan 12, 2023
2 parents bb7c4a5 + 201aeb3 commit 88039f1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
13 changes: 10 additions & 3 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ fn read_sample(c: &mut Criterion) {
}

fn parse_decode_ima_adpcm(c: &mut Criterion) {
let data = include_bytes!("../tests/resources/Sine440Hz_1ch_48000Hz_4bit_IMAADPCM.wav");
let mut player = ImaAdpcmPlayer::new(data);
let data = include_bytes!("../tests/resources/Sine440Hz_2ch_48000Hz_4bit_IMAADPCM.wav");
let mut buffer: [i16; 2] = [0i16, 0i16];

c.bench_function("Decode IMA-ADPCM", |b| {
let mut player = ImaAdpcmPlayer::new(data);
let buf = buffer.as_mut_slice();
b.iter(|| player.get_next_frame(buf))
b.iter(|| {
// player = ImaAdpcmPlayer::new(data);
player.rewind();
for _ in 0..192000 {
//4sec
player.get_next_frame(buf).unwrap();
}
})
});
}

Expand Down
13 changes: 13 additions & 0 deletions src/imaadpcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ impl<'a> ImaAdpcmPlayer<'a> {
self.reading_block = block;
}
}

/// 再生位置を先頭に戻します.
pub fn rewind(&mut self) {
self.frame_index = 0;
if !self.reading_block.is_empty() {
self.reading_block = &self.reading_block[0..0]; //reading_blockを空のスライスにする
}
for q in &mut self.nibble_queue {
for i in 0..q.len() {
q.dequeue().unwrap();
}
}
}
}

/// IMA-ADPCMのBlockのData word(32bit長)を8つのnibble(4bit長)にパースする.
Expand Down
1 change: 0 additions & 1 deletion src/wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub(super) fn parse_fmt(input: &[u8]) -> IResult<&[u8], WavFmtSpecs> {

if audio_format == AudioFormat::ImaAdpcmLe {
//IMA-ADPCMの拡張属性の取得
println!("block size: {}", block_size);
let num_block_align = block_size;
assert!(block_size % 4 == 0);
assert!(input.len() >= 4);
Expand Down
24 changes: 24 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3340,6 +3340,18 @@ fn ima_adpcm_4bit_play_to_end() {
//Error
let e = player.get_next_frame(buf);
assert!(e.is_err());

//再生位置を先頭に戻す
player.rewind();

// Play to the end
for _ in 0..spec.num_samples {
player.get_next_frame(buf).unwrap();
}

//Error
let e = player.get_next_frame(buf);
assert!(e.is_err());
}

#[test]
Expand Down Expand Up @@ -3389,4 +3401,16 @@ fn ima_adpcm_4bit_2ch_play_to_end() {
// Error
let e = player.get_next_frame(buf);
assert!(e.is_err());

// 再生位置を先頭に戻す.
player.rewind();

// Play to the end
for _ in 0..spec.num_samples {
player.get_next_frame(buf).unwrap();
}

// Error
let e = player.get_next_frame(buf);
assert!(e.is_err());
}

0 comments on commit 88039f1

Please sign in to comment.