output_i2s.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. #include "squeezelite.h"
  22. #include "driver/i2s.h"
  23. #include "perf_trace.h"
  24. #include <signal.h>
  25. #include "time.h"
  26. #define LOCK mutex_lock(outputbuf->mutex)
  27. #define UNLOCK mutex_unlock(outputbuf->mutex)
  28. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  29. // Prevent compile errors if dac output is
  30. // included in the build and not actually activated in menuconfig
  31. #ifndef CONFIG_I2S_BCK_IO
  32. #define CONFIG_I2S_BCK_IO -1
  33. #endif
  34. #ifndef CONFIG_I2S_WS_IO
  35. #define CONFIG_I2S_WS_IO -1
  36. #endif
  37. #ifndef CONFIG_I2S_DO_IO
  38. #define CONFIG_I2S_DO_IO -1
  39. #endif
  40. #ifndef CONFIG_I2S_NUM
  41. #define CONFIG_I2S_NUM -1
  42. #endif
  43. #define DECLARE_ALL_MIN_MAX \
  44. DECLARE_MIN_MAX(o); \
  45. DECLARE_MIN_MAX(s); \
  46. DECLARE_MIN_MAX(rec); \
  47. DECLARE_MIN_MAX(i2s_time); \
  48. DECLARE_MIN_MAX(buffering);
  49. #define RESET_ALL_MIN_MAX \
  50. RESET_MIN_MAX(o); \
  51. RESET_MIN_MAX(s); \
  52. RESET_MIN_MAX(rec); \
  53. RESET_MIN_MAX(i2s_time); \
  54. RESET_MIN_MAX(buffering);
  55. #define STATS_PERIOD_MS 5000
  56. extern struct outputstate output;
  57. extern struct buffer *streambuf;
  58. extern struct buffer *outputbuf;
  59. extern u8_t *silencebuf;
  60. static log_level loglevel;
  61. static bool running, isI2SStarted;
  62. static i2s_config_t i2s_config;
  63. static int bytes_per_frame;
  64. static thread_type thread, stats_thread;
  65. static u8_t *obuf;
  66. DECLARE_ALL_MIN_MAX;
  67. static int _i2s_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  68. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  69. static void *output_thread_i2s();
  70. static void *output_thread_i2s_stats();
  71. /****************************************************************************************
  72. * Initialize the DAC output
  73. */
  74. void output_init_i2s(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  75. loglevel = level;
  76. #ifdef CONFIG_I2S_BITS_PER_CHANNEL
  77. switch (CONFIG_I2S_BITS_PER_CHANNEL) {
  78. case 24:
  79. output.format = S24_BE;
  80. bytes_per_frame = 2*3;
  81. break;
  82. case 16:
  83. output.format = S16_BE;
  84. bytes_per_frame = 2*2;
  85. break;
  86. case 8:
  87. output.format = S8_BE;
  88. bytes_per_frame = 2*4;
  89. break;
  90. default:
  91. LOG_ERROR("Unsupported bit depth %d",CONFIG_I2S_BITS_PER_CHANNEL);
  92. break;
  93. }
  94. #else
  95. output.format = S16_LE;
  96. bytes_per_frame = 2*2;
  97. #endif
  98. output.write_cb = &_i2s_write_frames;
  99. obuf = malloc(FRAME_BLOCK * bytes_per_frame);
  100. if (!obuf) {
  101. LOG_ERROR("Cannot allocate i2s buffer");
  102. return;
  103. }
  104. running=true;
  105. // todo: move this to a hardware abstraction layer
  106. //hal_dac_init(device);
  107. i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_TX; // Only TX
  108. i2s_config.sample_rate = output.current_sample_rate;
  109. i2s_config.bits_per_sample = bytes_per_frame * 8 / 2;
  110. i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; //2-channels
  111. i2s_config.communication_format = I2S_COMM_FORMAT_I2S| I2S_COMM_FORMAT_I2S_MSB;
  112. // todo: tune this parameter. Expressed in number of samples. Byte size depends on bit depth.
  113. i2s_config.dma_buf_count = 10;
  114. // From the I2S driver source, the DMA buffer size is 4092 bytes.
  115. // so buf_len * 2 channels * 2 bytes/sample should be < 4092 or else it will be resized.
  116. i2s_config.dma_buf_len = FRAME_BLOCK/2;
  117. i2s_config.use_apll = false;
  118. i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; //Interrupt level 1
  119. i2s_pin_config_t pin_config = { .bck_io_num = CONFIG_I2S_BCK_IO, .ws_io_num =
  120. CONFIG_I2S_WS_IO, .data_out_num = CONFIG_I2S_DO_IO, .data_in_num = -1 //Not used
  121. };
  122. LOG_INFO("Initializing I2S with rate: %d, bits per sample: %d, buffer len: %d, number of buffers: %d ",
  123. i2s_config.sample_rate, i2s_config.bits_per_sample, i2s_config.dma_buf_len, i2s_config.dma_buf_count);
  124. i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL);
  125. i2s_set_pin(CONFIG_I2S_NUM, &pin_config);
  126. i2s_set_clk(CONFIG_I2S_NUM, output.current_sample_rate, i2s_config.bits_per_sample, 2);
  127. isI2SStarted=false;
  128. i2s_stop(CONFIG_I2S_NUM);
  129. pthread_attr_t attr;
  130. pthread_attr_init(&attr);
  131. pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  132. pthread_create_name(&thread, &attr, output_thread_i2s, NULL, "output_i2s");
  133. pthread_attr_destroy(&attr);
  134. // leave stack size to default
  135. pthread_create_name(&stats_thread, NULL, output_thread_i2s_stats, NULL, "output_i2s_sts");
  136. }
  137. /****************************************************************************************
  138. * Terminate DAC output
  139. */
  140. void output_close_i2s(void) {
  141. LOCK;
  142. running = false;
  143. UNLOCK;
  144. pthread_join(thread, NULL);
  145. pthread_join(stats_thread, NULL);
  146. i2s_driver_uninstall(CONFIG_I2S_NUM);
  147. free(obuf);
  148. }
  149. /****************************************************************************************
  150. * Write frames to the output buffer
  151. */
  152. static int _i2s_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  153. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  154. #if BYTES_PER_FRAME == 8
  155. s32_t *optr;
  156. #endif
  157. if (!silence) {
  158. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  159. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  160. }
  161. #if BYTES_PER_FRAME == 4
  162. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  163. _apply_gain(outputbuf, out_frames, gainL, gainR);
  164. }
  165. memcpy(obuf, outputbuf->readp, out_frames * bytes_per_frame);
  166. #else
  167. optr = (s32_t*) outputbuf->readp;
  168. #endif
  169. } else {
  170. #if BYTES_PER_FRAME == 4
  171. memcpy(obuf, silencebuf, out_frames * bytes_per_frame);
  172. #else
  173. optr = (s32_t*) silencebuf;
  174. #endif
  175. }
  176. #if BYTES_PER_FRAME == 8
  177. IF_DSD(
  178. if (output.outfmt == DOP) {
  179. update_dop((u32_t *) optr, out_frames, output.invert);
  180. } else if (output.outfmt != PCM && output.invert)
  181. dsd_invert((u32_t *) optr, out_frames);
  182. )
  183. _scale_and_pack_frames(obuf, optr, out_frames, gainL, gainR, output.format);
  184. #endif
  185. return out_frames;
  186. }
  187. /****************************************************************************************
  188. * Main output thread
  189. */
  190. static void *output_thread_i2s() {
  191. frames_t frames = 0;
  192. size_t bytes;
  193. uint32_t timer_start = 0;
  194. while (running) {
  195. TIME_MEASUREMENT_START(timer_start);
  196. LOCK;
  197. if (output.state == OUTPUT_OFF) {
  198. UNLOCK;
  199. LOG_INFO("Output state is off.");
  200. if(isI2SStarted) {
  201. isI2SStarted=false;
  202. i2s_stop(CONFIG_I2S_NUM);
  203. }
  204. usleep(200000);
  205. continue;
  206. }
  207. output.device_frames =0;
  208. output.updated = gettime_ms();
  209. output.frames_played_dmp = output.frames_played;
  210. frames = _output_frames( FRAME_BLOCK );
  211. SET_MIN_MAX_SIZED(frames,rec,FRAME_BLOCK);
  212. SET_MIN_MAX_SIZED(_buf_used(outputbuf),o,outputbuf->size);
  213. SET_MIN_MAX_SIZED(_buf_used(streambuf),s,streambuf->size);
  214. SET_MIN_MAX( TIME_MEASUREMENT_GET(timer_start),buffering);
  215. UNLOCK;
  216. if (frames) {
  217. // now send all the data
  218. TIME_MEASUREMENT_START(timer_start);
  219. if(!isI2SStarted)
  220. {
  221. isI2SStarted=true;
  222. LOG_INFO("Restarting I2S.");
  223. i2s_start(CONFIG_I2S_NUM);
  224. }
  225. // TODO: synchronize sample rate change when the block is at the DAC, not added to the DMA buffer
  226. if (i2s_config.sample_rate != output.current_sample_rate) {
  227. LOG_INFO("changing sampling rate %u to %u", i2s_config.sample_rate, output.current_sample_rate);
  228. i2s_config.sample_rate = output.current_sample_rate;
  229. i2s_set_sample_rates(CONFIG_I2S_NUM, i2s_config.sample_rate);
  230. }
  231. i2s_write(CONFIG_I2S_NUM, obuf, frames * bytes_per_frame, &bytes, portMAX_DELAY);
  232. if (bytes != frames * bytes_per_frame)
  233. {
  234. LOG_WARN("I2S DMA Overflow! available bytes: %d, I2S wrote %d bytes", frames * bytes_per_frame, bytes);
  235. }
  236. SET_MIN_MAX( TIME_MEASUREMENT_GET(timer_start),i2s_time);
  237. frames = 0;
  238. }
  239. }
  240. return 0;
  241. }
  242. /****************************************************************************************
  243. * Stats output thread
  244. */
  245. static void *output_thread_i2s_stats() {
  246. while (running) {
  247. LOCK;
  248. output_state state = output.state;
  249. UNLOCK;
  250. if(state>OUTPUT_STOPPED){
  251. LOG_INFO( "Output State: %d, current sample rate: %d, bytes per frame: %d",state,output.current_sample_rate, bytes_per_frame);
  252. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD1);
  253. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD2);
  254. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD3);
  255. LOG_INFO( LINE_MIN_MAX_FORMAT_HEAD4);
  256. LOG_INFO(LINE_MIN_MAX_FORMAT_STREAM, LINE_MIN_MAX_STREAM("stream",s));
  257. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("output",o));
  258. LOG_INFO(LINE_MIN_MAX_FORMAT_FOOTER);
  259. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("received",rec));
  260. LOG_INFO(LINE_MIN_MAX_FORMAT_FOOTER);
  261. LOG_INFO("");
  262. LOG_INFO(" ----------+----------+-----------+-----------+ ");
  263. LOG_INFO(" max (us) | min (us) | avg(us) | count | ");
  264. LOG_INFO(" ----------+----------+-----------+-----------+ ");
  265. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("Buffering(us)",buffering));
  266. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("i2s tfr(us)",i2s_time));
  267. LOG_INFO(" ----------+----------+-----------+-----------+");
  268. RESET_ALL_MIN_MAX;
  269. }
  270. usleep(STATS_PERIOD_MS *1000);
  271. }
  272. return NULL;
  273. }