output_i2s.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. /*
  22. Synchronisation is a bit of a hack with i2s. The esp32 driver is always
  23. full when it starts, so there is a delay of the total length of buffers.
  24. In other words, i2s_write blocks at first call, until at least one buffer
  25. has been written (it uses a queue with produce / consume).
  26. The first hack is to consume that length at the beginning of tracks when
  27. synchronization is active. It's about ~180ms @ 44.1kHz
  28. The second hack is that we never know exactly the number of frames in the
  29. DMA buffers when we update the output.frames_played_dmp. We assume that
  30. after i2s_write, these buffers are always full so by measuring the gap
  31. between time after i2s_write and update of frames_played_dmp, we have a
  32. good idea of the error.
  33. The third hack is when sample rate changes, buffers are reset and we also
  34. do the change too early, but can't do that exaclty at the right time. So
  35. there might be a pop and a de-sync when sampling rate change happens. Not
  36. sure that using rate_delay would fix that
  37. */
  38. #include "squeezelite.h"
  39. #include "esp_pthread.h"
  40. #include "driver/i2s.h"
  41. #include "driver/i2c.h"
  42. #include "driver/gpio.h"
  43. #include "perf_trace.h"
  44. #include <signal.h>
  45. #include "adac.h"
  46. #include "time.h"
  47. #include "led.h"
  48. #include "monitor.h"
  49. #include "config.h"
  50. #define LOCK mutex_lock(outputbuf->mutex)
  51. #define UNLOCK mutex_unlock(outputbuf->mutex)
  52. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  53. // must have an integer ratio with FRAME_BLOCK (see spdif comment)
  54. #define DMA_BUF_LEN 512
  55. #define DMA_BUF_COUNT 12
  56. #define DECLARE_ALL_MIN_MAX \
  57. DECLARE_MIN_MAX(o); \
  58. DECLARE_MIN_MAX(s); \
  59. DECLARE_MIN_MAX(rec); \
  60. DECLARE_MIN_MAX(i2s_time); \
  61. DECLARE_MIN_MAX(buffering);
  62. #define RESET_ALL_MIN_MAX \
  63. RESET_MIN_MAX(o); \
  64. RESET_MIN_MAX(s); \
  65. RESET_MIN_MAX(rec); \
  66. RESET_MIN_MAX(i2s_time); \
  67. RESET_MIN_MAX(buffering);
  68. #define STATS_PERIOD_MS 5000
  69. extern struct outputstate output;
  70. extern struct buffer *streambuf;
  71. extern struct buffer *outputbuf;
  72. extern u8_t *silencebuf;
  73. // by default no DAC selected
  74. struct adac_s *adac = &dac_null;
  75. static log_level loglevel;
  76. static bool jack_mutes_amp;
  77. static bool running, isI2SStarted;
  78. static i2s_config_t i2s_config;
  79. static int bytes_per_frame;
  80. static thread_type thread, stats_thread;
  81. static u8_t *obuf;
  82. static frames_t oframes;
  83. static bool spdif;
  84. static size_t dma_buf_frames;
  85. DECLARE_ALL_MIN_MAX;
  86. static int _i2s_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  87. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  88. static void *output_thread_i2s();
  89. static void *output_thread_i2s_stats();
  90. static void spdif_convert(ISAMPLE_T *src, size_t frames, u32_t *dst, size_t *count);
  91. static void (*jack_handler_chain)(bool inserted);
  92. // force all GPIOs to what we need
  93. #ifdef CONFIG_SQUEEZEAMP
  94. #define TAS57xx
  95. #undef CONFIG_SPDIF_BCK_IO
  96. #define CONFIG_SPDIF_BCK_IO 33
  97. #undef CONFIG_SPDIF_WS_IO
  98. #define CONFIG_SPDIF_WS_IO 25
  99. #undef CONFIG_SPDIF_DO_IO
  100. #define CONFIG_SPDIF_DO_IO 15
  101. #undef CONFIG_SPDIF_NUM
  102. #define CONFIG_SPDIF_NUM 0
  103. #undef CONFIG_I2S_NUM
  104. #define CONFIG_I2S_NUM 0
  105. #elif defined CONFIG_A1S
  106. #define A1S
  107. #undef CONFIG_I2S_NUM
  108. #define CONFIG_I2S_NUM 0
  109. #endif
  110. #define I2C_PORT 0
  111. /****************************************************************************************
  112. * jack insertion handler
  113. */
  114. static void jack_handler(bool inserted) {
  115. // jack detection bounces a bit but that seems fine
  116. if (jack_mutes_amp) {
  117. LOG_INFO("switching amplifier %s", inserted ? "OFF" : "ON");
  118. if (inserted) adac->speaker(false);
  119. else adac->speaker(true);
  120. }
  121. // activate headset
  122. if (inserted) adac->headset(true);
  123. else adac->headset(false);
  124. // and chain if any
  125. if (jack_handler_chain) (jack_handler_chain)(inserted);
  126. }
  127. /****************************************************************************************
  128. * Initialize the DAC output
  129. */
  130. void output_init_i2s(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  131. loglevel = level;
  132. char *p;
  133. p = config_alloc_get_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
  134. jack_mutes_amp = (strcmp(p,"1") == 0 ||strcasecmp(p,"y") == 0);
  135. free(p);
  136. #ifdef CONFIG_I2S_BITS_PER_CHANNEL
  137. switch (CONFIG_I2S_BITS_PER_CHANNEL) {
  138. case 24:
  139. output.format = S24_BE;
  140. bytes_per_frame = 2*3;
  141. break;
  142. case 16:
  143. output.format = S16_BE;
  144. bytes_per_frame = 2*2;
  145. break;
  146. case 8:
  147. output.format = S8_BE;
  148. bytes_per_frame = 2*4;
  149. break;
  150. default:
  151. LOG_ERROR("Unsupported bit depth %d",CONFIG_I2S_BITS_PER_CHANNEL);
  152. break;
  153. }
  154. #else
  155. output.format = S16_LE;
  156. bytes_per_frame = 2*2;
  157. #endif
  158. output.write_cb = &_i2s_write_frames;
  159. obuf = malloc(FRAME_BLOCK * bytes_per_frame);
  160. if (!obuf) {
  161. LOG_ERROR("Cannot allocate i2s buffer");
  162. return;
  163. }
  164. running = true;
  165. // common I2S initialization
  166. i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_TX;
  167. i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
  168. i2s_config.communication_format = I2S_COMM_FORMAT_I2S| I2S_COMM_FORMAT_I2S_MSB;
  169. // in case of overflow, do not replay old buffer
  170. i2s_config.tx_desc_auto_clear = true;
  171. i2s_config.use_apll = true;
  172. i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; //Interrupt level 1
  173. if (strcasestr(device, "spdif")) {
  174. spdif = true;
  175. i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { .bck_io_num = CONFIG_SPDIF_BCK_IO, .ws_io_num = CONFIG_SPDIF_WS_IO,
  176. .data_out_num = CONFIG_SPDIF_DO_IO, .data_in_num = -1 //Not used
  177. };
  178. i2s_config.sample_rate = output.current_sample_rate * 2;
  179. i2s_config.bits_per_sample = 32;
  180. // Normally counted in frames, but 16 sample are transformed into 32 bits in spdif
  181. i2s_config.dma_buf_len = DMA_BUF_LEN / 2;
  182. i2s_config.dma_buf_count = DMA_BUF_COUNT * 2;
  183. /*
  184. In DMA, we have room for (LEN * COUNT) frames of 32 bits samples that
  185. we push at sample_rate * 2. Each of these peuso-frames is a single true
  186. audio frame. So the real depth is true frames is (LEN * COUNT / 2)
  187. */
  188. dma_buf_frames = DMA_BUF_COUNT * DMA_BUF_LEN / 2;
  189. i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL);
  190. i2s_set_pin(CONFIG_I2S_NUM, &i2s_pin_config);
  191. LOG_INFO("SPDIF using I2S bck:%u, ws:%u, do:%u", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
  192. } else {
  193. #ifdef TAS57xx
  194. gpio_pad_select_gpio(CONFIG_SPDIF_DO_IO);
  195. gpio_set_direction(CONFIG_SPDIF_DO_IO, GPIO_MODE_OUTPUT);
  196. gpio_set_level(CONFIG_SPDIF_DO_IO, 0);
  197. adac = &dac_tas57xx;
  198. #elif defined(A1S)
  199. adac = &dac_a1s;
  200. #endif
  201. i2s_config.sample_rate = output.current_sample_rate;
  202. i2s_config.bits_per_sample = bytes_per_frame * 8 / 2;
  203. // Counted in frames (but i2s allocates a buffer <= 4092 bytes)
  204. i2s_config.dma_buf_len = DMA_BUF_LEN;
  205. i2s_config.dma_buf_count = DMA_BUF_COUNT;
  206. dma_buf_frames = DMA_BUF_COUNT * DMA_BUF_LEN;
  207. // finally let DAC driver initialize I2C and I2S
  208. adac->init(I2C_PORT, CONFIG_I2S_NUM, &i2s_config);
  209. }
  210. LOG_INFO("Initializing I2S mode %s with rate: %d, bits per sample: %d, buffer frames: %d, number of buffers: %d ",
  211. spdif ? "S/PDIF" : "normal",
  212. i2s_config.sample_rate, i2s_config.bits_per_sample, i2s_config.dma_buf_len, i2s_config.dma_buf_count);
  213. i2s_stop(CONFIG_I2S_NUM);
  214. i2s_zero_dma_buffer(CONFIG_I2S_NUM);
  215. isI2SStarted=false;
  216. adac->power(ADAC_STANDBY);
  217. jack_handler_chain = jack_handler_svc;
  218. jack_handler_svc = jack_handler;
  219. if (jack_mutes_amp && jack_inserted_svc()) adac->speaker(false);
  220. else adac->speaker(true);
  221. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  222. cfg.thread_name= "output_i2s";
  223. cfg.inherit_cfg = false;
  224. cfg.prio = CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT + 1;
  225. cfg.stack_size = PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE;
  226. esp_pthread_set_cfg(&cfg);
  227. pthread_create(&thread, NULL, output_thread_i2s, NULL);
  228. cfg.thread_name= "output_i2s_sts";
  229. cfg.prio = CONFIG_ESP32_PTHREAD_TASK_PRIO_DEFAULT - 1;
  230. cfg.stack_size = 2048;
  231. esp_pthread_set_cfg(&cfg);
  232. pthread_create(&stats_thread, NULL, output_thread_i2s_stats, NULL);
  233. }
  234. /****************************************************************************************
  235. * Terminate DAC output
  236. */
  237. void output_close_i2s(void) {
  238. LOCK;
  239. running = false;
  240. UNLOCK;
  241. pthread_join(thread, NULL);
  242. pthread_join(stats_thread, NULL);
  243. i2s_driver_uninstall(CONFIG_I2S_NUM);
  244. free(obuf);
  245. adac->deinit();
  246. }
  247. /****************************************************************************************
  248. * change volume
  249. */
  250. bool output_volume_i2s(unsigned left, unsigned right) {
  251. adac->volume(left, right);
  252. return false;
  253. }
  254. /****************************************************************************************
  255. * Write frames to the output buffer
  256. */
  257. static int _i2s_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  258. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  259. #if BYTES_PER_FRAME == 8
  260. s32_t *optr;
  261. #endif
  262. if (!silence) {
  263. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  264. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  265. }
  266. #if BYTES_PER_FRAME == 4
  267. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  268. _apply_gain(outputbuf, out_frames, gainL, gainR);
  269. }
  270. memcpy(obuf + oframes * bytes_per_frame, outputbuf->readp, out_frames * bytes_per_frame);
  271. #else
  272. optr = (s32_t*) outputbuf->readp;
  273. #endif
  274. } else {
  275. #if BYTES_PER_FRAME == 4
  276. memcpy(obuf + oframes * bytes_per_frame, silencebuf, out_frames * bytes_per_frame);
  277. #else
  278. optr = (s32_t*) silencebuf;
  279. #endif
  280. }
  281. #if BYTES_PER_FRAME == 8
  282. IF_DSD(
  283. if (output.outfmt == DOP) {
  284. update_dop((u32_t *) optr, out_frames, output.invert);
  285. } else if (output.outfmt != PCM && output.invert)
  286. dsd_invert((u32_t *) optr, out_frames);
  287. )
  288. _scale_and_pack_frames(obuf + oframes * bytes_per_frame, optr, out_frames, gainL, gainR, output.format);
  289. #endif
  290. oframes += out_frames;
  291. return out_frames;
  292. }
  293. /****************************************************************************************
  294. * Main output thread
  295. */
  296. static void *output_thread_i2s() {
  297. size_t count = 0, bytes;
  298. frames_t iframes = FRAME_BLOCK;
  299. uint32_t timer_start = 0;
  300. int discard = 0;
  301. uint32_t fullness = gettime_ms();
  302. bool synced;
  303. output_state state = OUTPUT_OFF;
  304. char *sbuf = NULL;
  305. // spdif needs 16 bytes per frame : 32 bits/sample, 2 channels, BMC encoded
  306. if (spdif && (sbuf = malloc(FRAME_BLOCK * 16)) == NULL) {
  307. LOG_ERROR("Cannot allocate SPDIF buffer");
  308. }
  309. while (running) {
  310. TIME_MEASUREMENT_START(timer_start);
  311. LOCK;
  312. // manage led display & analogue
  313. if (state != output.state) {
  314. LOG_INFO("Output state is %d", output.state);
  315. if (output.state == OUTPUT_OFF) led_blink(LED_GREEN, 100, 2500);
  316. else if (output.state == OUTPUT_STOPPED) {
  317. adac->speaker(false);
  318. led_blink(LED_GREEN, 200, 1000);
  319. } else if (output.state == OUTPUT_RUNNING) {
  320. if (!jack_mutes_amp || !jack_inserted_svc()) adac->speaker(true);
  321. led_on(LED_GREEN);
  322. }
  323. }
  324. state = output.state;
  325. if (output.state == OUTPUT_OFF) {
  326. UNLOCK;
  327. if (isI2SStarted) {
  328. isI2SStarted = false;
  329. i2s_stop(CONFIG_I2S_NUM);
  330. adac->power(ADAC_STANDBY);
  331. count = 0;
  332. }
  333. usleep(200000);
  334. continue;
  335. } else if (output.state == OUTPUT_STOPPED) {
  336. synced = false;
  337. }
  338. oframes = 0;
  339. output.updated = gettime_ms();
  340. output.frames_played_dmp = output.frames_played;
  341. // try to estimate how much we have consumed from the DMA buffer (calculation is incorrect at the very beginning ...)
  342. output.device_frames = dma_buf_frames - ((output.updated - fullness) * output.current_sample_rate) / 1000;
  343. _output_frames( iframes );
  344. // oframes must be a global updated by the write callback
  345. output.frames_in_process = oframes;
  346. SET_MIN_MAX_SIZED(oframes,rec,iframes);
  347. SET_MIN_MAX_SIZED(_buf_used(outputbuf),o,outputbuf->size);
  348. SET_MIN_MAX_SIZED(_buf_used(streambuf),s,streambuf->size);
  349. SET_MIN_MAX( TIME_MEASUREMENT_GET(timer_start),buffering);
  350. /* must skip first whatever is in the pipe (but not when resuming).
  351. This test is incorrect when we pause a track that has just started,
  352. but this is higly unlikely and I don't have a better one for now */
  353. if (output.state == OUTPUT_START_AT) {
  354. discard = output.frames_played_dmp ? 0 : output.device_frames;
  355. synced = true;
  356. } else if (discard) {
  357. discard -= oframes;
  358. iframes = discard ? min(FRAME_BLOCK, discard) : FRAME_BLOCK;
  359. UNLOCK;
  360. continue;
  361. }
  362. UNLOCK;
  363. // now send all the data
  364. TIME_MEASUREMENT_START(timer_start);
  365. if (!isI2SStarted ) {
  366. isI2SStarted = true;
  367. LOG_INFO("Restarting I2S.");
  368. i2s_zero_dma_buffer(CONFIG_I2S_NUM);
  369. i2s_start(CONFIG_I2S_NUM);
  370. adac->power(ADAC_ON);
  371. }
  372. // this does not work well as set_sample_rates resets the fifos (and it's too early)
  373. if (i2s_config.sample_rate != output.current_sample_rate) {
  374. LOG_INFO("changing sampling rate %u to %u", i2s_config.sample_rate, output.current_sample_rate);
  375. /*
  376. if (synced)
  377. // can sleep for a buffer_queue - 1 and then eat a buffer (discard) if we are synced
  378. usleep(((DMA_BUF_COUNT - 1) * DMA_BUF_LEN * bytes_per_frame * 1000) / 44100 * 1000);
  379. discard = DMA_BUF_COUNT * DMA_BUF_LEN * bytes_per_frame;
  380. }
  381. */
  382. i2s_config.sample_rate = output.current_sample_rate;
  383. i2s_set_sample_rates(CONFIG_I2S_NUM, spdif ? i2s_config.sample_rate * 2 : i2s_config.sample_rate);
  384. i2s_zero_dma_buffer(CONFIG_I2S_NUM);
  385. //return;
  386. }
  387. // we assume that here we have been able to entirely fill the DMA buffers
  388. if (spdif) {
  389. spdif_convert((ISAMPLE_T*) obuf, oframes, (u32_t*) sbuf, &count);
  390. i2s_write(CONFIG_I2S_NUM, sbuf, oframes * 16, &bytes, portMAX_DELAY);
  391. bytes /= 4;
  392. } else {
  393. i2s_write(CONFIG_I2S_NUM, obuf, oframes * bytes_per_frame, &bytes, portMAX_DELAY);
  394. }
  395. fullness = gettime_ms();
  396. if (bytes != oframes * bytes_per_frame) {
  397. LOG_WARN("I2S DMA Overflow! available bytes: %d, I2S wrote %d bytes", oframes * bytes_per_frame, bytes);
  398. }
  399. SET_MIN_MAX( TIME_MEASUREMENT_GET(timer_start),i2s_time);
  400. }
  401. if (spdif) free(sbuf);
  402. return 0;
  403. }
  404. /****************************************************************************************
  405. * Stats output thread
  406. */
  407. static void *output_thread_i2s_stats() {
  408. //return;
  409. while (running) {
  410. LOCK;
  411. output_state state = output.state;
  412. UNLOCK;
  413. if(state>OUTPUT_STOPPED){
  414. LOG_INFO( "Output State: %d, current sample rate: %d, bytes per frame: %d",state,output.current_sample_rate, bytes_per_frame);
  415. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD1);
  416. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD2);
  417. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD3);
  418. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD4);
  419. LOG_INFO(LINE_MIN_MAX_FORMAT_STREAM, LINE_MIN_MAX_STREAM("stream",s));
  420. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("output",o));
  421. LOG_INFO(LINE_MIN_MAX_FORMAT_FOOTER);
  422. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("received",rec));
  423. LOG_INFO(LINE_MIN_MAX_FORMAT_FOOTER);
  424. LOG_INFO("");
  425. LOG_INFO(" ----------+----------+-----------+-----------+ ");
  426. LOG_INFO(" max (us) | min (us) | avg(us) | count | ");
  427. LOG_INFO(" ----------+----------+-----------+-----------+ ");
  428. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("Buffering(us)",buffering));
  429. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("i2s tfr(us)",i2s_time));
  430. LOG_INFO(" ----------+----------+-----------+-----------+");
  431. RESET_ALL_MIN_MAX;
  432. }
  433. usleep(STATS_PERIOD_MS *1000);
  434. }
  435. return NULL;
  436. }
  437. /****************************************************************************************
  438. * SPDIF support
  439. */
  440. #define PREAMBLE_B (0xE8) //11101000
  441. #define PREAMBLE_M (0xE2) //11100010
  442. #define PREAMBLE_W (0xE4) //11100100
  443. #define VUCP ((0xCC) << 24)
  444. #define VUCP_MUTE ((0xD4) << 24) // To mute PCM, set VUCP = invalid.
  445. extern const u16_t spdif_bmclookup[256];
  446. /*
  447. SPDIF is supposed to be (before BMC encoding, from LSB to MSB)
  448. PPPP AAAA SSSS SSSS SSSS SSSS SSSS VUCP
  449. after BMC encoding, each bits becomes 2 hence this becomes a 64 bits word. The
  450. the trick is to start not with a PPPP sequence but with an VUCP sequence to that
  451. the 16 bits samples are aligned with a BMC word boundary. Note that the LSB of the
  452. audio is transmitted first (not the MSB) and that ESP32 libray sends R then L,
  453. contrary to what seems to be usually done, so (dst) order had to be changed
  454. */
  455. void spdif_convert(ISAMPLE_T *src, size_t frames, u32_t *dst, size_t *count) {
  456. u16_t hi, lo, aux;
  457. // frames are 2 channels of 16 bits
  458. frames *= 2;
  459. while (frames--) {
  460. #if BYTES_PER_FRAME == 4
  461. hi = spdif_bmclookup[(u8_t)(*src >> 8)];
  462. lo = spdif_bmclookup[(u8_t) *src];
  463. #else
  464. hi = spdif_bmclookup[(u8_t)(*src >> 24)];
  465. lo = spdif_bmclookup[(u8_t) *src >> 16];
  466. #endif
  467. lo ^= ~((s16_t)hi) >> 16;
  468. // 16 bits sample:
  469. *(dst+0) = ((u32_t)lo << 16) | hi;
  470. // 4 bits auxillary-audio-databits, the first used as parity
  471. aux = 0xb333 ^ (((u32_t)((s16_t)lo)) >> 17);
  472. // VUCP-Bits: Valid, Subcode, Channelstatus, Parity = 0
  473. // As parity is always 0, we can use fixed preambles
  474. if (++(*count) > 383) {
  475. *(dst+1) = VUCP | (PREAMBLE_B << 16 ) | aux; //special preamble for one of 192 frames
  476. *count = 0;
  477. } else {
  478. *(dst+1) = VUCP | ((((*count) & 0x01) ? PREAMBLE_W : PREAMBLE_M) << 16) | aux;
  479. }
  480. src++;
  481. dst += 2;
  482. }
  483. }
  484. const u16_t spdif_bmclookup[256] = { //biphase mark encoded values (least significant bit first)
  485. 0xcccc, 0x4ccc, 0x2ccc, 0xaccc, 0x34cc, 0xb4cc, 0xd4cc, 0x54cc,
  486. 0x32cc, 0xb2cc, 0xd2cc, 0x52cc, 0xcacc, 0x4acc, 0x2acc, 0xaacc,
  487. 0x334c, 0xb34c, 0xd34c, 0x534c, 0xcb4c, 0x4b4c, 0x2b4c, 0xab4c,
  488. 0xcd4c, 0x4d4c, 0x2d4c, 0xad4c, 0x354c, 0xb54c, 0xd54c, 0x554c,
  489. 0x332c, 0xb32c, 0xd32c, 0x532c, 0xcb2c, 0x4b2c, 0x2b2c, 0xab2c,
  490. 0xcd2c, 0x4d2c, 0x2d2c, 0xad2c, 0x352c, 0xb52c, 0xd52c, 0x552c,
  491. 0xccac, 0x4cac, 0x2cac, 0xacac, 0x34ac, 0xb4ac, 0xd4ac, 0x54ac,
  492. 0x32ac, 0xb2ac, 0xd2ac, 0x52ac, 0xcaac, 0x4aac, 0x2aac, 0xaaac,
  493. 0x3334, 0xb334, 0xd334, 0x5334, 0xcb34, 0x4b34, 0x2b34, 0xab34,
  494. 0xcd34, 0x4d34, 0x2d34, 0xad34, 0x3534, 0xb534, 0xd534, 0x5534,
  495. 0xccb4, 0x4cb4, 0x2cb4, 0xacb4, 0x34b4, 0xb4b4, 0xd4b4, 0x54b4,
  496. 0x32b4, 0xb2b4, 0xd2b4, 0x52b4, 0xcab4, 0x4ab4, 0x2ab4, 0xaab4,
  497. 0xccd4, 0x4cd4, 0x2cd4, 0xacd4, 0x34d4, 0xb4d4, 0xd4d4, 0x54d4,
  498. 0x32d4, 0xb2d4, 0xd2d4, 0x52d4, 0xcad4, 0x4ad4, 0x2ad4, 0xaad4,
  499. 0x3354, 0xb354, 0xd354, 0x5354, 0xcb54, 0x4b54, 0x2b54, 0xab54,
  500. 0xcd54, 0x4d54, 0x2d54, 0xad54, 0x3554, 0xb554, 0xd554, 0x5554,
  501. 0x3332, 0xb332, 0xd332, 0x5332, 0xcb32, 0x4b32, 0x2b32, 0xab32,
  502. 0xcd32, 0x4d32, 0x2d32, 0xad32, 0x3532, 0xb532, 0xd532, 0x5532,
  503. 0xccb2, 0x4cb2, 0x2cb2, 0xacb2, 0x34b2, 0xb4b2, 0xd4b2, 0x54b2,
  504. 0x32b2, 0xb2b2, 0xd2b2, 0x52b2, 0xcab2, 0x4ab2, 0x2ab2, 0xaab2,
  505. 0xccd2, 0x4cd2, 0x2cd2, 0xacd2, 0x34d2, 0xb4d2, 0xd4d2, 0x54d2,
  506. 0x32d2, 0xb2d2, 0xd2d2, 0x52d2, 0xcad2, 0x4ad2, 0x2ad2, 0xaad2,
  507. 0x3352, 0xb352, 0xd352, 0x5352, 0xcb52, 0x4b52, 0x2b52, 0xab52,
  508. 0xcd52, 0x4d52, 0x2d52, 0xad52, 0x3552, 0xb552, 0xd552, 0x5552,
  509. 0xccca, 0x4cca, 0x2cca, 0xacca, 0x34ca, 0xb4ca, 0xd4ca, 0x54ca,
  510. 0x32ca, 0xb2ca, 0xd2ca, 0x52ca, 0xcaca, 0x4aca, 0x2aca, 0xaaca,
  511. 0x334a, 0xb34a, 0xd34a, 0x534a, 0xcb4a, 0x4b4a, 0x2b4a, 0xab4a,
  512. 0xcd4a, 0x4d4a, 0x2d4a, 0xad4a, 0x354a, 0xb54a, 0xd54a, 0x554a,
  513. 0x332a, 0xb32a, 0xd32a, 0x532a, 0xcb2a, 0x4b2a, 0x2b2a, 0xab2a,
  514. 0xcd2a, 0x4d2a, 0x2d2a, 0xad2a, 0x352a, 0xb52a, 0xd52a, 0x552a,
  515. 0xccaa, 0x4caa, 0x2caa, 0xacaa, 0x34aa, 0xb4aa, 0xd4aa, 0x54aa,
  516. 0x32aa, 0xb2aa, 0xd2aa, 0x52aa, 0xcaaa, 0x4aaa, 0x2aaa, 0xaaaa
  517. };