pulseaudio.js 6.6 KB

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