Question: Show me the steps to solve Assignment Part 2 In the code above, we worked with text files. To the computer though, text is just
Show me the steps to solve Assignment Part
In the code above, we worked with text files. To the computer though, text is just bytes. So there is no reason we can't use the same techniques to work with a binary file. We are going to work with wav files for this project. A wav file is a type of uncompressed audio file.
Microphones take measurements of audio called samples. For an audio file, it is not uncommon for their to be samples per second or even more! These files include both metadata and samples. Like most other file types, wav files are very well structured. For instance, we know the following:
The first four bytes of the file will be the ASCII characters 'RIFF'.
The next four bytes are the size of the file with bytes subtracted. This value is stored as a bit integer.
The third set of four bytes are the ASCII characters 'WAVE'.
Bytes at the nd and rd position are for the number of channels, stored as a bit integer. If you have stereo sound, there will be two channels a left and a right channel Mono sound will have only one channel.
Let's see if we can make sense of a sample file. I've created a file called backwards.wav. Let's inspect the first bytes of it:
s
file sharedaudiobackwardswav
echo
hexdump Cn sharedaudiobackwardswav
sharedaudiobackwardswav: RIFF littleendian data, WAVE audio, Microsoft PCM bit, mono Hz
ad RIFFvWAVEfmt
ac DX
data.v
c
I just output the first bytes the metadata of the file; there are way more bytes in the file that make up the samples. We can see the 'RIFF' and 'WAVE' parts already. If we look closely at the output above, you will notice I ran the file command first. This command tells us somethings about the file. For instance, it shows that the file is mono channel and has a sample rate of Hz It isn't immediately obvious from the output, but these values are stored within the metadata of the file. For instance, if we look at the bytes at location and we get On this system, this bit byte value equals our decimal value of Since we know that bytes and are used to store the number of channels, we have just verified that our file is indeed mono.
Side note: How do we get from Well, this system is what we call little endian. This means that the least significant bit of a number comes first. If we were writing our decimal numbers in a little endian way, we would write the number as So really, the number we have stored for the number of channels is
Let's now look at the four bytes starting at location These bytes store the sample rate. We should see here. I see the bytes ac Since this is a little endian system this is really ac If you find an online hex converter and type in ac you will get as the converted value. Very cool!
keyboardarrowdown
Working with data
If we use the code we've already written, we can load the sample wave file into a byte array. We don't need to make any changes to it
Our readdata function simply pulls all the bytes off the disk and puts them into memory somewhere. We can think of the area of memory as an array of bytes. That's really all a char array is in C char and byte are basically equivalent. The difference is that we normally think of a char as something visible but it doesn't have to be For instance, the newline and tab characters, the system bell, the escape key, all of these are stored as a byte. Just because we can't see them doesn't mean the computer doesn't need a way to store their values.
So we have a big chunk of bytes. We know that some of these bytes are single characters and some are values stored as multiple byte sequences. We just need to interpret them the correct way. Let's try to print out the values of the first four bytes of the file. We know those should be the characters 'RIFF':
writefile reversal.c
#include
#include
#include "filetools.h
int mainint argc, char argv
unsigned char data;
sizet filesize readdatasharedaudiobackwardswav", &data;
printfSize: lu bytes.
filesize;
printfFirst four bytes: c c c c data data data data;
printf
;
return ;
Size: bytes.
First four bytes: RIFF
But what if we had interpreted those bytes differently? What if we wanted to see their hex values? We can tell C to change how it interprets the bytes by changing the print specifier. In the code above, we used the c to print a character. Let's modify it to use x that will print the hex value:
writefile reversal.c
#include
#include
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
