pulseaudio.js 6.5 KB

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