// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movq %rdi, %r14
movq %rsp, %rbx
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x20(%rsp,%rax)
pushq $-0x1
popq %rbx
je 0x2874
movq 0x37b4(%rip), %rdi      # 0x5ff0
leaq 0x1879(%rip), %rsi      # 0x40bc
callq 0x2100
movq %rax, %rdi
movq %r15, %rsi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
movq %rsp, %rdi
callq 0x2060
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movq %rsp, %rdi
pushq $0x4
popq %rdx
movq %r14, %rsi
callq 0x2150
movq (%rsp), %rax
movq -0x18(%rax), %rax
testb $0x1, 0x20(%rsp,%rax)
jne 0x285b
cmpl $0x46464952, (%r14)     # imm = 0x46464952
jne 0x285b
leaq 0x4(%r14), %rsi
movq %rsp, %rdi
pushq $0x4
popq %rdx
callq 0x2150
leaq 0x8(%r14), %r15
movq %rsp, %rdi
pushq $0x4
popq %rdx
movq %r15, %rsi
callq 0x2150
movq (%rsp), %rax
movq -0x18(%rax), %rax
testb $0x1, 0x20(%rsp,%rax)
jne 0x285b
cmpl $0x45564157, (%r15)     # imm = 0x45564157
jne 0x285b
leaq 0xc(%r14), %r15
movq %rsp, %rdi
pushq $0x4
popq %rdx
movq %r15, %rsi
callq 0x2150
cmpl $0x20746d66, (%r15)     # imm = 0x20746D66
jne 0x2a0b
leaq 0x10(%r14), %r12
movq %rsp, %rdi
pushq $0x4
popq %rdx
movq %r12, %rsi
callq 0x2150
leaq 0x17d3(%rip), %r15      # 0x40de
cmpl $0x10, (%r12)
jne 0x2a12
leaq 0x14(%r14), %r12
movq %rsp, %rdi
pushq $0x2
popq %rdx
movq %r12, %rsi
callq 0x2150
cmpw $0x1, (%r12)
jne 0x2a12
leaq 0x16(%r14), %rsi
movq %rsp, %rdi
pushq $0x2
popq %rdx
callq 0x2150
leaq 0x18(%r14), %rsi
movq %rsp, %rdi
pushq $0x4
popq %rdx
callq 0x2150
leaq 0x1c(%r14), %rsi
movq %rsp, %rdi
pushq $0x4
popq %rdx
callq 0x2150
leaq 0x20(%r14), %rsi
movq %rsp, %rdi
pushq $0x2
popq %rdx
callq 0x2150
leaq 0x22(%r14), %rsi
movq %rsp, %rdi
pushq $0x2
popq %rdx
callq 0x2150
leaq 0x24(%r14), %r15
movq %rsp, %rdi
pushq $0x4
popq %rdx
movq %r15, %rsi
callq 0x2150
movq (%rsp), %rax
movq -0x18(%rax), %rax
testb $0x1, 0x20(%rsp,%rax)
jne 0x285b
cmpl $0x61746164, (%r15)     # imm = 0x61746164
jne 0x285b
leaq 0x28(%r14), %rsi
movq %rsp, %rdi
pushq $0x4
popq %rdx
callq 0x2150
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 0x2a26
cmpl $0x1, %esi
jne 0x285b
cmpl $0x10, %ecx
je 0x2a42
cmpl $0x8, %ecx
jne 0x285b
movq %r15, %rdi
callq 0x2170
movq %rax, 0x38(%r14)
jmp 0x2a5c
leaq 0x16be(%rip), %r15      # 0x40d0
movq 0x35d7(%rip), %rdi      # 0x5ff0
movq %r15, %rsi
callq 0x2100
jmp 0x2853
cmpl $0x10, %ecx
je 0x2a50
cmpl $0x8, %ecx
jne 0x285b
movq %r15, %rdi
callq 0x2170
movq %rax, 0x48(%r14)
jmp 0x2a5c
movq %r15, %rdi
callq 0x2170
movq %rax, 0x40(%r14)
jmp 0x2a5c
movq %r15, %rdi
callq 0x2170
movq %rax, 0x50(%r14)
testq %rax, %rax
je 0x285b
movq %rsp, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x2150
xorl %ebx, %ebx
movq %rsp, %rdi
callq 0x2050
jmp 0x285b
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x2ad0
movq %rbx, %rdi
callq 0x2090
movq %rax, %rbx
leaq 0x1568(%rip), %rdi      # 0x4004
leaq 0x156e(%rip), %rsi      # 0x4011
leaq 0x1642(%rip), %rdx      # 0x40ec
leaq 0x1640(%rip), %r8       # 0x40f1
movl $0xd0, %ecx
xorl %eax, %eax
callq 0x2030
movq %rbx, %rdi
callq 0x21c0
pushq $-0x1
popq %rdi
callq 0x2140
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2060
movq %rbx, %rdi
callq 0x2200