output_dac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "squeezelite.h"
  2. #include "driver/i2s.h"
  3. #include "perf_trace.h"
  4. #include <signal.h>
  5. #define DECLARE_ALL_MIN_MAX \
  6. DECLARE_MIN_MAX(req, long,LONG); \
  7. DECLARE_MIN_MAX(rec, long,LONG); \
  8. DECLARE_MIN_MAX(over, long,LONG); \
  9. DECLARE_MIN_MAX(o, long,LONG); \
  10. DECLARE_MIN_MAX(s, long,LONG); \
  11. DECLARE_MIN_MAX(d, long,LONG); \
  12. DECLARE_MIN_MAX(loci2sbuf, long,LONG); \
  13. DECLARE_MIN_MAX(buffering, long,LONG);\
  14. DECLARE_MIN_MAX(i2s_time, long,LONG); \
  15. DECLARE_MIN_MAX(i2savailable, long,LONG);
  16. #define RESET_ALL_MIN_MAX \
  17. RESET_MIN_MAX(d,LONG); \
  18. RESET_MIN_MAX(o,LONG); \
  19. RESET_MIN_MAX(s,LONG); \
  20. RESET_MIN_MAX(loci2sbuf, LONG); \
  21. RESET_MIN_MAX(req,LONG); \
  22. RESET_MIN_MAX(rec,LONG); \
  23. RESET_MIN_MAX(over,LONG); \
  24. RESET_MIN_MAX(over,LONG); \
  25. RESET_MIN_MAX(i2savailable,LONG);\
  26. RESET_MIN_MAX(i2s_time,LONG);
  27. // Prevent compile errors if dac output is
  28. // included in the build and not actually activated in menuconfig
  29. #ifndef CONFIG_I2S_BCK_IO
  30. #define CONFIG_I2S_BCK_IO -1
  31. #endif
  32. #ifndef CONFIG_I2S_WS_IO
  33. #define CONFIG_I2S_WS_IO -1
  34. #endif
  35. #ifndef CONFIG_I2S_DO_IO
  36. #define CONFIG_I2S_DO_IO -1
  37. #endif
  38. #ifndef CONFIG_I2S_NUM
  39. #define CONFIG_I2S_NUM -1
  40. #endif
  41. static log_level loglevel;
  42. size_t dac_buffer_size =0;
  43. static bool running = true;
  44. static bool isI2SStarted=false;
  45. extern struct outputstate output;
  46. extern struct buffer *streambuf;
  47. extern struct buffer *outputbuf;
  48. extern u8_t *silencebuf;
  49. static struct buffer _dac_buffer_structure;
  50. struct buffer *dacbuffer=&_dac_buffer_structure;
  51. static i2s_config_t i2s_config;
  52. #if REPACK && BYTES_PER_FRAMES == 4
  53. #error "REPACK is not compatible with BYTES_PER_FRAME=4"
  54. #endif
  55. #define LOCK mutex_lock(outputbuf->mutex)
  56. #define UNLOCK mutex_unlock(outputbuf->mutex)
  57. #define FRAME_BLOCK MAX_SILENCE_FRAMES
  58. #define FRAME_TO_BYTES(f) f*out_bytes_per_frame
  59. #define BYTES_TO_FRAME(b) b/out_bytes_per_frame
  60. static int out_bytes_per_frame;
  61. static thread_type thread;
  62. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  63. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr);
  64. static void *output_thread();
  65. extern void wait_for_frames(size_t frames, uint8_t pct);
  66. /****************************************************************************************
  67. * set output volume
  68. */
  69. void set_volume_dac(unsigned left, unsigned right) {
  70. LOG_DEBUG("setting internal gain left: %u right: %u", left, right);
  71. LOCK;
  72. output.gainL = left;
  73. output.gainR = right;
  74. UNLOCK;
  75. }
  76. /****************************************************************************************
  77. * Initialize the DAC output
  78. */
  79. void output_init_dac(log_level level, char *device, unsigned output_buf_size, char *params, unsigned rates[], unsigned rate_delay, unsigned idle) {
  80. loglevel = level;
  81. LOG_INFO("Init output DAC.");
  82. LOG_DEBUG("Setting output parameters.");
  83. memset(&output, 0, sizeof(output));
  84. #ifdef CONFIG_I2S_BITS_PER_CHANNEL
  85. switch (CONFIG_I2S_BITS_PER_CHANNEL) {
  86. case 24:
  87. output.format = S24_BE;
  88. break;
  89. case 16:
  90. output.format = S16_BE;
  91. break;
  92. case 8:
  93. output.format = S8_BE;
  94. break;
  95. default:
  96. LOG_ERROR("Unsupported bit depth %d",CONFIG_I2S_BITS_PER_CHANNEL);
  97. break;
  98. }
  99. #else
  100. output.format = S16_LE;
  101. #endif
  102. // ensure output rate is specified to avoid test open
  103. if (!rates[0]) {
  104. rates[0] = 44100;
  105. }
  106. running=true;
  107. // get common output configuration details
  108. output_init_common(level, device, output_buf_size, rates, idle);
  109. out_bytes_per_frame = get_bytes_per_frame(output.format);
  110. output.start_frames = FRAME_BLOCK;
  111. output.write_cb = &_dac_write_frames;
  112. output.rate_delay = rate_delay;
  113. i2s_config.mode = I2S_MODE_MASTER | I2S_MODE_TX; // Only TX
  114. i2s_config.sample_rate = output.current_sample_rate;
  115. i2s_config.bits_per_sample = get_bytes_per_frame(output.format) * 8/2;
  116. i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; //2-channels
  117. i2s_config.communication_format = I2S_COMM_FORMAT_I2S| I2S_COMM_FORMAT_I2S_MSB;
  118. // todo: tune this parameter. Expressed in number of samples. Byte size depends on bit depth.
  119. i2s_config.dma_buf_count = 64; //todo: tune this parameter. Expressed in numbrer of buffers.
  120. i2s_config.dma_buf_len = 128;
  121. i2s_config.use_apll = false;
  122. i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; //Interrupt level 1
  123. i2s_pin_config_t pin_config = { .bck_io_num = CONFIG_I2S_BCK_IO, .ws_io_num =
  124. CONFIG_I2S_WS_IO, .data_out_num = CONFIG_I2S_DO_IO, .data_in_num = -1 //Not used
  125. };
  126. LOG_INFO("Initializing I2S with rate: %d, bits per sample: %d, buffer len: %d, number of buffers: %d ",
  127. i2s_config.sample_rate, i2s_config.bits_per_sample, i2s_config.dma_buf_len, i2s_config.dma_buf_count);
  128. i2s_driver_install(CONFIG_I2S_NUM, &i2s_config, 0, NULL);
  129. i2s_set_pin(CONFIG_I2S_NUM, &pin_config);
  130. i2s_set_clk(CONFIG_I2S_NUM, output.current_sample_rate, i2s_config.bits_per_sample, 2);
  131. isI2SStarted=false;
  132. i2s_stop(CONFIG_I2S_NUM);
  133. dac_buffer_size = 10*FRAME_BLOCK*get_bytes_per_frame(output.format);
  134. LOG_DEBUG("Allocating local DAC transfer buffer of %u bytes.",dac_buffer_size);
  135. buf_init(dacbuffer,dac_buffer_size );
  136. if (!dacbuffer->buf) {
  137. LOG_ERROR("unable to malloc i2s buffer");
  138. exit(0);
  139. }
  140. LOG_SDEBUG("Current buffer free: %d",_buf_space(dacbuffer));
  141. #if LINUX || OSX || FREEBSD || POSIX
  142. pthread_attr_t attr;
  143. pthread_attr_init(&attr);
  144. #ifdef PTHREAD_STACK_MIN
  145. pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
  146. #endif
  147. pthread_create(&thread, &attr, output_thread, NULL);
  148. pthread_attr_destroy(&attr);
  149. #endif
  150. #if WIN
  151. thread = CreateThread(NULL, OUTPUT_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&output_thread, NULL, 0, NULL);
  152. #endif
  153. LOG_INFO("Init completed.");
  154. }
  155. /****************************************************************************************
  156. * Terminate DAC output
  157. */
  158. void output_close_dac(void) {
  159. LOG_INFO("close output");
  160. LOCK;
  161. running = false;
  162. UNLOCK;
  163. i2s_driver_uninstall(CONFIG_I2S_NUM);
  164. output_close_common();
  165. buf_destroy(dacbuffer);
  166. }
  167. /****************************************************************************************
  168. * Write frames to the output buffer
  169. */
  170. static int _dac_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR,
  171. s32_t cross_gain_in, s32_t cross_gain_out, ISAMPLE_T **cross_ptr) {
  172. size_t actual_out_bytes=FRAME_TO_BYTES(out_frames);
  173. assert(out_bytes_per_frame>0);
  174. if (!silence) {
  175. if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
  176. _apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
  177. }
  178. #if !REPACK
  179. if (gainL != FIXED_ONE || gainR!= FIXED_ONE) {
  180. _apply_gain(outputbuf, out_frames, gainL, gainR);
  181. }
  182. IF_DSD(
  183. if (output.outfmt == DOP) {
  184. update_dop((u32_t *) outputbuf->readp, out_frames, output.invert);
  185. } else if (output.outfmt != PCM && output.invert)
  186. dsd_invert((u32_t *) outputbuf->readp, out_frames);
  187. )
  188. memcpy(dacbuffer->writep, outputbuf->readp, actual_out_bytes);
  189. #else
  190. obuf = outputbuf->readp;
  191. #endif
  192. } else {
  193. #if !REPACK
  194. IF_DSD(
  195. if (output.outfmt != PCM) {
  196. obuf = silencebuf_dsd;
  197. update_dop((u32_t *) obuf, out_frames, false); // don't invert silence
  198. }
  199. )
  200. memcpy(dacbuffer->writep, silencebuf, actual_out_bytes);
  201. #endif
  202. }
  203. #if REPACK
  204. _scale_and_pack_frames(optr, (s32_t *)(void *)obuf, out_frames, gainL, gainR, output.format);
  205. #endif
  206. _buf_inc_writep(dacbuffer,actual_out_bytes);
  207. return (int)BYTES_TO_FRAME(actual_out_bytes);
  208. }
  209. /****************************************************************************************
  210. * Main output thread
  211. */
  212. static void *output_thread() {
  213. frames_t frames=0;
  214. frames_t available_frames_space=0;
  215. size_t bytes_to_send_i2s=0, // Contiguous buffer which can be addressed
  216. i2s_bytes_written = 0; //actual size that the i2s port was able to write
  217. uint32_t timer_start=0;
  218. static int count = 0;
  219. DECLARE_ALL_MIN_MAX;
  220. while (running) {
  221. i2s_bytes_written=0;
  222. frames=0;
  223. available_frames_space=0;
  224. bytes_to_send_i2s=0, // Contiguous buffer which can be addressed
  225. i2s_bytes_written = 0; //actual size that the i2s port was able to write
  226. TIME_MEASUREMENT_START(timer_start);
  227. LOCK;
  228. if (output.state == OUTPUT_OFF) {
  229. UNLOCK;
  230. LOG_INFO("Output state is off.");
  231. LOG_SDEBUG("Current buffer free: %10d, cont read: %10d",_buf_space(dacbuffer),_buf_cont_read(dacbuffer));
  232. if(isI2SStarted) {
  233. isI2SStarted=false;
  234. i2s_stop(CONFIG_I2S_NUM);
  235. }
  236. usleep(500000);
  237. continue;
  238. }
  239. LOG_SDEBUG("Current buffer free: %10d, cont read: %10d",_buf_space(dacbuffer),_buf_cont_read(dacbuffer));
  240. available_frames_space = BYTES_TO_FRAME(min(_buf_space(dacbuffer), _buf_cont_write(dacbuffer)));
  241. frames = _output_frames( available_frames_space ); // Keep the transfer buffer full
  242. UNLOCK;
  243. LOG_SDEBUG("Current buffer free: %10d, cont read: %10d",_buf_space(dacbuffer),_buf_cont_read(dacbuffer));
  244. SET_MIN_MAX( TIME_MEASUREMENT_GET(timer_start),buffering);
  245. SET_MIN_MAX( available_frames_space,req);
  246. SET_MIN_MAX(frames,rec);
  247. if(frames>0){
  248. //LOG_DEBUG("Frames available : %u.",frames);
  249. }
  250. else
  251. {
  252. //LOG_DEBUG("No frame available");
  253. usleep(10000);
  254. }
  255. SET_MIN_MAX(_buf_used(dacbuffer),loci2sbuf);
  256. bytes_to_send_i2s = _buf_cont_read(dacbuffer);
  257. SET_MIN_MAX(bytes_to_send_i2s,i2savailable);
  258. if (bytes_to_send_i2s>0 )
  259. {
  260. TIME_MEASUREMENT_START(timer_start);
  261. if(!isI2SStarted)
  262. {
  263. isI2SStarted=true;
  264. LOG_INFO("Restarting I2S.");
  265. i2s_start(CONFIG_I2S_NUM);
  266. if( i2s_config.sample_rate != output.current_sample_rate)
  267. {
  268. i2s_config.sample_rate = output.current_sample_rate;
  269. i2s_set_sample_rates(CONFIG_I2S_NUM, i2s_config.sample_rate);
  270. }
  271. }
  272. count++;
  273. LOG_SDEBUG("Outputting to I2S");
  274. LOG_SDEBUG("Current buffer free: %10d, cont read: %10d",_buf_space(dacbuffer),_buf_cont_read(dacbuffer));
  275. i2s_write(CONFIG_I2S_NUM, dacbuffer->readp,bytes_to_send_i2s, &i2s_bytes_written, portMAX_DELAY);
  276. _buf_inc_readp(dacbuffer,i2s_bytes_written);
  277. if(i2s_bytes_written!=bytes_to_send_i2s)
  278. {
  279. LOG_WARN("I2S DMA Overflow! available bytes: %d, I2S wrote %d bytes", bytes_to_send_i2s,i2s_bytes_written);
  280. }
  281. LOG_SDEBUG("DONE Outputting to I2S. Wrote: %d bytes out of %d", i2s_bytes_written,bytes_to_send_i2s);
  282. LOG_SDEBUG("Current buffer free: %10d, cont read: %10d",_buf_space(dacbuffer),_buf_cont_read(dacbuffer));
  283. output.device_frames =0;
  284. output.updated = gettime_ms();
  285. output.frames_played_dmp = output.frames_played;
  286. SET_MIN_MAX( TIME_MEASUREMENT_GET(timer_start),i2s_time);
  287. }
  288. SET_MIN_MAX(bytes_to_send_i2s-i2s_bytes_written,over);
  289. SET_MIN_MAX(_buf_used(outputbuf),o);
  290. SET_MIN_MAX(_buf_used(streambuf),s);
  291. /*
  292. * Statistics reporting
  293. */
  294. #define STATS_PERIOD_MS 5000
  295. count++;
  296. TIMED_SECTION_START_MS(STATS_PERIOD_MS);
  297. LOG_INFO( "count:%d, current sample rate: %d, bytes per frame: %d, avg cycle duration (ms): %d",count,output.current_sample_rate, out_bytes_per_frame,STATS_PERIOD_MS/count);
  298. LOG_INFO( " ----------+----------+-----------+ +----------+----------+----------------+");
  299. LOG_INFO( " max | min | current| | max | min | current |");
  300. LOG_INFO( " (ms) | (ms) | (ms)| | (bytes) | (bytes) | (bytes) |");
  301. LOG_INFO( " ----------+----------+-----------+ +----------+----------+----------------+");
  302. LOG_INFO(LINE_MIN_MAX_FORMAT_STREAM, LINE_MIN_MAX_STREAM("stream",s));
  303. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("output",o));
  304. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("i2swrite",i2savailable));
  305. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("local free",loci2sbuf));
  306. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("requested",req));
  307. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("received",rec));
  308. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("overflow",over));
  309. LOG_INFO(" ----------+----------+-----------+ +----------+----------+----------------+");
  310. LOG_INFO("");
  311. LOG_INFO(" max (us) | min (us) |current(us)| ");
  312. LOG_INFO(" ----------+----------+-----------+ ");
  313. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("Buffering(us)",buffering));
  314. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("i2s tfr(us)",i2s_time));
  315. LOG_INFO(" ----------+----------+-----------+ ");
  316. RESET_ALL_MIN_MAX;
  317. count=0;
  318. TIMED_SECTION_END;
  319. /*
  320. * End Statistics reporting
  321. */
  322. //wait_for_frames(BYTES_TO_FRAME(i2s_bytes_written));
  323. /*
  324. * Statistics reporting
  325. */
  326. #define STATS_PERIOD_MS 5000
  327. count++;
  328. TIMED_SECTION_START_MS(STATS_PERIOD_MS);
  329. LOG_INFO( "count:%d, current sample rate: %d, bytes per frame: %d, avg cycle duration (ms): %d",count,output.current_sample_rate, out_bytes_per_frame,STATS_PERIOD_MS/count);
  330. LOG_INFO( " ----------+----------+-----------+ +----------+----------+----------------+");
  331. LOG_INFO( " max | min | current| | max | min | current |");
  332. LOG_INFO( " (ms) | (ms) | (ms)| | (bytes) | (bytes) | (bytes) |");
  333. LOG_INFO( " ----------+----------+-----------+ +----------+----------+----------------+");
  334. LOG_INFO(LINE_MIN_MAX_FORMAT_STREAM, LINE_MIN_MAX_STREAM("stream",s));
  335. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("output",o));
  336. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("i2swrite",i2savailable));
  337. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("local free",loci2sbuf));
  338. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("requested",req));
  339. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("received",rec));
  340. LOG_INFO(LINE_MIN_MAX_FORMAT,LINE_MIN_MAX("overflow",over));
  341. LOG_INFO(" ----------+----------+-----------+ +----------+----------+----------------+");
  342. LOG_INFO("");
  343. LOG_INFO(" max (us) | min (us) |current(us)| ");
  344. LOG_INFO(" ----------+----------+-----------+ ");
  345. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("Buffering(us)",buffering));
  346. LOG_INFO(LINE_MIN_MAX_DURATION_FORMAT,LINE_MIN_MAX_DURATION("i2s tfr(us)",i2s_time));
  347. LOG_INFO(" ----------+----------+-----------+ ");
  348. RESET_ALL_MIN_MAX;
  349. count=0;
  350. TIMED_SECTION_END;
  351. /*
  352. * End Statistics reporting
  353. */
  354. wait_for_frames(BYTES_TO_FRAME(i2s_bytes_written),75);
  355. }
  356. return 0;
  357. }
  358. bool test_open(const char *device, unsigned rates[], bool userdef_rates) {
  359. unsigned _rates[] = { 96000, 88200, 48000, 44100, 32000, 0 };
  360. memcpy(rates, _rates, sizeof(_rates));
  361. return true;
  362. }