Thread: WAV file handling and filtering

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    5

    WAV file handling and filtering

    Hi guys .. new to this forum .. hoping you can help me out ..

    I got this issue to tackle which consists of using an FIR LPF filter to filter a wav file using C programming.

    Till now I managed to tackle the filter part following this link:
    Implementation of FIR Filtering in C (Part 1) | Shawn's DSP Tutorials
    (I have my own filter co-efficients which have been produced using FDAtool on MATLAB and some other minor changes have been applied to match my requirements)

    Now, the link shown does not compensate for files having a header. I have three wav files which need to be filtered. Thus I need to first extract the data from the header .. this was done using RIFFpad. Now that I have the data I don't know exactly how to tackle this issue.

    Info from RIFFpad:

    fmt -
    Offset=20
    ID=fmt
    dwSize = 16
    wFormatTag = 1
    nChannel = 1 (Mono)
    nSamplesPerSec = 16000
    nAvgBytesPerSec = 32000
    nBlockAlign = 2
    wBitsPerSample = 16

    Data - (len=537440, off=44)



    I've got this hint to start with:


    1. Each audio datasample inside the wav_files can be made up of a number of bytes and these bytes arestored in the little-endian order. Inside your program you have to change this to a big-endian order.





    Any help?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    "Any help?" is not a question.
    How To Ask Questions The Smart Way

    My first suggestion would be to look up "little endian" and "big endian", so you can see how the two are related.
    Then you might learn how to convert from one to the other (it's dead easy BTW).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    Would like to thank you for providing me with a link on how to ask questions ... maybe it could come in handy in the future.

    If it's dead easy for you, this is a whole new thing for me .. and practically I asked "Any help?" since I need at least to know what to look for to start tackling the issue.

    I already searched about the relationship between little and big endian ..
    I know that: In big endian the most significant byte is stored at the smallest address.
    and that: In little endian, the least significant byte is stored at the smallest address.
    but can't figure out how to "fetch" and store them and finally convert them properly.

    Hope you get my concern now. Hopefully I get better help so that I can proceed.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you've gotten to here without being able to figure out "biggest" and "smallest" we've got other problems. What have you tried and how doesn't it work?

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    Sorry for taking long to reply .. was busy with other things ..
    Well to be honest with you I'm totally new to this wav file thing .. as mentioned in the first post, I followed that example and amended it to mach my filter requirements. The filter works fine, but only works for .pcm files (headerless files)

    I followed these links, but being new to wav file and practically green in C programming I'm not able to understand everything to make a functional code:
    Big and Little Endian
    https://ccrma.stanford.edu/courses/4...ts/WaveFormat/
    Wave File Format - The Sonic Spot

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    
    unsigned short be2le(unsigned short x) {
      // do something
      return x;
    }
    
    int main() {
      unsigned short int a = 0x1234;
      unsigned short int b = be2le(a);
      printf("%hx %hx\n", a, b );
      return 0;
    }
    Take this and make it print
    1234 3412
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    done:
    Code:
    #include <stdio.h>
    
    
    unsignedshort be2le(unsignedshort x) {
    
    
        x = ( (((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00) );
    
        return x;
    }
    
    
    int main() {
    unsignedshortint a = 0x1234;
    unsignedshortint b = be2le(a);
        printf("%hx %hx\n", a, b );
        return 0;
    }
    

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I've got this hint to start with:
    Each audio datasample inside the wav_files can be made up of a number of bytes and these bytes arestored in the little-endian order. Inside your program you have to change this to a big-endian order.
    OK, now start picking your way through the wav file one byte at a time. Use the format specifications to accumulate 2 and 4-byte integers, and use your new knowledge of endian conversion to do what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2013
    Posts
    5
    so to make sure I got you right:

    Now i need to store my wave file as single bytes right? Maybe using an array of "?" size?
    Next is using the part code you wrote and implementing it for this array?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help about file handling.
    By tgeandpudding in forum C Programming
    Replies: 3
    Last Post: 03-24-2013, 05:32 AM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  3. help needed - filtering a data file (with strings?)
    By Galligan in forum C Programming
    Replies: 1
    Last Post: 11-18-2002, 10:20 AM
  4. File Handling?!?
    By Twiggy in forum C Programming
    Replies: 1
    Last Post: 10-23-2001, 11:43 AM

Tags for this Thread