WAV file reading wrong data

Hello

I'm new here. I have been experimenting with a program that loads WAV file and tries to read 1s and 0s from audio file (binary coded information). So far I got working code, but the problem is that I'm not getting correct data. I do get somewhat similar data. The problem is that I get data that amplifies low amplitudes, so instead some noise and binary data I get lots of noise and hard to recognize binary data. My code is this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <iostream>
#include <math.h> 

using namespace std;

// An unsigned char can store 1 Bytes (8bits) of data (0-255)
typedef unsigned char BYTE;

// Get the size of a file
long getFileSize(FILE *file)
{
	long lCurPos, lEndPos;
	lCurPos = ftell(file);
	fseek(file, 0, 2);
	lEndPos = ftell(file);
	fseek(file, lCurPos, 0);
	return lEndPos;
}

int main()
{
	const char *filePath = "t2.wav";
	BYTE *fileBuf;			// Pointer to our buffered data
	//BYTE *amp;
	FILE *file = NULL;		// File pointer
	FILE *data;
	FILE *bin;

	if ((file = fopen(filePath, "rb")) == NULL)
		cout << "Could not open specified file" << endl;
	else
		cout << "File opened successfully" << endl;

	long fileSize = getFileSize(file);

	fileBuf = new BYTE[fileSize];
	//amp = new BYTE[fileSize];

	fread(fileBuf, fileSize, 1, file);
	data = fopen("data.txt", "w");
	bin = fopen("bin.txt", "w");

	for (int i = 44; i <20000; i+=1){
		int bit = 0;
		int sum = 0;
		if (fileBuf[i]>251){
			sum = sum + 1;
			//printf("%d", sum);
			if (sum>20)
				fprintf(bin, "1");
			else
				fprintf(bin, "0");
		}
		//int amp = fileBuf[i];
		int amp =fileBuf[i];
			
		//printf("%d ",amp);
		fprintf(data,"%i\n", amp);
		
	}
	cin.get();
	delete[]fileBuf;
	fclose(data);
	fclose(file);   // Almost forgot this 
	fclose(bin);
	cout << "Done" << endl;
	return 0;


}


This is wav file in binary viewer (after header)

0030 0032
0030 0035 0032 0032 0035 003A 0048 004A
004C 0049 004A 0051 0049 0043 0036 0038

and translated to decimal

48 50
48 53 50 50 53 58 72 74
76 73 74 81 73 67 54 56


and this is output from "wavosaur" program (and this one is correct)
0.001465
0.001526
0.001465
0.001617
0.001526
0.001526
0.001617
0.001770
0.002197
0.002258
0.002319
0.002228
0.002258
0.002472
0.002228
0.002045
0.001648
0.001709

You can see it's not the same. I do get the same data like I do from binary viewer, but I don't understand how they are translated to amplitude value. I'm sure I'm decoding it the wrong way - can you help me? Where is the catch?
So this is not really question about c++, but about wav file structure and reading data.
The audio is 16 bit, which means that each sample is represented by a 16 bit integer. The max amplitudes are the min/max values that can be stored in a 16 bit integer, -32768, 32767.

Wavosaur converts the values to float or double for processing. In floating point form, the max amplitudes are -1.0,1.0.

To convert from the integer representation to floating point representation, divide by 32768.

48/32768.0 = 0.00146484375
Last edited on
Well you certainly are right! I forgot to load 2 bytes. I now get right waveform. I only had to substract 32768 for values above 32768 and add 32768 for values under 32768. I don't know why. Never mind, as long as it works.
Topic archived. No new replies allowed.