-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWavPlayer.h
231 lines (159 loc) · 3.56 KB
/
WavPlayer.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* SoundWav.h
*
* Created on: Jun 2, 2016
* Author: michal
*/
#ifndef WAVPLAYER_H_
#define WAVPLAYER_H_
#include "Player.h"
#include <memory>
#include <fstream>
#include "PcmInterface.h"
namespace Audio {
#pragma pack (1)
// IFF file header
typedef struct WavFileHeader
{
/**
* \ RIFF or FORM
*/
char id[4];
/**
* \ Length of subsequent file (including remainder of header). This is
* \ in Intel reverse byte order if RIFF, Motorola format if FORM.
*/
unsigned int len;
/**
* \ WAVE or AIFF
*/
char type[4];
} WavFileHeader;
// IFF chunk header
typedef struct WavChunkHeader
{
/**
* Chunk id
*/
char id[4];
/* Length of subsequent data within this chunk. this is in Intel reverse byte
* order if RIFF, Motorola format if FORM. Note: this doesn't include any
* extra byte needed to pad the chunk out to an even size.
*/
unsigned int len;
} WavChunkHeader;
// WAVE fmt chunk
typedef struct WavFmtHeader {
short audioFormat;
unsigned short channels;
unsigned int sampleRate;
unsigned int byteRate;
unsigned short blockAlign;
unsigned short bitsPerSample;
} WavFmtHeader;
#pragma pack()
/**
* \ Structure with alsa library paramters
*/
typedef struct SoundParam {
snd_pcm_stream_t stream;
snd_pcm_access_t access;
snd_pcm_format_t format;
Channels channels;
unsigned int rate;
SoundParam(
snd_pcm_stream_t _stream,
snd_pcm_access_t _access,
snd_pcm_format_t _format,
Channels _channels,
unsigned int _rate) :
stream{_stream},
access{_access},
format {_format},
channels {_channels},
rate {_rate}
{
};
} AudioParam;
/**
* \ Buffer with smart pointer and size
*/
//template<class T>
struct Buffer
{
std::unique_ptr<char[]> data;
size_t len {};
};
class WavPlayer : public Player, public PcmInterface
{
AudioParam param;
snd_pcm_uframes_t waveSize {};
WavFmtHeader fmt {};
const char *filename { nullptr };
Buffer buf {nullptr, 0};
size_t size {};
/**
* \ Check the format of wav file is correct
*/
bool isWavFile(std::fstream& file);
/**
* \ Convert unsigned char to enumerated format type
*/
snd_pcm_format_t convertBitsToPcmFormat(const unsigned char bits);
/**
* \ load file method
*/
void load(const char* _filename);
public:
/**
* \ Constructor with file name and audio parameters
*/
WavPlayer(const char *_name, SoundParam param);
/**
* \ play method overloaded from player interface
*/
void play(const char* _filename) override;
};
/**
* \ Recored data class
*/
class Recorder : public PcmInterface
{
AudioParam param;
Buffer/*<short>*/ buf;
public:
Recorder(const char* _filename, SoundParam _param) : PcmInterface(_filename), param{_param}
{
pcm->opendev(param.stream);
pcm->paramsAllocateDefault();
pcm->setAccess(param.access);
pcm->setFormat(param.format);
pcm->setRateNear(param.rate);
pcm->setChannels(param.channels);
pcm->setParam();
pcm->paramsFree();
pcm->prepare();
}
std::pair<decltype(buf.data.get()), int> read(int maxFrames)
{
buf.len = maxFrames * getFormatWidth(param.format) / 8 * 2;
buf.data = std::make_unique<char[]>(buf.len);
snd_pcm_sframes_t frames {};
for (size_t i = 0; i < buf.len; ++i)
{
if ((frames = pcm->read(buf.data.get(), maxFrames)) != maxFrames)
{
std::cout << "frames : " << frames << std::endl;
fprintf(stderr, "read from audio interface failed (%s)\n",
snd_strerror(frames));
}
}
return std::pair<decltype(buf.data.get()), int>{buf.data.get(), buf.len};
}
~Recorder()
{
pcm->close();
}
};
} /* namespace Audio */
#endif /* WAVPLAYER_H_ */