-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starts adding option to enable/disable gapless playback
- Loading branch information
Showing
29 changed files
with
245 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/app/services/playback/audio-player/audio-player-factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { MathExtensions } from '../../../common/math-extensions'; | ||
import { Logger } from '../../../common/logger'; | ||
import { AudioPlayer } from './audio-player'; | ||
import { SettingsBase } from '../../../common/settings/settings.base'; | ||
import { GaplessAudioPlayer } from './gapless-audio-player'; | ||
import { AudioPlayerBase } from './audio-player.base'; | ||
@Injectable({ providedIn: 'root' }) | ||
export class AudioPlayerFactory { | ||
public constructor( | ||
private mathExtensions: MathExtensions, | ||
private settings: SettingsBase, | ||
private logger: Logger, | ||
) {} | ||
public create(): AudioPlayerBase { | ||
if (this.settings.enableGaplessPlayback) { | ||
return new GaplessAudioPlayer(this.mathExtensions, this.logger); | ||
} | ||
return new AudioPlayer(this.mathExtensions, this.logger); | ||
} | ||
} |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...pp/services/playback/audio-player.spec.ts → ...layback/audio-player/audio-player.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, Subject } from 'rxjs'; | ||
import { Logger } from '../../../common/logger'; | ||
import { MathExtensions } from '../../../common/math-extensions'; | ||
import { PromiseUtils } from '../../../common/utils/promise-utils'; | ||
import { StringUtils } from '../../../common/utils/string-utils'; | ||
import { AudioPlayerBase } from './audio-player.base'; | ||
|
||
@Injectable() | ||
export class AudioPlayer implements AudioPlayerBase { | ||
private _audio: HTMLAudioElement; | ||
|
||
public constructor( | ||
private mathExtensions: MathExtensions, | ||
private logger: Logger, | ||
) { | ||
this._audio = new Audio(); | ||
|
||
try { | ||
// This fails during unit tests because setSinkId() does not exist on HTMLAudioElement | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
this.audio.setSinkId('default'); | ||
} catch (e: unknown) { | ||
// Suppress this error, but log it, in case it happens in production. | ||
this.logger.error(e, 'Could not perform setSinkId()', 'AudioPlayer', 'constructor'); | ||
} | ||
|
||
this.audio.defaultPlaybackRate = 1; | ||
this.audio.playbackRate = 1; | ||
this.audio.volume = 1; | ||
this.audio.muted = false; | ||
|
||
this.audio.onended = () => this.playbackFinished.next(); | ||
} | ||
|
||
private playbackFinished: Subject<void> = new Subject(); | ||
public playbackFinished$: Observable<void> = this.playbackFinished.asObservable(); | ||
|
||
public get audio(): HTMLAudioElement { | ||
return this._audio; | ||
} | ||
|
||
public get progressSeconds(): number { | ||
if (isNaN(this.audio.currentTime)) { | ||
return 0; | ||
} | ||
|
||
return this.audio.currentTime; | ||
} | ||
|
||
public get totalSeconds(): number { | ||
if (isNaN(this.audio.duration)) { | ||
return 0; | ||
} | ||
|
||
return this.audio.duration; | ||
} | ||
|
||
public play(audioFilePath: string): void { | ||
const playableAudioFilePath: string = this.replaceUnplayableCharacters(audioFilePath); | ||
this.audio.src = 'file:///' + playableAudioFilePath; | ||
PromiseUtils.noAwait(this.audio.play()); | ||
} | ||
|
||
public stop(): void { | ||
this.audio.currentTime = 0; | ||
this.audio.pause(); | ||
} | ||
|
||
public pause(): void { | ||
this.audio.pause(); | ||
} | ||
|
||
public resume(): void { | ||
PromiseUtils.noAwait(this.audio.play()); | ||
} | ||
|
||
public setVolume(linearVolume: number): void { | ||
// log(0) is undefined. So we provide a minimum of 0.01. | ||
const logarithmicVolume: number = linearVolume > 0 ? this.mathExtensions.linearToLogarithmic(linearVolume, 0.01, 1) : 0; | ||
this.audio.volume = logarithmicVolume; | ||
} | ||
|
||
public mute(): void { | ||
this.audio.muted = true; | ||
} | ||
|
||
public unMute(): void { | ||
this.audio.muted = false; | ||
} | ||
|
||
public skipToSeconds(seconds: number): void { | ||
this.audio.currentTime = seconds; | ||
} | ||
|
||
private replaceUnplayableCharacters(audioFilePath: string): string { | ||
// HTMLAudioElement doesn't play paths which contain # and ?, so we escape them. | ||
let playableAudioFilePath: string = StringUtils.replaceAll(audioFilePath, '#', '%23'); | ||
playableAudioFilePath = StringUtils.replaceAll(playableAudioFilePath, '?', '%3F'); | ||
return playableAudioFilePath; | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
src/app/services/playback/audio-player.ts → ...back/audio-player/gapless-audio-player.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.