obs-audio-controls.c 23 KB

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