xref: /MusicFree/src/core/lyricManager.ts (revision 72dabe5bf960a31f313d66723a199bb90e42e944)
157277364S猫头猫/**
257277364S猫头猫 * 管理当前歌曲的歌词
357277364S猫头猫 */
457277364S猫头猫
557277364S猫头猫import {isSameMediaItem} from '@/utils/mediaItem';
657277364S猫头猫import PluginManager from './pluginManager';
757277364S猫头猫import LyricParser from '@/utils/lrcParser';
857277364S猫头猫import {GlobalState} from '@/utils/stateMapper';
957277364S猫头猫import {EDeviceEvents} from '@/constants/commonConst';
1057277364S猫头猫import {DeviceEventEmitter} from 'react-native';
110be848bcS猫头猫import Config from './config';
120be848bcS猫头猫import LyricUtil from '@/native/lyricUtil';
135500cea7S猫头猫import TrackPlayer from './trackPlayer';
14*72dabe5bS猫头猫import MediaExtra from './mediaExtra';
1557277364S猫头猫
1657277364S猫头猫const lyricStateStore = new GlobalState<{
1757277364S猫头猫    loading: boolean;
1857277364S猫头猫    lyricParser?: LyricParser;
1957277364S猫头猫    lyrics: ILyric.IParsedLrc;
2057277364S猫头猫    meta?: Record<string, string>;
2157277364S猫头猫}>({
2257277364S猫头猫    loading: true,
2357277364S猫头猫    lyrics: [],
2457277364S猫头猫});
2557277364S猫头猫
2657277364S猫头猫const currentLyricStore = new GlobalState<ILyric.IParsedLrcItem | null>(null);
2757277364S猫头猫
2857277364S猫头猫// 重新获取歌词
2957277364S猫头猫async function refreshLyric(fromStart?: boolean) {
305500cea7S猫头猫    const musicItem = TrackPlayer.getCurrentMusic();
3157277364S猫头猫    try {
325500cea7S猫头猫        if (!musicItem) {
335500cea7S猫头猫            lyricStateStore.setValue({
345500cea7S猫头猫                loading: false,
355500cea7S猫头猫                lyrics: [],
365500cea7S猫头猫            });
375500cea7S猫头猫
385500cea7S猫头猫            currentLyricStore.setValue({
395500cea7S猫头猫                lrc: 'MusicFree',
405500cea7S猫头猫                time: 0,
415500cea7S猫头猫            });
425500cea7S猫头猫
435500cea7S猫头猫            return;
445500cea7S猫头猫        }
455500cea7S猫头猫
46*72dabe5bS猫头猫        const currentParserMusicItem = lyricStateStore
47*72dabe5bS猫头猫            .getValue()
48*72dabe5bS猫头猫            ?.lyricParser?.getCurrentMusicItem();
49*72dabe5bS猫头猫
50*72dabe5bS猫头猫        let rawLrc: string | undefined;
51*72dabe5bS猫头猫        if (!isSameMediaItem(currentParserMusicItem, musicItem)) {
5257277364S猫头猫            lyricStateStore.setValue({
5357277364S猫头猫                loading: true,
5457277364S猫头猫                lyrics: [],
5557277364S猫头猫            });
5657277364S猫头猫            currentLyricStore.setValue(null);
5757277364S猫头猫
58*72dabe5bS猫头猫            rawLrc = await PluginManager.getByMedia(
5957277364S猫头猫                musicItem,
6057277364S猫头猫            )?.methods?.getLyricText(musicItem);
61*72dabe5bS猫头猫        } else {
62*72dabe5bS猫头猫            rawLrc = lyricStateStore.getValue().lyricParser!.raw;
63*72dabe5bS猫头猫        }
64*72dabe5bS猫头猫
655500cea7S猫头猫        const realtimeMusicItem = TrackPlayer.getCurrentMusic();
6657277364S猫头猫        if (isSameMediaItem(musicItem, realtimeMusicItem)) {
67*72dabe5bS猫头猫            if (rawLrc) {
68*72dabe5bS猫头猫                const mediaExtra = MediaExtra.get(musicItem);
69*72dabe5bS猫头猫                const parser = new LyricParser(rawLrc, musicItem, {
70*72dabe5bS猫头猫                    offset: (mediaExtra?.lyricOffset || 0) * -1,
71*72dabe5bS猫头猫                });
7257277364S猫头猫                lyricStateStore.setValue({
7357277364S猫头猫                    loading: false,
7457277364S猫头猫                    lyricParser: parser,
7557277364S猫头猫                    lyrics: parser.getLyric(),
7657277364S猫头猫                    meta: parser.getMeta(),
7757277364S猫头猫                });
7857277364S猫头猫                // 更新当前状态的歌词
7957277364S猫头猫                const currentLyric = fromStart
8057277364S猫头猫                    ? parser.getLyric()[0]
815500cea7S猫头猫                    : parser.getPosition(
825500cea7S猫头猫                          (await TrackPlayer.getProgress()).position,
835500cea7S猫头猫                      ).lrc;
8457277364S猫头猫                currentLyricStore.setValue(currentLyric || null);
8557277364S猫头猫            } else {
8657277364S猫头猫                // 没有歌词
8757277364S猫头猫                lyricStateStore.setValue({
8857277364S猫头猫                    loading: false,
8957277364S猫头猫                    lyrics: [],
9057277364S猫头猫                });
9157277364S猫头猫            }
9257277364S猫头猫        }
9357277364S猫头猫    } catch {
945500cea7S猫头猫        const realtimeMusicItem = TrackPlayer.getCurrentMusic();
9557277364S猫头猫        if (isSameMediaItem(musicItem, realtimeMusicItem)) {
9657277364S猫头猫            // 异常情况
9757277364S猫头猫            lyricStateStore.setValue({
9857277364S猫头猫                loading: false,
9957277364S猫头猫                lyrics: [],
10057277364S猫头猫            });
10157277364S猫头猫        }
10257277364S猫头猫    }
10357277364S猫头猫}
10457277364S猫头猫
10557277364S猫头猫// 获取歌词
10657277364S猫头猫async function setup() {
10757277364S猫头猫    DeviceEventEmitter.addListener(EDeviceEvents.REFRESH_LYRIC, refreshLyric);
10857277364S猫头猫
1090be848bcS猫头猫    if (Config.get('setting.lyric.showStatusBarLyric')) {
1100be848bcS猫头猫        LyricUtil.showStatusBarLyric(
1110be848bcS猫头猫            'MusicFree',
1120be848bcS猫头猫            Config.get('setting.lyric') ?? {},
1130be848bcS猫头猫        );
1140be848bcS猫头猫    }
1150be848bcS猫头猫
11657277364S猫头猫    refreshLyric();
11757277364S猫头猫}
11857277364S猫头猫
11957277364S猫头猫const LyricManager = {
12057277364S猫头猫    setup,
12157277364S猫头猫    useLyricState: lyricStateStore.useValue,
12257277364S猫头猫    getLyricState: lyricStateStore.getValue,
12357277364S猫头猫    useCurrentLyric: currentLyricStore.useValue,
12457277364S猫头猫    getCurrentLyric: currentLyricStore.getValue,
12557277364S猫头猫    setCurrentLyric: currentLyricStore.setValue,
126*72dabe5bS猫头猫    refreshLyric,
12757277364S猫头猫};
12857277364S猫头猫
12957277364S猫头猫export default LyricManager;
130