Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When you call malloc( ), the operating system sets aside \"generic\" memory and

ID: 3915840 • Letter: W

Question

When you call malloc( ), the operating system sets aside "generic" memory and returns a pointer to that memory; the pointer is of type void *. You must cast that "generic" memory to the data type of your choice before using it. Double *pd; pd=(double *)malloc(1000); This brings to your attention that a memory address pointing to a particular data type can be cast to another pointer pointing to a different data type. Think about what that means. If you have the memory address of the 0th of 4 bytes stored in int * pi, then using pointer pi means that you can read or write an int at that location. *pi=27; But the 4 bytes accessed by pi could just as easily be used to store e.g. a float variable, provided you could point a float pointer to that same memory address. You can do exactly that using an explicit cast from a pointer of type int to a pointer of type float. float *pf. pf=(float *)pi; Now you can read/write a float value to those same 4 bytes. *pf=3.1415; In the project below you will malloc( ) a 44 byte 1-D array to hold the header of an audio file of type WAV. Then you will use pointers to different data types to correctly interpret the various bytes in the header either as unsigned int, short int, or char.

Audio file formats are used to store audio data on a computer system, mp3 player, DVD, etc. There are many types of audio file formats. These include the MP3, WAV, AIFF, AU, RA formats. Each format has certain strengths and weaknesses. The WAV format, designed by Microsoft, will be used for your project. Files stored in WAV format must be stored in a file named with the file_extension *.wav . The WAV file format is a subset of Microsoft's RIFF specification for the storage of multimedia files. A RIFF file starts out with a file header followed by a sequence of data chunks as follows. A WAV file is typically just a RIFF file which consists of three chunks – (1) a chunk indicating the the file is a RIFF WAV file. (2) a header consisting of a "fmt " chunk that specifies the data format e.g. stereo, 44100 samples per sec, etc.. and (3) a "data" chunk containing the actual sample data stored in the format that has been indicated in the “fmt “ chunk. A WAV PCM file is one that is stored in uncompressed format. PCM stands for pulse-code-modulation which here simply refers to uncompressed mode.

Write a program to prompt the user for the path/filename of an audio WAV file 1) Use a subroutine to check to see that the extension on the file entered by the user (i.e. the last characters of the path/filename beginning at the period) is ".wav", ".WAV", "waV", ".wAv", etc. (because any combination of upper- and lower-case ofw a v is typically valid) 2) If the file extension entered by the user does not indicate a wav file, notify the user and prompt for a new path/filename If the file extension is valid, try to open the file in mode "rb" using fopen(). If the path/filename does not exist, notify the user and prompt for a new path/filename. Continue until the user enters the path/filename for a valid wav file 3) The header for the wav file is 44 bytes long; declare a char pointer called header char * header and malloc() 44 bytes to hold the header. Use fread() to read-in ALL of the header information into the malloc'd header using a single fread() statement. 4) Declare pointers of the appropriate data type for each piece of information in the wav header For example, you might use char * chunkid unsigned int * chunksize; char * format char * subchunklid; unsigned int * subchunkl size; unsigned short int * audioformat; etc 5) Point each of the pointers in (4) to the appropriate locations in the 1-D array header. Make sure to include an explicit cast from the char pointer location in header to a pointer of the For example chunkid-header; chunksize-(int *)(header +4); I/ Chunksize is information in 4-bytes denoting a non-negative size of the chunk. appropriate data type for each particular piece of information in the header // both are char pointers, so no cast is necessary // chunksize is an int pointer, not a char pointer like header format-header+8; // both are char pointers, so no cast is necessary subchunklid-header+12; //both are char pointers, so no cast is necessary subchunklsize-(int *)(header+16); 6) Print out ALL ofthe audio file characteristics to the screen using the pointers you declared in (4). Dereference each pointer to the value to which it points For example, printf(nn%0%0%c%cin', *chunkid,"(chunkid+1), * (chunkid-2), *(chunkid-3)); would print out 4 characters to the screen. It should show "RIFF" If you try to use a string format for this, there is a problem trying to restrict the printf0 statement to no more than 4 characters, because header[4] is not equal to ^0' the string termination character

Explanation / Answer

//Writing only the 1st Question as per the Chegg Q&A Guidelines of "Multi-Part Questions" since student didnt mention for a specific question to answer

//"Questions with >1 question: answer the first full question" so answering only the 1st Question.

//1) Answer for 1st Question.

//This program is a subroutine function to check the extension of a given file is in WAV format or not.

// if provided filename is in WAV format then this will return 1 else return 0 and it promts untill the provided filename was in WAV format

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int check_extension(const char *filename)
{
//strrchr returns a last pointer occurrence of character as we require extension with .
char *extension = strrchr(filename, '.')+1;
char a[10];
int i=0;
//Taking all the extension Characters in lowercase as we accept the file in .wav or .WAV or .WaV
while(*extension!='')
{
a[i]=tolower(*extension);
extension++;
i++;
}
char *ex="wav";
//Comparing whether the extension is in wav format.
if(strcmp(a,ex)==0)
{
for(i=0;a[i]!='';i++)
a[i]='';
printf("");
return 1;
}
else
{
for(i=0;a[i]!='';i++)
a[i]='';
printf("");
return 0;
}
}
void main()
{
//Assuming Maximum Length of filename is 100 Characters
char filename[100];
//Reading the Filename
printf("Enter Filename:");
scanf("%s", &filename);
//Checking till the file contains .wav format
while(check_extension(filename)==0)
{
printf("Enter the correct Filename in WAV format:");
scanf("%s", &filename);
}
printf("Correct File is %s",filename);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote