output.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.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. // Common output function
  22. #include "squeezelite.h"
  23. static log_level loglevel;
  24. struct outputstate output;
  25. static struct buffer buf;
  26. struct buffer *outputbuf = &buf;
  27. u8_t *silencebuf;
  28. #if DSD
  29. u8_t *silencebuf_dsd;
  30. #endif
  31. bool user_rates = false;
  32. #define LOCK mutex_lock(outputbuf->mutex)
  33. #define UNLOCK mutex_unlock(outputbuf->mutex)
  34. // functions starting _* are called with mutex locked
  35. frames_t _output_frames(frames_t avail) {
  36. frames_t frames, size;
  37. bool silence;
  38. u8_t flags = output.channels;
  39. s32_t cross_gain_in = 0, cross_gain_out = 0; ISAMPLE_T *cross_ptr = NULL;
  40. s32_t gainL = output.current_replay_gain ? gain(output.gainL, output.current_replay_gain) : output.gainL;
  41. s32_t gainR = output.current_replay_gain ? gain(output.gainR, output.current_replay_gain) : output.gainR;
  42. if (output.invert) { gainL = -gainL; gainR = -gainR; }
  43. frames = _buf_used(outputbuf) / BYTES_PER_FRAME;
  44. silence = false;
  45. // start when threshold met
  46. if (output.state == OUTPUT_BUFFER && (frames * BYTES_PER_FRAME) > output.threshold * output.next_sample_rate / 10 && frames > output.start_frames) {
  47. output.state = OUTPUT_RUNNING;
  48. LOG_INFO("start buffer frames: %u", frames);
  49. wake_controller();
  50. }
  51. // skip ahead - consume outputbuf but play nothing
  52. if (output.state == OUTPUT_SKIP_FRAMES) {
  53. if (frames > 0) {
  54. frames_t skip = min(frames, output.skip_frames);
  55. LOG_INFO("skip %u of %u frames", skip, output.skip_frames);
  56. frames -= skip;
  57. output.frames_played += skip;
  58. while (skip > 0) {
  59. frames_t cont_frames = min(skip, _buf_cont_read(outputbuf) / BYTES_PER_FRAME);
  60. skip -= cont_frames;
  61. _buf_inc_readp(outputbuf, cont_frames * BYTES_PER_FRAME);
  62. }
  63. }
  64. output.state = OUTPUT_RUNNING;
  65. }
  66. // pause frames - play silence for required frames
  67. if (output.state == OUTPUT_PAUSE_FRAMES) {
  68. LOG_INFO("pause %u frames", output.pause_frames);
  69. if (output.pause_frames == 0) {
  70. output.state = OUTPUT_RUNNING;
  71. } else {
  72. silence = true;
  73. frames = min(avail, output.pause_frames);
  74. frames = min(frames, MAX_SILENCE_FRAMES);
  75. output.pause_frames -= frames;
  76. }
  77. }
  78. // start at - play silence until jiffies reached
  79. if (output.state == OUTPUT_START_AT) {
  80. u32_t now = gettime_ms();
  81. if (now >= output.start_at || output.start_at > now + 10000) {
  82. output.state = OUTPUT_RUNNING;
  83. } else {
  84. u32_t delta_frames = (output.start_at - now) * output.current_sample_rate / 1000;
  85. silence = true;
  86. frames = min(avail, delta_frames);
  87. frames = min(frames, MAX_SILENCE_FRAMES);
  88. }
  89. }
  90. // play silence if buffering or no frames
  91. if (output.state <= OUTPUT_BUFFER || frames == 0) {
  92. silence = true;
  93. frames = min(avail, MAX_SILENCE_FRAMES);
  94. }
  95. LOG_SDEBUG("avail: %d frames: %d silence: %d", avail, frames, silence);
  96. frames = min(frames, avail);
  97. size = frames;
  98. while (size > 0) {
  99. frames_t out_frames;
  100. frames_t cont_frames = _buf_cont_read(outputbuf) / BYTES_PER_FRAME;
  101. int wrote;
  102. if (output.track_start && !silence) {
  103. if (output.track_start == outputbuf->readp) {
  104. unsigned delay = 0;
  105. if (output.current_sample_rate != output.next_sample_rate) {
  106. delay = output.rate_delay;
  107. }
  108. IF_DSD(
  109. if (output.outfmt != output.next_fmt) {
  110. delay = output.dsd_delay;
  111. }
  112. )
  113. frames -= size;
  114. // add silence delay in two halves, before and after track start on rate or pcm-dop change
  115. if (delay) {
  116. output.state = OUTPUT_PAUSE_FRAMES;
  117. if (!output.delay_active) {
  118. output.pause_frames = output.current_sample_rate * delay / 2000;
  119. output.delay_active = true; // first delay - don't process track start
  120. break;
  121. } else {
  122. output.pause_frames = output.next_sample_rate * delay / 2000;
  123. output.delay_active = false; // second delay - process track start
  124. }
  125. }
  126. LOG_INFO("track start sample rate: %u replay_gain: %u", output.next_sample_rate, output.next_replay_gain);
  127. output.frames_played = 0;
  128. output.track_started = true;
  129. output.track_start_time = gettime_ms();
  130. output.current_sample_rate = output.next_sample_rate;
  131. IF_DSD(
  132. output.outfmt = output.next_fmt;
  133. )
  134. if (output.fade == FADE_INACTIVE || output.fade_mode != FADE_CROSSFADE) {
  135. output.current_replay_gain = output.next_replay_gain;
  136. }
  137. output.track_start = NULL;
  138. break;
  139. } else if (output.track_start > outputbuf->readp) {
  140. // reduce cont_frames so we find the next track start at beginning of next chunk
  141. cont_frames = min(cont_frames, (output.track_start - outputbuf->readp) / BYTES_PER_FRAME);
  142. }
  143. }
  144. IF_DSD(
  145. if (output.outfmt != PCM) {
  146. gainL = gainR = FIXED_ONE;
  147. }
  148. )
  149. if (output.fade && !silence) {
  150. if (output.fade == FADE_DUE) {
  151. if (output.fade_start == outputbuf->readp) {
  152. LOG_INFO("fade start reached");
  153. output.fade = FADE_ACTIVE;
  154. } else if (output.fade_start > outputbuf->readp) {
  155. cont_frames = min(cont_frames, (output.fade_start - outputbuf->readp) / BYTES_PER_FRAME);
  156. }
  157. }
  158. if (output.fade == FADE_ACTIVE) {
  159. // find position within fade
  160. frames_t cur_f = outputbuf->readp >= output.fade_start ? (outputbuf->readp - output.fade_start) / BYTES_PER_FRAME :
  161. (outputbuf->readp + outputbuf->size - output.fade_start) / BYTES_PER_FRAME;
  162. frames_t dur_f = output.fade_end >= output.fade_start ? (output.fade_end - output.fade_start) / BYTES_PER_FRAME :
  163. (output.fade_end + outputbuf->size - output.fade_start) / BYTES_PER_FRAME;
  164. if (cur_f >= dur_f) {
  165. if (output.fade_mode == FADE_INOUT && output.fade_dir == FADE_DOWN) {
  166. LOG_INFO("fade down complete, starting fade up");
  167. output.fade_dir = FADE_UP;
  168. output.fade_start = outputbuf->readp;
  169. output.fade_end = outputbuf->readp + dur_f * BYTES_PER_FRAME;
  170. if (output.fade_end >= outputbuf->wrap) {
  171. output.fade_end -= outputbuf->size;
  172. }
  173. cur_f = 0;
  174. } else if (output.fade_mode == FADE_CROSSFADE) {
  175. LOG_INFO("crossfade complete");
  176. if (_buf_used(outputbuf) >= dur_f * BYTES_PER_FRAME) {
  177. _buf_inc_readp(outputbuf, dur_f * BYTES_PER_FRAME);
  178. LOG_INFO("skipped crossfaded start");
  179. } else {
  180. LOG_WARN("unable to skip crossfaded start");
  181. }
  182. output.fade = FADE_INACTIVE;
  183. output.current_replay_gain = output.next_replay_gain;
  184. } else {
  185. LOG_INFO("fade complete");
  186. output.fade = FADE_INACTIVE;
  187. }
  188. }
  189. // if fade in progress set fade gain, ensure cont_frames reduced so we get to end of fade at start of chunk
  190. if (output.fade) {
  191. if (output.fade_end > outputbuf->readp) {
  192. cont_frames = min(cont_frames, (output.fade_end - outputbuf->readp) / BYTES_PER_FRAME);
  193. }
  194. if (output.fade_dir == FADE_UP || output.fade_dir == FADE_DOWN) {
  195. // fade in, in-out, out handled via altering standard gain
  196. s32_t fade_gain;
  197. if (output.fade_dir == FADE_DOWN) {
  198. cur_f = dur_f - cur_f;
  199. }
  200. fade_gain = to_gain((float)cur_f / (float)dur_f);
  201. gainL = gain(gainL, fade_gain);
  202. gainR = gain(gainR, fade_gain);
  203. if (output.invert) { gainL = -gainL; gainR = -gainR; }
  204. }
  205. if (output.fade_dir == FADE_CROSS) {
  206. // cross fade requires special treatment - performed later based on these values
  207. // support different replay gain for old and new track by retaining old value until crossfade completes
  208. if (_buf_used(outputbuf) / BYTES_PER_FRAME > dur_f + size) {
  209. cross_gain_in = to_gain((float)cur_f / (float)dur_f);
  210. cross_gain_out = FIXED_ONE - cross_gain_in;
  211. if (output.current_replay_gain) {
  212. cross_gain_out = gain(cross_gain_out, output.current_replay_gain);
  213. }
  214. if (output.next_replay_gain) {
  215. cross_gain_in = gain(cross_gain_in, output.next_replay_gain);
  216. }
  217. gainL = output.gainL;
  218. gainR = output.gainR;
  219. if (output.invert) { gainL = -gainL; gainR = -gainR; }
  220. cross_ptr = (ISAMPLE_T *)(output.fade_end + cur_f * BYTES_PER_FRAME);
  221. } else {
  222. LOG_INFO("unable to continue crossfade - too few samples");
  223. output.fade = FADE_INACTIVE;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. out_frames = !silence ? min(size, cont_frames) : size;
  230. IF_DSD(
  231. if (output.outfmt != PCM) {
  232. flags = 0;
  233. }
  234. )
  235. wrote = output.write_cb(out_frames, silence, gainL, gainR, flags, cross_gain_in, cross_gain_out, &cross_ptr);
  236. if (wrote <= 0) {
  237. frames -= size;
  238. break;
  239. } else {
  240. out_frames = (frames_t)wrote;
  241. }
  242. size -= out_frames;
  243. _vis_export(outputbuf, &output, out_frames, silence);
  244. if (!silence) {
  245. _buf_inc_readp(outputbuf, out_frames * BYTES_PER_FRAME);
  246. output.frames_played += out_frames;
  247. }
  248. }
  249. LOG_SDEBUG("wrote %u frames", frames);
  250. return frames;
  251. }
  252. void _checkfade(bool start) {
  253. frames_t bytes;
  254. LOG_INFO("fade mode: %u duration: %u %s", output.fade_mode, output.fade_secs, start ? "track-start" : "track-end");
  255. bytes = output.next_sample_rate * BYTES_PER_FRAME * output.fade_secs;
  256. if (output.fade_mode == FADE_INOUT) {
  257. /* align on a frame boundary */
  258. bytes = ((bytes / 2) / BYTES_PER_FRAME) * BYTES_PER_FRAME;
  259. }
  260. if (start && (output.fade_mode == FADE_IN || (output.fade_mode == FADE_INOUT && _buf_used(outputbuf) == 0))) {
  261. bytes = min(bytes, outputbuf->size - BYTES_PER_FRAME); // shorter than full buffer otherwise start and end align
  262. LOG_INFO("fade IN: %u frames", bytes / BYTES_PER_FRAME);
  263. output.fade = FADE_DUE;
  264. output.fade_dir = FADE_UP;
  265. output.fade_start = outputbuf->writep;
  266. output.fade_end = output.fade_start + bytes;
  267. if (output.fade_end >= outputbuf->wrap) {
  268. output.fade_end -= outputbuf->size;
  269. }
  270. }
  271. if (!start && (output.fade_mode == FADE_OUT || output.fade_mode == FADE_INOUT)) {
  272. bytes = min(_buf_used(outputbuf), bytes);
  273. LOG_INFO("fade %s: %u frames", output.fade_mode == FADE_INOUT ? "IN-OUT" : "OUT", bytes / BYTES_PER_FRAME);
  274. output.fade = FADE_DUE;
  275. output.fade_dir = FADE_DOWN;
  276. output.fade_start = outputbuf->writep - bytes;
  277. if (output.fade_start < outputbuf->buf) {
  278. output.fade_start += outputbuf->size;
  279. }
  280. output.fade_end = outputbuf->writep;
  281. }
  282. if (start && output.fade_mode == FADE_CROSSFADE) {
  283. if (_buf_used(outputbuf) != 0) {
  284. if (output.next_sample_rate != output.current_sample_rate) {
  285. LOG_INFO("crossfade disabled as sample rates differ");
  286. return;
  287. }
  288. bytes = min(bytes, _buf_used(outputbuf)); // max of current remaining samples from previous track
  289. bytes = min(bytes, (frames_t)(outputbuf->size * 0.9)); // max of 90% of outputbuf as we consume additional buffer during crossfade
  290. LOG_INFO("CROSSFADE: %u frames", bytes / BYTES_PER_FRAME);
  291. output.fade = FADE_DUE;
  292. output.fade_dir = FADE_CROSS;
  293. output.fade_start = outputbuf->writep - bytes;
  294. if (output.fade_start < outputbuf->buf) {
  295. output.fade_start += outputbuf->size;
  296. }
  297. output.fade_end = outputbuf->writep;
  298. output.track_start = output.fade_start;
  299. } else if (outputbuf->size == OUTPUTBUF_SIZE && outputbuf->readp == outputbuf->buf) {
  300. // if default setting used and nothing in buffer attempt to resize to provide full crossfade support
  301. LOG_INFO("resize outputbuf for crossfade");
  302. _buf_resize(outputbuf, OUTPUTBUF_SIZE_CROSSFADE);
  303. #if LINUX || FREEBSD
  304. touch_memory(outputbuf->buf, outputbuf->size);
  305. #endif
  306. }
  307. }
  308. }
  309. void output_init_common(log_level level, const char *device, unsigned output_buf_size, unsigned rates[], unsigned idle) {
  310. unsigned i;
  311. loglevel = level;
  312. output_buf_size = output_buf_size - (output_buf_size % BYTES_PER_FRAME);
  313. LOG_DEBUG("outputbuf size: %u", output_buf_size);
  314. buf_init(outputbuf, output_buf_size);
  315. if (!outputbuf->buf) {
  316. LOG_ERROR("unable to malloc output buffer");
  317. exit(0);
  318. }
  319. silencebuf = malloc(MAX_SILENCE_FRAMES * BYTES_PER_FRAME);
  320. if (!silencebuf) {
  321. LOG_ERROR("unable to malloc silence buffer");
  322. exit(0);
  323. }
  324. memset(silencebuf, 0, MAX_SILENCE_FRAMES * BYTES_PER_FRAME);
  325. IF_DSD(
  326. silencebuf_dsd = malloc(MAX_SILENCE_FRAMES * BYTES_PER_FRAME);
  327. if (!silencebuf_dsd) {
  328. LOG_ERROR("unable to malloc silence dsd buffer");
  329. exit(0);
  330. }
  331. dsd_silence_frames((u32_t *)silencebuf_dsd, MAX_SILENCE_FRAMES);
  332. )
  333. LOG_DEBUG("idle timeout: %u", idle);
  334. output.state = idle ? OUTPUT_OFF: OUTPUT_STOPPED;
  335. output.device = device;
  336. output.fade = FADE_INACTIVE;
  337. output.invert = false;
  338. output.error_opening = false;
  339. output.idle_to = (u32_t) idle;
  340. /* Skip test_open for stdout, set default sample rates */
  341. #if !EMBEDDED
  342. if ( output.device[0] == '-' ) {
  343. for (i = 0; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  344. output.supported_rates[i] = rates[i];
  345. }
  346. }
  347. else {
  348. #else
  349. {
  350. #endif
  351. if (!test_open(output.device, output.supported_rates, user_rates)) {
  352. LOG_ERROR("unable to open output device: %s", output.device);
  353. exit(0);
  354. }
  355. }
  356. if (user_rates) {
  357. for (i = 0; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  358. output.supported_rates[i] = rates[i];
  359. }
  360. }
  361. // set initial sample rate, preferring 44100
  362. for (i = 0; i < MAX_SUPPORTED_SAMPLERATES; ++i) {
  363. if (output.supported_rates[i] == 44100) {
  364. output.default_sample_rate = 44100;
  365. break;
  366. }
  367. }
  368. if (!output.default_sample_rate) {
  369. output.default_sample_rate = output.supported_rates[0];
  370. }
  371. output.current_sample_rate = output.default_sample_rate;
  372. if (loglevel >= lINFO) {
  373. char rates_buf[10 * MAX_SUPPORTED_SAMPLERATES] = "";
  374. for (i = 0; output.supported_rates[i]; ++i) {
  375. char s[10];
  376. sprintf(s, "%d ", output.supported_rates[i]);
  377. strcat(rates_buf, s);
  378. }
  379. LOG_INFO("supported rates: %s", rates_buf);
  380. }
  381. }
  382. void output_close_common(void) {
  383. buf_destroy(outputbuf);
  384. free(silencebuf);
  385. IF_DSD(
  386. free(silencebuf_dsd);
  387. )
  388. }
  389. void output_flush(void) {
  390. LOG_INFO("flush output buffer");
  391. buf_flush(outputbuf);
  392. LOCK;
  393. output.fade = FADE_INACTIVE;
  394. if (output.state != OUTPUT_OFF) {
  395. output.state = OUTPUT_STOPPED;
  396. if (output.error_opening) {
  397. output.current_sample_rate = output.default_sample_rate;
  398. }
  399. output.delay_active = false;
  400. }
  401. output.frames_played = 0;
  402. UNLOCK;
  403. }