2023-02-25 14:16:19 +00:00
|
|
|
import { audio_element, type AudioElementStore } from '$lib/audio/audio-element';
|
2023-02-25 14:23:56 +00:00
|
|
|
import { episode_details, type EpisodeDetailsStore } from '$lib/audio/episode-details';
|
2023-02-25 14:33:17 +00:00
|
|
|
import { user_preferences } from '$lib/preferences';
|
2023-02-25 14:16:19 +00:00
|
|
|
import { podcast_progress } from '$lib/progress';
|
2023-02-25 14:23:56 +00:00
|
|
|
import type { EpisodeAttributes, EpisodeDetails } from '$lib/types';
|
2023-02-25 14:16:19 +00:00
|
|
|
import { warn } from '$lib/utility/package/log';
|
|
|
|
|
import clamp from 'just-clamp';
|
|
|
|
|
import { derived, get } from 'svelte/store';
|
|
|
|
|
|
|
|
|
|
const default_episode_attributes = {
|
|
|
|
|
will_autoplay: false,
|
2023-02-25 16:33:08 +00:00
|
|
|
is_paused: true,
|
2023-02-25 14:16:19 +00:00
|
|
|
duration: 0,
|
|
|
|
|
start_at: 0,
|
|
|
|
|
details: null,
|
|
|
|
|
} satisfies Omit<EpisodeAttributes, 'src'>;
|
|
|
|
|
|
|
|
|
|
const episode_attributes = derived<
|
|
|
|
|
[AudioElementStore, EpisodeDetailsStore],
|
|
|
|
|
EpisodeAttributes | null
|
|
|
|
|
>([audio_element, episode_details], ([$audio, $details], set) => {
|
|
|
|
|
if (!$audio) return set(null);
|
|
|
|
|
|
|
|
|
|
function set_value() {
|
|
|
|
|
if (!$audio?.src) return null;
|
|
|
|
|
set({
|
|
|
|
|
...default_episode_attributes,
|
|
|
|
|
src: $audio.src,
|
|
|
|
|
duration: $audio.duration,
|
|
|
|
|
details: $details,
|
2023-02-25 16:33:08 +00:00
|
|
|
will_autoplay: $audio.autoplay,
|
|
|
|
|
is_paused: $audio.paused,
|
2023-02-25 14:16:19 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 16:33:08 +00:00
|
|
|
$audio.addEventListener('loadeddata', set_value);
|
|
|
|
|
$audio.addEventListener('pause', set_value);
|
|
|
|
|
$audio.addEventListener('playing', set_value);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
$audio.removeEventListener('loadeddata', set_value);
|
|
|
|
|
$audio.removeEventListener('pause', set_value);
|
|
|
|
|
$audio.removeEventListener('playing', set_value);
|
|
|
|
|
};
|
2023-02-25 14:16:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
type HandleType = 'toggle' | 'set';
|
|
|
|
|
|
|
|
|
|
const no_element = (action: string) => warn(`could not ${action} :: no audio element exists yet`);
|
|
|
|
|
|
|
|
|
|
export const episode_audio = {
|
|
|
|
|
subscribe: episode_attributes.subscribe,
|
|
|
|
|
load: (src: string, details: EpisodeDetails) => {
|
2023-02-25 14:46:07 +00:00
|
|
|
podcast_progress.stash_episode();
|
2023-02-25 14:16:19 +00:00
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('load');
|
2023-02-25 14:46:07 +00:00
|
|
|
const progress = podcast_progress.get_episode(src);
|
2023-02-25 14:16:19 +00:00
|
|
|
el.src = src;
|
|
|
|
|
el.currentTime = progress?.start_at || 0;
|
|
|
|
|
|
2023-02-25 14:33:17 +00:00
|
|
|
const preferences = get(user_preferences);
|
2023-02-25 14:16:19 +00:00
|
|
|
el.playbackRate = preferences.playback_rate;
|
|
|
|
|
el.volume = preferences.volume;
|
2023-02-25 16:33:08 +00:00
|
|
|
el.muted = false;
|
2023-02-25 14:16:19 +00:00
|
|
|
|
|
|
|
|
episode_details.set(details);
|
|
|
|
|
},
|
|
|
|
|
unload: () => {
|
2023-02-25 14:46:07 +00:00
|
|
|
podcast_progress.stash_episode();
|
2023-02-25 14:16:19 +00:00
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('unload');
|
|
|
|
|
el.src = '';
|
|
|
|
|
episode_details.set(null);
|
|
|
|
|
},
|
|
|
|
|
play: (t?: HandleType) => {
|
|
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('play');
|
|
|
|
|
|
|
|
|
|
if (t === 'toggle') {
|
|
|
|
|
el.paused ? el.play() : el.pause();
|
|
|
|
|
} else {
|
|
|
|
|
el.play();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
pause: (t?: HandleType) => {
|
2023-02-25 14:46:07 +00:00
|
|
|
podcast_progress.stash_episode();
|
2023-02-25 14:16:19 +00:00
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('pause');
|
|
|
|
|
|
|
|
|
|
if (t === 'toggle') {
|
|
|
|
|
el.paused ? el.play() : el.pause();
|
|
|
|
|
} else {
|
|
|
|
|
el.pause();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mute: (t?: HandleType) => {
|
|
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('mute');
|
|
|
|
|
|
|
|
|
|
if (t === 'toggle') {
|
|
|
|
|
el.muted = !el.muted;
|
|
|
|
|
} else {
|
|
|
|
|
el.muted = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
unmute: (t?: HandleType) => {
|
|
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('unmute');
|
|
|
|
|
|
|
|
|
|
if (t === 'toggle') {
|
|
|
|
|
el.muted = !el.muted;
|
|
|
|
|
} else {
|
|
|
|
|
el.muted = false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
seek(seconds: number, from: 'from-start' | 'from-end' = 'from-start') {
|
|
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('seek');
|
|
|
|
|
|
|
|
|
|
if (from === 'from-end') {
|
|
|
|
|
el.currentTime = clamp(0, el.duration, el.duration - seconds);
|
|
|
|
|
} else {
|
|
|
|
|
el.currentTime = clamp(0, el.duration, seconds);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
skip(seconds: number, type: 'forward' | 'backward' = 'forward') {
|
|
|
|
|
const el = get(audio_element);
|
|
|
|
|
if (!el) return no_element('skip');
|
|
|
|
|
|
|
|
|
|
if (type === 'backward') {
|
|
|
|
|
el.currentTime = clamp(0, el.duration, el.currentTime - seconds);
|
|
|
|
|
} else {
|
|
|
|
|
el.currentTime = clamp(0, el.duration, el.currentTime + seconds);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|