obs-audio-controls.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #pragma once
  15. #include "obs.h"
  16. /**
  17. * @file
  18. * @brief header for audio controls
  19. *
  20. * @brief Audio controls for use in GUIs
  21. */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /**
  26. * @brief Fader types
  27. */
  28. enum obs_fader_type {
  29. /**
  30. * @brief A simple cubic fader for controlling audio levels
  31. *
  32. * This is a very common type of software fader since it yields good
  33. * results while being quite performant.
  34. * The input value is mapped to mul values with the simple formula x^3.
  35. */
  36. OBS_FADER_CUBIC,
  37. /**
  38. * @brief A fader compliant to IEC 60-268-18
  39. *
  40. * This type of fader has several segments with different slopes that
  41. * map deflection linearly to dB values. The segments are defined as
  42. * in the following table:
  43. *
  44. @code
  45. Deflection | Volume
  46. ------------------------------------------
  47. [ 100 %, 75 % ] | [ 0 dB, -9 dB ]
  48. [ 75 %, 50 % ] | [ -9 dB, -20 dB ]
  49. [ 50 %, 30 % ] | [ -20 dB, -30 dB ]
  50. [ 30 %, 15 % ] | [ -30 dB, -40 dB ]
  51. [ 15 %, 7.5 % ] | [ -40 dB, -50 dB ]
  52. [ 7.5 %, 2.5 % ] | [ -50 dB, -60 dB ]
  53. [ 2.5 %, 0 % ] | [ -60 dB, -inf dB ]
  54. @endcode
  55. */
  56. OBS_FADER_IEC,
  57. /**
  58. * @brief Logarithmic fader
  59. */
  60. OBS_FADER_LOG
  61. };
  62. /**
  63. * @brief Peak meter types
  64. */
  65. enum obs_peak_meter_type {
  66. /**
  67. * @brief A simple peak meter measuring the maximum of all samples.
  68. *
  69. * This was a very common type of peak meter used for audio, but
  70. * is not very accurate with regards to further audio processing.
  71. */
  72. SAMPLE_PEAK_METER,
  73. /**
  74. * @brief An accurate peak meter measure the maximum of inter-samples.
  75. *
  76. * This meter is more computational intensive due to 4x oversampling
  77. * to determine the true peak to an accuracy of +/- 0.5 dB.
  78. */
  79. TRUE_PEAK_METER
  80. };
  81. /**
  82. * @brief Create a fader
  83. * @param type the type of the fader
  84. * @return pointer to the fader object
  85. *
  86. * A fader object is used to map input values from a gui element to dB and
  87. * subsequently multiplier values used by libobs to mix audio.
  88. * The current "position" of the fader is internally stored as dB value.
  89. */
  90. EXPORT obs_fader_t *obs_fader_create(enum obs_fader_type type);
  91. /**
  92. * @brief Destroy a fader
  93. * @param fader pointer to the fader object
  94. *
  95. * Destroy the fader and free all related data
  96. */
  97. EXPORT void obs_fader_destroy(obs_fader_t *fader);
  98. /**
  99. * @brief Set the fader dB value
  100. * @param fader pointer to the fader object
  101. * @param db new dB value
  102. * @return true if value was set without clamping
  103. */
  104. EXPORT bool obs_fader_set_db(obs_fader_t *fader, const float db);
  105. /**
  106. * @brief Get the current fader dB value
  107. * @param fader pointer to the fader object
  108. * @return current fader dB value
  109. */
  110. EXPORT float obs_fader_get_db(obs_fader_t *fader);
  111. /**
  112. * @brief Set the fader value from deflection
  113. * @param fader pointer to the fader object
  114. * @param def new deflection
  115. * @return true if value was set without clamping
  116. *
  117. * This sets the new fader value from the supplied deflection, in case the
  118. * resulting value was clamped due to limits this function will return false.
  119. * The deflection is typically in the range [0.0, 1.0] but may be higher in
  120. * order to provide some amplification. In order for this to work the high dB
  121. * limit has to be set.
  122. */
  123. EXPORT bool obs_fader_set_deflection(obs_fader_t *fader, const float def);
  124. /**
  125. * @brief Get the current fader deflection
  126. * @param fader pointer to the fader object
  127. * @return current fader deflection
  128. */
  129. EXPORT float obs_fader_get_deflection(obs_fader_t *fader);
  130. /**
  131. * @brief Set the fader value from multiplier
  132. * @param fader pointer to the fader object
  133. * @return true if the value was set without clamping
  134. */
  135. EXPORT bool obs_fader_set_mul(obs_fader_t *fader, const float mul);
  136. /**
  137. * @brief Get the current fader multiplier value
  138. * @param fader pointer to the fader object
  139. * @return current fader multiplier
  140. */
  141. EXPORT float obs_fader_get_mul(obs_fader_t *fader);
  142. /**
  143. * @brief Attach the fader to a source
  144. * @param fader pointer to the fader object
  145. * @param source pointer to the source object
  146. * @return true on success
  147. *
  148. * When the fader is attached to a source it will automatically sync it's state
  149. * to the volume of the source.
  150. */
  151. EXPORT bool obs_fader_attach_source(obs_fader_t *fader, obs_source_t *source);
  152. /**
  153. * @brief Detach the fader from the currently attached source
  154. * @param fader pointer to the fader object
  155. */
  156. EXPORT void obs_fader_detach_source(obs_fader_t *fader);
  157. typedef void (*obs_fader_changed_t)(void *param, float db);
  158. EXPORT void obs_fader_add_callback(obs_fader_t *fader,
  159. obs_fader_changed_t callback, void *param);
  160. EXPORT void obs_fader_remove_callback(obs_fader_t *fader,
  161. obs_fader_changed_t callback,
  162. void *param);
  163. /**
  164. * @brief Create a volume meter
  165. * @param type the mapping type to use for the volume meter
  166. * @return pointer to the volume meter object
  167. *
  168. * A volume meter object is used to prepare the sound levels reported by audio
  169. * sources for display in a GUI.
  170. * It will automatically take source volume into account and map the levels
  171. * to a range [0.0f, 1.0f].
  172. */
  173. EXPORT obs_volmeter_t *obs_volmeter_create(enum obs_fader_type type);
  174. /**
  175. * @brief Destroy a volume meter
  176. * @param volmeter pointer to the volmeter object
  177. *
  178. * Destroy the volume meter and free all related data
  179. */
  180. EXPORT void obs_volmeter_destroy(obs_volmeter_t *volmeter);
  181. /**
  182. * @brief Attach the volume meter to a source
  183. * @param volmeter pointer to the volume meter object
  184. * @param source pointer to the source object
  185. * @return true on success
  186. *
  187. * When the volume meter is attached to a source it will start to listen to
  188. * volume updates on the source and after preparing the data emit its own
  189. * signal.
  190. */
  191. EXPORT bool obs_volmeter_attach_source(obs_volmeter_t *volmeter,
  192. obs_source_t *source);
  193. /**
  194. * @brief Detach the volume meter from the currently attached source
  195. * @param volmeter pointer to the volume meter object
  196. */
  197. EXPORT void obs_volmeter_detach_source(obs_volmeter_t *volmeter);
  198. /**
  199. * @brief Set the peak meter type for the volume meter
  200. * @param volmeter pointer to the volume meter object
  201. * @param peak_meter_type set if true-peak needs to be measured.
  202. */
  203. EXPORT void
  204. obs_volmeter_set_peak_meter_type(obs_volmeter_t *volmeter,
  205. enum obs_peak_meter_type peak_meter_type);
  206. /**
  207. * @brief Set the update interval for the volume meter
  208. * @param volmeter pointer to the volume meter object
  209. * @param ms update interval in ms
  210. *
  211. * This sets the update interval in milliseconds that should be processed before
  212. * the resulting values are emitted by the levels_updated signal. The resulting
  213. * number of audio samples is rounded to an integer.
  214. *
  215. * Please note that due to way obs does receive audio data from the sources
  216. * this is no hard guarantee for the timing of the signal itself. When the
  217. * volume meter receives a chunk of data that is multiple the size of the sample
  218. * interval, all data will be sampled and the values updated accordingly, but
  219. * only the signal for the last segment is actually emitted.
  220. * On the other hand data might be received in a way that will cause the signal
  221. * to be emitted in shorter intervals than specified here under some
  222. * circumstances.
  223. */
  224. OBS_DEPRECATED
  225. EXPORT void obs_volmeter_set_update_interval(obs_volmeter_t *volmeter,
  226. const unsigned int ms);
  227. /**
  228. * @brief Get the update interval currently used for the volume meter
  229. * @param volmeter pointer to the volume meter object
  230. * @return update interval in ms
  231. */
  232. OBS_DEPRECATED
  233. EXPORT unsigned int obs_volmeter_get_update_interval(obs_volmeter_t *volmeter);
  234. /**
  235. * @brief Get the number of channels which are configured for this source.
  236. * @param volmeter pointer to the volume meter object
  237. */
  238. EXPORT int obs_volmeter_get_nr_channels(obs_volmeter_t *volmeter);
  239. typedef void (*obs_volmeter_updated_t)(
  240. void *param, const float magnitude[MAX_AUDIO_CHANNELS],
  241. const float peak[MAX_AUDIO_CHANNELS],
  242. const float input_peak[MAX_AUDIO_CHANNELS]);
  243. EXPORT void obs_volmeter_add_callback(obs_volmeter_t *volmeter,
  244. obs_volmeter_updated_t callback,
  245. void *param);
  246. EXPORT void obs_volmeter_remove_callback(obs_volmeter_t *volmeter,
  247. obs_volmeter_updated_t callback,
  248. void *param);
  249. EXPORT float obs_mul_to_db(float mul);
  250. EXPORT float obs_db_to_mul(float db);
  251. #ifdef __cplusplus
  252. }
  253. #endif