obs-audio-controls.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[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 "util/sse-intrin.h"
  16. #include "util/threading.h"
  17. #include "util/bmem.h"
  18. #include "media-io/audio-math.h"
  19. #include "obs.h"
  20. #include "obs-internal.h"
  21. #include "obs-audio-controls.h"
  22. /* These are pointless warnings generated not by our code, but by a standard
  23. * library macro, INFINITY */
  24. #ifdef _MSC_VER
  25. #pragma warning(disable : 4056)
  26. #pragma warning(disable : 4756)
  27. #endif
  28. #define CLAMP(x, min, max) ((x) < min ? min : ((x) > max ? max : (x)))
  29. struct fader_cb {
  30. obs_fader_changed_t callback;
  31. void *param;
  32. };
  33. struct obs_fader {
  34. pthread_mutex_t mutex;
  35. obs_fader_conversion_t def_to_db;
  36. obs_fader_conversion_t db_to_def;
  37. obs_source_t *source;
  38. enum obs_fader_type type;
  39. float max_db;
  40. float min_db;
  41. float cur_db;
  42. bool ignore_next_signal;
  43. pthread_mutex_t callback_mutex;
  44. DARRAY(struct fader_cb) callbacks;
  45. };
  46. struct meter_cb {
  47. obs_volmeter_updated_t callback;
  48. void *param;
  49. };
  50. struct obs_volmeter {
  51. pthread_mutex_t mutex;
  52. obs_source_t *source;
  53. enum obs_fader_type type;
  54. float cur_db;
  55. pthread_mutex_t callback_mutex;
  56. DARRAY(struct meter_cb) callbacks;
  57. enum obs_peak_meter_type peak_meter_type;
  58. unsigned int update_ms;
  59. float prev_samples[MAX_AUDIO_CHANNELS][4];
  60. float magnitude[MAX_AUDIO_CHANNELS];
  61. float peak[MAX_AUDIO_CHANNELS];
  62. };
  63. static float cubic_def_to_db(const float def)
  64. {
  65. if (def == 1.0f)
  66. return 0.0f;
  67. else if (def <= 0.0f)
  68. return -INFINITY;
  69. return mul_to_db(def * def * def);
  70. }
  71. static float cubic_db_to_def(const float db)
  72. {
  73. if (db == 0.0f)
  74. return 1.0f;
  75. else if (db == -INFINITY)
  76. return 0.0f;
  77. return cbrtf(db_to_mul(db));
  78. }
  79. static float iec_def_to_db(const float def)
  80. {
  81. if (def == 1.0f)
  82. return 0.0f;
  83. else if (def <= 0.0f)
  84. return -INFINITY;
  85. float db;
  86. if (def >= 0.75f)
  87. db = (def - 1.0f) / 0.25f * 9.0f;
  88. else if (def >= 0.5f)
  89. db = (def - 0.75f) / 0.25f * 11.0f - 9.0f;
  90. else if (def >= 0.3f)
  91. db = (def - 0.5f) / 0.2f * 10.0f - 20.0f;
  92. else if (def >= 0.15f)
  93. db = (def - 0.3f) / 0.15f * 10.0f - 30.0f;
  94. else if (def >= 0.075f)
  95. db = (def - 0.15f) / 0.075f * 10.0f - 40.0f;
  96. else if (def >= 0.025f)
  97. db = (def - 0.075f) / 0.05f * 10.0f - 50.0f;
  98. else if (def >= 0.001f)
  99. db = (def - 0.025f) / 0.025f * 90.0f - 60.0f;
  100. else
  101. db = -INFINITY;
  102. return db;
  103. }
  104. static float iec_db_to_def(const float db)
  105. {
  106. if (db == 0.0f)
  107. return 1.0f;
  108. else if (db == -INFINITY)
  109. return 0.0f;
  110. float def;
  111. if (db >= -9.0f)
  112. def = (db + 9.0f) / 9.0f * 0.25f + 0.75f;
  113. else if (db >= -20.0f)
  114. def = (db + 20.0f) / 11.0f * 0.25f + 0.5f;
  115. else if (db >= -30.0f)
  116. def = (db + 30.0f) / 10.0f * 0.2f + 0.3f;
  117. else if (db >= -40.0f)
  118. def = (db + 40.0f) / 10.0f * 0.15f + 0.15f;
  119. else if (db >= -50.0f)
  120. def = (db + 50.0f) / 10.0f * 0.075f + 0.075f;
  121. else if (db >= -60.0f)
  122. def = (db + 60.0f) / 10.0f * 0.05f + 0.025f;
  123. else if (db >= -114.0f)
  124. def = (db + 150.0f) / 90.0f * 0.025f;
  125. else
  126. def = 0.0f;
  127. return def;
  128. }
  129. #define LOG_OFFSET_DB 6.0f
  130. #define LOG_RANGE_DB 96.0f
  131. /* equals -log10f(LOG_OFFSET_DB) */
  132. #define LOG_OFFSET_VAL -0.77815125038364363f
  133. /* equals -log10f(-LOG_RANGE_DB + LOG_OFFSET_DB) */
  134. #define LOG_RANGE_VAL -2.00860017176191756f
  135. static float log_def_to_db(const float def)
  136. {
  137. if (def >= 1.0f)
  138. return 0.0f;
  139. else if (def <= 0.0f)
  140. return -INFINITY;
  141. return -(LOG_RANGE_DB + LOG_OFFSET_DB) *
  142. powf((LOG_RANGE_DB + LOG_OFFSET_DB) / LOG_OFFSET_DB,
  143. -def) +
  144. LOG_OFFSET_DB;
  145. }
  146. static float log_db_to_def(const float db)
  147. {
  148. if (db >= 0.0f)
  149. return 1.0f;
  150. else if (db <= -96.0f)
  151. return 0.0f;
  152. return (-log10f(-db + LOG_OFFSET_DB) - LOG_RANGE_VAL) /
  153. (LOG_OFFSET_VAL - LOG_RANGE_VAL);
  154. }
  155. static void signal_volume_changed(struct obs_fader *fader, const float db)
  156. {
  157. pthread_mutex_lock(&fader->callback_mutex);
  158. for (size_t i = fader->callbacks.num; i > 0; i--) {
  159. struct fader_cb cb = fader->callbacks.array[i - 1];
  160. cb.callback(cb.param, db);
  161. }
  162. pthread_mutex_unlock(&fader->callback_mutex);
  163. }
  164. static void signal_levels_updated(struct obs_volmeter *volmeter,
  165. const float magnitude[MAX_AUDIO_CHANNELS],
  166. const float peak[MAX_AUDIO_CHANNELS],
  167. const float input_peak[MAX_AUDIO_CHANNELS])
  168. {
  169. pthread_mutex_lock(&volmeter->callback_mutex);
  170. for (size_t i = volmeter->callbacks.num; i > 0; i--) {
  171. struct meter_cb cb = volmeter->callbacks.array[i - 1];
  172. cb.callback(cb.param, magnitude, peak, input_peak);
  173. }
  174. pthread_mutex_unlock(&volmeter->callback_mutex);
  175. }
  176. static void fader_source_volume_changed(void *vptr, calldata_t *calldata)
  177. {
  178. struct obs_fader *fader = (struct obs_fader *)vptr;
  179. pthread_mutex_lock(&fader->mutex);
  180. if (fader->ignore_next_signal) {
  181. fader->ignore_next_signal = false;
  182. pthread_mutex_unlock(&fader->mutex);
  183. return;
  184. }
  185. const float mul = (float)calldata_float(calldata, "volume");
  186. const float db = mul_to_db(mul);
  187. fader->cur_db = db;
  188. pthread_mutex_unlock(&fader->mutex);
  189. signal_volume_changed(fader, db);
  190. }
  191. static void volmeter_source_volume_changed(void *vptr, calldata_t *calldata)
  192. {
  193. struct obs_volmeter *volmeter = (struct obs_volmeter *)vptr;
  194. pthread_mutex_lock(&volmeter->mutex);
  195. float mul = (float)calldata_float(calldata, "volume");
  196. volmeter->cur_db = mul_to_db(mul);
  197. pthread_mutex_unlock(&volmeter->mutex);
  198. }
  199. static void fader_source_destroyed(void *vptr, calldata_t *calldata)
  200. {
  201. UNUSED_PARAMETER(calldata);
  202. struct obs_fader *fader = (struct obs_fader *)vptr;
  203. obs_fader_detach_source(fader);
  204. }
  205. static void volmeter_source_destroyed(void *vptr, calldata_t *calldata)
  206. {
  207. UNUSED_PARAMETER(calldata);
  208. struct obs_volmeter *volmeter = (struct obs_volmeter *)vptr;
  209. obs_volmeter_detach_source(volmeter);
  210. }
  211. static int get_nr_channels_from_audio_data(const struct audio_data *data)
  212. {
  213. int nr_channels = 0;
  214. for (int i = 0; i < MAX_AV_PLANES; i++) {
  215. if (data->data[i])
  216. nr_channels++;
  217. }
  218. return CLAMP(nr_channels, 0, MAX_AUDIO_CHANNELS);
  219. }
  220. /* msb(h, g, f, e) lsb(d, c, b, a) --> msb(h, h, g, f) lsb(e, d, c, b)
  221. */
  222. #define SHIFT_RIGHT_2PS(msb, lsb) \
  223. { \
  224. __m128 tmp = \
  225. _mm_shuffle_ps(lsb, msb, _MM_SHUFFLE(0, 0, 3, 3)); \
  226. lsb = _mm_shuffle_ps(lsb, tmp, _MM_SHUFFLE(2, 1, 2, 1)); \
  227. msb = _mm_shuffle_ps(msb, msb, _MM_SHUFFLE(3, 3, 2, 1)); \
  228. }
  229. /* x(d, c, b, a) --> (|d|, |c|, |b|, |a|)
  230. */
  231. #define abs_ps(v) _mm_andnot_ps(_mm_set1_ps(-0.f), v)
  232. /* Take cross product of a vector with a matrix resulting in vector.
  233. */
  234. #define VECTOR_MATRIX_CROSS_PS(out, v, m0, m1, m2, m3) \
  235. { \
  236. out = _mm_mul_ps(v, m0); \
  237. __m128 mul1 = _mm_mul_ps(v, m1); \
  238. __m128 mul2 = _mm_mul_ps(v, m2); \
  239. __m128 mul3 = _mm_mul_ps(v, m3); \
  240. \
  241. _MM_TRANSPOSE4_PS(out, mul1, mul2, mul3); \
  242. \
  243. out = _mm_add_ps(out, mul1); \
  244. out = _mm_add_ps(out, mul2); \
  245. out = _mm_add_ps(out, mul3); \
  246. }
  247. /* x4(d, c, b, a) --> max(a, b, c, d)
  248. */
  249. #define hmax_ps(r, x4) \
  250. do { \
  251. float x4_mem[4]; \
  252. _mm_storeu_ps(x4_mem, x4); \
  253. r = x4_mem[0]; \
  254. r = fmaxf(r, x4_mem[1]); \
  255. r = fmaxf(r, x4_mem[2]); \
  256. r = fmaxf(r, x4_mem[3]); \
  257. } while (false)
  258. /* Calculate the true peak over a set of samples.
  259. * The algorithm implements 5x oversampling by using Whittaker-Shannon
  260. * interpolation over four samples.
  261. *
  262. * The four samples have location t=-1.5, -0.5, +0.5, +1.5
  263. * The oversamples are taken at locations t=-0.3, -0.1, +0.1, +0.3
  264. *
  265. * @param previous_samples Last 4 samples from the previous iteration.
  266. * @param samples The samples to find the peak in.
  267. * @param nr_samples Number of sets of 4 samples.
  268. * @returns 5 times oversampled true-peak from the set of samples.
  269. */
  270. static float get_true_peak(__m128 previous_samples, const float *samples,
  271. size_t nr_samples)
  272. {
  273. /* These are normalized-sinc parameters for interpolating over sample
  274. * points which are located at x-coords: -1.5, -0.5, +0.5, +1.5.
  275. * And oversample points at x-coords: -0.3, -0.1, 0.1, 0.3. */
  276. const __m128 m3 =
  277. _mm_set_ps(-0.155915f, 0.935489f, 0.233872f, -0.103943f);
  278. const __m128 m1 =
  279. _mm_set_ps(-0.216236f, 0.756827f, 0.504551f, -0.189207f);
  280. const __m128 p1 =
  281. _mm_set_ps(-0.189207f, 0.504551f, 0.756827f, -0.216236f);
  282. const __m128 p3 =
  283. _mm_set_ps(-0.103943f, 0.233872f, 0.935489f, -0.155915f);
  284. __m128 work = previous_samples;
  285. __m128 peak = previous_samples;
  286. for (size_t i = 0; (i + 3) < nr_samples; i += 4) {
  287. __m128 new_work = _mm_load_ps(&samples[i]);
  288. __m128 intrp_samples;
  289. /* Include the actual sample values in the peak. */
  290. __m128 abs_new_work = abs_ps(new_work);
  291. peak = _mm_max_ps(peak, abs_new_work);
  292. /* Shift in the next point. */
  293. SHIFT_RIGHT_2PS(new_work, work);
  294. VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
  295. peak = _mm_max_ps(peak, abs_ps(intrp_samples));
  296. SHIFT_RIGHT_2PS(new_work, work);
  297. VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
  298. peak = _mm_max_ps(peak, abs_ps(intrp_samples));
  299. SHIFT_RIGHT_2PS(new_work, work);
  300. VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
  301. peak = _mm_max_ps(peak, abs_ps(intrp_samples));
  302. SHIFT_RIGHT_2PS(new_work, work);
  303. VECTOR_MATRIX_CROSS_PS(intrp_samples, work, m3, m1, p1, p3);
  304. peak = _mm_max_ps(peak, abs_ps(intrp_samples));
  305. }
  306. float r;
  307. hmax_ps(r, peak);
  308. return r;
  309. }
  310. /* points contain the first four samples to calculate the sinc interpolation
  311. * over. They will have come from a previous iteration.
  312. */
  313. static float get_sample_peak(__m128 previous_samples, const float *samples,
  314. size_t nr_samples)
  315. {
  316. __m128 peak = previous_samples;
  317. for (size_t i = 0; (i + 3) < nr_samples; i += 4) {
  318. __m128 new_work = _mm_load_ps(&samples[i]);
  319. peak = _mm_max_ps(peak, abs_ps(new_work));
  320. }
  321. float r;
  322. hmax_ps(r, peak);
  323. return r;
  324. }
  325. static void volmeter_process_peak_last_samples(obs_volmeter_t *volmeter,
  326. int channel_nr, float *samples,
  327. size_t nr_samples)
  328. {
  329. /* Take the last 4 samples that need to be used for the next peak
  330. * calculation. If there are less than 4 samples in total the new
  331. * samples shift out the old samples. */
  332. switch (nr_samples) {
  333. case 0:
  334. break;
  335. case 1:
  336. volmeter->prev_samples[channel_nr][0] =
  337. volmeter->prev_samples[channel_nr][1];
  338. volmeter->prev_samples[channel_nr][1] =
  339. volmeter->prev_samples[channel_nr][2];
  340. volmeter->prev_samples[channel_nr][2] =
  341. volmeter->prev_samples[channel_nr][3];
  342. volmeter->prev_samples[channel_nr][3] = samples[nr_samples - 1];
  343. break;
  344. case 2:
  345. volmeter->prev_samples[channel_nr][0] =
  346. volmeter->prev_samples[channel_nr][2];
  347. volmeter->prev_samples[channel_nr][1] =
  348. volmeter->prev_samples[channel_nr][3];
  349. volmeter->prev_samples[channel_nr][2] = samples[nr_samples - 2];
  350. volmeter->prev_samples[channel_nr][3] = samples[nr_samples - 1];
  351. break;
  352. case 3:
  353. volmeter->prev_samples[channel_nr][0] =
  354. volmeter->prev_samples[channel_nr][3];
  355. volmeter->prev_samples[channel_nr][1] = samples[nr_samples - 3];
  356. volmeter->prev_samples[channel_nr][2] = samples[nr_samples - 2];
  357. volmeter->prev_samples[channel_nr][3] = samples[nr_samples - 1];
  358. break;
  359. default:
  360. volmeter->prev_samples[channel_nr][0] = samples[nr_samples - 4];
  361. volmeter->prev_samples[channel_nr][1] = samples[nr_samples - 3];
  362. volmeter->prev_samples[channel_nr][2] = samples[nr_samples - 2];
  363. volmeter->prev_samples[channel_nr][3] = samples[nr_samples - 1];
  364. }
  365. }
  366. static void volmeter_process_peak(obs_volmeter_t *volmeter,
  367. const struct audio_data *data,
  368. int nr_channels)
  369. {
  370. int nr_samples = data->frames;
  371. int channel_nr = 0;
  372. for (int plane_nr = 0; channel_nr < nr_channels; plane_nr++) {
  373. float *samples = (float *)data->data[plane_nr];
  374. if (!samples) {
  375. continue;
  376. }
  377. if (((uintptr_t)samples & 0xf) > 0) {
  378. printf("Audio plane %i is not aligned %p skipping "
  379. "peak volume measurement.\n",
  380. plane_nr, samples);
  381. volmeter->peak[channel_nr] = 1.0;
  382. channel_nr++;
  383. continue;
  384. }
  385. /* volmeter->prev_samples may not be aligned to 16 bytes;
  386. * use unaligned load. */
  387. __m128 previous_samples =
  388. _mm_loadu_ps(volmeter->prev_samples[channel_nr]);
  389. float peak;
  390. switch (volmeter->peak_meter_type) {
  391. case TRUE_PEAK_METER:
  392. peak = get_true_peak(previous_samples, samples,
  393. nr_samples);
  394. break;
  395. case SAMPLE_PEAK_METER:
  396. default:
  397. peak = get_sample_peak(previous_samples, samples,
  398. nr_samples);
  399. break;
  400. }
  401. volmeter_process_peak_last_samples(volmeter, channel_nr,
  402. samples, nr_samples);
  403. volmeter->peak[channel_nr] = peak;
  404. channel_nr++;
  405. }
  406. /* Clear the peak of the channels that have not been handled. */
  407. for (; channel_nr < MAX_AUDIO_CHANNELS; channel_nr++) {
  408. volmeter->peak[channel_nr] = 0.0;
  409. }
  410. }
  411. static void volmeter_process_magnitude(obs_volmeter_t *volmeter,
  412. const struct audio_data *data,
  413. int nr_channels)
  414. {
  415. size_t nr_samples = data->frames;
  416. int channel_nr = 0;
  417. for (int plane_nr = 0; channel_nr < nr_channels; plane_nr++) {
  418. float *samples = (float *)data->data[plane_nr];
  419. if (!samples) {
  420. continue;
  421. }
  422. float sum = 0.0;
  423. for (size_t i = 0; i < nr_samples; i++) {
  424. float sample = samples[i];
  425. sum += sample * sample;
  426. }
  427. volmeter->magnitude[channel_nr] = sqrtf(sum / nr_samples);
  428. channel_nr++;
  429. }
  430. }
  431. static void volmeter_process_audio_data(obs_volmeter_t *volmeter,
  432. const struct audio_data *data)
  433. {
  434. int nr_channels = get_nr_channels_from_audio_data(data);
  435. volmeter_process_peak(volmeter, data, nr_channels);
  436. volmeter_process_magnitude(volmeter, data, nr_channels);
  437. }
  438. static void volmeter_source_data_received(void *vptr, obs_source_t *source,
  439. const struct audio_data *data,
  440. bool muted)
  441. {
  442. struct obs_volmeter *volmeter = (struct obs_volmeter *)vptr;
  443. float mul;
  444. float magnitude[MAX_AUDIO_CHANNELS];
  445. float peak[MAX_AUDIO_CHANNELS];
  446. float input_peak[MAX_AUDIO_CHANNELS];
  447. pthread_mutex_lock(&volmeter->mutex);
  448. volmeter_process_audio_data(volmeter, data);
  449. // Adjust magnitude/peak based on the volume level set by the user.
  450. // And convert to dB.
  451. mul = muted && !obs_source_muted(source) ? 0.0f
  452. : db_to_mul(volmeter->cur_db);
  453. for (int channel_nr = 0; channel_nr < MAX_AUDIO_CHANNELS;
  454. channel_nr++) {
  455. magnitude[channel_nr] =
  456. mul_to_db(volmeter->magnitude[channel_nr] * mul);
  457. peak[channel_nr] = mul_to_db(volmeter->peak[channel_nr] * mul);
  458. /* The input-peak is NOT adjusted with volume, so that the user
  459. * can check the input-gain. */
  460. input_peak[channel_nr] = mul_to_db(volmeter->peak[channel_nr]);
  461. }
  462. pthread_mutex_unlock(&volmeter->mutex);
  463. signal_levels_updated(volmeter, magnitude, peak, input_peak);
  464. }
  465. obs_fader_t *obs_fader_create(enum obs_fader_type type)
  466. {
  467. struct obs_fader *fader = bzalloc(sizeof(struct obs_fader));
  468. if (!fader)
  469. return NULL;
  470. pthread_mutex_init_value(&fader->mutex);
  471. pthread_mutex_init_value(&fader->callback_mutex);
  472. if (pthread_mutex_init(&fader->mutex, NULL) != 0)
  473. goto fail;
  474. if (pthread_mutex_init(&fader->callback_mutex, NULL) != 0)
  475. goto fail;
  476. switch (type) {
  477. case OBS_FADER_CUBIC:
  478. fader->def_to_db = cubic_def_to_db;
  479. fader->db_to_def = cubic_db_to_def;
  480. fader->max_db = 0.0f;
  481. fader->min_db = -INFINITY;
  482. break;
  483. case OBS_FADER_IEC:
  484. fader->def_to_db = iec_def_to_db;
  485. fader->db_to_def = iec_db_to_def;
  486. fader->max_db = 0.0f;
  487. fader->min_db = -INFINITY;
  488. break;
  489. case OBS_FADER_LOG:
  490. fader->def_to_db = log_def_to_db;
  491. fader->db_to_def = log_db_to_def;
  492. fader->max_db = 0.0f;
  493. fader->min_db = -96.0f;
  494. break;
  495. default:
  496. goto fail;
  497. break;
  498. }
  499. fader->type = type;
  500. return fader;
  501. fail:
  502. obs_fader_destroy(fader);
  503. return NULL;
  504. }
  505. void obs_fader_destroy(obs_fader_t *fader)
  506. {
  507. if (!fader)
  508. return;
  509. obs_fader_detach_source(fader);
  510. da_free(fader->callbacks);
  511. pthread_mutex_destroy(&fader->callback_mutex);
  512. pthread_mutex_destroy(&fader->mutex);
  513. bfree(fader);
  514. }
  515. bool obs_fader_set_db(obs_fader_t *fader, const float db)
  516. {
  517. if (!fader)
  518. return false;
  519. pthread_mutex_lock(&fader->mutex);
  520. bool clamped = false;
  521. fader->cur_db = db;
  522. if (fader->cur_db > fader->max_db) {
  523. fader->cur_db = fader->max_db;
  524. clamped = true;
  525. }
  526. if (fader->cur_db < fader->min_db) {
  527. fader->cur_db = -INFINITY;
  528. clamped = true;
  529. }
  530. fader->ignore_next_signal = true;
  531. obs_source_t *src = fader->source;
  532. const float mul = db_to_mul(fader->cur_db);
  533. pthread_mutex_unlock(&fader->mutex);
  534. if (src)
  535. obs_source_set_volume(src, mul);
  536. return !clamped;
  537. }
  538. float obs_fader_get_db(obs_fader_t *fader)
  539. {
  540. if (!fader)
  541. return 0.0f;
  542. pthread_mutex_lock(&fader->mutex);
  543. const float db = fader->cur_db;
  544. pthread_mutex_unlock(&fader->mutex);
  545. return db;
  546. }
  547. bool obs_fader_set_deflection(obs_fader_t *fader, const float def)
  548. {
  549. if (!fader)
  550. return false;
  551. return obs_fader_set_db(fader, fader->def_to_db(def));
  552. }
  553. float obs_fader_get_deflection(obs_fader_t *fader)
  554. {
  555. if (!fader)
  556. return 0.0f;
  557. pthread_mutex_lock(&fader->mutex);
  558. const float def = fader->db_to_def(fader->cur_db);
  559. pthread_mutex_unlock(&fader->mutex);
  560. return def;
  561. }
  562. bool obs_fader_set_mul(obs_fader_t *fader, const float mul)
  563. {
  564. if (!fader)
  565. return false;
  566. return obs_fader_set_db(fader, mul_to_db(mul));
  567. }
  568. float obs_fader_get_mul(obs_fader_t *fader)
  569. {
  570. if (!fader)
  571. return 0.0f;
  572. pthread_mutex_lock(&fader->mutex);
  573. const float mul = db_to_mul(fader->cur_db);
  574. pthread_mutex_unlock(&fader->mutex);
  575. return mul;
  576. }
  577. bool obs_fader_attach_source(obs_fader_t *fader, obs_source_t *source)
  578. {
  579. signal_handler_t *sh;
  580. float vol;
  581. if (!fader || !source)
  582. return false;
  583. obs_fader_detach_source(fader);
  584. sh = obs_source_get_signal_handler(source);
  585. signal_handler_connect(sh, "volume", fader_source_volume_changed,
  586. fader);
  587. signal_handler_connect(sh, "destroy", fader_source_destroyed, fader);
  588. vol = obs_source_get_volume(source);
  589. pthread_mutex_lock(&fader->mutex);
  590. fader->source = source;
  591. fader->cur_db = mul_to_db(vol);
  592. pthread_mutex_unlock(&fader->mutex);
  593. return true;
  594. }
  595. void obs_fader_detach_source(obs_fader_t *fader)
  596. {
  597. signal_handler_t *sh;
  598. obs_source_t *source;
  599. if (!fader)
  600. return;
  601. pthread_mutex_lock(&fader->mutex);
  602. source = fader->source;
  603. fader->source = NULL;
  604. pthread_mutex_unlock(&fader->mutex);
  605. if (!source)
  606. return;
  607. sh = obs_source_get_signal_handler(source);
  608. signal_handler_disconnect(sh, "volume", fader_source_volume_changed,
  609. fader);
  610. signal_handler_disconnect(sh, "destroy", fader_source_destroyed, fader);
  611. }
  612. void obs_fader_add_callback(obs_fader_t *fader, obs_fader_changed_t callback,
  613. void *param)
  614. {
  615. struct fader_cb cb = {callback, param};
  616. if (!obs_ptr_valid(fader, "obs_fader_add_callback"))
  617. return;
  618. pthread_mutex_lock(&fader->callback_mutex);
  619. da_push_back(fader->callbacks, &cb);
  620. pthread_mutex_unlock(&fader->callback_mutex);
  621. }
  622. void obs_fader_remove_callback(obs_fader_t *fader, obs_fader_changed_t callback,
  623. void *param)
  624. {
  625. struct fader_cb cb = {callback, param};
  626. if (!obs_ptr_valid(fader, "obs_fader_remove_callback"))
  627. return;
  628. pthread_mutex_lock(&fader->callback_mutex);
  629. da_erase_item(fader->callbacks, &cb);
  630. pthread_mutex_unlock(&fader->callback_mutex);
  631. }
  632. obs_volmeter_t *obs_volmeter_create(enum obs_fader_type type)
  633. {
  634. struct obs_volmeter *volmeter = bzalloc(sizeof(struct obs_volmeter));
  635. if (!volmeter)
  636. return NULL;
  637. pthread_mutex_init_value(&volmeter->mutex);
  638. pthread_mutex_init_value(&volmeter->callback_mutex);
  639. if (pthread_mutex_init(&volmeter->mutex, NULL) != 0)
  640. goto fail;
  641. if (pthread_mutex_init(&volmeter->callback_mutex, NULL) != 0)
  642. goto fail;
  643. volmeter->type = type;
  644. return volmeter;
  645. fail:
  646. obs_volmeter_destroy(volmeter);
  647. return NULL;
  648. }
  649. void obs_volmeter_destroy(obs_volmeter_t *volmeter)
  650. {
  651. if (!volmeter)
  652. return;
  653. obs_volmeter_detach_source(volmeter);
  654. da_free(volmeter->callbacks);
  655. pthread_mutex_destroy(&volmeter->callback_mutex);
  656. pthread_mutex_destroy(&volmeter->mutex);
  657. bfree(volmeter);
  658. }
  659. bool obs_volmeter_attach_source(obs_volmeter_t *volmeter, obs_source_t *source)
  660. {
  661. signal_handler_t *sh;
  662. float vol;
  663. if (!volmeter || !source)
  664. return false;
  665. obs_volmeter_detach_source(volmeter);
  666. sh = obs_source_get_signal_handler(source);
  667. signal_handler_connect(sh, "volume", volmeter_source_volume_changed,
  668. volmeter);
  669. signal_handler_connect(sh, "destroy", volmeter_source_destroyed,
  670. volmeter);
  671. obs_source_add_audio_capture_callback(
  672. source, volmeter_source_data_received, volmeter);
  673. vol = obs_source_get_volume(source);
  674. pthread_mutex_lock(&volmeter->mutex);
  675. volmeter->source = source;
  676. volmeter->cur_db = mul_to_db(vol);
  677. pthread_mutex_unlock(&volmeter->mutex);
  678. return true;
  679. }
  680. void obs_volmeter_detach_source(obs_volmeter_t *volmeter)
  681. {
  682. signal_handler_t *sh;
  683. obs_source_t *source;
  684. if (!volmeter)
  685. return;
  686. pthread_mutex_lock(&volmeter->mutex);
  687. source = volmeter->source;
  688. volmeter->source = NULL;
  689. pthread_mutex_unlock(&volmeter->mutex);
  690. if (!source)
  691. return;
  692. sh = obs_source_get_signal_handler(source);
  693. signal_handler_disconnect(sh, "volume", volmeter_source_volume_changed,
  694. volmeter);
  695. signal_handler_disconnect(sh, "destroy", volmeter_source_destroyed,
  696. volmeter);
  697. obs_source_remove_audio_capture_callback(
  698. source, volmeter_source_data_received, volmeter);
  699. }
  700. void obs_volmeter_set_peak_meter_type(obs_volmeter_t *volmeter,
  701. enum obs_peak_meter_type peak_meter_type)
  702. {
  703. pthread_mutex_lock(&volmeter->mutex);
  704. volmeter->peak_meter_type = peak_meter_type;
  705. pthread_mutex_unlock(&volmeter->mutex);
  706. }
  707. int obs_volmeter_get_nr_channels(obs_volmeter_t *volmeter)
  708. {
  709. int source_nr_audio_channels;
  710. int obs_nr_audio_channels;
  711. if (volmeter->source) {
  712. source_nr_audio_channels = get_audio_channels(
  713. volmeter->source->sample_info.speakers);
  714. } else {
  715. source_nr_audio_channels = 0;
  716. }
  717. struct obs_audio_info audio_info;
  718. if (obs_get_audio_info(&audio_info)) {
  719. obs_nr_audio_channels = get_audio_channels(audio_info.speakers);
  720. } else {
  721. obs_nr_audio_channels = 2;
  722. }
  723. return CLAMP(source_nr_audio_channels, 0, obs_nr_audio_channels);
  724. }
  725. void obs_volmeter_add_callback(obs_volmeter_t *volmeter,
  726. obs_volmeter_updated_t callback, void *param)
  727. {
  728. struct meter_cb cb = {callback, param};
  729. if (!obs_ptr_valid(volmeter, "obs_volmeter_add_callback"))
  730. return;
  731. pthread_mutex_lock(&volmeter->callback_mutex);
  732. da_push_back(volmeter->callbacks, &cb);
  733. pthread_mutex_unlock(&volmeter->callback_mutex);
  734. }
  735. void obs_volmeter_remove_callback(obs_volmeter_t *volmeter,
  736. obs_volmeter_updated_t callback, void *param)
  737. {
  738. struct meter_cb cb = {callback, param};
  739. if (!obs_ptr_valid(volmeter, "obs_volmeter_remove_callback"))
  740. return;
  741. pthread_mutex_lock(&volmeter->callback_mutex);
  742. da_erase_item(volmeter->callbacks, &cb);
  743. pthread_mutex_unlock(&volmeter->callback_mutex);
  744. }
  745. float obs_mul_to_db(float mul)
  746. {
  747. return mul_to_db(mul);
  748. }
  749. float obs_db_to_mul(float db)
  750. {
  751. return db_to_mul(db);
  752. }
  753. obs_fader_conversion_t obs_fader_db_to_def(obs_fader_t *fader)
  754. {
  755. return fader->db_to_def;
  756. }