obs-audio-controls.c 24 KB

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