-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcm_wrapper.cpp
More file actions
212 lines (181 loc) · 6.23 KB
/
Copy pathpcm_wrapper.cpp
File metadata and controls
212 lines (181 loc) · 6.23 KB
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
#include <iostream>
#include <string>
#ifdef __linux__
#include <alsa/asoundlib.h>
#include <alsa/pcm.h>
static std::string device = "default";
static snd_output_t *output = 0;
static snd_pcm_t *handle;
static int err;
#else
#include <fstream>
using namespace std;
static ofstream pcm_out("/opt/chippy_files/pcm_out.txt");
#endif
using namespace std;
#include "pcm_wrapper.h"
#include "util.h"
uint8_t audio_buffer[BUFFER_LEN];
#ifdef __linux__
static void xrun_recovery() {
if (err == -EPIPE) {
// under-run
snd_pcm_prepare(handle);
err = 0;
} else if (err == -ESTRPIPE) {
// wait until the suspend flag is released
while ((err = snd_pcm_resume(handle)) == -EAGAIN)
sleep(1);
if (err < 0) {
snd_pcm_prepare(handle);
}
err = 0;
}
}
static void wait_for_poll(struct pollfd *ufds, int fdcount) {
uint16_t revents;
while (1) {
poll(ufds, fdcount, -1);
snd_pcm_poll_descriptors_revents(handle, ufds, fdcount, &revents);
if (revents & POLLERR) {
if (err < 0) {
if (snd_pcm_state(handle) == SND_PCM_STATE_XRUN ||
snd_pcm_state(handle) == SND_PCM_STATE_SUSPENDED) {
err = snd_pcm_state(handle) == SND_PCM_STATE_XRUN ? -EPIPE : -ESTRPIPE;
xrun_recovery();
if (err < 0) {
throw runtime_error("write error: " + string(snd_strerror(err)));
}
} else {
throw runtime_error("wait for poll failed: " + string(snd_strerror(err)));
}
}
xrun_recovery();
if (err < 0) {
throw runtime_error("unable to poll: " + string(snd_strerror(err)));
}
}
if (revents & POLLOUT)
break;
}
}
#endif
void pcm_open(void) {
#ifdef __linux__
if ((err = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
throw runtime_error("unable to open PCM device: " + string(snd_strerror(err)));
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_U8,
SND_PCM_ACCESS_RW_INTERLEAVED,
1,
SAMPLE_FREQUENCY,
1,
1000000)) < 0) {
throw runtime_error("unable to set PCM params: " + string(snd_strerror(err)));
}
#endif
}
static bool need_poll = false;
void pcm_write() {
#ifdef __linux__
int fdcount = snd_pcm_poll_descriptors_count(handle);
if (fdcount <= 0) {
throw runtime_error("invalid poll descriptors count");
}
static struct pollfd *ufds = (struct pollfd*) malloc(fdcount * sizeof(struct pollfd));
if ((err = snd_pcm_poll_descriptors(handle, ufds, fdcount)) < 0) {
throw runtime_error("unable to obtain poll descriptors for playback: " + string(snd_strerror(err)));
}
int len = BUFFER_LEN;
uint8_t *buffer = audio_buffer;
while (1) {
snd_pcm_sframes_t frames = snd_pcm_writei(handle, buffer, BUFFER_LEN);
if (frames < 0) {
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
throw runtime_error("unable to write PCM data: " + string(snd_strerror(frames)));
}
}
if (snd_pcm_state(handle) == SND_PCM_STATE_RUNNING)
need_poll = true;
len -= frames;
buffer += frames;
if (len <= 0)
break;
wait_for_poll(ufds, fdcount);
need_poll = false;
}
#else
/*
for (int i = 0; i < BUFFER_LEN; ++i) {
for (int j = 0; j < audio_buffer[i] / 2; ++j) {
pcm_out << ' ';
}
pcm_out << 'o' << endl;
}
*/
#endif
}
void pcm_close() {
#ifdef __linux__
snd_pcm_close(handle);
#else
pcm_out.close();
#endif
}
struct wav_file_header {
wav_file_header(uint32_t data_size):
size(sizeof(wav_file_header) + data_size * 2 - 8),
data_size(data_size * 2) {
}
const uint32_t riff {0x52494646};
const uint32_t size;
const uint32_t wave {0x57415645};
const uint32_t fmt_label {0x666d7420};
const uint32_t fmt_length {16};
const uint16_t fmt_type {1};
const uint16_t fmt_num_channels {1};
const uint32_t fmt_sample_rate {SAMPLE_FREQUENCY};
const uint32_t fmt_bps {SAMPLE_FREQUENCY * 2};
const uint16_t fmt_block_align {16};
const uint16_t fmt_bitrate {16};
const uint32_t data_label {0x64616461};
const uint32_t data_size;
};
#include <cmath>
void pcm_write_wav(ostream &out, const uint8_t *data, const uint32_t data_size) {
wav_file_header header(data_size);
write_stream(out, to_big_endian(header.riff));
write_stream(out, to_little_endian(header.size));
write_stream(out, to_big_endian(header.wave));
write_stream(out, to_big_endian(header.fmt_label));
write_stream(out, to_little_endian(header.fmt_length));
write_stream(out, to_little_endian(header.fmt_type));
write_stream(out, to_little_endian(header.fmt_num_channels));
write_stream(out, to_little_endian(header.fmt_sample_rate));
write_stream(out, to_little_endian(header.fmt_bps));
write_stream(out, to_little_endian(header.fmt_block_align));
write_stream(out, to_little_endian(header.fmt_bitrate));
write_stream(out, to_big_endian(header.data_label));
write_stream(out, to_little_endian(header.data_size));
for (uint32_t i = 0; i < data_size; ++i) {
int16_t value = sin(((double) i * 2 * 3.14159 * 440 ) / SAMPLE_FREQUENCY) * 0xffff;
out.put(value & 0xff).put((value << 8) & 0xff);
}
//out.write(reinterpret_cast<const char*>(data), data_size);
}
#ifdef WAV_TEST
#include <iostream>
int main(void) {
const uint32_t size = SAMPLE_FREQUENCY * 3;
uint8_t wav_data[size];
for (int i = 0; i < size; ++i) {
wav_data[i] = (uint8_t) i / 10;
}
ofstream out("/opt/chippy_files/test.wav");
pcm_write_wav(out, wav_data, SAMPLE_FREQUENCY * 3);
out.close();
return 0;
}
#endif