audio-io.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <math.h>
  15. #include <inttypes.h>
  16. #include "../util/threading.h"
  17. #include "../util/darray.h"
  18. #include "../util/circlebuf.h"
  19. #include "../util/platform.h"
  20. #include "audio-io.h"
  21. #include "audio-resampler.h"
  22. /* #define DEBUG_AUDIO */
  23. #define nop() do {int invalid = 0;} while(0)
  24. struct audio_input {
  25. struct audio_convert_info conversion;
  26. audio_resampler_t resampler;
  27. void (*callback)(void *param, const struct audio_data *data);
  28. void *param;
  29. };
  30. static inline audio_input_free(struct audio_input *input)
  31. {
  32. audio_resampler_destroy(input->resampler);
  33. }
  34. struct audio_line {
  35. char *name;
  36. struct audio_output *audio;
  37. struct circlebuf buffers[MAX_AV_PLANES];
  38. pthread_mutex_t mutex;
  39. DARRAY(uint8_t) volume_buffers[MAX_AV_PLANES];
  40. uint64_t base_timestamp;
  41. uint64_t last_timestamp;
  42. /* states whether this line is still being used. if not, then when the
  43. * buffer is depleted, it's destroyed */
  44. bool alive;
  45. struct audio_line **prev_next;
  46. struct audio_line *next;
  47. };
  48. static inline void audio_line_destroy_data(struct audio_line *line)
  49. {
  50. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  51. circlebuf_free(&line->buffers[i]);
  52. da_free(line->volume_buffers[i]);
  53. }
  54. pthread_mutex_destroy(&line->mutex);
  55. bfree(line->name);
  56. bfree(line);
  57. }
  58. struct audio_output {
  59. struct audio_output_info info;
  60. size_t block_size;
  61. size_t channels;
  62. size_t planes;
  63. pthread_t thread;
  64. event_t stop_event;
  65. DARRAY(uint8_t) mix_buffers[MAX_AV_PLANES];
  66. bool initialized;
  67. pthread_mutex_t line_mutex;
  68. struct audio_line *first_line;
  69. pthread_mutex_t input_mutex;
  70. DARRAY(struct audio_input) inputs;
  71. };
  72. static inline void audio_output_removeline(struct audio_output *audio,
  73. struct audio_line *line)
  74. {
  75. pthread_mutex_lock(&audio->line_mutex);
  76. *line->prev_next = line->next;
  77. if (line->next)
  78. line->next->prev_next = line->prev_next;
  79. pthread_mutex_unlock(&audio->line_mutex);
  80. audio_line_destroy_data(line);
  81. }
  82. /* ------------------------------------------------------------------------- */
  83. /* the following functions are used to calculate frame offsets based upon
  84. * timestamps. this will actually work accurately as long as you handle the
  85. * values correctly */
  86. static inline double ts_to_frames(audio_t audio, uint64_t ts)
  87. {
  88. double audio_offset_d = (double)ts;
  89. audio_offset_d /= 1000000000.0;
  90. audio_offset_d *= (double)audio->info.samples_per_sec;
  91. return audio_offset_d;
  92. }
  93. static inline double positive_round(double val)
  94. {
  95. return floor(val+0.5);
  96. }
  97. static size_t ts_diff_frames(audio_t audio, uint64_t ts1, uint64_t ts2)
  98. {
  99. double diff = ts_to_frames(audio, ts1) - ts_to_frames(audio, ts2);
  100. return (size_t)positive_round(diff);
  101. }
  102. static size_t ts_diff_bytes(audio_t audio, uint64_t ts1, uint64_t ts2)
  103. {
  104. return ts_diff_frames(audio, ts1, ts2) * audio->block_size;
  105. }
  106. /* unless the value is 3+ hours worth of frames, this won't overflow */
  107. static inline uint64_t conv_frames_to_time(audio_t audio, uint32_t frames)
  108. {
  109. return (uint64_t)frames * 1000000000ULL /
  110. (uint64_t)audio->info.samples_per_sec;
  111. }
  112. /* ------------------------------------------------------------------------- */
  113. static inline void clear_excess_audio_data(struct audio_line *line,
  114. uint64_t prev_time)
  115. {
  116. size_t size = ts_diff_bytes(line->audio, prev_time,
  117. line->base_timestamp);
  118. blog(LOG_WARNING, "Excess audio data for audio line '%s', somehow "
  119. "audio data went back in time by %"PRIu32" bytes. "
  120. "prev_time: %"PRIu64", line->base_timestamp: %"PRIu64,
  121. line->name, (uint32_t)size,
  122. prev_time, line->base_timestamp);
  123. for (size_t i = 0; i < line->audio->planes; i++) {
  124. size_t clear_size = (size > line->buffers[i].size) ?
  125. (size_t)size : line->buffers[i].size;
  126. circlebuf_pop_front(&line->buffers[i], NULL, clear_size);
  127. }
  128. }
  129. static inline uint64_t min_uint64(uint64_t a, uint64_t b)
  130. {
  131. return a < b ? a : b;
  132. }
  133. static inline size_t min_size(size_t a, size_t b)
  134. {
  135. return a < b ? a : b;
  136. }
  137. #ifndef CLAMP
  138. #define CLAMP(val, minval, maxval) \
  139. ((val > maxval) ? maxval : ((val < minval) ? minval : val))
  140. #endif
  141. #define MIN_S8 -128
  142. #define MAX_S8 127
  143. #define MIN_S16 -32767
  144. #define MAX_S16 32767
  145. #define MIN_S32 -2147483647
  146. #define MAX_S32 2147483647
  147. #define MIX_BUFFER_SIZE 256
  148. /* TODO: optimize mixing */
  149. static void mix_u8(uint8_t *mix, struct circlebuf *buf, size_t size)
  150. {
  151. uint8_t vals[MIX_BUFFER_SIZE];
  152. register int16_t mix_val;
  153. while (size) {
  154. size_t pop_count = min_size(size, sizeof(vals));
  155. size -= pop_count;
  156. circlebuf_pop_front(buf, vals, pop_count);
  157. for (size_t i = 0; i < pop_count; i++) {
  158. mix_val = (int16_t)*mix - 128;
  159. mix_val += (int16_t)vals[i] - 128;
  160. mix_val = CLAMP(mix_val, MIN_S8, MAX_S8) + 128;
  161. *(mix++) = (uint8_t)mix_val;
  162. }
  163. }
  164. }
  165. static void mix_s16(uint8_t *mix_in, struct circlebuf *buf, size_t size)
  166. {
  167. int16_t *mix = (int16_t*)mix_in;
  168. int16_t vals[MIX_BUFFER_SIZE];
  169. register int32_t mix_val;
  170. while (size) {
  171. size_t pop_count = min_size(size, sizeof(vals));
  172. size -= pop_count;
  173. circlebuf_pop_front(buf, vals, pop_count);
  174. pop_count /= sizeof(int16_t);
  175. for (size_t i = 0; i < pop_count; i++) {
  176. mix_val = (int32_t)*mix;
  177. mix_val += (int32_t)vals[i];
  178. *(mix++) = (int16_t)CLAMP(mix_val, MIN_S16, MAX_S16);
  179. }
  180. }
  181. }
  182. static void mix_s32(uint8_t *mix_in, struct circlebuf *buf, size_t size)
  183. {
  184. int32_t *mix = (int32_t*)mix_in;
  185. int32_t vals[MIX_BUFFER_SIZE];
  186. register int64_t mix_val;
  187. while (size) {
  188. size_t pop_count = min_size(size, sizeof(vals));
  189. size -= pop_count;
  190. circlebuf_pop_front(buf, vals, pop_count);
  191. pop_count /= sizeof(int32_t);
  192. for (size_t i = 0; i < pop_count; i++) {
  193. mix_val = (int64_t)*mix;
  194. mix_val += (int64_t)vals[i];
  195. *(mix++) = (int32_t)CLAMP(mix_val, MIN_S32, MAX_S32);
  196. }
  197. }
  198. }
  199. static void mix_float(uint8_t *mix_in, struct circlebuf *buf, size_t size)
  200. {
  201. float *mix = (float*)mix_in;
  202. float vals[MIX_BUFFER_SIZE];
  203. register float mix_val;
  204. while (size) {
  205. size_t pop_count = min_size(size, sizeof(vals));
  206. size -= pop_count;
  207. circlebuf_pop_front(buf, vals, pop_count);
  208. pop_count /= sizeof(float);
  209. for (size_t i = 0; i < pop_count; i++) {
  210. mix_val = *mix + vals[i];
  211. *(mix++) = CLAMP(mix_val, -1.0f, 1.0f);
  212. }
  213. }
  214. }
  215. static inline void mix_audio(enum audio_format format,
  216. uint8_t *mix, struct circlebuf *buf, size_t size)
  217. {
  218. switch (format) {
  219. case AUDIO_FORMAT_UNKNOWN:
  220. break;
  221. case AUDIO_FORMAT_U8BIT:
  222. case AUDIO_FORMAT_U8BIT_PLANAR:
  223. mix_u8(mix, buf, size); break;
  224. case AUDIO_FORMAT_16BIT:
  225. case AUDIO_FORMAT_16BIT_PLANAR:
  226. mix_s16(mix, buf, size); break;
  227. case AUDIO_FORMAT_32BIT:
  228. case AUDIO_FORMAT_32BIT_PLANAR:
  229. mix_s32(mix, buf, size); break;
  230. case AUDIO_FORMAT_FLOAT:
  231. case AUDIO_FORMAT_FLOAT_PLANAR:
  232. mix_float(mix, buf, size); break;
  233. }
  234. }
  235. static inline bool mix_audio_line(struct audio_output *audio,
  236. struct audio_line *line, size_t size, uint64_t timestamp)
  237. {
  238. size_t time_offset = ts_diff_bytes(audio,
  239. line->base_timestamp, timestamp);
  240. if (time_offset > size)
  241. return false;
  242. size -= time_offset;
  243. #ifdef DEBUG_AUDIO
  244. blog(LOG_DEBUG, "shaved off %lu bytes", size);
  245. #endif
  246. for (size_t i = 0; i < audio->planes; i++) {
  247. size_t pop_size = min_size(size, line->buffers[i].size);
  248. mix_audio(audio->info.format,
  249. audio->mix_buffers[i].array + time_offset,
  250. &line->buffers[i], pop_size);
  251. }
  252. return true;
  253. }
  254. static bool resample_audio_output(struct audio_input *input,
  255. struct audio_data *data)
  256. {
  257. bool success = true;
  258. if (input->resampler) {
  259. uint8_t *output[MAX_AV_PLANES];
  260. uint32_t frames;
  261. uint64_t offset;
  262. memset(output, 0, sizeof(output));
  263. success = audio_resampler_resample(input->resampler,
  264. output, &frames, &offset,
  265. data->data, data->frames);
  266. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  267. data->data[i] = output[i];
  268. data->frames = frames;
  269. data->timestamp -= offset;
  270. }
  271. return success;
  272. }
  273. static inline void do_audio_output(struct audio_output *audio,
  274. uint64_t timestamp, uint32_t frames)
  275. {
  276. struct audio_data data;
  277. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  278. data.data[i] = audio->mix_buffers[i].array;
  279. data.frames = frames;
  280. data.timestamp = timestamp;
  281. data.volume = 1.0f;
  282. pthread_mutex_lock(&audio->input_mutex);
  283. for (size_t i = 0; i < audio->inputs.num; i++) {
  284. struct audio_input *input = audio->inputs.array+i;
  285. if (resample_audio_output(input, &data))
  286. input->callback(input->param, &data);
  287. }
  288. pthread_mutex_unlock(&audio->input_mutex);
  289. }
  290. static uint64_t mix_and_output(struct audio_output *audio, uint64_t audio_time,
  291. uint64_t prev_time)
  292. {
  293. struct audio_line *line = audio->first_line;
  294. uint32_t frames = (uint32_t)ts_diff_frames(audio, audio_time,
  295. prev_time);
  296. size_t bytes = frames * audio->block_size;
  297. #ifdef DEBUG_AUDIO
  298. blog(LOG_DEBUG, "audio_time: %llu, prev_time: %llu, bytes: %lu",
  299. audio_time, prev_time, bytes);
  300. #endif
  301. /* return an adjusted audio_time according to the amount
  302. * of data that was sampled to ensure seamless transmission */
  303. audio_time = prev_time + conv_frames_to_time(audio, frames);
  304. /* resize and clear mix buffers */
  305. for (size_t i = 0; i < audio->planes; i++) {
  306. da_resize(audio->mix_buffers[i], bytes);
  307. memset(audio->mix_buffers[i].array, 0, bytes);
  308. }
  309. /* mix audio lines */
  310. while (line) {
  311. struct audio_line *next = line->next;
  312. /* if line marked for removal, destroy and move to the next */
  313. if (!line->buffers[0].size) {
  314. if (!line->alive) {
  315. audio_output_removeline(audio, line);
  316. line = next;
  317. continue;
  318. }
  319. }
  320. pthread_mutex_lock(&line->mutex);
  321. if (line->buffers[0].size && line->base_timestamp < prev_time) {
  322. clear_excess_audio_data(line, prev_time);
  323. line->base_timestamp = prev_time;
  324. }
  325. if (mix_audio_line(audio, line, bytes, prev_time))
  326. line->base_timestamp = audio_time;
  327. pthread_mutex_unlock(&line->mutex);
  328. line = next;
  329. }
  330. /* output */
  331. do_audio_output(audio, prev_time, frames);
  332. return audio_time;
  333. }
  334. /* sample audio 40 times a second */
  335. #define AUDIO_WAIT_TIME (1000/40)
  336. static void *audio_thread(void *param)
  337. {
  338. struct audio_output *audio = param;
  339. uint64_t buffer_time = audio->info.buffer_ms * 1000000;
  340. uint64_t prev_time = os_gettime_ns() - buffer_time;
  341. uint64_t audio_time;
  342. while (event_try(&audio->stop_event) == EAGAIN) {
  343. os_sleep_ms(AUDIO_WAIT_TIME);
  344. pthread_mutex_lock(&audio->line_mutex);
  345. audio_time = os_gettime_ns() - buffer_time;
  346. audio_time = mix_and_output(audio, audio_time, prev_time);
  347. prev_time = audio_time;
  348. pthread_mutex_unlock(&audio->line_mutex);
  349. }
  350. return NULL;
  351. }
  352. /* ------------------------------------------------------------------------- */
  353. static size_t audio_get_input_idx(audio_t video,
  354. void (*callback)(void *param, const struct audio_data *data),
  355. void *param)
  356. {
  357. for (size_t i = 0; i < video->inputs.num; i++) {
  358. struct audio_input *input = video->inputs.array+i;
  359. if (input->callback == callback && input->param == param)
  360. return i;
  361. }
  362. return DARRAY_INVALID;
  363. }
  364. static inline bool audio_input_init(struct audio_input *input,
  365. struct audio_output *audio)
  366. {
  367. if (input->conversion.format != audio->info.format ||
  368. input->conversion.samples_per_sec != audio->info.samples_per_sec ||
  369. input->conversion.speakers != audio->info.speakers) {
  370. struct resample_info from = {
  371. .format = audio->info.format,
  372. .samples_per_sec = audio->info.samples_per_sec,
  373. .speakers = audio->info.speakers
  374. };
  375. struct resample_info to = {
  376. .format = input->conversion.format,
  377. .samples_per_sec = input->conversion.samples_per_sec,
  378. .speakers = input->conversion.speakers
  379. };
  380. input->resampler = audio_resampler_create(&to, &from);
  381. if (!input->resampler) {
  382. blog(LOG_WARNING, "audio_input_init: Failed to "
  383. "create resampler");
  384. return false;
  385. }
  386. } else {
  387. input->resampler = NULL;
  388. }
  389. return true;
  390. }
  391. bool audio_output_connect(audio_t audio,
  392. struct audio_convert_info *conversion,
  393. void (*callback)(void *param, const struct audio_data *data),
  394. void *param)
  395. {
  396. bool success = false;
  397. pthread_mutex_lock(&audio->input_mutex);
  398. if (audio_get_input_idx(audio, callback, param) == DARRAY_INVALID) {
  399. struct audio_input input;
  400. input.callback = callback;
  401. input.param = param;
  402. if (conversion) {
  403. input.conversion = *conversion;
  404. } else {
  405. input.conversion.format = audio->info.format;
  406. input.conversion.speakers = audio->info.speakers;
  407. input.conversion.samples_per_sec =
  408. audio->info.samples_per_sec;
  409. }
  410. success = audio_input_init(&input, audio);
  411. if (success)
  412. da_push_back(audio->inputs, &input);
  413. }
  414. pthread_mutex_unlock(&audio->input_mutex);
  415. return success;
  416. }
  417. void audio_output_disconnect(audio_t audio,
  418. void (*callback)(void *param, const struct audio_data *data),
  419. void *param)
  420. {
  421. pthread_mutex_lock(&audio->input_mutex);
  422. size_t idx = audio_get_input_idx(audio, callback, param);
  423. if (idx != DARRAY_INVALID) {
  424. audio_input_free(audio->inputs.array+idx);
  425. da_erase(audio->inputs, idx);
  426. }
  427. pthread_mutex_unlock(&audio->input_mutex);
  428. }
  429. static inline bool valid_audio_params(struct audio_output_info *info)
  430. {
  431. return info->format && info->name && info->samples_per_sec > 0 &&
  432. info->speakers > 0;
  433. }
  434. int audio_output_open(audio_t *audio, struct audio_output_info *info)
  435. {
  436. struct audio_output *out;
  437. pthread_mutexattr_t attr;
  438. bool planar = is_audio_planar(info->format);
  439. if (!valid_audio_params(info))
  440. return AUDIO_OUTPUT_INVALIDPARAM;
  441. out = bzalloc(sizeof(struct audio_output));
  442. memcpy(&out->info, info, sizeof(struct audio_output_info));
  443. pthread_mutex_init_value(&out->line_mutex);
  444. out->channels = get_audio_channels(info->speakers);
  445. out->planes = planar ? out->channels : 1;
  446. out->block_size = (planar ? 1 : out->channels) *
  447. get_audio_bytes_per_channel(info->format);
  448. if (pthread_mutexattr_init(&attr) != 0)
  449. goto fail;
  450. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  451. goto fail;
  452. if (pthread_mutex_init(&out->line_mutex, &attr) != 0)
  453. goto fail;
  454. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  455. goto fail;
  456. if (event_init(&out->stop_event, EVENT_TYPE_MANUAL) != 0)
  457. goto fail;
  458. if (pthread_create(&out->thread, NULL, audio_thread, out) != 0)
  459. goto fail;
  460. out->initialized = true;
  461. *audio = out;
  462. return AUDIO_OUTPUT_SUCCESS;
  463. fail:
  464. audio_output_close(out);
  465. return AUDIO_OUTPUT_FAIL;
  466. }
  467. void audio_output_close(audio_t audio)
  468. {
  469. void *thread_ret;
  470. struct audio_line *line;
  471. if (!audio)
  472. return;
  473. if (audio->initialized) {
  474. event_signal(&audio->stop_event);
  475. pthread_join(audio->thread, &thread_ret);
  476. }
  477. line = audio->first_line;
  478. while (line) {
  479. struct audio_line *next = line->next;
  480. audio_line_destroy_data(line);
  481. line = next;
  482. }
  483. for (size_t i = 0; i < audio->inputs.num; i++)
  484. audio_input_free(audio->inputs.array+i);
  485. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  486. da_free(audio->mix_buffers[i]);
  487. da_free(audio->inputs);
  488. event_destroy(&audio->stop_event);
  489. pthread_mutex_destroy(&audio->line_mutex);
  490. bfree(audio);
  491. }
  492. audio_line_t audio_output_createline(audio_t audio, const char *name)
  493. {
  494. struct audio_line *line = bzalloc(sizeof(struct audio_line));
  495. line->alive = true;
  496. line->audio = audio;
  497. if (pthread_mutex_init(&line->mutex, NULL) != 0) {
  498. blog(LOG_ERROR, "audio_output_createline: Failed to create "
  499. "mutex");
  500. bfree(line);
  501. return NULL;
  502. }
  503. pthread_mutex_lock(&audio->line_mutex);
  504. if (audio->first_line) {
  505. audio->first_line->prev_next = &line->next;
  506. line->next = audio->first_line;
  507. }
  508. line->prev_next = &audio->first_line;
  509. audio->first_line = line;
  510. pthread_mutex_unlock(&audio->line_mutex);
  511. line->name = bstrdup(name ? name : "(unnamed audio line)");
  512. return line;
  513. }
  514. const struct audio_output_info *audio_output_getinfo(audio_t audio)
  515. {
  516. return &audio->info;
  517. }
  518. void audio_line_destroy(struct audio_line *line)
  519. {
  520. if (line) {
  521. if (!line->buffers[0].size)
  522. audio_output_removeline(line->audio, line);
  523. else
  524. line->alive = false;
  525. }
  526. }
  527. size_t audio_output_blocksize(audio_t audio)
  528. {
  529. return audio->block_size;
  530. }
  531. size_t audio_output_planes(audio_t audio)
  532. {
  533. return audio->planes;
  534. }
  535. size_t audio_output_channels(audio_t audio)
  536. {
  537. return audio->channels;
  538. }
  539. /* TODO: Optimization of volume multiplication functions */
  540. static inline void mul_vol_u8bit(void *array, float volume, size_t total_num)
  541. {
  542. uint8_t *vals = array;
  543. int32_t vol = (int32_t)(volume * 127.0f);
  544. for (size_t i = 0; i < total_num; i++) {
  545. int32_t val = (int32_t)vals[i] - 128;
  546. int32_t output = val * vol / 127;
  547. vals[i] = (uint8_t)(CLAMP(output, MIN_S8, MAX_S8) + 128);
  548. }
  549. }
  550. static inline void mul_vol_16bit(void *array, float volume, size_t total_num)
  551. {
  552. uint16_t *vals = array;
  553. int64_t vol = (int64_t)(volume * 32767.0f);
  554. for (size_t i = 0; i < total_num; i++) {
  555. int64_t output = (int64_t)vals[i] * vol / 32767;
  556. vals[i] = (int32_t)CLAMP(output, MIN_S16, MAX_S16);
  557. }
  558. }
  559. static inline float conv_24bit_to_float(uint8_t *vals)
  560. {
  561. int32_t val = ((int32_t)vals[0]) |
  562. ((int32_t)vals[1] << 8) |
  563. ((int32_t)vals[2] << 16);
  564. if ((val & 0x800000) != 0)
  565. val |= 0xFF000000;
  566. return (float)val / 8388607.0f;
  567. }
  568. static inline void conv_float_to_24bit(float fval, uint8_t *vals)
  569. {
  570. int32_t val = (int32_t)(fval * 8388607.0f);
  571. vals[0] = (val) & 0xFF;
  572. vals[1] = (val >> 8) & 0xFF;
  573. vals[2] = (val >> 16) & 0xFF;
  574. }
  575. static inline void mul_vol_24bit(void *array, float volume, size_t total_num)
  576. {
  577. uint8_t *vals = array;
  578. for (size_t i = 0; i < total_num; i++) {
  579. float val = conv_24bit_to_float(vals) * volume;
  580. conv_float_to_24bit(CLAMP(val, -1.0f, 1.0f), vals);
  581. vals += 3;
  582. }
  583. }
  584. static inline void mul_vol_32bit(void *array, float volume, size_t total_num)
  585. {
  586. int32_t *vals = array;
  587. double dvol = (double)volume;
  588. for (size_t i = 0; i < total_num; i++) {
  589. double val = (double)vals[i] / 2147483647.0;
  590. double output = val * volume;
  591. vals[i] = (int32_t)(CLAMP(output, -1.0, 1.0) * 2147483647.0);
  592. }
  593. }
  594. static inline void mul_vol_float(void *array, float volume, size_t total_num)
  595. {
  596. float *vals = array;
  597. for (size_t i = 0; i < total_num; i++)
  598. vals[i] *= volume;
  599. }
  600. static void audio_line_place_data_pos(struct audio_line *line,
  601. const struct audio_data *data, size_t position)
  602. {
  603. bool planar = line->audio->planes > 1;
  604. size_t total_num = data->frames * (planar ? 1 : line->audio->channels);
  605. size_t total_size = data->frames * line->audio->block_size;
  606. for (size_t i = 0; i < line->audio->planes; i++) {
  607. da_copy_array(line->volume_buffers[i], data->data[i],
  608. total_size);
  609. uint8_t *array = line->volume_buffers[i].array;
  610. switch (line->audio->info.format) {
  611. case AUDIO_FORMAT_U8BIT:
  612. case AUDIO_FORMAT_U8BIT_PLANAR:
  613. mul_vol_u8bit(array, data->volume, total_num);
  614. break;
  615. case AUDIO_FORMAT_16BIT:
  616. case AUDIO_FORMAT_16BIT_PLANAR:
  617. mul_vol_16bit(array, data->volume, total_num);
  618. break;
  619. case AUDIO_FORMAT_32BIT:
  620. case AUDIO_FORMAT_32BIT_PLANAR:
  621. mul_vol_32bit(array, data->volume, total_num);
  622. break;
  623. case AUDIO_FORMAT_FLOAT:
  624. case AUDIO_FORMAT_FLOAT_PLANAR:
  625. mul_vol_float(array, data->volume, total_num);
  626. break;
  627. case AUDIO_FORMAT_UNKNOWN:
  628. blog(LOG_ERROR, "audio_line_place_data_pos: "
  629. "Unknown format");
  630. break;
  631. }
  632. circlebuf_place(&line->buffers[i], position,
  633. line->volume_buffers[i].array, total_size);
  634. }
  635. }
  636. void audio_line_place_data(struct audio_line *line,
  637. const struct audio_data *data)
  638. {
  639. size_t pos = ts_diff_bytes(line->audio, data->timestamp,
  640. line->base_timestamp);
  641. #ifdef DEBUG_AUDIO
  642. blog(LOG_DEBUG, "data->timestamp: %llu, line->base_timestamp: %llu, "
  643. "pos: %lu, bytes: %lu, buf size: %lu",
  644. data->timestamp, line->base_timestamp, pos,
  645. data->frames * line->audio->block_size,
  646. line->buffers[0].size);
  647. #endif
  648. audio_line_place_data_pos(line, data, pos);
  649. }
  650. void audio_line_output(audio_line_t line, const struct audio_data *data)
  651. {
  652. /* TODO: prevent insertation of data too far away from expected
  653. * audio timing */
  654. pthread_mutex_lock(&line->mutex);
  655. if (!line->buffers[0].size) {
  656. /* XXX: not entirely sure if this is the wisest course of
  657. * action in all circumstances */
  658. line->base_timestamp = data->timestamp -
  659. line->audio->info.buffer_ms * 1000000;
  660. audio_line_place_data(line, data);
  661. } else if (line->base_timestamp <= data->timestamp) {
  662. audio_line_place_data(line, data);
  663. } else {
  664. blog(LOG_DEBUG, "Bad timestamp for audio line '%s', "
  665. "data->timestamp: %"PRIu64", "
  666. "line->base_timestamp: %"PRIu64". This can "
  667. "sometimes happen when there's a pause in "
  668. "the threads.", line->name, data->timestamp,
  669. line->base_timestamp);
  670. }
  671. pthread_mutex_unlock(&line->mutex);
  672. }