davidlamhauge
18th November 2012, 01:30
I try to extract information from a wav-file, and it works for most info.
Here is some of my code:
QFile audioFil(fileName);
if (audioFil.open(QIODevice::ReadOnly)){
QByteArray tst;
audioFil.seek(0);
tst = audioFil.read(4);
qDebug() << "RIFF..? : " << tst;
.....
audioFil.seek(20);
tst = audioFil.read(2);
audioFormat = GetLittleEndianInteger(tst,0);
qDebug() << "audioformat (1 = PCM): " << audioFormat;
audioFil.seek(22);
tst = audioFil.read(2);
channels = GetLittleEndianInteger(tst,0);
qDebug() << "Channels: " << channels;
audioFil.seek(24);
tst = audioFil.read(4);
sampleRate = GetLittleEndianInteger(tst,0);
qDebug() << "sampleRate: " << sampleRate;
audioFil.seek(28);
tst = audioFil.read(4);
byteRate = GetLittleEndianInteger(tst,0);
qDebug() << "ByteRate: " << byteRate;
audioFil.seek(32);
tst = audioFil.read(2);
blockAlign = GetLittleEndianInteger(tst,0);
qDebug() << "BlockAlign: " << blockAlign;
The GetLittleEndianInteger function is here
int MainWindow::GetLittleEndianInteger(QByteArray data, int startIndex)
{
return (data[startIndex + 3] << 24)
| (data[startIndex + 2] << 16)
| (data[startIndex + 1] << 8)
| data[startIndex];
}
84258426
As you can see from the two images, it extracts the correct values, except when it comes to SampleRate and ByteRate.
SampleRate should be 44100 (44 ac 00 00). How does it become -21436?
ByteRate should be 176400 (10 b1 02 00). How does it become -20208?
This has driven me crazy the last hours :( Any ideas?
Here is some of my code:
QFile audioFil(fileName);
if (audioFil.open(QIODevice::ReadOnly)){
QByteArray tst;
audioFil.seek(0);
tst = audioFil.read(4);
qDebug() << "RIFF..? : " << tst;
.....
audioFil.seek(20);
tst = audioFil.read(2);
audioFormat = GetLittleEndianInteger(tst,0);
qDebug() << "audioformat (1 = PCM): " << audioFormat;
audioFil.seek(22);
tst = audioFil.read(2);
channels = GetLittleEndianInteger(tst,0);
qDebug() << "Channels: " << channels;
audioFil.seek(24);
tst = audioFil.read(4);
sampleRate = GetLittleEndianInteger(tst,0);
qDebug() << "sampleRate: " << sampleRate;
audioFil.seek(28);
tst = audioFil.read(4);
byteRate = GetLittleEndianInteger(tst,0);
qDebug() << "ByteRate: " << byteRate;
audioFil.seek(32);
tst = audioFil.read(2);
blockAlign = GetLittleEndianInteger(tst,0);
qDebug() << "BlockAlign: " << blockAlign;
The GetLittleEndianInteger function is here
int MainWindow::GetLittleEndianInteger(QByteArray data, int startIndex)
{
return (data[startIndex + 3] << 24)
| (data[startIndex + 2] << 16)
| (data[startIndex + 1] << 8)
| data[startIndex];
}
84258426
As you can see from the two images, it extracts the correct values, except when it comes to SampleRate and ByteRate.
SampleRate should be 44100 (44 ac 00 00). How does it become -21436?
ByteRate should be 176400 (10 b1 02 00). How does it become -20208?
This has driven me crazy the last hours :( Any ideas?