#include <stdio.h>
#include <vector>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <stdint.h>
//signed short decode(unsigned char t);
 int16_t decode(uint8_t t);

main()
{
  /*
   * old_headersize, f und o vorher setzen*/
  std::vector<unsigned char> data;
  FILE* f = fopen("outputfileulaw.wav", "rb");
  rewind(f);
  //int old_header_size = 0;
  while(!feof(f))
  {
    unsigned char s;
    fread(&s, sizeof s, 1, f);
    data.push_back(s);
    int* last_word = (int*)&data[data.size()-4];
  }
  FILE* o = fopen("outputfileulaw.raw", "wb");
  int headersize = 4;
  int old_header_size = 58;
  unsigned int samples = data.size() - old_header_size;
  unsigned char* new_data = (unsigned char*)malloc(sizeof(char) * (samples + headersize));
  //little endian: alles umdrehen...
  int samples_old = samples;
  new_data[0] = (unsigned char) samples;
  printf("value: %#x\n", (unsigned char)samples);
  samples >>= 8;
  new_data[1] = (unsigned char) samples;
  printf("value: %#x\n", (unsigned char)samples);
  samples >>= 8;
  new_data[2] = (unsigned char) samples;
  printf("value: %#x\n", (unsigned char)samples);
  samples >>= 8;
  new_data[3] = (unsigned char) samples;
  printf("value: %#x\n", (unsigned char)samples);
  //memcpy(&new_data[0], &samples, sizeof(int));
  memcpy(&new_data[4], &data[old_header_size], samples_old);
  fwrite(&new_data[0], sizeof(unsigned char), samples_old+headersize, o);
  
}
 int16_t decode(uint8_t t)
{
  int16_t exp = 0x8 - (0x7 & (t >> 4));
  uint16_t base = (0x1 << (exp+ 5)) - 34;
  int16_t step = 0x1 << exp;
  int16_t ret = base - step  * (0xf & t);
  ret = -ret - 1 +  (2 * ret + 1) * (t >> 7);
  return ret;
}
/*signed short decode(unsigned char t)
{
  int exp = 0x8 - (0x7 & (t >> 4));
  int base = (0x1 << (exp+ 5)) - 34;
  int step = 0x1 << exp;
  signed short ret = base - step  * (0xf & t);
  ret = -ret - 1 +  (2 * ret + 1) * (t >> 7);
  return ret;
}*/