Files
oki-podcast-reader/src/lib/context/audio.ts
T

106 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-02-24 13:47:07 +00:00
import {
2023-02-24 14:01:43 +00:00
audio_autoplay,
2023-02-24 13:47:07 +00:00
audio_element,
2023-02-24 14:43:33 +00:00
audio_muted,
2023-02-24 13:47:07 +00:00
audio_src,
2023-02-24 14:43:33 +00:00
audio_start_at,
2023-02-24 13:47:07 +00:00
} from '$lib/context/audio-internals';
2023-02-24 19:24:58 +00:00
import { episode_progress } from '$lib/context/episode-progress';
import { metadata } from '$lib/stores';
2023-02-24 22:59:12 +00:00
import type { AudioLoadData, AudioLoadOptions } from '$lib/types';
2023-02-24 13:32:10 +00:00
import { info, warn } from '$pkg/log';
2023-02-22 23:41:29 +00:00
import clamp from 'just-clamp';
type HandleType = 'toggle' | 'set';
export function play(type: HandleType = 'set') {
2023-02-24 13:27:53 +00:00
info('play: ', type);
2023-02-24 13:47:07 +00:00
return audio_element.subscribe((el) => {
2023-02-22 23:41:29 +00:00
if (!el) return warn('no audio element');
if (type === 'toggle') {
el.paused ? el.play() : el.pause();
} else {
el.play();
}
})();
}
export function pause(type: HandleType = 'set') {
2023-02-24 13:27:53 +00:00
info('pause: ', type);
2023-02-24 13:47:07 +00:00
return audio_element.subscribe((el) => {
2023-02-22 23:41:29 +00:00
if (!el) return warn('no audio element');
if (type === 'toggle') {
el.paused ? el.play() : el.pause();
} else {
el.pause();
}
})();
}
export function mute(type: HandleType = 'set') {
2023-02-24 13:27:53 +00:00
info('mute: ', type);
2023-02-24 14:43:33 +00:00
if (type === 'toggle') {
audio_muted.update((muted) => !muted);
} else {
audio_muted.set(true);
}
2023-02-22 23:41:29 +00:00
}
export function unmute(type: HandleType = 'set') {
2023-02-24 13:27:53 +00:00
info('unmute: ', type);
2023-02-24 14:43:33 +00:00
if (type === 'toggle') {
audio_muted.update((muted) => !muted);
} else {
audio_muted.set(false);
}
2023-02-22 23:41:29 +00:00
}
export const load = (data: AudioLoadData, opts: AudioLoadOptions) => {
episode_progress.stash();
const { src, ...meta } = data;
const progress = episode_progress.use(src);
const start_at = progress?.start_at || opts.start_at || 0;
console.log('progress', progress);
2023-02-24 13:27:53 +00:00
info('load: ', src);
2023-02-24 14:01:43 +00:00
audio_autoplay.set(opts.autoplay);
2023-02-24 13:47:07 +00:00
audio_src.set(src);
metadata.set(meta);
audio_start_at.set(start_at);
2023-02-24 14:43:33 +00:00
// opts.current_time ? seek(opts.current_time) : null;
//opts.autoplay && play();
2023-02-24 15:38:08 +00:00
};
export function unload() {
episode_progress.stash();
2023-02-24 13:27:53 +00:00
info('unload: ');
2023-02-24 14:43:33 +00:00
pause();
audio_autoplay.set(false);
audio_src.set('');
metadata.set(null);
2023-02-24 14:43:33 +00:00
audio_start_at.set(0);
2023-02-22 23:41:29 +00:00
}
export function seek(seconds: number, from: 'from-start' | 'from-end' = 'from-start') {
2023-02-24 13:27:53 +00:00
info('seek: ', seconds, from);
2023-02-24 13:47:07 +00:00
return audio_element.subscribe((el) => {
2023-02-22 23:41:29 +00:00
if (!el) return warn('no audio element');
if (from === 'from-end') {
el.currentTime = clamp(0, el.duration, el.duration - seconds);
} else {
el.currentTime = clamp(0, el.duration, seconds);
}
})();
}
export function skip(seconds: number, type: 'forward' | 'backward' = 'forward') {
2023-02-24 13:27:53 +00:00
info('skip: ', seconds, type);
2023-02-24 13:47:07 +00:00
return audio_element.subscribe((el) => {
2023-02-22 23:41:29 +00:00
if (!el) return warn('no audio element');
if (type === 'backward') {
el.currentTime = clamp(0, el.duration, el.currentTime - seconds);
} else {
el.currentTime = clamp(0, el.duration, el.currentTime + seconds);
}
})();
}