output.c 14 KB

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