obs-audio-controls.c 23 KB

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