-
Notifications
You must be signed in to change notification settings - Fork 3
/
WAV.h
128 lines (106 loc) · 3.21 KB
/
WAV.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef _H_WAV_
#define _H_WAV_
#include <stdlib.h>
#include <stdio.h>
//for types
#include <stdint.h>
#include <cstdlib>
#include <string.h>
#include <string>
#include <vector>
// Data Type is fixed as short
class WAV {
/*
* http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
* */
private:
uint32_t head_size=44;
bool non_pcm;
// 4bytes fixed size header infomation -> uint32_t
char riff_id[4]; // riff string
uint32_t riff_size; // overall size of fp in bytes
char wave_id[4]; // wave string
char fmt_id[4]; // fmt string with trailing null char
// | pcm | non-pcm
// fmt_size | 16 | 18
//
uint32_t fmt_size; // format chunk size 16,18,40
short
fmt_type; // format type 1-PCM 3-IEEE float 6- 8bit A law, 7- 8bit ,u law
unsigned short channels; // no of channel
uint32_t sample_rate; // SampleRate(blocks per second)
uint32_t byte_rate; // ByteRate = SampleRate * NumChannels * BitsPerSample/8
short block_align; // NumChannels * BitsPerSample/8
short bit_per_sample; // bits per sample, 8 - 8bits, 16-16bits etc
/* (if non -pcm )*/
uint32_t cb_size; //size of the extension
char fact_id[4];
uint32_t fact_size;
uint32_t dwSampleLength;
char data_id[4]; // DATA string or FLLR string
uint32_t data_size; // NumSamples * NumChannels * BitsPerSample/8 - size of
// the nex chunk that will be read
FILE *fp;
bool IsOpen;
char file_name[1024];
// For Input usage only
bool use_buf;
int frame_size;
int shift_size;
int size_unit;
void* buf;
bool isRead;
/*for extensible format*/
bool extensible;
int w_valid_bits_per_sample;
int dw_channel_mask;
int sub_format;
// short* buf;
public:
WAV();
WAV(short _ch, uint32_t _rate);
WAV(short _ch, uint32_t _rate, int frame_size, int shift_size);
~WAV();
int NewFile(const char *_file_name);
int NewFile(std::string file_name_);
int OpenFile(const char *_file_name);
int OpenFile(std::string file_name_);
int Append(short *app_data, unsigned int app_size);
int Append(float*app_data, unsigned int app_size);
void WriteHeader();
// set default
void Init();
// reset data;
void Clear();
void Finish();
void ReadHeader();
/* There might be compile error for ReadUnit() in Visual Studio.
* in this case, try to update your VS to most recent version. */
size_t ReadUnit(short*dest,int unit);
size_t ReadUnit(float*dest,int unit);
int IsEOF() const;
void Print() const;
void Rewind();
int Convert2ShiftedArray(double **raw);
int Convert2ShiftedArray(double *raw);
int Convert2Array(double **raw);
// Split 2 channel Wav into two 1 channel wav files.
void SplitBy2(const char* f1,const char* f2);
void SetSizes(int frame,int shift);
int GetChannels();
bool GetIsOpen();
uint32_t GetSize();
uint32_t GetSizeUnit();
uint32_t GetSampleRate();
uint32_t GetNumOfSamples();
const char* GetFileName();
short GetFmtType();
void UseBuf(int frame_size,int shift_size);
bool checkValidHeader();
FILE* GetFilePointer();
// Normalize WAV
void Normalize();
/*Split Wav fp into each channel */
static void Split(char* );
};
#endif