-
Notifications
You must be signed in to change notification settings - Fork 0
/
WavPlayer.cpp
164 lines (126 loc) · 3.16 KB
/
WavPlayer.cpp
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
/*
* SoundWav.cpp
*
* Created on: Jun 2, 2016
* Author: michal
*/
#include "WavPlayer.h"
namespace Audio
{
WavPlayer::WavPlayer(const char *_name, AudioParam _param) :
PcmInterface{ _name },
param{ _param }
{
pcm->opendev(param.stream);
pcm->paramsAllocateDefault();
}
bool WavPlayer::isWavFile(std::fstream& file)
{
WavFileHeader fileHeader{};
// check if data size is correct
if (file.read(reinterpret_cast<char *>(&fileHeader), sizeof(WavFileHeader)))
{
// check if format is RIFF or FORM
const auto id = std::string{ fileHeader.id, 4 };
if (!(id == "RIFF" || id == "FORM"))
return false;
// check if type is WAVE or AIFF
const auto type = std::string{ fileHeader.type, 4 };
if (!(type == "WAVE" || type == "AIFF"))
return false;
buf.len = fileHeader.len;
return true;
}
else
return false;
}
void WavPlayer::load(const char* _filename)
{
std::fstream fs;
WavChunkHeader chunkHeader{};
filename = _filename;
fs.open(filename, std::fstream::in);
if (!fs.is_open())
abort();
if (!isWavFile(fs))
abort();
while (fs.read(reinterpret_cast<char *>(&chunkHeader), sizeof(WavChunkHeader)))
{
const auto id = std::string{ chunkHeader.id, 4 };
if (id == "fmt ")
{
if (fs.read(reinterpret_cast<char *>(&fmt), sizeof(WavFmtHeader)))
{
if (fmt.audioFormat != 1)
{
log_writeln("Compressed WAVE not supported yet");
abort();
}
}
else
break;
}
else if (id == "data")
{
buf.data = std::make_unique<char[]>(buf.len);
if (buf.data.get() == nullptr)
{
log_writeln("Allocate memory error");
abort();
}
const auto offsite{ 100 };
fs.read(buf.data.get(), buf.len - offsite); /// TODO - wtf with len
if (fs)
waveSize = (buf.len * 8) / (static_cast<unsigned int>(fmt.bitsPerSample)
* static_cast<unsigned int>(fmt.channels));
else
{
log_writeln("Error read, only " + std::to_string(fs.gcount()) + " could be read");
break;
}
}
}
pcm->setParams(convertBitsToPcmFormat(fmt.bitsPerSample), param.access,
static_cast<Channels>(fmt.channels), fmt.byteRate, 1, 500000);
}
snd_pcm_format_t WavPlayer::convertBitsToPcmFormat(const unsigned char bits)
{
snd_pcm_format_t format;
switch (bits)
{
case 8:
format = SND_PCM_FORMAT_U8;
break;
case 16:
format = SND_PCM_FORMAT_S16;
break;
case 24:
format = SND_PCM_FORMAT_S24;
break;
case 32:
format = SND_PCM_FORMAT_S32;
break;
default:
format = SND_PCM_FORMAT_UNKNOWN;
}
return format;
}
void WavPlayer::play(const char* _filename)
{
register snd_pcm_uframes_t count{}, frames{};
load(_filename);
if (buf.data.get() != nullptr)
{
do
{
frames = pcm->write(buf.data.get() + count, waveSize - count);
count += frames;
} while (count < waveSize);
// Wait for playback to completely finish
if (count >= waveSize)
pcm->stopPresentingFrames();
}
else
log_write("File not loaded correctly");
}
} /* namespace Audio */