Skip to content

Commit

Permalink
Add option to select type of fallback sound (#1839)
Browse files Browse the repository at this point in the history
* Add option to select type of fallback sound

* Set to advanced only
  • Loading branch information
Kuuuube authored Feb 24, 2025
1 parent 1c1d104 commit 57b0538
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ Yomitan uses several third-party libraries to function.

## Attribution

`button.mp3` is provided by [UNIVERSFIELD](https://pixabay.com/sound-effects/error-8-206492/) and licensed under the [Pixabay Content License](https://pixabay.com/service/license-summary/).
`fallback-bloop.mp3` is provided by [UNIVERSFIELD](https://pixabay.com/sound-effects/error-8-206492/) and licensed under the [Pixabay Content License](https://pixabay.com/service/license-summary/).
File renamed without changes.
Binary file added ext/data/audio/fallback-click.mp3
Binary file not shown.
9 changes: 5 additions & 4 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
"enabled",
"volume",
"autoPlay",
"playFallbackSound",
"fallbackSoundType",
"sources"
],
"properties": {
Expand All @@ -409,9 +409,10 @@
"type": "boolean",
"default": false
},
"playFallbackSound": {
"type": "boolean",
"default": true
"fallbackSoundType": {
"type": "string",
"enum": ["none", "click", "bloop"],
"default": "click"
},
"sources": {
"type": "array",
Expand Down
12 changes: 12 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ export class OptionsUtil {
this._updateVersion57,
this._updateVersion58,
this._updateVersion59,
this._updateVersion60,
];
/* eslint-enable @typescript-eslint/unbound-method */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
Expand Down Expand Up @@ -1594,6 +1595,17 @@ export class OptionsUtil {
}
}

/**
* - Replaced audio.playFallbackSound with audio.fallbackSoundType
* @type {import('options-util').UpdateFunction}
*/
async _updateVersion60(options) {
for (const profile of options.profiles) {
profile.options.audio.fallbackSoundType = profile.options.audio.playFallbackSound ? 'click' : 'none';
delete profile.options.audio.playFallbackSound;
}
}

/**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
Expand Down
10 changes: 5 additions & 5 deletions ext/js/display/display-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class DisplayAudio {
this._playbackVolume = 1;
/** @type {boolean} */
this._autoPlay = false;
/** @type {boolean} */
this._playFallbackSound = true;
/** @type {import('settings').FallbackSoundType} */
this._fallbackSoundType = 'none';
/** @type {?import('core').Timeout} */
this._autoPlayAudioTimer = null;
/** @type {number} */
Expand Down Expand Up @@ -170,10 +170,10 @@ export class DisplayAudio {
_onOptionsUpdated({options}) {
const {
general: {language},
audio: {enabled, autoPlay, playFallbackSound, volume, sources},
audio: {enabled, autoPlay, fallbackSoundType, volume, sources},
} = options;
this._autoPlay = enabled && autoPlay;
this._playFallbackSound = playFallbackSound;
this._fallbackSoundType = fallbackSoundType;
this._playbackVolume = Number.isFinite(volume) ? Math.max(0, Math.min(1, volume / 100)) : 1;

/** @type {Set<import('settings').AudioSourceType>} */
Expand Down Expand Up @@ -454,7 +454,7 @@ export class DisplayAudio {
const sourceIndex = sources.indexOf(source);
title = `From source ${1 + sourceIndex}: ${source.name}`;
} else {
audio = this._audioSystem.getFallbackAudio(this._playFallbackSound);
audio = this._audioSystem.getFallbackAudio(this._fallbackSoundType);
title = 'Could not find audio';
}

Expand Down
26 changes: 18 additions & 8 deletions ext/js/media/audio-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class AudioSystem extends EventDispatcher {
super();
/** @type {?HTMLAudioElement} */
this._fallbackAudio = null;
/** @type {?boolean} */
this._playFallbackSound = null;
/** @type {?import('settings').FallbackSoundType} */
this._fallbackSoundType = null;
}

/**
Expand All @@ -45,14 +45,24 @@ export class AudioSystem extends EventDispatcher {
}

/**
* @param {boolean} playFallbackSound
* @param {import('settings').FallbackSoundType} fallbackSoundType
* @returns {HTMLAudioElement}
*/
getFallbackAudio(playFallbackSound) {
if (this._fallbackAudio === null || this._playFallbackSound !== playFallbackSound) {
// audio handler expects audio url to always be present, empty string must be used instead of `new Audio()`
this._fallbackAudio = playFallbackSound ? new Audio('/data/audio/button.mp3') : new Audio('');
this._playFallbackSound = playFallbackSound;
getFallbackAudio(fallbackSoundType) {
if (this._fallbackAudio === null || this._fallbackSoundType !== fallbackSoundType) {
this._fallbackSoundType = fallbackSoundType;
switch (fallbackSoundType) {
case 'click':
this._fallbackAudio = new Audio('/data/audio/fallback-click.mp3');
break;
case 'bloop':
this._fallbackAudio = new Audio('/data/audio/fallback-bloop.mp3');
break;
case 'none':
// audio handler expects audio url to always be present, empty string must be used instead of `new Audio()`
this._fallbackAudio = new Audio('');
break;
}
}
return this._fallbackAudio;
}
Expand Down
24 changes: 16 additions & 8 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -1461,15 +1461,23 @@ <h1>Yomitan Settings</h1>
<label class="toggle"><input type="checkbox" data-setting="audio.autoPlay"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
<div class="settings-item advanced-only"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Enable audio fallback sound</div>
<div class="settings-item-description">Play sound when Yomitan fails to fetch audio.</div>
</div>
<div class="settings-item-right">
<label class="toggle"><input type="checkbox" data-setting="audio.playFallbackSound"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
<div class="settings-item advanced-only">
<div class="settings-item-inner settings-item-inner-wrappable">
<div class="settings-item-left">
<div class="settings-item-label">Audio fallback sound</div>
<div class="settings-item-description">
The sound to play when Yomitan fails to fetch audio.
</div>
</div>
<div class="settings-item-right">
<select data-setting="audio.fallbackSoundType">
<option value="none">None</option>
<option value="click">Click</option>
<option value="bloop">Bloop</option>
</select>
</div>
</div>
</div></div>
</div>
<div class="settings-item"><div class="settings-item-inner settings-item-inner-wrappable">
<div class="settings-item-left">
<div class="settings-item-label">Audio volume</div>
Expand Down
6 changes: 3 additions & 3 deletions test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function createProfileOptionsTestData1() {
sources: ['jpod101', 'text-to-speech', 'custom', 'jpod101-alternate'],
volume: 100,
autoPlay: false,
playFallbackSound: true,
fallbackSoundType: 'click',
customSourceUrl: 'http://localhost/audio.mp3?term={expression}&reading={reading}',
textToSpeechVoice: 'example-voice',
},
Expand Down Expand Up @@ -337,7 +337,7 @@ function createProfileOptionsUpdatedTestData1() {
],
volume: 100,
autoPlay: false,
playFallbackSound: true,
fallbackSoundType: 'click',
},
scanning: {
selectText: true,
Expand Down Expand Up @@ -680,7 +680,7 @@ function createOptionsUpdatedTestData1() {
},
],
profileCurrent: 0,
version: 59,
version: 60,
global: {
database: {
prefixWildcardsSupported: false,
Expand Down
4 changes: 3 additions & 1 deletion types/ext/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ export type AudioOptions = {
enabled: boolean;
volume: number;
autoPlay: boolean;
playFallbackSound: boolean;
fallbackSoundType: FallbackSoundType;
sources: AudioSourceOptions[];
};

export type FallbackSoundType = 'none' | 'click' | 'bloop';

export type AudioSourceOptions = {
type: AudioSourceType;
url: string;
Expand Down

0 comments on commit 57b0538

Please sign in to comment.