// File: Sleepwalking[P]world-octave/test.cpp
// Function: main
int main(int argc, char *argv[]) {
  if (argc != 2 && argc != 3 && argc != 4 && argc != 5) {
    printf("error\n");
    return 0;
  }
  int fs, nbit, x_length;
  double *x = wavread(argv[1], &fs, &nbit, &x_length);

  if (CheckLoadedFile(x, fs, nbit, x_length) == false) {
    printf("error: File not found.\n");
    return 0;
  }

  // Allocate memories
  // The number of samples for F0
  int f0_length = GetSamplesForDIO(fs, x_length, FRAMEPERIOD);
  double *f0 = new double[f0_length];
  double *time_axis = new double[f0_length];

  // FFT size for CheapTrick
  int fft_size = GetFFTSizeForCheapTrick(fs);
  double **spectrogram = new double *[f0_length];
  double **aperiodicity = new double *[f0_length];
  for (int i = 0; i < f0_length; ++i) {
    spectrogram[i] = new double[fft_size / 2 + 1];
    aperiodicity[i] = new double[fft_size / 2 + 1];
  }

  // F0 estimation
  F0Estimation(x, x_length, fs, f0_length, f0, time_axis);

  // Spectral envelope estimation
  SpectralEnvelopeEstimation(x, x_length, fs, time_axis, f0, f0_length,
      spectrogram);

  // Aperiodicity estimation by D4C
  AperiodicityEstimation(x, x_length, fs, time_axis, f0, f0_length,
      fft_size, aperiodicity);

  // Note that F0 must not be changed until all parameters are estimated.
  ParameterModification(argc, argv, fs, f0, f0_length, spectrogram);

  // The length of the output waveform
  int y_length =
    static_cast<int>((f0_length - 1) * FRAMEPERIOD / 1000.0 * fs) + 1;
  double *y = new double[y_length];
  // Synthesis
  WaveformSynthesis(f0, f0_length, spectrogram, aperiodicity, fft_size,
      FRAMEPERIOD, fs, y_length, y);

  // Output
  wavwrite(y, y_length, fs, 16, argv[2]);

  printf("complete.\n");

  delete[] x;
  delete[] time_axis;
  delete[] f0;
  delete[] y;
  for (int i = 0; i < f0_length; ++i) {
    delete[] spectrogram[i];
    delete[] aperiodicity[i];
  }
  delete[] spectrogram;
  delete[] aperiodicity;

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x1b0bb
cmpl $0x3, -0x8(%rbp)
je 0x1b0bb
cmpl $0x4, -0x8(%rbp)
je 0x1b0bb
cmpl $0x5, -0x8(%rbp)
je 0x1b0bb
leaq 0x1398(%rip), %rdi      # 0x1c440
movb $0x0, %al
callq 0x1040
movl $0x0, -0x4(%rbp)
jmp 0x1b4c4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x14(%rbp), %rsi
leaq -0x18(%rbp), %rdx
leaq -0x1c(%rbp), %rcx
callq 0x7e70
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
callq 0x1b4d0
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x1b10e
leaq 0x134c(%rip), %rdi      # 0x1c447
movb $0x0, %al
callq 0x1040
movl $0x0, -0x4(%rbp)
jmp 0x1b4c4
movl -0x14(%rbp), %edi
movl -0x1c(%rbp), %esi
movsd 0xf6c(%rip), %xmm0      # 0x1c088
callq 0x2420
movl %eax, -0x2c(%rbp)
movslq -0x2c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x38(%rbp)
movslq -0x2c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x40(%rbp)
movl -0x14(%rbp), %edi
callq 0x84a0
movl %eax, -0x44(%rbp)
movslq -0x2c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x50(%rbp)
movslq -0x2c(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1b274
movl -0x44(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rdx
movq -0x50(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x44(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, %rdx
movq -0x58(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x1b1da
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x14(%rbp), %edx
movl -0x2c(%rbp), %ecx
movq -0x38(%rbp), %r8
movq -0x40(%rbp), %r9
callq 0x1b560
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x14(%rbp), %edx
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x2c(%rbp), %r9d
movq -0x50(%rbp), %rax
movq %rax, (%rsp)
callq 0x1b730
movq -0x28(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl -0x14(%rbp), %edx
movq -0x40(%rbp), %rcx
movq -0x38(%rbp), %r8
movl -0x2c(%rbp), %r9d
movl -0x44(%rbp), %r10d
movq -0x58(%rbp), %rax
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1b7a0
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movl -0x14(%rbp), %edx
movq -0x38(%rbp), %rcx
movl -0x2c(%rbp), %r8d
movq -0x50(%rbp), %r9
callq 0x1b820
movl -0x2c(%rbp), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm0
movsd 0xd7e(%rip), %xmm1      # 0x1c088
mulsd %xmm1, %xmm0
movsd 0xd62(%rip), %xmm1      # 0x1c078
divsd %xmm1, %xmm0
cvtsi2sdl -0x14(%rbp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
movslq -0x60(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x1030
movq %rax, -0x68(%rbp)
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %esi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movl -0x44(%rbp), %r8d
movl -0x14(%rbp), %r9d
movl -0x60(%rbp), %r10d
movq -0x68(%rbp), %rax
movsd 0xd0b(%rip), %xmm0      # 0x1c088
movl %r10d, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1bc30
movq -0x68(%rbp), %rdi
movl -0x60(%rbp), %esi
movl -0x14(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %r8
movl $0x10, %ecx
callq 0x7c10
leaq 0x10b1(%rip), %rdi      # 0x1c45f
movb $0x0, %al
callq 0x1040
movq -0x28(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, %rax
je 0x1b3cc
movq -0x78(%rbp), %rdi
callq 0x1110
movq -0x40(%rbp), %rax
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
je 0x1b3e3
movq -0x80(%rbp), %rdi
callq 0x1110
movq -0x38(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x1b400
movq -0x88(%rbp), %rdi
callq 0x1110
movq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
cmpq $0x0, %rax
je 0x1b41d
movq -0x90(%rbp), %rdi
callq 0x1110
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1b483
movq -0x50(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x1b451
movq -0x98(%rbp), %rdi
callq 0x1110
movq -0x58(%rbp), %rax
movslq -0x6c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x1b476
movq -0xa0(%rbp), %rdi
callq 0x1110
jmp 0x1b478
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1b424
movq -0x50(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x1b4a0
movq -0xa8(%rbp), %rdi
callq 0x1110
movq -0x58(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x1b4bd
movq -0xb0(%rbp), %rdi
callq 0x1110
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq