Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #151: Add volume control in settings to adjust notification sounds #404

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Sounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Chrome from './Chrome';
import M from './Messages';

const defaultOptions = { volume: 1.0 };

function createNotificationSounds() {
let sounds = [
{ name: M.tone, file: 'f62b45bc.mp3' },
Expand Down Expand Up @@ -60,7 +62,7 @@ function createTimerSounds() {
return sounds;
}

async function play(filename) {
async function play(filename, { volume } = defaultOptions) {
if (!filename) {
return;
}
Expand All @@ -70,7 +72,13 @@ async function play(filename) {
let context = new AudioContext();

let source = context.createBufferSource();
source.connect(context.destination);
// Adds gain node for volume control
let gain = context.createGain();
gain.gain.setValueAtTime(volume, context.currentTime);

source.connect(gain);
gain.connect(context.destination);

source.buffer = await new Promise(async (resolve, reject) => {
let content = await Chrome.files.readBinary(filename);
context.decodeAudioData(content, buffer => resolve(buffer), error => reject(error));
Expand All @@ -93,4 +101,4 @@ export {
notification,
timer,
play
};
};
24 changes: 19 additions & 5 deletions src/background/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function clone(obj) {
class SettingsSchema
{
get version() {
return 7;
return 8;
}

get default() {
Expand All @@ -36,7 +36,8 @@ class SettingsSchema
notifications: {
desktop: true,
tab: true,
sound: null
sound: null,
volume: 1.0
}
},
shortBreak: {
Expand All @@ -50,7 +51,8 @@ class SettingsSchema
notifications: {
desktop: true,
tab: true,
sound: null
sound: null,
volume: 1.0
}
},
longBreak: {
Expand All @@ -65,7 +67,8 @@ class SettingsSchema
notifications: {
desktop: true,
tab: true,
sound: null
sound: null,
volume: 1.0
}
},
autostart: {
Expand Down Expand Up @@ -204,6 +207,17 @@ class SettingsSchema

return v7;
}

from7To8(v7) {
let v8 = clone(v7);
v8.version = 8;

v8.focus.notifications.volume = 1.0;
v8.shortBreak.notifications.volume = 1.0;
v8.longBreak.notifications.volume = 1.0;

return v8;
}
}

class PersistentSettings
Expand All @@ -228,4 +242,4 @@ class PersistentSettings
export {
SettingsSchema,
PersistentSettings
};
};
8 changes: 4 additions & 4 deletions src/options/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<p class="field">
<label>
<span>{{ M.play_audio_notification }}</span>
<SoundSelect v-model="settings.focus.notifications.sound" :sounds="notificationSounds"></SoundSelect>
<SoundSelect v-model="settings.focus.notifications" :sounds="notificationSounds"></SoundSelect>
</label>
</p>
</div>
Expand Down Expand Up @@ -103,7 +103,7 @@
<p class="field">
<label>
<span>{{ M.play_audio_notification }}</span>
<SoundSelect v-model="settings.shortBreak.notifications.sound" :sounds="notificationSounds"></SoundSelect>
<SoundSelect v-model="settings.shortBreak.notifications" :sounds="notificationSounds"></SoundSelect>
</label>
</p>
</div>
Expand Down Expand Up @@ -157,7 +157,7 @@
<p class="field">
<label>
<span>{{ M.play_audio_notification }}</span>
<SoundSelect v-model="settings.longBreak.notifications.sound" :sounds="notificationSounds"></SoundSelect>
<SoundSelect v-model="settings.longBreak.notifications" :sounds="notificationSounds"></SoundSelect>
</label>
</p>
</div>
Expand Down Expand Up @@ -379,4 +379,4 @@ export default {
SoundSelect
}
};
</script>
</script>
42 changes: 35 additions & 7 deletions src/options/SoundSelect.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
<template>
<select :value="value" @input="setSound($event.target.value)">
<option :value="null">{{ M.none }}</option>
<option v-for="sound in sounds" :value="sound.file">{{ sound.name }}</option>
</select>
<div>
<select :value="value.sound" @input="setSound($event.target.value)">
<option :value="null">{{ M.none }}</option>
<option v-for="sound in sounds" :value="sound.file">{{ sound.name }}</option>
</select>
<span>
<button @click="playSound(value.sound)" :disabled="isPlaying">▶️</button>
<input
type="range"
min="0.1"
max="1.0"
step="0.1"
:value="value.volume"
@input="setVolume($event.target.value)">Volume: {{value.volume * 10}}
</span>
</div>
</template>

<script>
import * as Sounds from '../Sounds';

export default {
props: ['value', 'sounds'],
data() {
return {isPlaying: false}
},
methods: {
setVolume(amount) {
this.value.volume = parseFloat(amount);
},

setSound(filename) {
this.$emit('input', filename);
Sounds.play(filename);
this.value.sound = filename;

this.$emit('input', this.value);
},

async playSound(filename) {
if (filename) {
this.isPlaying = true;
await Sounds.play(filename, { volume: this.value.volume });
this.isPlaying = false;
}
}
}
};
</script>
</script>