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

126 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-02-24 13:47:07 +00:00
import {
audio_current_time,
audio_element,
audio_metadata,
audio_src,
} from '$lib/context/audio-internals';
2023-02-24 13:15:34 +00:00
import { secondsToTimestamp } from '$lib/utility/seconds-to-timestamp';
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';
2023-02-24 13:47:07 +00:00
import { derived } from 'svelte/store';
2023-02-22 23:41:29 +00:00
2023-02-24 13:47:07 +00:00
const audio_state = derived([audio_metadata, audio_current_time], ([$metadata, $current_time]) => {
return {
current_time: $current_time,
timestamp: secondsToTimestamp($current_time),
...$metadata,
};
2023-02-22 23:41:29 +00:00
});
type HandleType = 'toggle' | 'set';
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();
}
})();
}
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();
}
})();
}
function mute(type: HandleType = 'set') {
2023-02-24 13:27:53 +00:00
info('mute: ', 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.muted = !el.muted;
} else {
el.muted = true;
}
})();
}
function unmute(type: HandleType = 'set') {
2023-02-24 13:27:53 +00:00
info('unmute: ', 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.muted = !el.muted;
} else {
el.muted = false;
}
})();
}
function load(src: string) {
2023-02-24 13:27:53 +00:00
info('load: ', src);
2023-02-24 13:47:07 +00:00
audio_src.set(src);
return audio_element.subscribe((el) => {
2023-02-22 23:41:29 +00:00
if (!el) return warn('no audio element');
el.volume = 1;
el.pause();
})();
}
function unload() {
2023-02-24 13:27:53 +00:00
info('unload: ');
2023-02-24 13:47:07 +00:00
audio_src.set(null);
2023-02-22 23:41:29 +00:00
}
function setPlaybackRate(rate: number) {
2023-02-24 13:27:53 +00:00
info('setPlaybackRate: ', rate);
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');
el.playbackRate = clamp(0, 10, rate);
})();
}
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);
}
})();
}
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);
}
})();
}
export const audio = {
2023-02-24 13:47:07 +00:00
subscribe: audio_state.subscribe,
2023-02-22 23:41:29 +00:00
play,
pause,
mute,
unmute,
load,
unload,
setPlaybackRate,
seek,
skip,
};