// File: /workspace/github_cpp_makefile_star2_O1/jp-myk[P]WAVUtil/src/SimpleWAV/WAV.cpp
// Function: WAV::Read(char const*)
int WAV::Read(const char* WAVFName){
  std::ifstream ifs(WAVFName, std::ios::binary);
  //WAVHeader wav;
  //unsigned char header_buf[44];
  if(!ifs){
    std::cerr << "[ERROR] can't open " << WAVFName << std::endl;
    return -1;
  }
  
  //ifs.read((char*)header_buf, 44);

  //ファイルがRIFF形式であるか;  
  ifs.read(_riffID,4);
  if(ifs.bad() || strncmp(_riffID,"RIFF",4)!=0) return -1;
  ifs.read((char*)&_size,4); // fileSize
  
  
  //ファイルがWAVEファイルであるか
  ifs.read((char*)_wavID,4);
  if(ifs.bad() || strncmp(_wavID,"WAVE",4)!=0) return -1;
  
  //fmt のチェック
  ifs.read((char*)_fmtID,4);
  if(strncmp(_fmtID, "fmt ", 4)){
    std::cerr << "fmt not found" << std::endl;
    return -1;
  }
  // fmt チャンクのバイト数
  ifs.read((char*)&_fmtSize,4);
  if(_fmtSize != 16){
    std::cerr << "not LinearPCM" << std::endl;
    return -1;
  }
  //フォーマットIDから拡張部分までのヘッダ部分を取り込む
  // LinearPCMなので16byte分のデータ読み込む
  ifs.read((char*)&_format,2); //LinearPCMファイルならば1が入る
  if(_format!=1){
    std::cerr << "not LinearPCM" << std::endl;
    return -1;
  }
  ifs.read((char*)&_channels,2); 
  ifs.read((char*)&_sampleRate,4); 
  ifs.read((char*)&_bytePerSec,4); 
  ifs.read((char*)&_blockSize,2); 
  ifs.read((char*)&_bit,2);
  //--
  
   // "data"
  ifs.read((char*)_dataID,4);
  if(ifs.bad() || strncmp(_dataID,"data",4)!=0) return -1;
   // 波形データのバイト数
  ifs.read((char*)&_dataSize,4);
  
  //モノラルならサンプル数を、ステレオなら左右１サンプルずつの組の数
  _sampleCount=_dataSize / (_channels*(_bit/8));
  //_monoral8=NULL;
  //_monoral16=NULL;
  //_stereo8=NULL;
  //_stereo16=NULL;
  try{
    if(_channels==1){
      if(_bit==8){
	if((_monoral8=(Monoral8*)malloc(_dataSize)) == NULL){
	  return -1;
	}
	ifs.read((char*)_monoral8, _dataSize);
      }else if(_bit==16){
	if((_monoral16=(Monoral16*)malloc(_dataSize)) == NULL){
	  return -1;
	}
	//std::cout << "1block=" << sizeof(Monoral16) << std::endl;
	//std::cout << "samplecount=" << _sampleCount << std::endl;
	//std::cout << "dataSize=1block*sampleCount=" << sizeof(Monoral16)* _sampleCount << "=" <<_dataSize << std::endl;
	ifs.read((char*)_monoral16, _dataSize);
	
      }else{
	return -1;
      }
    }else   if(_channels==2){
      // ToDO LR insert data
      if(_bit==8){
	if((_stereo8=(Stereo8*)malloc(_dataSize)) == NULL){
	  return -1;
	}
	ifs.read((char*)_stereo8, _dataSize);
      }else if(_bit==16){
	if((_stereo16=(Stereo16*)malloc(_dataSize)) == NULL){
	    return -1;
	}
	ifs.read((char*)_stereo16, _dataSize);
      }else{
	return -1;
      }
    }else{
      return -1;
    }
  }catch(const char* e){
    printLOG("malloc error");
    printf("%s\n", e);
    exit(-1);
  }

  
  ifs.close();
  return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rsi, %rbx
movq %rdi, %r14
movq %rsp, %r15
movq %r15, %rdi
movl $0x4, %edx
callq 0x2280
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rsp,%rax)
je 0x2af8
movq 0x3537(%rip), %r14      # 0x5ff0
leaq 0x15fc(%rip), %rsi      # 0x40bc
movl $0x13, %edx
movq %r14, %rdi
callq 0x2190
testq %rbx, %rbx
je 0x2cc0
movq %rbx, %rdi
callq 0x20c0
movq %rax, %r15
movq 0x3508(%rip), %rdi      # 0x5ff0
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2190
jmp 0x2cd8
movq %rsp, %rdi
movl $0x4, %edx
movq %r14, %rsi
callq 0x21c0
movq (%rsp), %rax
movq -0x18(%rax), %rax
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb $0x1, 0x20(%rsp,%rax)
jne 0x2d0c
cmpl $0x46464952, (%r14)     # imm = 0x46464952
jne 0x2d0c
leaq 0x4(%r14), %rsi
movq %rsp, %rdi
movl $0x4, %edx
callq 0x21c0
leaq 0x8(%r14), %r15
movq %rsp, %rdi
movl $0x4, %edx
movq %r15, %rsi
callq 0x21c0
movq (%rsp), %rax
movq -0x18(%rax), %rax
testb $0x1, 0x20(%rsp,%rax)
jne 0x2d0c
cmpl $0x45564157, (%r15)     # imm = 0x45564157
jne 0x2d0c
leaq 0xc(%r14), %r12
movq %rsp, %rdi
movl $0x4, %edx
movq %r12, %rsi
callq 0x21c0
movl $0xd, %r15d
cmpl $0x20746d66, (%r12)     # imm = 0x20746D66
jne 0x2d25
leaq 0x10(%r14), %r12
movq %rsp, %rdi
movl $0x4, %edx
movq %r12, %rsi
callq 0x21c0
cmpl $0x10, (%r12)
jne 0x2d31
leaq 0x14(%r14), %r15
movq %rsp, %rdi
movl $0x2, %edx
movq %r15, %rsi
callq 0x21c0
cmpw $0x1, (%r15)
jne 0x2d3d
leaq 0x16(%r14), %rsi
movq %rsp, %rdi
movl $0x2, %edx
callq 0x21c0
leaq 0x18(%r14), %rsi
movq %rsp, %rdi
movl $0x4, %edx
callq 0x21c0
leaq 0x1c(%r14), %rsi
movq %rsp, %rdi
movl $0x4, %edx
callq 0x21c0
leaq 0x20(%r14), %rsi
movq %rsp, %rdi
movl $0x2, %edx
callq 0x21c0
leaq 0x22(%r14), %rsi
movq %rsp, %rdi
movl $0x2, %edx
callq 0x21c0
leaq 0x24(%r14), %r15
movq %rsp, %rdi
movl $0x4, %edx
movq %r15, %rsi
callq 0x21c0
movq (%rsp), %rax
movq -0x18(%rax), %rax
testb $0x1, 0x20(%rsp,%rax)
jne 0x2d0c
cmpl $0x61746164, (%r15)     # imm = 0x61746164
jne 0x2d0c
leaq 0x28(%r14), %rsi
movq %rsp, %rdi
movl $0x4, %edx
callq 0x21c0
movl 0x28(%r14), %r15d
movzwl 0x16(%r14), %esi
movzwl 0x22(%r14), %ecx
movl %ecx, %edi
shrl $0x3, %edi
imull %esi, %edi
movl %r15d, %eax
xorl %edx, %edx
divl %edi
movq %rax, 0x30(%r14)
cmpl $0x2, %esi
je 0x2d63
cmpl $0x1, %esi
jne 0x2d0c
cmpl $0x10, %ecx
je 0x2d7b
cmpl $0x8, %ecx
jne 0x2d0c
movq %r15, %rdi
callq 0x21e0
movq %rax, 0x38(%r14)
jmp 0x2d95
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2250
movq 0x3311(%rip), %rdi      # 0x5ff0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x2150
movsbl %al, %esi
movq 0x32f6(%rip), %rdi      # 0x5ff0
callq 0x2030
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %rax, %rdi
callq 0x2100
movq %rsp, %rdi
callq 0x2070
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x13a4(%rip), %rbx      # 0x40d0
jmp 0x2ae1
leaq 0x13a6(%rip), %rbx      # 0x40de
jmp 0x2ae1
movq 0x32ac(%rip), %rdi      # 0x5ff0
leaq 0x1393(%rip), %rsi      # 0x40de
movl $0xd, %edx
callq 0x2190
movq 0x3294(%rip), %rdi      # 0x5ff0
callq 0x20d0
jmp 0x2d0c
cmpl $0x10, %ecx
je 0x2d89
cmpl $0x8, %ecx
jne 0x2d0c
movq %r15, %rdi
callq 0x21e0
movq %rax, 0x48(%r14)
jmp 0x2d95
movq %r15, %rdi
callq 0x21e0
movq %rax, 0x40(%r14)
jmp 0x2d95
movq %r15, %rdi
callq 0x21e0
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x2d0c
movq %rsp, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x21c0
xorl %ebx, %ebx
movq %rsp, %rdi
callq 0x2060
jmp 0x2d0c
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x2dd0
movq %rbx, %rdi
movl %edx, %esi
callq 0x22df
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2070
movq %rbx, %rdi
callq 0x2270