xref: /MusicFree/src/core/lyricManager.ts (revision bae3deccff7661525a230abc21e4747926cdcf0b)
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';
1472dabe5bS猫头猫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猫头猫// 重新获取歌词
29*bae3deccS猫头猫async function refreshLyric(fromStart?: boolean, forceRequest = false) {
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猫头猫
4672dabe5bS猫头猫        const currentParserMusicItem = lyricStateStore
4772dabe5bS猫头猫            .getValue()
4872dabe5bS猫头猫            ?.lyricParser?.getCurrentMusicItem();
4972dabe5bS猫头猫
5072dabe5bS猫头猫        let rawLrc: string | undefined;
51*bae3deccS猫头猫        if (
52*bae3deccS猫头猫            forceRequest ||
53*bae3deccS猫头猫            !isSameMediaItem(currentParserMusicItem, musicItem)
54*bae3deccS猫头猫        ) {
5557277364S猫头猫            lyricStateStore.setValue({
5657277364S猫头猫                loading: true,
5757277364S猫头猫                lyrics: [],
5857277364S猫头猫            });
5957277364S猫头猫            currentLyricStore.setValue(null);
6057277364S猫头猫
6172dabe5bS猫头猫            rawLrc = await PluginManager.getByMedia(
6257277364S猫头猫                musicItem,
6357277364S猫头猫            )?.methods?.getLyricText(musicItem);
6472dabe5bS猫头猫        } else {
6572dabe5bS猫头猫            rawLrc = lyricStateStore.getValue().lyricParser!.raw;
6672dabe5bS猫头猫        }
6772dabe5bS猫头猫
685500cea7S猫头猫        const realtimeMusicItem = TrackPlayer.getCurrentMusic();
6957277364S猫头猫        if (isSameMediaItem(musicItem, realtimeMusicItem)) {
7072dabe5bS猫头猫            if (rawLrc) {
7172dabe5bS猫头猫                const mediaExtra = MediaExtra.get(musicItem);
7272dabe5bS猫头猫                const parser = new LyricParser(rawLrc, musicItem, {
7372dabe5bS猫头猫                    offset: (mediaExtra?.lyricOffset || 0) * -1,
7472dabe5bS猫头猫                });
7557277364S猫头猫                lyricStateStore.setValue({
7657277364S猫头猫                    loading: false,
7757277364S猫头猫                    lyricParser: parser,
7857277364S猫头猫                    lyrics: parser.getLyric(),
7957277364S猫头猫                    meta: parser.getMeta(),
8057277364S猫头猫                });
8157277364S猫头猫                // 更新当前状态的歌词
8257277364S猫头猫                const currentLyric = fromStart
8357277364S猫头猫                    ? parser.getLyric()[0]
845500cea7S猫头猫                    : parser.getPosition(
855500cea7S猫头猫                          (await TrackPlayer.getProgress()).position,
865500cea7S猫头猫                      ).lrc;
8757277364S猫头猫                currentLyricStore.setValue(currentLyric || null);
8857277364S猫头猫            } else {
8957277364S猫头猫                // 没有歌词
9057277364S猫头猫                lyricStateStore.setValue({
9157277364S猫头猫                    loading: false,
9257277364S猫头猫                    lyrics: [],
9357277364S猫头猫                });
9457277364S猫头猫            }
9557277364S猫头猫        }
9657277364S猫头猫    } catch {
975500cea7S猫头猫        const realtimeMusicItem = TrackPlayer.getCurrentMusic();
9857277364S猫头猫        if (isSameMediaItem(musicItem, realtimeMusicItem)) {
9957277364S猫头猫            // 异常情况
10057277364S猫头猫            lyricStateStore.setValue({
10157277364S猫头猫                loading: false,
10257277364S猫头猫                lyrics: [],
10357277364S猫头猫            });
10457277364S猫头猫        }
10557277364S猫头猫    }
10657277364S猫头猫}
10757277364S猫头猫
10857277364S猫头猫// 获取歌词
10957277364S猫头猫async function setup() {
11057277364S猫头猫    DeviceEventEmitter.addListener(EDeviceEvents.REFRESH_LYRIC, refreshLyric);
11157277364S猫头猫
1120be848bcS猫头猫    if (Config.get('setting.lyric.showStatusBarLyric')) {
1130be848bcS猫头猫        LyricUtil.showStatusBarLyric(
1140be848bcS猫头猫            'MusicFree',
1150be848bcS猫头猫            Config.get('setting.lyric') ?? {},
1160be848bcS猫头猫        );
1170be848bcS猫头猫    }
1180be848bcS猫头猫
11957277364S猫头猫    refreshLyric();
12057277364S猫头猫}
12157277364S猫头猫
12257277364S猫头猫const LyricManager = {
12357277364S猫头猫    setup,
12457277364S猫头猫    useLyricState: lyricStateStore.useValue,
12557277364S猫头猫    getLyricState: lyricStateStore.getValue,
12657277364S猫头猫    useCurrentLyric: currentLyricStore.useValue,
12757277364S猫头猫    getCurrentLyric: currentLyricStore.getValue,
12857277364S猫头猫    setCurrentLyric: currentLyricStore.setValue,
12972dabe5bS猫头猫    refreshLyric,
13057277364S猫头猫};
13157277364S猫头猫
13257277364S猫头猫export default LyricManager;
133