Skip to content

A platform for community discussion. Free, open, simple.

UX: Replace chat notification sounds with Web Audio themes (#41175)

Rafael dos Santos Silva70aacf72e37e
diff --git a/plugins/chat/assets/javascripts/discourse/initializers/chat-audio.js b/plugins/chat/assets/javascripts/discourse/initializers/chat-audio.js
index 9d74ccfaf67..afefe2ef9d2 100644
--- a/plugins/chat/assets/javascripts/discourse/initializers/chat-audio.js
+++ b/plugins/chat/assets/javascripts/discourse/initializers/chat-audio.js
@@ -54,7 +54,9 @@ export default {
const chatAudioManager = container.lookup(
"service:chat-audio-manager"
);
- chatAudioManager.play(user.chat_sound);
+ chatAudioManager.play(user.chat_sound, {
+ type: isMention ? "mention" : "incoming",
+ });
}
});
}
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js
index 0a85f5b1111..00c9f59a57a 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat-audio-manager.js
@@ -1,22 +1,188 @@
import Service from "@ember/service";
import { isTesting } from "discourse/lib/environment";
-import { getURLWithCDN } from "discourse/lib/get-url";
export const CHAT_SOUNDS = {
- bell: [{ src: "/plugins/chat/audio/bell.mp3", type: "audio/mpeg" }],
- ding: [{ src: "/plugins/chat/audio/ding.mp3", type: "audio/mpeg" }],
+ classic: {},
+ soft: {},
+ retro: {},
+ bubble: {},
+ ethereal: {},
};
-const DEFAULT_SOUND_NAME = "bell";
+const DEFAULT_SOUND_NAME = "classic";
const THROTTLE_TIME = 3000; // 3 seconds
+const MIN_GAIN = 0.001;
+
+let sharedAudioContext;
+
+export function normalizeChatSoundName(name) {
+ if (!name) {
+ return null;
+ }
+
+ return CHAT_SOUNDS[name] ? name : DEFAULT_SOUND_NAME;
+}
+
+async function getAudioContext() {
+ const AudioContext = window.AudioContext || window.webkitAudioContext;
+
+ if (!sharedAudioContext || sharedAudioContext.state === "closed") {
+ sharedAudioContext = new AudioContext();
+ }
+
+ if (sharedAudioContext.state === "suspended") {
+ await sharedAudioContext.resume();
+ }
+
+ return sharedAudioContext;
+}
+
+function playOscillator(ctx, type, frequency, startTime, duration, maxGain) {
+ const oscillator = ctx.createOscillator();
+ const gain = ctx.createGain();
+
+ oscillator.type = type;
+ oscillator.frequency.value = frequency;
+ gain.gain.setValueAtTime(maxGain, startTime);
+ gain.gain.exponentialRampToValueAtTime(MIN_GAIN, startTime + duration);
+
+ oscillator.connect(gain).connect(ctx.destination);
+ oscillator.start(startTime);
+ oscillator.stop(startTime + duration);
+}
+
+function playSweep(ctx, options) {
+ const oscillator = ctx.createOscillator();
+ const gain = ctx.createGain();
+
+ if (options.type) {
+ oscillator.type = options.type;
+ }
+
+ oscillator.frequency.setValueAtTime(options.from, options.startTime);
+
+ if (options.ramp === "linear") {
+ oscillator.frequency.linearRampToValueAtTime(
+ options.to,
+ options.rampEndTime
+ );
+ } else {
+ oscillator.frequency.exponentialRampToValueAtTime(
+ options.to,
+ options.rampEndTime
+ );
+ }
+
+ gain.gain.setValueAtTime(options.gain, options.startTime);
+ gain.gain.exponentialRampToValueAtTime(MIN_GAIN, options.endTime);
+
+ oscillator.connect(gain).connect(ctx.destination);
+ oscillator.start(options.startTime);
+ oscillator.stop(options.endTime);
+}
+
+const SOUND_SEQUENCES = {
+ classic: {
+ incoming(ctx, now) {
+ playOscillator(ctx, "sine", 587.33, now, 0.1, 0.15);
+ playOscillator(ctx, "sine", 783.99, now + 0.1, 0.3, 0.15);
+ },
+
+ mention(ctx, now) {
+ playOscillator(ctx, "sine", 523.25, now, 0.1, 0.12);
+ playOscillator(ctx, "sine", 659.25, now + 0.08, 0.15, 0.12);
+ playOscillator(ctx, "sine", 1046.5, now + 0.16, 0.4, 0.15);
+ },
+ },
+
+ soft: {
+ incoming(ctx, now) {
+ playOscillator(ctx, "sine", 349.23, now, 0.15, 0.2);
+ playOscillator(ctx, "sine", 466.16, now + 0.1, 0.25, 0.2);
+ },
+
+ mention(ctx, now) {
+ playOscillator(ctx, "sine", 349.23, now, 0.15, 0.2);
+ playOscillator(ctx, "sine", 440, now + 0.08, 0.15, 0.2);
+ playOscillator(ctx, "sine", 523.25, now + 0.16, 0.3, 0.2);
+ },
+ },
+
+ retro: {
+ incoming(ctx, now) {
+ playOscillator(ctx, "square", 392, now, 0.08, 0.04);
+ playOscillator(ctx, "square", 493.88, now + 0.08, 0.15, 0.04);
+ },
+
+ mention(ctx, now) {
+ [523.25, 659.25, 783.99, 1046.5].forEach((frequency, index) => {
+ playOscillator(ctx, "square", frequency, now + index * 0.06, 0.1, 0.03);
+ });
+ },
+ },
+
+ bubble: {
+ incoming(ctx, now) {
+ playSweep(ctx, {
+ from: 300,
+ to: 700,
+ startTime: now,
+ rampEndTime: now + 0.1,
+ endTime: now + 0.15,
+ gain: 0.2,
+ });
+ },
+
+ mention(ctx, now) {
+ playSweep(ctx, {
+ from: 400,
+ to: 800,
+ startTime: now,
+ rampEndTime: now + 0.08,
+ endTime: now + 0.12,
+ gain: 0.15,
+ });
+ playSweep(ctx, {
+ from: 500,
+ to: 1000,
+ startTime: now + 0.1,
+ rampEndTime: now + 0.18,
+ endTime: now + 0.25,
+ gain: 0.15,
+ });
+ },
+ },
+
+ ethereal: {
+ incoming(ctx, now) {
+ [523.25, 659.25, 783.99].forEach((frequency) => {
+ playOscillator(ctx, "sine", frequency, now, 1.2, 0.05);
+ });
+ },
+
+ mention(ctx, now) {
+ playSweep(ctx, {
+ type: "sine",
+ ramp: "linear",
+ from: 440,
+ to: 880,
+ startTime: now,
+ rampEndTime: now + 0.15,
+ endTime: now + 1,
+ gain: 0.1,
+ });
+ playOscillator(ctx, "sine", 1320, now + 0.15, 0.8, 0.03);
+ },
+ },
+};
export default class ChatAudioManager extends Service {
canPlay = true;
- async play(name) {
+ async play(name, { type = "incoming" } = {}) {
if (this.canPlay) {
- await this.#tryPlay(name);
+ await this.#tryPlay(name, type);
this.canPlay = false;
setTimeout(() => {
this.canPlay = true;
@@ -24,13 +190,11 @@ export default class ChatAudioManager extends Service {
}
}
- async #tryPlay(name) {
- const src = getURLWithCDN(
- (CHAT_SOUNDS[name] || CHAT_SOUNDS[DEFAULT_SOUND_NAME])[0].src
- );
- const audio = new Audio(src);
+ async #tryPlay(name, type) {
try {
- await audio.play();
+ const ctx = await getAudioContext();
+ const sequence = SOUND_SEQUENCES[normalizeChatSoundName(name)];
+ (sequence[type] || sequence.incoming)(ctx, ctx.currentTime);
} catch {
if (!isTesting()) {
// eslint-disable-next-line no-console
diff --git a/plugins/chat/assets/javascripts/discourse/templates/preferences/chat.gjs b/plugins/chat/assets/javascripts/discourse/templates/preferences/chat.gjs
index 4937b92e30d..5101f2bce9f 100644
--- a/plugins/chat/assets/javascripts/discourse/templates/preferences/chat.gjs
+++ b/plugins/chat/assets/javascripts/discourse/templates/preferences/chat.gjs
@@ -26,7 +26,10 @@ import {
HEADER_INDICATOR_PREFERENCE_NEVER,
HEADER_INDICATOR_PREFERENCE_ONLY_MENTIONS,
} from "discourse/plugins/chat/discourse/lib/chat-constants";
-import { CHAT_SOUNDS } from "discourse/plugins/chat/discourse/services/chat-audio-manager";
+import {
+ CHAT_SOUNDS,
+ normalizeChatSoundName,
+} from "discourse/plugins/chat/discourse/services/chat-audio-manager";
export default class Chat extends Component {
@service chatAudioManager;
@@ -118,7 +121,7 @@ export default class Chat extends Component {
chat_quick_reactions_custom: emojis,
only_chat_push_notifications: userOption.only_chat_push_notifications,
ignore_channel_wide_mention: userOption.ignore_channel_wide_mention,
- chat_sound: userOption.chat_sound,
+ chat_sound: normalizeChatSoundName(userOption.chat_sound),
chat_header_indicator_preference:
userOption.chat_header_indicator_preference,
chat_separate_sidebar_mode: userOption.chat_separate_sidebar_mode,
diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.yml
index afa745fa51d..962c1dd0709 100644
--- a/plugins/chat/config/locales/client.en.yml
+++ b/plugins/chat/config/locales/client.en.yml
@@ -321,8 +321,11 @@ en:
title: "Desktop chat notification sound"
sounds:
none: "None"
- bell: "Bell"
- ding: "Ding"
+ classic: "Classic and clean"
+ soft: "Soft marimba"
+ retro: "Retro 8-bit"
+ bubble: "Playful bubble"
+ ethereal: "Ethereal glass"
title: "chat"
title_capitalized: "Chat"
upload: "Attach a file"
diff --git a/plugins/chat/public/audio/bell.mp3 b/plugins/chat/public/audio/bell.mp3
deleted file mode 100644
index 1f5a34a427a..00000000000
Binary files a/plugins/chat/public/audio/bell.mp3 and /dev/null differ
diff --git a/plugins/chat/public/audio/ding.mp3 b/plugins/chat/public/audio/ding.mp3
deleted file mode 100644
index 82c885ce073..00000000000
Binary files a/plugins/chat/public/audio/ding.mp3 and /dev/null differ
diff --git a/plugins/chat/spec/system/user_chat_preferences_spec.rb b/plugins/chat/spec/system/user_chat_preferences_spec.rb
index 257d6661c09..c00542a462d 100644
--- a/plugins/chat/spec/system/user_chat_preferences_spec.rb
+++ b/plugins/chat/spec/system/user_chat_preferences_spec.rb
@@ -102,7 +102,7 @@ RSpec.describe "User chat preferences" do
describe "chat sound" do
include_examples "select and save" do
let(:field_name) { "chat_sound" }
- let(:val) { "bell" }
+ let(:val) { "classic" }
end
end
diff --git a/plugins/chat/test/javascripts/unit/lib/chat-audio-test.js b/plugins/chat/test/javascripts/unit/lib/chat-audio-test.js
index f6fdf717e01..49eace0fbe4 100644
--- a/plugins/chat/test/javascripts/unit/lib/chat-audio-test.js
+++ b/plugins/chat/test/javascripts/unit/lib/chat-audio-test.js
@@ -19,7 +19,7 @@ module("Unit | chat-audio", function (hooks) {
this.siteSettings = getOwner(this).lookup("service:site-settings");
this.siteSettings.chat_enabled = true;
- this.currentUser.chat_sound = "ding";
+ this.currentUser.chat_sound = "classic";
this.currentUser.user_option.has_chat_enabled = true;
this.currentUser.user_option.chat_header_indicator_preference = "all_new";
@@ -51,21 +51,25 @@ module("Unit | chat-audio", function (hooks) {
test("plays chat sound", async function (assert) {
await this.handleNotification();
- assert.true(this.playStub.calledOnce);
+ assert.true(this.playStub.calledOnce, "plays a sound");
+ assert.true(
+ this.playStub.calledWith("classic", { type: "incoming" }),
+ "uses the incoming sound variant"
+ );
});
test("skips chat sound for user in DND mode", async function (assert) {
this.currentUser.isInDoNotDisturb = () => true;
await this.handleNotification();
- assert.true(this.playStub.notCalled);
+ assert.true(this.playStub.notCalled, "does not play a sound");
});
test("skips chat sound for user with no chat sound set", async function (assert) {
this.currentUser.chat_sound = null;
await this.handleNotification();
- assert.true(this.playStub.notCalled);
+ assert.true(this.playStub.notCalled, "does not play a sound");
});
test("plays a chat sound for mentions", async function (assert) {
@@ -74,7 +78,11 @@ module("Unit | chat-audio", function (hooks) {
await this.handleNotification({ notification_type: 29 });
- assert.true(this.playStub.calledOnce);
+ assert.true(this.playStub.calledOnce, "plays a sound");
+ assert.true(
+ this.playStub.calledWith("classic", { type: "mention" }),
+ "uses the mention sound variant"
+ );
});
test("skips chat sound for non-mentions", async function (assert) {
@@ -83,7 +91,7 @@ module("Unit | chat-audio", function (hooks) {
await this.handleNotification();
- assert.true(this.playStub.notCalled);
+ assert.true(this.playStub.notCalled, "does not play a sound");
});
test("plays a chat sound for DMs", async function (assert) {
@@ -92,7 +100,7 @@ module("Unit | chat-audio", function (hooks) {
await this.handleNotification({ is_direct_message_channel: true });
- assert.true(this.playStub.calledOnce);
+ assert.true(this.playStub.calledOnce, "plays a sound");
});
test("skips chat sound for non-DM messages", async function (assert) {
@@ -101,13 +109,13 @@ module("Unit | chat-audio", function (hooks) {
await this.handleNotification({ is_direct_message_channel: false });
- assert.true(this.playStub.notCalled);
+ assert.true(this.playStub.notCalled, "does not play a sound");
});
test("skips chat sound when service worker returns false", async function (assert) {
chatAudioInitializer.canPlaySound.returns(Promise.resolve(false));
await this.handleNotification();
- assert.true(this.playStub.notCalled);
+ assert.true(this.playStub.notCalled, "does not play a sound");
});
});
diff --git a/plugins/chat/test/javascripts/unit/services/chat-audio-manager-test.js b/plugins/chat/test/javascripts/unit/services/chat-audio-manager-test.js
new file mode 100644
index 00000000000..c9835a9a262
--- /dev/null
+++ b/plugins/chat/test/javascripts/unit/services/chat-audio-manager-test.js
@@ -0,0 +1,105 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import { normalizeChatSoundName } from "discourse/plugins/chat/discourse/services/chat-audio-manager";
+
+class MockAudioContext {
+ state = "running";
+ currentTime = 1;
+ destination = {};
+ oscillators = [];
+
+ createOscillator() {
+ const oscillator = {
+ frequency: {
+ value: null,
+ setValueAtTime() {
+ return undefined;
+ },
+ exponentialRampToValueAtTime() {
+ return undefined;
+ },
+ linearRampToValueAtTime() {
+ return undefined;
+ },
+ },
+ connect: (node) => node,
+ start() {
+ return undefined;
+ },
+ stop() {
+ return undefined;
+ },
+ type: null,
+ };
+
+ this.oscillators.push(oscillator);
+
+ return oscillator;
+ }
+
+ createGain() {
+ return {
+ connect: (node) => node,
+ gain: {
+ setValueAtTime() {
+ return undefined;
+ },
+ exponentialRampToValueAtTime() {
+ return undefined;
+ },
+ },
+ };
+ }
+}
+
+module("Unit | Service | chat-audio-manager", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.originalAudioContext = window.AudioContext;
+ this.context = new MockAudioContext();
+ const context = this.context;
+
+ window.AudioContext = class {
+ constructor() {
+ return context;
+ }
+ };
+
+ this.subject = getOwner(this).lookup("service:chat-audio-manager");
+ });
+
+ hooks.afterEach(function () {
+ this.context.state = "closed";
+ window.AudioContext = this.originalAudioContext;
+ });
+
+ test("plays the incoming variant for the selected theme", async function (assert) {
+ await this.subject.play("classic");
+
+ assert.strictEqual(
+ this.context.oscillators.length,
+ 2,
+ "schedules the incoming sound sequence"
+ );
+ });
+
+ test("plays the mention variant for the selected theme", async function (assert) {
+ await this.subject.play("classic", { type: "mention" });
+
+ assert.strictEqual(
+ this.context.oscillators.length,
+ 3,
+ "schedules the mention sound sequence"
+ );
+ });
+
+ test("normalizes legacy sound names to the default theme", function (assert) {
+ assert.strictEqual(
+ normalizeChatSoundName("ding"),
+ "classic",
+ "uses the default theme for legacy sound names"
+ );
+ });
+});