output_dac.c 12 KB

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