forked from earlephilhower/ESP8266Audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioOutputSPDIF.cpp
288 lines (262 loc) · 10 KB
/
AudioOutputSPDIF.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
AudioOutputSPDIF
S/PDIF output via I2S
Needs transciever from CMOS level to either optical or coaxial interface
See: https://www.epanorama.net/documents/audio/spdif.html
Original idea and sources:
Forum thread dicussing implementation
https://forum.pjrc.com/threads/28639-S-pdif
Teensy Audio Library
https://github.com/PaulStoffregen/Audio/blob/master/output_spdif2.cpp
Adapted for ESP8266Audio
NOTE: This module operates I2S at 4x sampling rate, as it needs to
send out each bit as two output symbols, packed into
32-bit words. Even for mono sound, S/PDIF is specified minimum
for 2 channels, each as 32-bits sub-frame. This drains I2S
buffers 4x more quickly so you may need 4x bigger output
buffers than usual, configurable with 'dma_buf_count'
constructor parameter.
Copyright (C) 2020 Ivan Kostoski
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#if defined(ESP32)
#include "driver/i2s.h"
#include "soc/rtc.h"
#elif defined(ESP8266)
#include "driver/SinglePinI2SDriver.h"
#endif
#include "AudioOutputSPDIF.h"
// BMC (Biphase Mark Coded) values (bit order reversed, i.e. LSB first)
static const uint16_t spdif_bmclookup[256] PROGMEM = {
0xcccc, 0x4ccc, 0x2ccc, 0xaccc, 0x34cc, 0xb4cc, 0xd4cc, 0x54cc,
0x32cc, 0xb2cc, 0xd2cc, 0x52cc, 0xcacc, 0x4acc, 0x2acc, 0xaacc,
0x334c, 0xb34c, 0xd34c, 0x534c, 0xcb4c, 0x4b4c, 0x2b4c, 0xab4c,
0xcd4c, 0x4d4c, 0x2d4c, 0xad4c, 0x354c, 0xb54c, 0xd54c, 0x554c,
0x332c, 0xb32c, 0xd32c, 0x532c, 0xcb2c, 0x4b2c, 0x2b2c, 0xab2c,
0xcd2c, 0x4d2c, 0x2d2c, 0xad2c, 0x352c, 0xb52c, 0xd52c, 0x552c,
0xccac, 0x4cac, 0x2cac, 0xacac, 0x34ac, 0xb4ac, 0xd4ac, 0x54ac,
0x32ac, 0xb2ac, 0xd2ac, 0x52ac, 0xcaac, 0x4aac, 0x2aac, 0xaaac,
0x3334, 0xb334, 0xd334, 0x5334, 0xcb34, 0x4b34, 0x2b34, 0xab34,
0xcd34, 0x4d34, 0x2d34, 0xad34, 0x3534, 0xb534, 0xd534, 0x5534,
0xccb4, 0x4cb4, 0x2cb4, 0xacb4, 0x34b4, 0xb4b4, 0xd4b4, 0x54b4,
0x32b4, 0xb2b4, 0xd2b4, 0x52b4, 0xcab4, 0x4ab4, 0x2ab4, 0xaab4,
0xccd4, 0x4cd4, 0x2cd4, 0xacd4, 0x34d4, 0xb4d4, 0xd4d4, 0x54d4,
0x32d4, 0xb2d4, 0xd2d4, 0x52d4, 0xcad4, 0x4ad4, 0x2ad4, 0xaad4,
0x3354, 0xb354, 0xd354, 0x5354, 0xcb54, 0x4b54, 0x2b54, 0xab54,
0xcd54, 0x4d54, 0x2d54, 0xad54, 0x3554, 0xb554, 0xd554, 0x5554,
0x3332, 0xb332, 0xd332, 0x5332, 0xcb32, 0x4b32, 0x2b32, 0xab32,
0xcd32, 0x4d32, 0x2d32, 0xad32, 0x3532, 0xb532, 0xd532, 0x5532,
0xccb2, 0x4cb2, 0x2cb2, 0xacb2, 0x34b2, 0xb4b2, 0xd4b2, 0x54b2,
0x32b2, 0xb2b2, 0xd2b2, 0x52b2, 0xcab2, 0x4ab2, 0x2ab2, 0xaab2,
0xccd2, 0x4cd2, 0x2cd2, 0xacd2, 0x34d2, 0xb4d2, 0xd4d2, 0x54d2,
0x32d2, 0xb2d2, 0xd2d2, 0x52d2, 0xcad2, 0x4ad2, 0x2ad2, 0xaad2,
0x3352, 0xb352, 0xd352, 0x5352, 0xcb52, 0x4b52, 0x2b52, 0xab52,
0xcd52, 0x4d52, 0x2d52, 0xad52, 0x3552, 0xb552, 0xd552, 0x5552,
0xccca, 0x4cca, 0x2cca, 0xacca, 0x34ca, 0xb4ca, 0xd4ca, 0x54ca,
0x32ca, 0xb2ca, 0xd2ca, 0x52ca, 0xcaca, 0x4aca, 0x2aca, 0xaaca,
0x334a, 0xb34a, 0xd34a, 0x534a, 0xcb4a, 0x4b4a, 0x2b4a, 0xab4a,
0xcd4a, 0x4d4a, 0x2d4a, 0xad4a, 0x354a, 0xb54a, 0xd54a, 0x554a,
0x332a, 0xb32a, 0xd32a, 0x532a, 0xcb2a, 0x4b2a, 0x2b2a, 0xab2a,
0xcd2a, 0x4d2a, 0x2d2a, 0xad2a, 0x352a, 0xb52a, 0xd52a, 0x552a,
0xccaa, 0x4caa, 0x2caa, 0xacaa, 0x34aa, 0xb4aa, 0xd4aa, 0x54aa,
0x32aa, 0xb2aa, 0xd2aa, 0x52aa, 0xcaaa, 0x4aaa, 0x2aaa, 0xaaaa
};
AudioOutputSPDIF::AudioOutputSPDIF(int dout_pin, int port, int dma_buf_count)
{
this->portNo = port;
#if defined(ESP32)
// Configure ESP32 I2S to roughly compatible to ESP8266 peripheral
i2s_config_t i2s_config_spdif = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 88200, // 2 x sampling_rate
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // 32bit words
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, // Right than left
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // lowest interrupt priority
.dma_buf_count = dma_buf_count,
.dma_buf_len = DMA_BUF_SIZE_DEFAULT, // bigger buffers, reduces interrupts
.use_apll = true // Audio PLL is needed for low clock jitter
};
if (i2s_driver_install((i2s_port_t)portNo, &i2s_config_spdif, 0, NULL) != ESP_OK) {
audioLogger->println(F("ERROR: Unable to install I2S drivers"));
return;
}
i2s_zero_dma_buffer((i2s_port_t)portNo);
SetPinout(I2S_PIN_NO_CHANGE, I2S_PIN_NO_CHANGE, dout_pin);
rate_multiplier = 2; // 2x32bit words
#elif defined(ESP8266)
(void) dout_pin;
if (!I2SDriver.begin(dma_buf_count, DMA_BUF_SIZE_DEFAULT)) {
audioLogger->println(F("ERROR: Unable to start I2S driver"));
return;
}
rate_multiplier = 4; // 4x16 bit words
#endif
i2sOn = true;
mono = false;
bps = 16;
channels = 2;
frame_num = 0;
SetGain(1.0);
hertz = 0;
SetRate(44100);
}
AudioOutputSPDIF::~AudioOutputSPDIF()
{
#if defined(ESP32)
if (i2sOn) {
i2s_stop((i2s_port_t)this->portNo);
audioLogger->printf("UNINSTALL I2S\n");
i2s_driver_uninstall((i2s_port_t)this->portNo); //stop & destroy i2s driver
}
#elif defined(ESP8266)
if (i2sOn) I2SDriver.stop();
#endif
i2sOn = false;
}
bool AudioOutputSPDIF::SetPinout(int bclk, int wclk, int dout)
{
#if defined(ESP32)
i2s_pin_config_t pins = {
.bck_io_num = bclk,
.ws_io_num = wclk,
.data_out_num = dout,
.data_in_num = I2S_PIN_NO_CHANGE
};
if (i2s_set_pin((i2s_port_t)portNo, &pins) != ESP_OK) {
audioLogger->println("ERROR setting up S/PDIF I2S pins\n");
return false;
}
return true;
#else
(void) bclk;
(void) wclk;
(void) dout;
return false;
#endif
}
bool AudioOutputSPDIF::SetRate(int hz)
{
if (!i2sOn) return false;
if (hz < 32000) return false;
if (hz == this->hertz) return true;
this->hertz = hz;
int adjustedHz = AdjustI2SRate(hz);
#if defined(ESP32)
if (i2s_set_sample_rates((i2s_port_t)portNo, adjustedHz) == ESP_OK) {
if (adjustedHz == 88200) {
// Manually fix the APLL rate for 44100.
// See: https://github.com/espressif/esp-idf/issues/2634
// sdm0 = 28, sdm1 = 8, sdm2 = 5, odir = 0 -> 88199.977
rtc_clk_apll_enable(1, 28, 8, 5, 0);
}
} else {
audioLogger->println("ERROR changing S/PDIF sample rate");
}
#elif defined(ESP8266)
I2SDriver.setRate(adjustedHz);
audioLogger->printf_P(PSTR("S/PDIF rate set: %.3f\n"), I2SDriver.getActualRate()/4);
#endif
return true;
}
bool AudioOutputSPDIF::SetBitsPerSample(int bits)
{
if ( (bits != 16) && (bits != 8) ) return false;
this->bps = bits;
return true;
}
bool AudioOutputSPDIF::SetChannels(int channels)
{
if ( (channels < 1) || (channels > 2) ) return false;
this->channels = channels;
return true;
}
bool AudioOutputSPDIF::SetOutputModeMono(bool mono)
{
this->mono = mono;
// Just use the left channel for mono
if (mono) SetChannels(1);
return true;
}
bool AudioOutputSPDIF::begin()
{
return true;
}
bool AudioOutputSPDIF::ConsumeSample(int16_t sample[2])
{
if (!i2sOn) return true; // Sink the data
int16_t ms[2];
uint16_t hi, lo, aux;
uint32_t buf[4];
ms[0] = sample[0];
ms[1] = sample[1];
MakeSampleStereo16(ms);
// S/PDIF encoding:
// http://www.hardwarebook.info/S/PDIF
// Original sources: Teensy Audio Library
// https://github.com/PaulStoffregen/Audio/blob/master/output_spdif2.cpp
//
// Order of bits, before BMC encoding, from the definition of SPDIF format
// PPPP AAAA SSSS SSSS SSSS SSSS SSSS VUCP
// are sent rearanged as
// VUCP PPPP AAAA 0000 SSSS SSSS SSSS SSSS
// This requires a bit less shifting as 16 sample bits align and can be
// BMC encoded with two table lookups (and at the same time flipped to LSB first).
// There is no separate word-clock, so hopefully the receiver won't notice.
uint16_t sample_left = Amplify(ms[LEFTCHANNEL]);
// BMC encode and flip left channel bits
hi = pgm_read_word(&spdif_bmclookup[(uint8_t)(sample_left >> 8)]);
lo = pgm_read_word(&spdif_bmclookup[(uint8_t)sample_left]);
// Low word is inverted depending on first bit of high word
lo ^= (~((int16_t)hi) >> 16);
buf[0] = ((uint32_t)lo << 16) | hi;
// Fixed 4 bits auxillary-audio-databits, the first used as parity
// Depending on first bit of low word, invert the bits
aux = 0xb333 ^ (((uint32_t)((int16_t)lo)) >> 17);
// Send 'B' preamble only for the first frame of data-block
if (frame_num == 0) {
buf[1] = VUCP_PREAMBLE_B | aux;
} else {
buf[1] = VUCP_PREAMBLE_M | aux;
}
uint16_t sample_right = Amplify(ms[RIGHTCHANNEL]);
// BMC encode right channel, similar as above
hi = pgm_read_word(&spdif_bmclookup[(uint8_t)(sample_right >> 8)]);
lo = pgm_read_word(&spdif_bmclookup[(uint8_t)sample_right]);
lo ^= (~((int16_t)hi) >> 16);
buf[2] = ((uint32_t)lo << 16) | hi;
aux = 0xb333 ^ (((uint32_t)((int16_t)lo)) >> 17);
buf[3] = VUCP_PREAMBLE_W | aux;
#if defined(ESP32)
// Assume DMA buffers are multiples of 16 bytes. Either we write all bytes or none.
uint32_t bytes_written;
esp_err_t ret = i2s_write((i2s_port_t)portNo, (const char*)&buf, 8 * channels, &bytes_written, 0);
// If we didn't write all bytes, return false early and do not increment frame_num
if ((ret != ESP_OK) || (bytes_written != (8 * channels))) return false;
#elif defined(ESP8266)
if (!I2SDriver.writeInterleaved(buf)) return false;
#endif
// Increment and rotate frame number
if (++frame_num > 191) frame_num = 0;
return true;
}
bool AudioOutputSPDIF::stop()
{
#if defined(ESP32)
i2s_zero_dma_buffer((i2s_port_t)portNo);
#elif defined(ESP8266)
I2SDriver.stop();
#endif
frame_num = 0;
return true;
}