WAV file header


#1

I’ve just started using openframeworks and I’m trying to interpret a .wav file header byte-by-byte. I’m getting some really wierd output for my little-endian conversions!

The code - I thought the "(chunk1 << 8) | chunk2 would give me a nice unsigned short, but the length comes out so wrong as does the other numerical data!

void ofApp::draw(){
ofSetHexColor(0x000000);
ofDrawRectangle(0,0,ofGetWidth(),ofGetHeight());

ofSetHexColor(0xaaaaff);

wordString = “”;
for(i=0; i<4; i++)
{
thisLetter = bufferN.getData()[i];
wordString += thisLetter;
}
thisX = xOffset + (int)(chW * 6);
thisY = lineH * 2;
bold.drawString(“Word1:”,xOffset,thisY);
font.drawString(wordString,thisX, thisY);

allBytes.resize(4);
for(i=0; i<4; i++)
{
allBytes[i] = bufferN.getData()[i + 4];
}
//wordVal = (byte1 << 8) | byte2;
//word32 = (wordVal << 16) | shortZeroes;
chunk1 = (allBytes[0] << 8) | allBytes[1];
if(debug == 1)
{
cout << "chunk1 = ";
cout << chunk1 << endl;
}
chunk2 = (allBytes[2] << 8) | allBytes[3];
if(debug == 1)
{
cout << "chunk2 = ";
cout << chunk2 << endl;
}
chunk32 = (chunk1 << 16) | chunk2;
if(debug == 1)
{
cout << "chunk32 = ";
cout << chunk32 << endl;
debug = 0;
}
thisY = lineH * 3;
bold.drawString(“Size:”,xOffset,thisY);
font.drawString(ofToString(chunk32),thisX,thisY);

wordString = “”;
for(i=8; i<12; i++)
{
thisLetter = bufferN.getData()[i];
wordString += thisLetter;
}
thisY = lineH * 4;
bold.drawString(“File:”,xOffset,thisY);
font.drawString(wordString,thisX,thisY);

allBytes[0] = bufferN.getData()[20]; allBytes[1] = bufferN.getData()[21];
allBytes[2] = bufferN.getData()[22]; allBytes[3] = bufferN.getData()[23];
chunk1 = (allBytes[0] << 8) | allBytes[1];
chunk2 = (allBytes[2] << 8) | allBytes[3];
thisY = lineH * 7;
bold.drawString(“Type:”,xOffset,thisY);
font.drawString(ofToString(chunk1),thisX,thisY);
thisY = lineH * 8;
bold.drawString(“Chns:”,xOffset,thisY);
font.drawString(ofToString(chunk2),thisX,thisY);

allBytes[0] = bufferN.getData()[24]; allBytes[1] = bufferN.getData()[25];
allBytes[2] = bufferN.getData()[26]; allBytes[3] = bufferN.getData()[27];
chunk1 = (allBytes[0] << 8) | allBytes[1];
chunk2 = (allBytes[2] << 8) | allBytes[3];
chunk32 = (chunk1 << 8) | chunk2;
thisY = lineH * 9;
bold.drawString("SR: ",xOffset,thisY);
font.drawString(ofToString(chunk32),thisX,thisY);

allBytes[0] = bufferN.getData()[28]; allBytes[1] = bufferN.getData()[29];
allBytes[2] = bufferN.getData()[30]; allBytes[3] = bufferN.getData()[31];
chunk1 = (allBytes[0] << 8) | allBytes[1];
chunk2 = (allBytes[2] << 8) | allBytes[3];
chunk32 = (chunk1 << 8) | chunk2;
thisY = lineH * 10;
bold.drawString(“SBC8:”,xOffset,thisY);
font.drawString(ofToString(chunk32),thisX,thisY);

allBytes[0] = bufferN.getData()[32]; allBytes[1] = bufferN.getData()[33];
allBytes[2] = bufferN.getData()[34]; allBytes[3] = bufferN.getData()[35];
chunk1 = (allBytes[0] << 8) | allBytes[1];
chunk2 = (allBytes[2] << 8) | allBytes[3];
thisY = lineH * 11;
bold.drawString("BC8: ",xOffset,thisY);
font.drawString(ofToString(chunk1),thisX,thisY);
thisY = lineH * 12;
bold.drawString(“Bits:”,xOffset,thisY);
font.drawString(ofToString(chunk2),thisX,thisY);

allBytes[0] = bufferN.getData()[36]; allBytes[1] = bufferN.getData()[37];
allBytes[2] = bufferN.getData()[38]; allBytes[3] = bufferN.getData()[39];
wordString = “”;
thisY = lineH * 13;
wordString += allBytes[0];
wordString += allBytes[1];
wordString += allBytes[2];
wordString += allBytes[3];
wordString += “:”;
bold.drawString(wordString,xOffset,thisY);

allBytes[0] = bufferN.getData()[40]; allBytes[1] = bufferN.getData()[41];
allBytes[2] = bufferN.getData()[42]; allBytes[3] = bufferN.getData()[43];
chunk1 = (allBytes[0] << 8) | allBytes[1];
chunk2 = (allBytes[2] << 8) | allBytes[3];
chunk32 = (chunk1 << 8) | chunk2;
font.drawString(ofToString(chunk32),thisX,thisY);
}

The output:
OF_WM3a

The wave file being interrogated is 16bit, 44100 sample rate, 1 second long, mono. allBytes is a vector of chars, and bufferN is the file.