1

How do you

  1. Read a 16 bit stereo raw audio file in binary mode
  2. Convert it's values to integer for right and for left.
  3. Save those integers back to a new 16 bit stereo raw audio file.

The final two files should be the same, with my code, they are not. Why?

My code:

char ergebnis[80];                                                  
sprintf(ergebnis,"%s.neu.raw",Datei);
FILE* ausgabe = fopen(ergebnis, "wb");
FILE* f = fopen(Datei, "rb");                    
if (f == NULL)
  return;
int i = -1;
int r1 = 0;
int r2 = 0;
int l1 = 0;
int l2 = 0;
int l = 0;
int r = 0;
while((getc(f))!=EOF)
{  
  i++;
  if (i == 0)
    r1 = (unsigned)fgetc(f);
  if (i == 1)
  {
    r2 = (unsigned)fgetc(f);
    r = r1 | r2 << 8;  
  }
  if (i == 2)
    l1 = (unsigned)fgetc(f);
  if (i == 3)
  {
    l2 = (unsigned)fgetc(f);
    l = l1 | l2 << 8;
    putc(r,ausgabe);
    putc(l,ausgabe);
    i = -1;
  }
}  
fclose(f);
fclose(ausgabe);
exit(-1);

EDIT: Solved it with

int wo = 0;
while(wo !=EOF)
{
  wo = getc(f);  
  i++;
  if (i == 0)
    r1 = (unsigned)wo;
  if (i == 1)
  {
    r2 = (unsigned)wo;
    r = (r2<<8)+r1; //r1 | r2 << 8;  
  }
  if (i == 2)
    l1 = (unsigned)wo;
  if (i == 3)
  {
    l2 = (unsigned)wo;
    l = (l2<<8)+l1; //l1 | l2 << 8;   
    putc((char) ( r       & 0xff),ausgabe);
    putc((char) ((r >> 8) & 0xff),ausgabe); 
    putc((char) ( l       & 0xff),ausgabe);
    putc((char) ((l >> 8) & 0xff),ausgabe);
    i = -1;
  }
}
11
  • What differences do you see?
    – LostBoy
    Jul 11, 2013 at 14:12
  • Won't putc write a single char, discarding the upper component of your 16-bit value?
    – simonc
    Jul 11, 2013 at 14:23
  • @simonc what would you write instead? Jul 11, 2013 at 14:27
  • Try fwrite instead. Or write out the data byte at a time using putc or fputc. In either case, you'll have to decide which endianess you expect from your source file and want in your destination file.
    – simonc
    Jul 11, 2013 at 14:37
  • @simonc I tried fwrite (r, sizeof(int), sizeof(r), ausgabe); but that gives me warning: passing argument 1 of fwrite makes pointer from integer without a cast [enabled by default]. Jul 11, 2013 at 14:57

1 Answer 1

1

Getc in the while will skip one character.

Also if you have 16bit per channel instead of

putc(r,ausgabe);
putc(l,ausgabe);

do

putc((char) ( r       & 0xff),ausgabe);
putc((char) ((r >> 8) & 0xff),ausgabe);
putc((char) ( l       & 0xff),ausgabe);
putc((char) ((l >> 8) & 0xff),ausgabe);

Possibly you want a different endianness then just swaplines 1 and 2 and lines 3 and 4

4
  • This is an original: i.imgur.com/xdpTMdk.jpg and this is what your code created out of it i.imgur.com/EdmSzJV.jpg Also the original is 663kb in size, the modified one 1,29MB Jul 11, 2013 at 21:30
  • Btw. are you aware that the getc in the while statement will skip one character?
    – LostBoy
    Jul 11, 2013 at 21:44
  • That's the solution. If you put this into your main reply, others will see. Jul 11, 2013 at 21:52
  • Is your input file 16bit per channel or 16bit in total? If it's per channel then you will need the double character output per channel, otherwise your bitshifting during the read in is wrong as you are accumulating two 8bit chars into one channel.
    – LostBoy
    Jul 11, 2013 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.