// File: jp-myk[P]WAVUtil/src/SimpleWAV/WAVUtil.cpp
// Function: SimpleWAV::trim_wav(float, float, WAV&, WAV&)
int trim_wav(float startTime_sec, float endTime_sec, WAV& wav, WAV& outwav){
  int datasize=0;
  void* data;
  int res=0;
  unsigned int startSamplePos= startTime_sec*wav.getSampleRate();
  unsigned int endSamplePos  = endTime_sec*wav.getSampleRate();
  if(endSamplePos>wav.getSampleCount()){
    endSamplePos=wav.getSampleCount();
    endTime_sec=(float)endSamplePos/wav.getSampleRate();
  }
  printf("trim %.5f - %.5f [sec] / %.5f [sec]\n", startTime_sec, endTime_sec, (float)wav.getSampleCount()/wav.getSampleRate());
  
  unsigned int startBytePos  = startSamplePos*wav.getChannels()*wav.getBit()/8;
  unsigned int endBytePos  =  endSamplePos*wav.getChannels()*wav.getBit()/8;

  outwav.createHeader(wav.getChannels(), wav.getSampleRate(), wav.getBit());
  datasize=endBytePos-startBytePos;
  data=(void*)malloc(endBytePos-startBytePos);
  memcpy(data, (char*)wav.getData()+startBytePos, datasize);
  res=outwav.setData(data, datasize);
  free(data);
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movss %xmm0, -0x4(%rbp)
movss %xmm1, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x2c(%rbp)
movss -0x4(%rbp), %xmm0
movss %xmm0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38c0
movss -0x48(%rbp), %xmm0
movl %eax, %eax
cvtsi2ss %rax, %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x30(%rbp)
movss -0x8(%rbp), %xmm0
movss %xmm0, -0x44(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38c0
movss -0x44(%rbp), %xmm0
movl %eax, %eax
cvtsi2ss %rax, %xmm1
mulss %xmm1, %xmm0
cvttss2si %xmm0, %rax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x42b0
movl %eax, %ecx
movl -0x40(%rbp), %eax
cmpl %ecx, %eax
jbe 0x39f0
movq -0x10(%rbp), %rdi
callq 0x42b0
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38c0
movss -0x4c(%rbp), %xmm0
movl %eax, %eax
cvtsi2ss %rax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x8(%rbp)
movss -0x4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x88(%rbp)
movss -0x8(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x80(%rbp)
movq -0x10(%rbp), %rdi
callq 0x42b0
movl %eax, %eax
cvtsi2ss %rax, %xmm0
movss %xmm0, -0x8c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38c0
movss -0x8c(%rbp), %xmm2
movsd -0x88(%rbp), %xmm0
movsd -0x80(%rbp), %xmm1
movl %eax, %eax
cvtsi2ss %rax, %xmm3
divss %xmm3, %xmm2
cvtss2sd %xmm2, %xmm2
leaq 0x2755(%rip), %rdi      # 0x61b0
movb $0x3, %al
callq 0x2030
movl -0x30(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38a0
movw %ax, %cx
movl -0x78(%rbp), %eax
movzwl %cx, %ecx
imull %ecx, %eax
movl %eax, -0x74(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38e0
movw %ax, %cx
movl -0x74(%rbp), %eax
movzwl %cx, %ecx
imull %ecx, %eax
shrl $0x3, %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38a0
movw %ax, %cx
movl -0x70(%rbp), %eax
movzwl %cx, %ecx
imull %ecx, %eax
movl %eax, -0x6c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38e0
movw %ax, %cx
movl -0x6c(%rbp), %eax
movzwl %cx, %ecx
imull %ecx, %eax
shrl $0x3, %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38a0
movw %ax, -0x5e(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38c0
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x38e0
movq -0x68(%rbp), %rdi
movw -0x5e(%rbp), %cx
movl -0x5c(%rbp), %edx
movzwl %cx, %esi
movzwl %ax, %ecx
callq 0x24c0
movl -0x3c(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x3c(%rbp), %eax
subl -0x38(%rbp), %eax
movl %eax, %eax
movl %eax, %edi
callq 0x2180
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2720
movq -0x58(%rbp), %rdi
movq %rax, %rsi
movl -0x38(%rbp), %eax
addq %rax, %rsi
movslq -0x1c(%rbp), %rdx
callq 0x20e0
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x2570
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rdi
callq 0x2140
xorl %eax, %eax
addq $0x90, %rsp
popq %rbp
retq
nop