// File: 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 %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2230
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x20a0
movb %al, -0x239(%rbp)
jmp 0x2ec3
movb -0x239(%rbp), %al
testb $0x1, %al
jne 0x2ecf
jmp 0x2f46
movq 0x611a(%rip), %rdi      # 0x8ff0
leaq 0x31f3(%rip), %rsi      # 0x60d0
callq 0x2100
movq %rax, -0x250(%rbp)
jmp 0x2eeb
movq -0x250(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2100
movq %rax, -0x258(%rbp)
jmp 0x2f04
movq -0x258(%rbp), %rdi
movq 0x60ae(%rip), %rsi      # 0x8fc0
callq 0x2130
jmp 0x2f19
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x3624
movq -0x248(%rbp), %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x2f60
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x2220
movb %al, -0x259(%rbp)
jmp 0x2f80
movb -0x259(%rbp), %al
testb $0x1, %al
jne 0x2fa9
jmp 0x2f8c
movq -0x248(%rbp), %rdi
leaq 0x30ed(%rip), %rsi      # 0x6087
movl $0x4, %edx
callq 0x20b0
cmpl $0x0, %eax
je 0x2fbf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rsi
addq $0x4, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x2fdd
movq -0x248(%rbp), %rsi
addq $0x8, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x2ffb
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x2220
movb %al, -0x25a(%rbp)
jmp 0x301b
movb -0x25a(%rbp), %al
testb $0x1, %al
jne 0x3048
jmp 0x3027
movq -0x248(%rbp), %rdi
addq $0x8, %rdi
leaq 0x3053(%rip), %rsi      # 0x608c
movl $0x4, %edx
callq 0x20b0
cmpl $0x0, %eax
je 0x305e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rsi
addq $0xc, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x307c
movq -0x248(%rbp), %rdi
addq $0xc, %rdi
leaq 0x3003(%rip), %rsi      # 0x6091
movl $0x4, %edx
callq 0x20b0
cmpl $0x0, %eax
je 0x30e4
movq 0x5f4c(%rip), %rdi      # 0x8ff0
leaq 0x3039(%rip), %rsi      # 0x60e4
callq 0x2100
movq %rax, -0x268(%rbp)
jmp 0x30b9
movq -0x268(%rbp), %rdi
movq 0x5ef9(%rip), %rsi      # 0x8fc0
callq 0x2130
jmp 0x30ce
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rsi
addq $0x10, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x3102
movq -0x248(%rbp), %rax
cmpl $0x10, 0x10(%rax)
je 0x3156
movq 0x5eda(%rip), %rdi      # 0x8ff0
leaq 0x2fd5(%rip), %rsi      # 0x60f2
callq 0x2100
movq %rax, -0x270(%rbp)
jmp 0x312b
movq -0x270(%rbp), %rdi
movq 0x5e87(%rip), %rsi      # 0x8fc0
callq 0x2130
jmp 0x3140
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rsi
addq $0x14, %rsi
leaq -0x220(%rbp), %rdi
movl $0x2, %edx
callq 0x2160
jmp 0x3174
movq -0x248(%rbp), %rax
movzwl 0x14(%rax), %eax
cmpl $0x1, %eax
je 0x31cb
movq 0x5e65(%rip), %rdi      # 0x8ff0
leaq 0x2f60(%rip), %rsi      # 0x60f2
callq 0x2100
movq %rax, -0x278(%rbp)
jmp 0x31a0
movq -0x278(%rbp), %rdi
movq 0x5e12(%rip), %rsi      # 0x8fc0
callq 0x2130
jmp 0x31b5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rsi
addq $0x16, %rsi
leaq -0x220(%rbp), %rdi
movl $0x2, %edx
callq 0x2160
jmp 0x31e9
movq -0x248(%rbp), %rsi
addq $0x18, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x3207
movq -0x248(%rbp), %rsi
addq $0x1c, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x3225
movq -0x248(%rbp), %rsi
addq $0x20, %rsi
leaq -0x220(%rbp), %rdi
movl $0x2, %edx
callq 0x2160
jmp 0x3243
movq -0x248(%rbp), %rsi
addq $0x22, %rsi
leaq -0x220(%rbp), %rdi
movl $0x2, %edx
callq 0x2160
jmp 0x3261
movq -0x248(%rbp), %rsi
addq $0x24, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x327f
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x2220
movb %al, -0x279(%rbp)
jmp 0x329f
movb -0x279(%rbp), %al
testb $0x1, %al
jne 0x32cc
jmp 0x32ab
movq -0x248(%rbp), %rdi
addq $0x24, %rdi
leaq 0x2dd9(%rip), %rsi      # 0x6096
movl $0x4, %edx
callq 0x20b0
cmpl $0x0, %eax
je 0x32e2
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rsi
addq $0x28, %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x2160
jmp 0x3300
movq -0x248(%rbp), %rax
movl 0x28(%rax), %ecx
movl %ecx, -0x280(%rbp)
movzwl 0x16(%rax), %ecx
movzwl 0x22(%rax), %eax
movl $0x8, %esi
cltd
idivl %esi
movl %eax, %edx
movl -0x280(%rbp), %eax
imull %edx, %ecx
xorl %edx, %edx
divl %ecx
movl %eax, %ecx
movq -0x248(%rbp), %rax
movl %ecx, %ecx
movq %rcx, 0x30(%rax)
movzwl 0x16(%rax), %eax
cmpl $0x1, %eax
jne 0x34c2
movq -0x248(%rbp), %rax
movzwl 0x22(%rax), %eax
cmpl $0x8, %eax
jne 0x343d
movq -0x248(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %edi
callq 0x2180
movq -0x248(%rbp), %rcx
movq %rax, 0x38(%rcx)
cmpq $0x0, %rax
jne 0x3397
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rax
movq 0x38(%rax), %rsi
movl 0x28(%rax), %eax
movl %eax, %edx
leaq -0x220(%rbp), %rdi
callq 0x2160
jmp 0x33b5
jmp 0x34bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x3624
movq -0x228(%rbp), %rdi
callq 0x2090
movq %rax, -0x238(%rbp)
leaq 0x2c0b(%rip), %rdi      # 0x6004
leaq 0x2c11(%rip), %rsi      # 0x6011
leaq 0x2cf9(%rip), %rdx      # 0x6100
leaq 0x2cf7(%rip), %r8       # 0x6105
xorl %eax, %eax
movl $0xd0, %ecx
callq 0x2030
jmp 0x341c
movq -0x238(%rbp), %rsi
leaq 0x2be3(%rip), %rdi      # 0x600d
xorl %eax, %eax
callq 0x2030
jmp 0x3433
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x2150
movq -0x248(%rbp), %rax
movzwl 0x22(%rax), %eax
cmpl $0x10, %eax
jne 0x34a5
movq -0x248(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %edi
callq 0x2180
movq -0x248(%rbp), %rcx
movq %rax, 0x40(%rcx)
cmpq $0x0, %rax
jne 0x3485
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rax
movq 0x40(%rax), %rsi
movl 0x28(%rax), %eax
movl %eax, %edx
leaq -0x220(%rbp), %rdi
callq 0x2160
jmp 0x34a3
jmp 0x34bb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
jmp 0x34bd
jmp 0x35d2
movq -0x248(%rbp), %rax
movzwl 0x16(%rax), %eax
cmpl $0x2, %eax
jne 0x35bd
movq -0x248(%rbp), %rax
movzwl 0x22(%rax), %eax
cmpl $0x8, %eax
jne 0x353e
movq -0x248(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %edi
callq 0x2180
movq -0x248(%rbp), %rcx
movq %rax, 0x48(%rcx)
cmpq $0x0, %rax
jne 0x351e
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rax
movq 0x48(%rax), %rsi
movl 0x28(%rax), %eax
movl %eax, %edx
leaq -0x220(%rbp), %rdi
callq 0x2160
jmp 0x353c
jmp 0x35bb
movq -0x248(%rbp), %rax
movzwl 0x22(%rax), %eax
cmpl $0x10, %eax
jne 0x35a6
movq -0x248(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %edi
callq 0x2180
movq -0x248(%rbp), %rcx
movq %rax, 0x50(%rcx)
cmpq $0x0, %rax
jne 0x3586
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
movq -0x248(%rbp), %rax
movq 0x50(%rax), %rsi
movl 0x28(%rax), %eax
movl %eax, %edx
leaq -0x220(%rbp), %rdi
callq 0x2160
jmp 0x35a4
jmp 0x35b9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
jmp 0x35bb
jmp 0x35d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x230(%rbp)
jmp 0x360c
jmp 0x35d2
jmp 0x35ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x21e0
jmp 0x3624
leaq -0x220(%rbp), %rdi
callq 0x2050
jmp 0x35fb
movl $0x0, -0x4(%rbp)
movl $0x1, -0x230(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x2060
movl -0x4(%rbp), %eax
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
leaq -0x220(%rbp), %rdi
callq 0x2060
movq -0x228(%rbp), %rdi
callq 0x2210
nopl (%rax)