Results 1 to 6 of 6

Thread: Error in reading wav-file header

  1. #1
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    28
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Error in reading wav-file header

    I try to extract information from a wav-file, and it works for most info.
    Here is some of my code:
    Qt Code:
    1. QFile audioFil(fileName);
    2. if (audioFil.open(QIODevice::ReadOnly)){
    3. audioFil.seek(0);
    4. tst = audioFil.read(4);
    5. qDebug() << "RIFF..? : " << tst;
    6. .....
    7. audioFil.seek(20);
    8. tst = audioFil.read(2);
    9. audioFormat = GetLittleEndianInteger(tst,0);
    10. qDebug() << "audioformat (1 = PCM): " << audioFormat;
    11.  
    12. audioFil.seek(22);
    13. tst = audioFil.read(2);
    14. channels = GetLittleEndianInteger(tst,0);
    15. qDebug() << "Channels: " << channels;
    16.  
    17. audioFil.seek(24);
    18. tst = audioFil.read(4);
    19. sampleRate = GetLittleEndianInteger(tst,0);
    20. qDebug() << "sampleRate: " << sampleRate;
    21.  
    22. audioFil.seek(28);
    23. tst = audioFil.read(4);
    24. byteRate = GetLittleEndianInteger(tst,0);
    25. qDebug() << "ByteRate: " << byteRate;
    26.  
    27. audioFil.seek(32);
    28. tst = audioFil.read(2);
    29. blockAlign = GetLittleEndianInteger(tst,0);
    30. qDebug() << "BlockAlign: " << blockAlign;
    To copy to clipboard, switch view to plain text mode 
    The GetLittleEndianInteger function is here
    Qt Code:
    1. int MainWindow::GetLittleEndianInteger(QByteArray data, int startIndex)
    2. {
    3. return (data[startIndex + 3] << 24)
    4. | (data[startIndex + 2] << 16)
    5. | (data[startIndex + 1] << 8)
    6. | data[startIndex];
    7. }
    To copy to clipboard, switch view to plain text mode 

    debugMsg.jpgheader.jpg
    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Error in reading wav-file header

    Why don't you just use qFromLittleEndian()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    28
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Error in reading wav-file header

    I didn't know about qFromLittleEndian().
    I have read about, but can't make it work. Could you give me a hint on how to use in my context?

    And - it still puzzles me that the the function GetLittleEndianInteger returns a negative samplerate.
    I tried to debug, and the value of my QByteArray tst, shows that 0xAC is read as '-84 / 172' (see image)
    debug_qbytearray.jpg
    Why?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Error in reading wav-file header

    Quote Originally Posted by davidlamhauge View Post
    I didn't know about qFromLittleEndian().
    I have read about, but can't make it work. Could you give me a hint on how to use in my context?
    Read four bytes, cast them to uchar* and pass to qFromLittleEndian().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    28
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Error in reading wav-file header

    Quote Originally Posted by wysota View Post
    Read four bytes, cast them to uchar* and pass to qFromLittleEndian().
    Is it possible to elaborate a little?
    I've tried many combinations, and read many posts on the net, but...
    Is it like this you mean?
    Qt Code:
    1. audioFil.seek(24);
    2. tst = audioFil.read(4);
    3. tst[0] = (uchar)tst[0];
    4. tst[1] = (uchar)tst[1];
    5. tst[2] = (uchar)tst[2];
    6. tst[3] = (uchar)tst[3];
    7. sampleRate = qFromLittleEndian<quint32>(*tst);
    8. // sampleRate = GetLittleEndianInteger(tst,0);
    9. qDebug() << "sampleRate: " << sampleRate;
    To copy to clipboard, switch view to plain text mode 
    The debug-output is 'sampleRate: 68'. 68 = 'D' = tst[0].
    I don't really get it. Hence 'Beginner'...

    EDITED:
    when I read my reply it dawned a little. This works:
    Qt Code:
    1. uchar* cc;
    2. audioFil.seek(24);
    3. tst = audioFil.read(4);
    4. cc[0] = tst[0];
    5. cc[1] = tst[1];
    6. cc[2] = tst[2];
    7. cc[3] = tst[3];
    8. sampleRate = qFromLittleEndian<qint32>(cc);
    9. // sampleRate = GetLittleEndianInteger(tst,0);
    10. qDebug() << "sampleRate: " << sampleRate;
    To copy to clipboard, switch view to plain text mode 
    ...but could it be done more effectively?
    Last edited by davidlamhauge; 18th November 2012 at 15:14.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Error in reading wav-file header

    Qt Code:
    1. QByteArray ba = audoFil.read(4);
    2. qint32 sampleRate = qFromLittleEndian((const uchar*)ba.constData());
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    davidlamhauge (18th November 2012)

Similar Threads

  1. Migrating content from header file to source file
    By ehnuh in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2012, 18:00
  2. unknown error reading svg file
    By RickF in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2010, 03:12
  3. Reading TCP datagram header by Qt
    By Kill3rReaper in forum Qt Programming
    Replies: 11
    Last Post: 8th June 2010, 13:49
  4. Fatal IO error with QGLWidget and reading a file
    By mcn in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2010, 16:21
  5. Replies: 2
    Last Post: 8th September 2006, 12:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.