The demand of the project is to write a program in C which reads data of sound only with getchar
from the input according to wav format/template.
The program must check if the given data is correct. When the first mistake is detected an appropriate message appears and the program stops. The use of arrays, functions, pointers and mathematical libraries is forbidden. Also it checks if we have insufficient data or bad file size.
The wave file format:
http://tiny.systems/software/soundProgrammer/WavFormatDocs.pdf http://www.topherlee.com/software/pcm-tut-wavformat.html
example I thought this code for reading numbers:
while ( (ch = getchar()) != '\n')
{
if (ch >= '0' && ch <= '9')
{
bytes_sec = 10 * bytes_sec + (ch - '0');
}
}
fprintf(stderr, "bytes/sec:%d\n", bytes_sec);
but it isn't correct because we want the number to be saved as one byte.
and for characters I thought this code:
flag = 0;
fores = 0;
while ( ( (ch=getchar()) !='\n') && (flag==0) ) {
fores = fores + 1;
if (fores == 1) {
if (ch != 'R')
{
flag = 1;
}
}
else if (fores == 2) {
if (ch != 'I')
{
flag = 1;
}
}
else if (fores == 3) {
if (ch!='F') {
flag = 1;
}
}
else {
if ((fores != 4) || (ch != 'F')) {
flag = 1;
}
}
}
if (flag == 1) {
fprintf(stderr, "Error! \"RIFF\" not found\n");
return 0;
}
Also I didn't understand in which form (binary, hex, decimal) the data is given (I know that data in wav format is in binary but I still can't understand what exactly is given as data and how - form and if it is given as a single piece or separately).
Finally the fact that we can not enter in the terminal data that do not correspond to printable characters really confuses me.
Let's say we have the contents of a legal wav file (hex):
0000000 52 49 46 46 85 00 00 00 57 41 56 45 66 6d 74 20
0000020 10 00 00 00 01 00 01 00 44 ac 00 00 88 58 01 00
0000040 02 00 10 00 64 61 74 61 58 00 00 00 00 00 d4 07
0000060 a1 0f 5e 17 04 1f 8a 26 ea 2d 1c 35 18 3c d7 42
0000100 54 49 86 4f 69 55 f6 5a 27 60 f8 64 63 69 64 6d
0000120 f7 70 18 74 c5 76 fa 78 b6 7a f6 7b b9 7c ff 7c
0000140 c8 7c 12 7c e0 7a 33 79 0b 77 6c 74 58 71 d1 6d
0000160 dd 69 7e 65 b8 60 92 5b 0f 56 36 50 0c 4a 97 43
0000200 df 3c ea 35 45 78 74 72 61 44 61 74 61
0000215
Ι thought to convert it from hex to decimal and then use a notepad to create a data file in order to use the command fopen(fname, "rb")
but then I will have to use pointer which is forbidden.
So I still haven't understand how the program will get prices/values at the input.
Also after the suggestions of @AndreasWenzel I came up with this code for little-endian (note: the condition in while
is incorrect, it's been chosen in order to be easy to check in a C compiler):
#include <stdio.h>
int ch, sample_rate, help, fores, sum, i;
int main()
{
fores = 0;
sample_rate = 0;
while ( (ch = getchar()) != '\n' )
{
help = (ch - '0');
fprintf(stderr, "help:%d\n", help);
if (help == 1)
{
sum = 1;
for (i=1 ; i<=fores ; i++)
{
sum = sum*2;
}
sample_rate = sample_rate + (sum);
fprintf(stderr, "sample:%d\n", sample_rate);
}
fores = fores + 1;
}
fprintf(stderr, "sample rate:%d\n", sample_rate);
}
If we have as input the 10100100(binary)=164(decimal) it will print 37(decimal). Is it correct?