Read 20000 .wav data

I try to read a .wav file and output 20000 data, then apply fft, but I got some problems. (realize the function 'wavread' of Matlab in c++)
Here is my code.

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 <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream ifs ("/Users/LilianaVess/Desktop/daremoshiranai.wav", ios_base::in);
    if(ifs.is_open())
    {
        char First_Chunk_ID[5] ={NULL} ;  
        ifs.read(First_Chunk_ID,4);

        long Chunk_Data_Size; 
        ifs.read( reinterpret_cast<char*>(&Chunk_Data_Size), sizeof(4) );

        char RIFF_Type[5] ={NULL} ;
              ifs.read(RIFF_Type,4);

        char Wave_Chunk_ID[5] = {NULL} ;
        ifs.read(Wave_Chunk_ID,4);

    long Wave_Chunck_Size;
    ifs.read( reinterpret_cast<char*>(&Wave_Chunck_Size), sizeof(4) );

    char Audio_Format[3]={NULL};
    ifs.read(Audio_Format,2);

    char Num_Channel[3]={NULL};
    ifs.read(Num_Channel,2);

    char Sample_Rate[5]={NULL};
    ifs.read(Sample_Rate,4);

    char Avg_bps[5]={NULL};
    ifs.read(Avg_bps,4);

    char Block_Align[3]={NULL};
    ifs.read(Block_Align,2);

    char Significant_Bits_Sample[3]={NULL};
    ifs.read(Significant_Bits_Sample,2);

    char Third_Chunk_ID[5] = {NULL} ;
    ifs.read(Third_Chunk_ID, 4);

    long Data_Size; 
    ifs.read( reinterpret_cast<char*>(&Data_Size), sizeof(4) );

    char Data[20000];  
    ifs.read(Data,20000);  

    cout << "First_Chunk_ID : " << First_Chunk_ID << endl
    << "Chunk_Data_Size : " << Chunk_Data_Size << endl
    << "RIFF_Type : " << RIFF_Type<< endl
    << "Wave_Chunk_ID: " <<  Wave_Chunk_ID<< endl
    << "Wave_Chunck_Size : " << Wave_Chunck_Size << endl
    << "Audio_Format : " << Audio_Format<< endl
    << "Num_Channel : " << Num_Channel << endl
    << "Sample_Rate : " << Sample_Rate << endl
    << "Avg_bps :" << Avg_bps << endl
    << "Block_Align :" << Block_Align << endl
    << "Significant_Bits_Sample :" << Significant_Bits_Sample << endl
    << "Third_Chunk_ID : " << Third_Chunk_ID << endl
    << " Data_Size " <<  Data_Size << endl
    << "Data gcount : " << ifs.gcount() << endl;

    }
}




And I got:
First_Chunk_ID : RIFF
Chunk_Data_Size : 43078112
RIFF_Type : WAVE
Wave_Chunk_ID: fmt
Wave_Chunck_Size : 16
Audio_Format :
Num_Channel :
Sample_Rate : D?
Avg_bps :?
Block_Align :
Significant_Bits_Sample :
Third_Chunk_ID : data
Data_Size 43078076
Data gcount : 20000

So I can't read the information between Audio_Format and Significant_Bits_Sample. But I don't know what's wrong with my code.
And I don't know how to read 20000 data in wav file from the 45th bytes (according to a document which present the structure of wav, the real data begin at the 45th bytes) as a beginner in c++ programming...
Can you do some correction in my code?
Thanks a lot!

Last edited on
Since it's binary data better add ios_base::binary on line 8

lines 15/24/48: sizeof(4)?
In fact, the code was sizeof(long), someone helped me to write this. But when I use sizeof(long), the program returns me some stuff weird.
I changed "long" to "4" accidently, but it returns me something correct before "audio_format" so I kept it...but I really don't know what should I write for the lecture of file_size or data_size...
Thank you for answering me! :)
But when I use sizeof(long), the program returns me some stuff weird.
The reason is that sizeof(long) is not 4 but might be 8 bytes. What the result is of copying the 4 bytes (if sizeof(4) -> 4) to a long depends entirely on the byte order.

Does this
Chunk_Data_Size : 43078112
look correct?

So I took a look at the format:
Size:
B 8-bit Byte
W 16-bit Word (numeric byte order: lo->hi)
DW 32-bit DoubleWord (numeric byte order: lo->hi)

DW -> 'RIFF'
DW -> nDataBytes
DW -> 'WAVE'
DW -> 'fmt'
DW -> nDataBytes
W -> formatTag -> coding: PCM = 1
W -> nChannels
DW -> nSamplesPerSec
DW -> nAvgBytesPerSec
W -> nBlockAlign
W -> nBitsPerSample
DW -> 'data'
DW -> nDataBytes
...<sample><sample>...
So: everything after 'fmt' is numeric. For DW use uint32 and for W use uint16.
Topic archived. No new replies allowed.