1
0

obs-audio-controls.c 24 KB

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