Skip to content

Normalize() #4

@kooBH

Description

@kooBH

정규화를 내부에서 버퍼 단위로 하려고 했는데

void WAV::Normalize() {
  short max=0;
  int cnt;
  int length = 0;
  int cur_idx = 0;
  float rate = 0;

  if (fmt_type != 1) {
    printf("SORRY::Currently, I do not support Normalize() for other than short type.");
    return;
  }
  short* temp_buf = new short[channels * 128];
  fclose(fp);
  printf("%s\n",file_name);
  fp = fopen(file_name, "rb+");

  Rewind();

  while (!this->IsEOF()) {
    cnt = ReadUnit(temp_buf, channels * 128);
    length += cnt;

    for (int i = 0; i < channels * 128; i++) {
      if (std::abs(temp_buf[i]) > max)
        max = std::abs(temp_buf[i]);
    }
  }
  printf("max : %d\n",max);
  Rewind();
  while (length >= cur_idx) {
   printf("read at : %d\n",ftell(fp));
    cnt = ReadUnit(temp_buf, channels * 128);
    cur_idx += cnt;
  // printf("read : %d | %d | %d\n", cnt,ftell(fp),size_unit);
   printf("read done : %d\n",ftell(fp));
    for (int i = 0; i < cnt; i++) {
      printf("%d ", temp_buf[i]);
      rate = (float)((32767.0 / max));
      temp_buf[i] = (short)(temp_buf[i] * rate);
      //printf("%d\n", temp_buf[i]);
    }
    printf("\n");
    fseek(fp, -size_unit*cnt, SEEK_CUR);
    printf("rollback : %d\n",ftell(fp));
    printf("write at %d\n",ftell(fp));
    fwrite(temp_buf, size_unit, cnt, fp);
    printf("wrote %d\n------------------------------------\n",ftell(fp));
  }

  delete[] temp_buf;
}

잘 안되네,
그냥 내부에 파일 통째로 버퍼로 해서 해야겠다.

-------- WAV HEADER INFOMATION of [ ../s.wav ] ----------
riff_id        : RIFF
riff_size      : 311332
wave_id        : WAVE
fmt_id         : fmt
fmt_size       : 16
fmt_type       : 1 - PCM
channels       : 2
sample_rate    : 16000
byte_rate      : 64000
block_align    : 4
bit_per_sample : 16
data_id        : data
data_size      : 311296
duration       : 4.865 sec
../s.wav
INFO::Rewind
max : 1034
INFO::Rewind
read at : 44
read done : 556
1 4 1 2 0 1 1 1 2 9 3 5 1 10 4 6 -1 4 2 -1 -1 -3 -3 -2 1 -5 -3 -1 2 0 1 3 1 7 0 -3 -4 -8 -2 -12 -4 -8 -1 -6 -2 -5 -2 -3 1 -6 -6 -7 0 -11 -9 -22 -6 -21 -5 -17 -1 1 7 15 3 17 3 4 -1 -3 -2 -4 0 -3 -2 -3 2 4 1 6 2 2 0 4 1 1 2 5 -2 -1 0 -5 -1 1 2 1 0 2 3 12 4 12 5 14 3 10 2 4 2 7 1 7 4 9 3 10 1 8 4 2 -3 4 3 -4 -6 -8 1 -4 -2 1 2 1 0 0 3 11 1 0 0 7 4 5 -2 -1 2 3 -1 -2 -2 -5 -5 -19 -9 -24 -5 -17 -4 -12 -1 -6 -2 -3 0 -6 -6 -7 0 -13 -7 -11 -2 -14 0 3 0 1 7 19 2 10 5 18 7 13 1 15 2 -3 -1 -2 -1 6 5 3 -1 5 -1 -2 2 -3 -3 2 3 2 0 3 1 4 4 7 2 13 5 11 1 7 -1 -6 -4 -13 -2 -7 0 5 3 1 -3 -2 -2 -11 -4 -13 -4 -7 -2 -13 -3 -4 1 -5 -5 -6 -1 -14 -7 -10 3 -1 0 7 5 11 5 17 1 4
rollback : 44
write at 44
wrote 556
------------------------------------
read at : 556
read done : 1068
-2 -13 -3 -4 1 -5 -5 -6 -1 -14 -7 -10 3 -1 0 7 5 11 5 17 1 4 0 -2 -2 -3 1 2 0 -2 -7 -22 -5 -14 -1 -6 -2 -2 1 -1 -2 -3 2 4 1 3 2 7 2 8 -2 -8 -2 -7 -4 -8 0 -7 -3 -4 3 7 6 16 0 8 3 1 -1 3 1 1 1 2 -1 -1 2 4 0 3 -1 -4 -1 -3 -1 -1 -1 -8 -2 -1 2 0 0 8 4 6 -3 -3 2 0 -1 2 2 3 7 20 6 25 4 10 0 3 -2 -11 -3 -5 2 -3 -4 3 7 7 1 14 4 4 0 5 2 1 -2 2 2 -1 0 6 1 -4 -4 -5 2 -1 1 9 8 22 6 20 0 1 -5 -16 -2 -8 0 2 5 14 -1 0 0 -6 -6 -11 0 -5 -1 -1 -1 -3 0 0 0 -5 -3 -1 -3 -15 -6 -18 -5 -11 0 -6 -3 -9 1 9 5 7 4 20 4 9 -1 -4 -6 -16 -3 -9 -1 -8 2 12 8 17 3 17 3 8 0 -6 -7 -11 0 -14 -6 -6 2 -3 0 5 -1 -8 -2 1 2 -3 -2 4 0 -10 -7 -15 -3 -15 -3 -4 2 0 -6 -13 -5 -21 -11 -31 -6 -21 -6 -20 -9 -27 -5 -14 -1 -6
rollback : 556
write at 556
wrote 1068
------------------------------------
read at : 1068
read done : 1580
-2 -13 -3 -4 1 -5 -5 -6 -1 -14 -7 -10 3 -1 0 7 5 11 5 17 1 4 0 -2 -2 -3 1 2 0 -2 -7 -22 -5 -14 -1 -6 -2 -2 1 -1 -2 -3 2 4 1 3 2 7 2 8 -2 -8 -2 -7 -4 -8 0 -7 -3 -4 3 7 6 16 0 8 3 1 -1 3 1 1 1 2 -1 -1 2 4 0 3 -1 -4 -1 -3 -1 -1 -1 -8 -2 -1 2 0 0 8 4 6 -3 -3 2 0 -1 2 2 3 7 20 6 25 4 10 0 3 -2 -11 -3 -5 2 -3 -4 3 7 7 1 14 4 4 0 5 2 1 -2 2 2 -1 0 6 1 -4 -4 -5 2 -1 1 9 8 22 6 20 0 1 -5 -16 -2 -8 0 2 5 14 -1 0 0 -6 -6 -11 0 -5 -1 -1 -1 -3 0 0 0 -5 -3 -1 -3 -15 -6 -18 -5 -11 0 -6 -3 -9 1 9 5 7 4 20 4 9 -1 -4 -6 -16 -3 -9 -1 -8 2 12 8 17 3 17 3 8 0 -6 -7 -11 0 -14 -6 -6 2 -3 0 5 -1 -8 -2 1 2 -3 -2 4 0 -10 -7 -15 -3 -15 -3 -4 2 0 -6 -13 -5 -21 -11 -31 -6 -21 -6 -20 -9 -27 -5 -14 -1 -6
rollback : 1068
write at 1068
wrote 1580
------------------------------------
read at : 1580
read done : 2092
-2 -13 -3 -4 1 -5 -5 -6 -1 -14 -7 -10 3 -1 0 7 5 11 5 17 1 4 0 -2 -2 -3 1 2 0 -2 -7 -22 -5 -14 -1 -6 -2 -2 1 -1 -2 -3 2 4 1 3 2 7 2 8 -2 -8 -2 -7 -4 -8 0 -7 -3 -4 3 7 6 16 0 8 3 1 -1 3 1 1 1 2 -1 -1 2 4 0 3 -1 -4 -1 -3 -1 -1 -1 -8 -2 -1 2 0 0 8 4 6 -3 -3 2 0 -1 2 2 3 7 20 6 25 4 10 0 3 -2 -11 -3 -5 2 -3 -4 3 7 7 1 14 4 4 0 5 2 1 -2 2 2 -1 0 6 1 -4 -4 -5 2 -1 1 9 8 22 6 20 0 1 -5 -16 -2 -8 0 2 5 14 -1 0 0 -6 -6 -11 0 -5 -1 -1 -1 -3 0 0 0 -5 -3 -1 -3 -15 -6 -18 -5 -11 0 -6 -3 -9 1 9 5 7 4 20 4 9 -1 -4 -6 -16 -3 -9 -1 -8 2 12 8 17 3 17 3 8 0 -6 -7 -11 0 -14 -6 -6 2 -3 0 5 -1 -8 -2 1 2 -3 -2 4 0 -10 -7 -15 -3 -15 -3 -4 2 0 -6 -13 -5 -21 -11 -31 -6 -21 -6 -20 -9 -27 -5 -14 -1 -6
rollback : 1580
write at 1580
wrote 2092

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions