Question
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
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.
Heres what it should do
Open the file and read the header Youll 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 arrays 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 Dont 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, its an array of 8-bit integers. If its 16, its 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 channels sample, and the second number is the right channels.
Since your reversing function doesnt 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 doesnt have generics or function overloading, youll 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started