pulseaudio.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-FileCopyrightText: GSConnect Developers https://github.com/GSConnect
  2. //
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. import GIRepository from 'gi://GIRepository';
  5. import GLib from 'gi://GLib';
  6. import GObject from 'gi://GObject';
  7. import Config from '../../config.js';
  8. const Tweener = imports.tweener.tweener;
  9. let Gvc = null;
  10. try {
  11. // Add gnome-shell's typelib dir to the search path
  12. const typelibDir = GLib.build_filenamev([Config.GNOME_SHELL_LIBDIR, 'gnome-shell']);
  13. if (GIRepository.Repository.hasOwnProperty('prepend_search_path')) {
  14. // GNOME <= 48 / GIRepository 2.0
  15. GIRepository.Repository.prepend_search_path(typelibDir);
  16. GIRepository.Repository.prepend_library_path(typelibDir);
  17. } else {
  18. // GNOME 49+ / GIRepository 3.0
  19. const repo = GIRepository.Repository.dup_default();
  20. repo.prepend_search_path(typelibDir);
  21. repo.prepend_library_path(typelibDir);
  22. }
  23. Gvc = (await import('gi://Gvc')).default;
  24. } catch {}
  25. /**
  26. * Extend Gvc.MixerStream with a property for returning a user-visible name
  27. */
  28. if (Gvc) {
  29. Object.defineProperty(Gvc.MixerStream.prototype, 'display_name', {
  30. get: function () {
  31. try {
  32. if (!this.get_ports().length)
  33. return this.description;
  34. return `${this.get_port().human_port} (${this.description})`;
  35. } catch {
  36. return this.description;
  37. }
  38. },
  39. });
  40. }
  41. /**
  42. * A convenience wrapper for Gvc.MixerStream
  43. */
  44. class Stream {
  45. constructor(mixer, stream) {
  46. this._mixer = mixer;
  47. this._stream = stream;
  48. this._max = mixer.get_vol_max_norm();
  49. }
  50. get muted() {
  51. return this._stream.is_muted;
  52. }
  53. set muted(bool) {
  54. this._stream.change_is_muted(bool);
  55. }
  56. // Volume is a double in the range 0-1
  57. get volume() {
  58. return Math.floor(100 * this._stream.volume / this._max) / 100;
  59. }
  60. set volume(num) {
  61. this._stream.volume = Math.floor(num * this._max);
  62. this._stream.push_volume();
  63. }
  64. /**
  65. * Gradually raise or lower the stream volume to @value
  66. *
  67. * @param {number} value - A number in the range 0-1
  68. * @param {number} [duration] - Duration to fade in seconds
  69. */
  70. fade(value, duration = 1) {
  71. Tweener.removeTweens(this);
  72. if (this._stream.volume > value) {
  73. this._mixer.fading = true;
  74. Tweener.addTween(this, {
  75. volume: value,
  76. time: duration,
  77. transition: 'easeOutCubic',
  78. onComplete: () => {
  79. this._mixer.fading = false;
  80. },
  81. });
  82. } else if (this._stream.volume < value) {
  83. this._mixer.fading = true;
  84. Tweener.addTween(this, {
  85. volume: value,
  86. time: duration,
  87. transition: 'easeInCubic',
  88. onComplete: () => {
  89. this._mixer.fading = false;
  90. },
  91. });
  92. }
  93. }
  94. }
  95. /**
  96. * A subclass of Gvc.MixerControl with convenience functions for controlling the
  97. * default input/output volumes.
  98. *
  99. * The Mixer class uses GNOME Shell's Gvc library to control the system volume
  100. * and offers a few convenience functions.
  101. */
  102. const Mixer = !Gvc ? null : GObject.registerClass({
  103. GTypeName: 'GSConnectAudioMixer',
  104. }, class Mixer extends Gvc.MixerControl {
  105. _init(params) {
  106. super._init({name: 'GSConnect'});
  107. this._previousVolume = undefined;
  108. this._volumeMuted = false;
  109. this._microphoneMuted = false;
  110. this.open();
  111. }
  112. get fading() {
  113. if (this._fading === undefined)
  114. this._fading = false;
  115. return this._fading;
  116. }
  117. set fading(bool) {
  118. if (this.fading === bool)
  119. return;
  120. this._fading = bool;
  121. if (this.fading)
  122. this.emit('stream-changed', this._output._stream.id);
  123. }
  124. get input() {
  125. if (this._input === undefined)
  126. this.vfunc_default_source_changed();
  127. return this._input;
  128. }
  129. get output() {
  130. if (this._output === undefined)
  131. this.vfunc_default_sink_changed();
  132. return this._output;
  133. }
  134. vfunc_default_sink_changed(id) {
  135. try {
  136. const sink = this.get_default_sink();
  137. this._output = (sink) ? new Stream(this, sink) : null;
  138. } catch (e) {
  139. logError(e);
  140. }
  141. }
  142. vfunc_default_source_changed(id) {
  143. try {
  144. const source = this.get_default_source();
  145. this._input = (source) ? new Stream(this, source) : null;
  146. } catch (e) {
  147. logError(e);
  148. }
  149. }
  150. vfunc_state_changed(new_state) {
  151. try {
  152. if (new_state === Gvc.MixerControlState.READY) {
  153. this.vfunc_default_sink_changed(null);
  154. this.vfunc_default_source_changed(null);
  155. }
  156. } catch (e) {
  157. logError(e);
  158. }
  159. }
  160. /**
  161. * Store the current output volume then lower it to %15
  162. *
  163. * @param {number} duration - Duration in seconds to fade
  164. */
  165. lowerVolume(duration = 1) {
  166. try {
  167. if (this.output && this.output.volume > 0.15) {
  168. this._previousVolume = Number(this.output.volume);
  169. this.output.fade(0.15, duration);
  170. }
  171. } catch (e) {
  172. logError(e);
  173. }
  174. }
  175. /**
  176. * Mute the output volume (speakers)
  177. */
  178. muteVolume() {
  179. try {
  180. if (!this.output || this.output.muted)
  181. return;
  182. this.output.muted = true;
  183. this._volumeMuted = true;
  184. } catch (e) {
  185. logError(e);
  186. }
  187. }
  188. /**
  189. * Mute the input volume (microphone)
  190. */
  191. muteMicrophone() {
  192. try {
  193. if (!this.input || this.input.muted)
  194. return;
  195. this.input.muted = true;
  196. this._microphoneMuted = true;
  197. } catch (e) {
  198. logError(e);
  199. }
  200. }
  201. /**
  202. * Restore all mixer levels to their previous state
  203. */
  204. restore() {
  205. try {
  206. // If we muted the microphone, unmute it before restoring the volume
  207. if (this._microphoneMuted) {
  208. this.input.muted = false;
  209. this._microphoneMuted = false;
  210. }
  211. // If we muted the volume, unmute it before restoring the volume
  212. if (this._volumeMuted) {
  213. this.output.muted = false;
  214. this._volumeMuted = false;
  215. }
  216. // If a previous volume is defined, raise it back up to that level
  217. if (this._previousVolume !== undefined) {
  218. this.output.fade(this._previousVolume);
  219. this._previousVolume = undefined;
  220. }
  221. } catch (e) {
  222. logError(e);
  223. }
  224. }
  225. destroy() {
  226. this.close();
  227. }
  228. });
  229. /**
  230. * The service class for this component
  231. */
  232. export default Mixer;