Skip to content

Commit

Permalink
Merge pull request #44 from AkiyukiOkayasu/develop
Browse files Browse the repository at this point in the history
Rewrite comments in English
  • Loading branch information
AkiyukiOkayasu authored Aug 9, 2024
2 parents cf65111 + f4ddf5b commit 8157d83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
17 changes: 9 additions & 8 deletions src/imaadpcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ fn decode_sample(
(predicted_sample, step_size_table_index)
}

/// step_sizeの更新
/// Update step_size of IMA-ADPCM table
fn compute_step_size(nibble: u4, mut step_size_table_index: i8) -> i8 {
// adjust index into step_size lookup table using original_sample
step_size_table_index += INDEX_TABLE[nibble.value() as usize];
step_size_table_index = step_size_table_index.clamp(0, 88); //check overflow and underflow
step_size_table_index
}

/// サンプル数を計算する.
/// Calculate the number of samples per channel for IMA-ADPCM files.
pub(crate) fn calc_num_samples_per_channel(
data_chunk_size_in_bytes: u32,
spec: &PcmSpecs,
Expand All @@ -136,16 +136,17 @@ pub(crate) fn calc_num_samples_per_channel(
pub struct ImaAdpcmPlayer<'a> {
/// A reader to access basic information about the PCM file.
pub reader: PcmReader<'a>,
/// 現在読んでいるサンプル位置.
/// Frame index of the current block.
frame_index: u32,
/// デコードされた直近の値.
/// The last decoded sample value.
last_predicted_sample: [I1F15; MAX_NUM_CHANNELS],
/// STEP_SIZE_TABLEのindex.
/// The current index of STEP_SIZE_TABLE.
step_size_table_index: [i8; MAX_NUM_CHANNELS],
/// 現在読み込み中のIMA-ADPCMのブロック.
/// The current block of IMA-ADPCM being read.
reading_block: &'a [u8],
/// Data word読み込み時のnibble配列を保管するqueue.
nibble_queue: [Queue<u4, 9>; MAX_NUM_CHANNELS], //todo Queueサイズは2の冪乗の方がパフォーマンスよい。
/// A queue that stores nibble arrays when reading data words.
/// TODO: Queue size is better to be a power of 2 for performance.
nibble_queue: [Queue<u4, 9>; MAX_NUM_CHANNELS],
}

impl<'a> ImaAdpcmPlayer<'a> {
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,23 @@ impl<'a> PcmReader<'a> {
Ok((input, &[]))
}

/// PCMReader is a struct that reads PCM data from a byte array.
/// It supports Linear PCM and IEEE Float.
/// * 'input' - PCM data byte array
pub fn new(input: &'a [u8]) -> Self {
let file_length = input.len();

let mut reader: PcmReader = Default::default();

//WAVの場合
//WAVE
if let Ok((input, riff)) = wav::parse_riff_header(input) {
assert_eq!((file_length - 8) as u32, riff.size);
if let Ok((_, _)) = reader.parse_wav(input) {
return reader;
}
};

//AIFFの場合
//AIFF
if let Ok((input, _aiff)) = aiff::parse_aiff_header(input) {
// assert_eq!((file_length - 8) as u32, aiff.size);
if let Ok((_, _)) = reader.parse_aiff(input) {
Expand All @@ -203,6 +205,7 @@ impl<'a> PcmReader<'a> {
};

//WAVでもAIFFでもなかった場合

panic!();
}

Expand All @@ -226,11 +229,11 @@ impl<'a> PcmReader<'a> {
}
}

/// DATAチャンクを読んでサンプルを読みだす
/// フォーマットに関わらず+/-1の範囲に正規化された数を返す
/// TODO f32以外Q15やQ23, f64などでも返せるようにしたい
/// もしくはf32かf64を選択できるようにする
/// 固定小数点の取得はread_raw_sample()的な関数とそのジェネリスクで対応するのがいいかもしれない
/// Decode a sample from a byte array.
/// Returns a normalized value in the range +/-1.0 regardless of AudioFormat.
/// TODO return not only f32 but also Q15, Q23, f64, etc.
/// Or make it possible to select f32 or f64.
/// It may be better to use a function like read_raw_sample() to get fixed-point numbers.
fn decode_sample(specs: &PcmSpecs, data: &[u8]) -> anyhow::Result<f32> {
match specs.audio_format {
AudioFormat::Unknown => {
Expand Down

0 comments on commit 8157d83

Please sign in to comment.