Files
AllMusic/netapi/LyricDecoder.java
T

171 lines
5.6 KiB
Java
Raw Normal View History

2026-02-26 20:18:57 +08:00
package com.coloryr.allmusic.server.netapi;
2020-03-17 15:18:29 +08:00
2024-04-19 09:46:54 +08:00
import com.coloryr.allmusic.server.core.AllMusic;
import com.coloryr.allmusic.server.core.objs.music.LyricItemObj;
import com.coloryr.allmusic.server.core.utils.Function;
2026-02-26 20:35:11 +08:00
import com.coloryr.allmusic.server.netapi.obj.music.lyric.WLyricObj;
2020-03-29 15:25:34 +08:00
import java.util.*;
2023-01-09 15:56:26 +08:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-03-17 15:18:29 +08:00
2026-02-26 20:18:57 +08:00
public class LyricDecoder {
2023-01-09 15:56:26 +08:00
private static final Pattern p = Pattern.compile("\\(([0-9]+),[0-9]+.[0-9]\\)");
2024-09-21 14:13:00 +08:00
private final Map<Long, LyricItemObj> temp = new LinkedHashMap<>();
2020-04-05 15:58:58 +08:00
public boolean isHave = false;
2023-01-08 16:03:28 +08:00
public boolean isHaveK = false;
2024-09-21 14:13:00 +08:00
public Map<Long, String> kly = new LinkedHashMap<>();
2026-02-26 20:35:11 +08:00
2024-09-21 14:13:00 +08:00
public Map<Long, LyricItemObj> getTemp() {
2020-03-29 15:25:34 +08:00
return temp;
2020-03-17 15:18:29 +08:00
}
2026-02-26 20:35:11 +08:00
2024-09-21 14:13:00 +08:00
public Map<Long, String> getKLyric() {
2023-01-08 16:03:28 +08:00
return kly;
}
2020-03-17 15:18:29 +08:00
2023-01-06 17:27:35 +08:00
/**
* 检查歌词
2023-01-08 14:31:20 +08:00
*
2023-01-06 17:27:35 +08:00
* @param obj 歌词
* @return 结果
*/
2023-01-09 16:54:42 +08:00
public boolean check(WLyricObj obj) {
2020-03-29 15:25:34 +08:00
String[] lyric;
2020-03-17 15:18:29 +08:00
2020-03-29 15:25:34 +08:00
boolean haveT = false;
2023-01-06 18:15:10 +08:00
if (!obj.isOk())
2020-03-29 15:25:34 +08:00
return true;
2020-03-30 14:55:38 +08:00
else if (obj.isNone())
return false;
2020-03-29 15:25:34 +08:00
lyric = obj.getLyric().split("\n");
2023-01-08 14:31:20 +08:00
2024-09-21 14:13:00 +08:00
Map<Long, String> temp = getTime(Arrays.asList(lyric));
Map<Long, String> temp1 = new HashMap<>();
2023-01-08 14:31:20 +08:00
2020-03-29 15:25:34 +08:00
if (obj.getTlyric() != null) {
2023-01-08 14:31:20 +08:00
List<String> tlyric = Arrays.asList(obj.getTlyric().split("\n"));
2020-03-29 15:25:34 +08:00
haveT = true;
2023-01-08 14:31:20 +08:00
temp1 = getTime(tlyric);
2020-03-29 15:25:34 +08:00
}
2023-01-08 14:31:20 +08:00
2023-01-08 21:21:23 +08:00
String temp2 = obj.getYrc() == null ? obj.getKlyric() : obj.getYrc();
2023-01-08 16:21:11 +08:00
if (temp2 != null && !temp2.isEmpty()) {
String[] klyric = temp2.split("\n");
2023-01-08 14:31:20 +08:00
for (String item : klyric) {
2024-09-21 14:13:00 +08:00
Map<Long, String> temp4 = getKTime(item, obj.getVersion());
2023-01-08 14:31:20 +08:00
if (temp4 != null) {
2023-01-08 16:03:28 +08:00
kly.putAll(temp4);
2023-01-08 14:31:20 +08:00
}
2020-03-29 15:25:34 +08:00
}
2023-01-08 16:03:28 +08:00
isHaveK = true;
2023-01-08 14:31:20 +08:00
}
2024-09-21 14:13:00 +08:00
for (Map.Entry<Long, String> item : temp.entrySet()) {
2023-01-08 16:03:28 +08:00
this.temp.put(item.getKey(), new LyricItemObj(item.getValue(),
2023-01-08 16:28:12 +08:00
haveT ? temp1.get(item.getKey()) : null));
2020-03-29 15:25:34 +08:00
}
2023-01-08 14:31:20 +08:00
2023-01-08 16:03:28 +08:00
2020-03-31 11:02:27 +08:00
isHave = true;
2020-03-29 15:25:34 +08:00
return false;
2020-03-20 22:25:51 +08:00
}
2023-01-06 17:27:35 +08:00
/**
* 将歌词转换成时间 字
2023-01-08 14:31:20 +08:00
*
2023-01-06 17:27:35 +08:00
* @param lyric 歌词
* @return 结果
*/
2024-09-21 14:13:00 +08:00
private Map<Long, String> getTime(List<String> lyric) {
Map<Long, String> res = new LinkedHashMap<>();
2020-03-29 15:25:34 +08:00
String min;
String sec;
String mil;
2024-09-21 14:13:00 +08:00
long time;
long milt;
2020-03-29 15:25:34 +08:00
for (String s : lyric) {
2023-01-08 14:31:20 +08:00
if (!s.startsWith("["))
continue;
String temp = Function.getString(s, "[", "]");
if (!temp.contains(".") || !temp.contains(":"))
continue;
if (Function.countChar(temp, ':') > 1) {
2020-03-29 15:25:34 +08:00
String[] a = s.split(":");
min = a[0].substring(1);
sec = a[1];
mil = a[2];
} else {
min = Function.getString(s, "[", ":");
sec = Function.getString(s, ":", ".");
mil = Function.getString(s, ".", "]");
}
if (!Function.isInteger(min))
continue;
if (!Function.isInteger(sec))
continue;
if (!Function.isInteger(mil))
continue;
if (min.isEmpty() || sec.isEmpty() || mil.isEmpty())
continue;
2024-09-21 14:13:00 +08:00
milt = Long.parseLong(mil);
2020-03-29 15:25:34 +08:00
if (mil.length() == 3) {
milt /= 10;
}
2024-09-21 14:13:00 +08:00
time = Long.parseLong(min) * 60 * 1000 + Long.parseLong(sec) * 1000 + milt * 10;
2024-04-17 22:15:12 +08:00
if (time > 0 && time + AllMusic.getConfig().lyricDelay > 0)
time += AllMusic.getConfig().lyricDelay / 10 * 10;
2023-01-06 17:27:35 +08:00
res.put(time, Function.getString(s, "]", null));
2020-03-29 15:25:34 +08:00
}
2023-01-06 17:27:35 +08:00
return res;
2020-03-17 15:18:29 +08:00
}
2023-01-08 14:31:20 +08:00
2024-09-21 14:13:00 +08:00
private Map<Long, String> getKTime(String lyric, boolean yrc) {
Map<Long, String> res = new LinkedHashMap<>();
2023-01-08 14:31:20 +08:00
if (!lyric.startsWith("[") || !lyric.contains("]("))
return null;
2023-01-09 15:56:26 +08:00
String[] datas = lyric.split("\\(([0-9]+),[0-9]+.[0-9]\\)");
Matcher m = p.matcher(lyric);
List<String> temp1111 = new ArrayList<>();
while (m.find()) {
temp1111.add(m.group()
.replace("(", "")
.replace(")", ""));
}
if (datas.length == 1)
2023-01-08 14:31:20 +08:00
return null;
2023-01-08 16:03:28 +08:00
String temp = Function.getString(lyric, "[", "]");
String[] temp11 = temp.split(",");
2024-09-21 14:13:00 +08:00
long now = Integer.parseInt(temp11[0]) / 10 * 10;
2023-01-09 15:56:26 +08:00
for (int a = 1; a < datas.length; a++) {
String data = datas[a];
String temp3 = temp1111.get(a - 1);
2023-01-08 17:26:38 +08:00
String[] temp8 = temp3.split(",");
2024-09-21 14:13:00 +08:00
long temp5;
2023-01-08 17:26:38 +08:00
if (yrc) {
temp5 = (Integer.parseInt(temp8[0]) / 10 * 10);
2024-04-17 22:15:12 +08:00
if (temp5 > 0 && temp5 + AllMusic.getConfig().ktvLyricDelay > 0)
temp5 += (AllMusic.getConfig().ktvLyricDelay / 10 * 10);
2023-01-09 15:56:26 +08:00
res.put(temp5, data);
2023-01-08 20:55:21 +08:00
} else {
2023-01-08 17:26:38 +08:00
try {
temp5 = (Integer.parseInt(temp8[1]) / 10 * 10);
} catch (Exception e) {
2026-03-04 16:05:57 +08:00
AllMusic.log.data("不支持的Ktv歌词");
AllMusic.log.data("请带上音乐ID联系开发者");
2023-01-08 17:26:38 +08:00
return null;
}
2024-04-17 22:15:12 +08:00
if (temp5 > 0 && temp5 + AllMusic.getConfig().ktvLyricDelay > 0)
temp5 += (AllMusic.getConfig().ktvLyricDelay / 10 * 10);
2023-01-09 15:56:26 +08:00
res.put(now, data);
2023-01-08 17:26:38 +08:00
now += temp5;
2023-01-08 16:28:12 +08:00
}
2023-01-08 14:31:20 +08:00
}
return res;
}
2020-03-17 15:18:29 +08:00
}