Skip to content

Commit

Permalink
simplify wav I/O module.
Browse files Browse the repository at this point in the history
  • Loading branch information
aikiriao committed Aug 11, 2024
1 parent 70675e4 commit 20a1f56
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
18 changes: 9 additions & 9 deletions libs/wav/src/wav.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,22 +388,22 @@ struct WAVFile* WAV_Create(const struct WAVFileFormat* format)
/* 8bitPCM形式を32bit形式に変換 */
static int32_t WAV_Convert8bitPCMto32bitPCM(int32_t in_8bitpcm)
{
/* 無音に相当する128を引いてから32bit整数に切り上げる */
return (in_8bitpcm - 128) << 24;
/* 無音に相当する128を引く */
return (in_8bitpcm - 128);
}

/* 16bitPCM形式を32bit形式に変換 */
static int32_t WAV_Convert16bitPCMto32bitPCM(int32_t in_16bitpcm)
{
/* そのまま16bit左シフト */
return in_16bitpcm << 16;
/* 一旦32bit幅にしてから算術右シフトで符号をつける */
return (in_16bitpcm << 16) >> 16;
}

/* 24bitPCM形式を32bit形式に変換 */
static int32_t WAV_Convert24bitPCMto32bitPCM(int32_t in_24bitpcm)
{
/* そのまま8bit左シフト */
return in_24bitpcm << 8;
/* 一旦32bit幅にしてから算術右シフトで符号をつける */
return (in_24bitpcm << 8) >> 8;
}

/* 32bitPCM形式を32bit形式に変換 */
Expand Down Expand Up @@ -673,7 +673,7 @@ static WAVError WAVWriter_PutWAVPcmData(
buffer = (uint8_t *)writer->buffer.bytes;
for (smpl = 0; smpl < num_process_smpls; smpl++) {
for (ch = 0; ch < wavfile->format.num_channels; ch++) {
(*buffer++) = (uint8_t)(((WAVFile_PCM(wavfile, progress + smpl, ch) >> 24) + 128) & 0xFF);
(*buffer++) = (uint8_t)((WAVFile_PCM(wavfile, progress + smpl, ch) + 128) & 0xFF);
}
}
if (WAVWrite_FWriteLittleEndian(writer->buffer.bytes,
Expand All @@ -695,7 +695,7 @@ static WAVError WAVWriter_PutWAVPcmData(
buffer = (int16_t *)writer->buffer.bytes;
for (smpl = 0; smpl < num_process_smpls; smpl++) {
for (ch = 0; ch < wavfile->format.num_channels; ch++) {
(*buffer++) = (int16_t)((WAVFile_PCM(wavfile, progress + smpl, ch) >> 16) & 0xFFFF);
(*buffer++) = (int16_t)(WAVFile_PCM(wavfile, progress + smpl, ch) & 0xFFFF);
}
}
if (WAVWrite_FWriteLittleEndian(writer->buffer.bytes,
Expand All @@ -720,9 +720,9 @@ static WAVError WAVWriter_PutWAVPcmData(
for (smpl = 0; smpl < num_process_smpls; smpl++) {
for (ch = 0; ch < wavfile->format.num_channels; ch++) {
int32_t pcm = WAVFile_PCM(wavfile, progress + smpl, ch);
(*buffer++) = (uint8_t)((pcm >> 0) & 0xFF);
(*buffer++) = (uint8_t)((pcm >> 8) & 0xFF);
(*buffer++) = (uint8_t)((pcm >> 16) & 0xFF);
(*buffer++) = (uint8_t)((pcm >> 24) & 0xFF);
}
}
if (WAVWrite_FWriteLittleEndian(writer->buffer.bytes,
Expand Down
25 changes: 2 additions & 23 deletions tools/srla_codec/srla_codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ static int do_encode(const char *in_filename, const char *out_filename,
struct SRLAEncoderConfig config;
struct SRLAEncodeParameter parameter;
struct stat fstat;
int32_t *input[SRLA_MAX_NUM_CHANNELS];
uint8_t *buffer;
uint32_t buffer_size, encoded_data_size;
SRLAApiResult ret;
Expand Down Expand Up @@ -97,18 +96,8 @@ static int do_encode(const char *in_filename, const char *out_filename,
/* 入力wavの2倍よりは大きくならないだろうという想定 */
buffer_size = (uint32_t)(2 * fstat.st_size);

/* エンコードデータ/入力データ領域を作成 */
/* エンコードデータ領域を作成 */
buffer = (uint8_t *)malloc(buffer_size);
for (ch = 0; ch < num_channels; ch++) {
input[ch] = (int32_t *)malloc(sizeof(int32_t) * num_samples);
}

/* 情報が失われない程度に右シフト */
for (ch = 0; ch < num_channels; ch++) {
for (smpl = 0; smpl < num_samples; smpl++) {
input[ch][smpl] = (int32_t)(WAVFile_PCM(in_wav, smpl, ch) >> (32 - in_wav->format.bits_per_sample));
}
}

/* エンコード関数の選択 */
encode_function = (variable_block_num_divisions == 0) ? SRLAEncoder_EncodeBlock : SRLAEncoder_EncodeOptimalPartitionedBlock;
Expand Down Expand Up @@ -146,7 +135,7 @@ static int do_encode(const char *in_filename, const char *out_filename,

/* サンプル参照位置のセット */
for (ch = 0; ch < (uint32_t)num_channels; ch++) {
input_ptr[ch] = &input[ch][progress];
input_ptr[ch] = &(WAVFile_PCM(in_wav, progress, ch));
}

/* ブロックエンコード */
Expand Down Expand Up @@ -185,9 +174,6 @@ static int do_encode(const char *in_filename, const char *out_filename,
/* リソース破棄 */
fclose(out_fp);
free(buffer);
for (ch = 0; ch < num_channels; ch++) {
free(input[ch]);
}
WAV_Destroy(in_wav);
SRLAEncoder_Destroy(encoder);

Expand Down Expand Up @@ -254,13 +240,6 @@ static int do_decode(const char *in_filename, const char *out_filename, uint8_t
return 1;
}

/* エンコード時に右シフトした分を戻し、32bit化 */
for (ch = 0; ch < out_wav->format.num_channels; ch++) {
for (smpl = 0; smpl < out_wav->format.num_samples; smpl++) {
WAVFile_PCM(out_wav, smpl, ch) <<= (32 - out_wav->format.bits_per_sample);
}
}

/* WAVファイル書き出し */
if (WAV_WriteToFile(out_filename, out_wav) != WAV_APIRESULT_OK) {
fprintf(stderr, "Failed to write wav file. \n");
Expand Down

0 comments on commit 20a1f56

Please sign in to comment.