0

I have to parse wav file (plain C) and validate some parameters for passing next functions. One of them is sample rate which is not so short to use bit positions for multiple test in if statement:

#define     WAV_ALLOWED_SAMPLE_RATE         (48000)

    if(wavFile.Header.SampleRate != WAV_ALLOWED_SAMPLE_RATE)    /* Sample Rate */
                        return WAV_HDR_CHUNK_FMT_INVALID_SAMPLE_RATE;

There is a simple way for testing multiple values that I want to pre-define at design time? Something like this:

#define     WAV_ALLOWED_SAMPLE_RATE         (11025 || 22050 || 44100 || 48000)

Maybe 2,3 or 4 values, depending on project needs.

Thanks in advance,

4
  • 3
    Put those values in an array and run a loop over it to find a match. Commented Jul 31, 2016 at 6:26
  • What is your question? Do you use a specific API for reading WAV files? Into what format do you read the sample rate from header? What bo you mean with "sample rate which is not so short to use bit positions for multiple test in if statement"? Commented Jul 31, 2016 at 6:26
  • 1
    Create a function to validate that the sample rate is valid, and have it loop over the values in an array of valid rates as suggested by @SameerNaik. Then call that function with the sample rate that you read from the file to see whether the number is OK. Commented Jul 31, 2016 at 6:30
  • I thought so, but I was also looking for a trick :) Commented Jul 31, 2016 at 6:32

2 Answers 2

2

In Wave Format Standard sampling rate is a 32-bit integer. So, if you read it into an int, you can easily use if statements and integer equality comparison. The simplest statement, based on your input:

int R = wavFile.Header.SampleRate;

if ( (R != 48000) && (R != 44100) && (R != 22050) && (R != 11025) )
    return WAV_HDR_CHUNK_FMT_INVALID_SAMPLE_RATE;
Sign up to request clarification or add additional context in comments.

2 Comments

Depending on sample rate, I have to use a multiple crystal quartz on microcontroller, so that's why I like to #define allowed at compile time. Some values allow (together) multiple sample rates, others don't. Anyhow, seems that array search will be the best, thanks very much.
Thanks for your reply, perhaps taking a look at how some audio file APIs enumerate sampling rates can also be helpful link… Anyway, if you have hardware constraints, it's a good starting point.
0
#define     WAV_ALLOWED_SAMPLE_RATE         (11025 || 22050 || 44100 || 48000)


    if(wavFile.Header.SampleRate != WAV_ALLOWED_SAMPLE_RATE)    /* Sample Rate */
                        return WAV_HDR_CHUNK_FMT_INVALID_SAMPLE_RATE;

this code will not go as you want. the value (11025 || 22050 || 44100 || 48000) will be interpreted as 1 and thus falsify the if condition...

You could write a simple function that checks for all 4 values, or do it inside your code as is

1 Comment

Write the function — don't do it inline. (You might make the function into a static inline function; that's a separate discussion.)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.