1. Engineering
  2. Computer Science
  3. wavc clanguage this will change the file by reading all...

Question: wavc clanguage this will change the file by reading all...

Question details

WAV.c

C-language

This will change the file, by reading all the samples, reversing them, and then writing them all back out again. It should work on 8- or 16-bit, mono or stereo sounds.

Here’s what it should do

Open the file and read the header
You’ll have to open it for reading AND writing
Check that it really is a WAV file, like (data_size / block_alignment)
Calculate the data length in samples, like before
Allocate an array of the right type and that length (see below)
Use a single fread call to read the whole array from the file
After you read the header, the file is already in the right place to read the data
Reverse the array’s values
That is, swap the first and last values; then the second and second-to-last; etc.
Seek back to the beginning of the file plus sizeof(WAVHeader)
Use a single fwrite call to write the whole array back to the file
Don’t forget to close the file!
How samples are stored:
For mono (1-channel) sounds, the samples are simply an array. If bits_per_sample is 8, it’s an array of 8-bit integers. If it’s 16, it’s an array of 16-bit integers.

For stereo (2-channel) sounds, each sample is stored as a pair of 8- or 16-bit integers. The first number in the pair is the left channel’s sample, and the second number is the right channel’s.

Since your reversing function doesn’t have to treat the stereo channels individually, you can treat the data array as follows:

If bits_per_sample is 8 and number_of_channels is 1:
treat it as an array of 8-bit integers.
If bits_per_sample is 16 and number_of_channels is 1; OR
If bits_per_sample is 8 and number_of_channels is 2:
treat it as an array of 16-bit integers.
If bits_per_sample is 16 and number_of_channels is 2:
treat it as an array of 32-bit integers.
Since C doesn’t have generics or function overloading, you’ll have to repeat this code three times, but with different types.

using this struct Positions

Sample Value   Description


0 - 3   "RIFF"   Marks the file as a riff file. Characters are each 1 byte long.
4 - 7   File size (integer)   Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you'd fill this in after creation.
8 -11   "WAVE"   File Type Header. For our purposes, it always equals "WAVE".
12-15   "fmt "   Format chunk marker. Includes trailing null
16-19   16   Length of format data as listed above
20-21   1   Type of format (1 is PCM) - 2 byte integer
22-23   2   Number of Channels - 2 byte integer
24-27   44100   Sample Rate - 32 byte integer. Common values are 44100 (CD), 48000 (DAT). Sample Rate = Number of Samples per second, or Hertz.
28-31   176400   Bytes per second = (Sample Rate * BitsPerSample * Channels) / 8.
32-33   4   Block align = (BitsPerSample * Channels) / 8.1 - 8 bit mono2 - 8 bit stereo/16 bit mono4 - 16 bit stereo
34-35   16   Bits per sample
36-39   "data"   "data" chunk header. Marks the beginning of the data section.
40-43   File size (data)   Size of the data section.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution