index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import { computed, defineComponent, onMounted, onUnmounted, reactive, ref, watch } from "vue";
  2. import styles from "./index.module.less";
  3. import {
  4. getMidiCurrentTime,
  5. getMidiDuration,
  6. handleTogglePlayMidi,
  7. hanldeInitMidiData,
  8. hanldeSetMidiPlaybackRate,
  9. setMidiCurrentTime,
  10. } from "./midiPlayer";
  11. import state, { IPlayState, onEnded, onPlay } from "/src/state";
  12. import { api_playProgress, api_cloudTimeUpdae, api_cloudplayed, api_remove_cloudplayed, api_remove_cloudTimeUpdae } from "/src/helpers/communication";
  13. import { evaluatingData } from "/src/view/evaluating";
  14. import { cloudToggleState } from "/src/helpers/midiPlay"
  15. export const audioData = reactive({
  16. songEle: null as HTMLAudioElement | null, // 原生
  17. backgroundEle: null as HTMLAudioElement | null, //伴唱
  18. mingSongEle: null as HTMLAudioElement | null, //唱名
  19. songCollection: { // 音乐源合集 带节拍器和不带节拍器的
  20. songEle: null as HTMLAudioElement | null,
  21. backgroundEle: null as HTMLAudioElement | null,
  22. fanSongEle: null as HTMLAudioElement | null,
  23. banSongEle: null as HTMLAudioElement | null,
  24. mingSongEle: null as HTMLAudioElement | null,
  25. beatSongEle: null as HTMLAudioElement | null,
  26. betaBackgroundEle: null as HTMLAudioElement | null,
  27. betaFanSongEle: null as HTMLAudioElement | null,
  28. betaBanSongEle: null as HTMLAudioElement | null,
  29. betaMingSongEle: null as HTMLAudioElement | null
  30. },
  31. midiRender: false,
  32. progress: 0, // midi播放进度(单位:秒)
  33. duration: 0 // 音频总时长(单位:秒)
  34. });
  35. const midiRef = ref();
  36. /** 播放或暂停 */
  37. export const audioListStart = (type: "play" | "paused") => {
  38. // 开始播放之前, 先设置倍数
  39. if (type === "play" && state.originSpeed !== 0) {
  40. const actualRate = state.originAudioPlayRate * state.basePlayRate;
  41. // console.log('音频播放倍率',actualRate)
  42. setAudioPlaybackRate(actualRate);
  43. }
  44. // console.log('api','midi状态1',type,state.isAppPlay)
  45. // 如果是midi播放
  46. if (state.isAppPlay) {
  47. // handleTogglePlayMidi(type);
  48. cloudToggleState(type);
  49. return;
  50. }
  51. if (type === "play") {
  52. audioData.songEle?.play();
  53. audioData.backgroundEle?.play();
  54. audioData.mingSongEle?.play();
  55. } else if (type === "paused") {
  56. audioData.songEle?.pause();
  57. audioData.backgroundEle?.pause();
  58. audioData.mingSongEle?.pause();
  59. }
  60. };
  61. /** 设置倍数播放 */
  62. export const setAudioPlaybackRate = (rate: number) => {
  63. // 如果是midi播放
  64. if (state.isAppPlay) {
  65. if (state.modeType === "evaluating") return
  66. hanldeSetMidiPlaybackRate(rate);
  67. return;
  68. }
  69. audioData.songEle && (audioData.songEle.playbackRate = rate);
  70. audioData.backgroundEle && (audioData.backgroundEle.playbackRate = rate);
  71. audioData.mingSongEle && (audioData.mingSongEle.playbackRate = rate);
  72. };
  73. /** 获取当前播放的时间 */
  74. export const getAudioCurrentTime = () => {
  75. // 如果是midi播放
  76. if (state.isAppPlay) {
  77. // const c = getMidiCurrentTime();
  78. return audioData.progress;
  79. }
  80. // console.log('返回的时间',state.playSource, audioData.songEle?.currentTime,audioData.progress)
  81. if (state.playSource === "music") return audioData.songEle?.currentTime || audioData.progress;
  82. if (state.playSource === "background") return audioData.backgroundEle?.currentTime || audioData.progress;
  83. if (state.playSource === "mingSong") return audioData.mingSongEle?.currentTime || audioData.progress;
  84. return audioData.songEle?.currentTime || audioData.progress;
  85. };
  86. /** 获取曲谱的总时间 */
  87. export const getAudioDuration = () => {
  88. // 如果是midi播放
  89. if (state.isAppPlay) {
  90. // const d = getMidiDuration();
  91. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  92. return audioData.duration || songEndTime;
  93. }
  94. return audioData.songEle?.duration || audioData.backgroundEle?.duration || audioData.mingSongEle?.duration || audioData.duration;
  95. };
  96. /** 设置播放的开始时间 */
  97. export const setAudioCurrentTime = (time: number, index = 0) => {
  98. // console.log('开始时间12345',time)
  99. // 如果是midi播放
  100. if (state.isAppPlay) {
  101. setMidiCurrentTime(index);
  102. return;
  103. }
  104. audioData.songEle && (audioData.songEle.currentTime = time);
  105. audioData.backgroundEle && (audioData.backgroundEle.currentTime = time);
  106. audioData.mingSongEle && (audioData.mingSongEle.currentTime = time);
  107. audioData.progress = time;
  108. };
  109. /** 设置当前没有播放的音频静音 */
  110. export const toggleMutePlayAudio = (source: IPlayState, muted: boolean) => {
  111. if (source === "music") {
  112. if (audioData.songEle) {
  113. audioData.songEle.muted = muted;
  114. }
  115. } else if (source === "background") {
  116. if (audioData.backgroundEle) {
  117. audioData.backgroundEle.muted = muted;
  118. }
  119. } else if(source === "mingSong"){
  120. if (audioData.mingSongEle) {
  121. audioData.mingSongEle.muted = muted;
  122. }
  123. }
  124. };
  125. /** 切换节拍器音源 */
  126. export const changeSongSourceByBate = (isDisBate:boolean) => {
  127. // isDisBate 为true 切换到不带节拍的,为false 切换到带节拍的
  128. const songEleCurrentTime = audioData.songEle?.currentTime || 0
  129. const backgroundEleCurrentTime = audioData.backgroundEle?.currentTime || 0
  130. const mingSongEleCurrentTime = audioData.mingSongEle?.currentTime || 0
  131. if (isDisBate) {
  132. if(state.playType === "play"){
  133. audioData.songEle = audioData.songCollection.songEle
  134. audioData.backgroundEle = audioData.songCollection.backgroundEle
  135. } else {
  136. audioData.songEle = audioData.songCollection.fanSongEle
  137. audioData.backgroundEle = audioData.songCollection.banSongEle
  138. audioData.mingSongEle = audioData.songCollection.mingSongEle
  139. }
  140. } else {
  141. // 没有节拍器资源的时候 用 不带节拍器的资源,防止播放不了
  142. if(state.playType === "play"){
  143. audioData.songEle = audioData.songCollection.beatSongEle || audioData.songCollection.songEle
  144. audioData.backgroundEle = audioData.songCollection.betaBackgroundEle || audioData.songCollection.backgroundEle
  145. } else {
  146. audioData.songEle = audioData.songCollection.betaFanSongEle || audioData.songCollection.fanSongEle
  147. audioData.backgroundEle = audioData.songCollection.betaBanSongEle || audioData.songCollection.banSongEle
  148. audioData.mingSongEle = audioData.songCollection.betaMingSongEle || audioData.songCollection.mingSongEle
  149. }
  150. }
  151. audioData.songEle && (audioData.songEle.currentTime = songEleCurrentTime)
  152. audioData.backgroundEle && (audioData.backgroundEle.currentTime = backgroundEleCurrentTime)
  153. audioData.mingSongEle && (audioData.mingSongEle.currentTime = mingSongEleCurrentTime)
  154. // 设置静音与取消静音
  155. if (state.playSource === "music") {
  156. audioData.songEle && (audioData.songEle.muted = false);
  157. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  158. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  159. } else if (state.playSource === "background") {
  160. audioData.songEle && (audioData.songEle.muted = true);
  161. audioData.backgroundEle && (audioData.backgroundEle.muted = false);
  162. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  163. }else {
  164. audioData.songEle && (audioData.songEle.muted = true);
  165. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  166. audioData.mingSongEle && (audioData.mingSongEle.muted = false);
  167. }
  168. }
  169. export default defineComponent({
  170. name: "audio-list",
  171. setup() {
  172. /** iframe 加载完成后, 加载midiURL */
  173. const handleLoad = () => {
  174. midiRef.value.contentWindow.handleRendered = () => {
  175. audioData.midiRender = true
  176. };
  177. hanldeInitMidiData(midiRef.value);
  178. };
  179. watch(
  180. () => state.playSource,
  181. () => {
  182. if (state.modeType === "evaluating" && !state.setting.enableAccompaniment) {
  183. console.log("评测模式设置了关闭伴奏,不切换原音伴奏");
  184. return;
  185. }
  186. if (state.playSource === "music") {
  187. audioData.songEle && (audioData.songEle.muted = false);
  188. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  189. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  190. } else if (state.playSource === "background") {
  191. audioData.songEle && (audioData.songEle.muted = true);
  192. audioData.backgroundEle && (audioData.backgroundEle.muted = false);
  193. audioData.mingSongEle && (audioData.mingSongEle.muted = true);
  194. }else {
  195. audioData.songEle && (audioData.songEle.muted = true);
  196. audioData.backgroundEle && (audioData.backgroundEle.muted = true);
  197. audioData.mingSongEle && (audioData.mingSongEle.muted = false);
  198. }
  199. }
  200. );
  201. const createAudio = (src?: string): Promise<HTMLAudioElement | null> => {
  202. if(!src){
  203. return Promise.resolve(null)
  204. }
  205. return new Promise((resolve) => {
  206. const a = new Audio(src + '?v=' + Date.now());
  207. a.load();
  208. a.onloadedmetadata = () => {
  209. resolve(a);
  210. };
  211. a.onerror = () => {
  212. resolve(null);
  213. };
  214. });
  215. };
  216. // 监听评测曲谱音频播放进度,返回
  217. const progress = (res: any) => {
  218. const currentTime = res?.currentTime || res?.content?.currentTime;
  219. const total = res?.totalDuration || res?.content?.totalDuration;
  220. const time = currentTime / 1000;
  221. audioData.progress = time;
  222. audioData.songEle && (audioData.songEle.currentTime = time);
  223. audioData.backgroundEle && (audioData.backgroundEle.currentTime = time);
  224. audioData.mingSongEle && (audioData.mingSongEle.currentTime = time);
  225. audioData.duration = total / 1000;
  226. if (
  227. res?.content?.totalDuration > 1000 &&
  228. currentTime >= total
  229. ) {
  230. if (evaluatingData.isAudioPlayEnd) return
  231. evaluatingData.isAudioPlayEnd = true
  232. onEnded();
  233. }
  234. };
  235. // midi播放进度回调
  236. const midiProgress = (res: any) => {
  237. // console.log('api',res)
  238. if (audioData.duration == 0) {
  239. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  240. audioData.duration = songEndTime
  241. }
  242. const currentTime = res?.currentTime || res?.content?.currentTime;
  243. const total = res?.totalDuration || res?.content?.totalDuration;
  244. const time = currentTime / 1000;
  245. audioData.progress = time;
  246. // if (
  247. // audioData.duration > 1000 &&
  248. // currentTime >= audioData.duration * 1000
  249. // ) {
  250. // console.log('midi结束')
  251. // onEnded();
  252. // }
  253. // 选段模式,播放结束
  254. if (state.sectionStatus && state.section.length == 2 && currentTime >= state.section) {
  255. //
  256. }
  257. }
  258. // midi播放结束回调
  259. const midiPlayEnd = (res: any) => {
  260. if (res) {
  261. console.log('midi结束')
  262. audioData.progress = 0
  263. onEnded();
  264. }
  265. }
  266. // 加载音源
  267. function loadAudio(){
  268. return Promise.all([createAudio(state.music), createAudio(state.accompany), createAudio(state.fanSong), createAudio(state.banSong), createAudio(state.mingSong)])
  269. }
  270. // 加载节拍器音源
  271. function loadBeatAudio(){
  272. return Promise.all([createAudio(state.beatSong.music), createAudio(state.beatSong.accompany), createAudio(state.beatSong.fanSong), createAudio(state.beatSong.banSong), createAudio(state.beatSong.mingSong)])
  273. }
  274. onMounted(async () => {
  275. // 预览的时候不走音频加载逻辑
  276. if(state.isPreView){
  277. state.isLoading = false;
  278. return
  279. }
  280. if (state.playMode !== "MIDI") {
  281. console.time("音频加载时间")
  282. // 处理音源
  283. const [music, accompany, fanSong, banSong, mingSong] = await loadAudio()
  284. audioData.backgroundEle = accompany;
  285. audioData.songEle = music;
  286. Object.assign(audioData.songCollection, {
  287. songEle:music,
  288. backgroundEle:accompany,
  289. fanSongEle:fanSong,
  290. banSongEle:banSong,
  291. mingSongEle:mingSong
  292. })
  293. if (music) {
  294. music.addEventListener("play", onPlay);
  295. music.addEventListener("ended", onEnded);
  296. accompany && (accompany.muted = true);
  297. } else if (accompany) {
  298. accompany.addEventListener("play", onPlay);
  299. accompany.addEventListener("ended", onEnded);
  300. }
  301. if(fanSong){
  302. fanSong.addEventListener("play", onPlay);
  303. fanSong.addEventListener("ended", onEnded);
  304. banSong && (banSong.muted = true);
  305. mingSong && (mingSong.muted = true);
  306. }else if(banSong){
  307. banSong.addEventListener("play", onPlay);
  308. banSong.addEventListener("ended", onEnded);
  309. mingSong && (mingSong.muted = true);
  310. } else if(mingSong){
  311. mingSong.addEventListener("play", onPlay);
  312. mingSong.addEventListener("ended", onEnded);
  313. }
  314. // 处理带节拍器的音源
  315. const [beatMusic, beatAccompany, beatFanSong, beatBanSong, beatMingSong] = await loadBeatAudio()
  316. Object.assign(audioData.songCollection, {
  317. beatSongEle:beatMusic,
  318. betaBackgroundEle:beatAccompany,
  319. betaFanSongEle:beatFanSong,
  320. betaBanSongEle:beatBanSong,
  321. betaMingSongEle:beatMingSong
  322. })
  323. if (beatMusic) {
  324. beatMusic.addEventListener("play", onPlay);
  325. beatMusic.addEventListener("ended", onEnded);
  326. beatAccompany && (beatAccompany.muted = true);
  327. } else if (beatAccompany) {
  328. beatAccompany.addEventListener("play", onPlay);
  329. beatAccompany.addEventListener("ended", onEnded);
  330. }
  331. if(beatFanSong){
  332. beatFanSong.addEventListener("play", onPlay);
  333. beatFanSong.addEventListener("ended", onEnded);
  334. beatBanSong && (beatBanSong.muted = true);
  335. beatMingSong && (beatMingSong.muted = true);
  336. }else if(beatBanSong){
  337. beatBanSong.addEventListener("play", onPlay);
  338. beatBanSong.addEventListener("ended", onEnded);
  339. beatMingSong && (beatMingSong.muted = true);
  340. } else if(beatMingSong){
  341. beatMingSong.addEventListener("play", onPlay);
  342. beatMingSong.addEventListener("ended", onEnded);
  343. }
  344. state.audioDone = true;
  345. state.isLoading = false
  346. console.timeEnd("音频加载时间")
  347. console.log("音频数据:",audioData)
  348. api_playProgress(progress);
  349. } else {
  350. state.audioDone = true;
  351. state.isLoading = false
  352. const songEndTime = state.times[state.times.length - 1 || 0]?.endtime || 0
  353. audioData.duration = songEndTime
  354. // 监听midi播放进度
  355. api_cloudTimeUpdae(midiProgress);
  356. // 监听midi播放结束
  357. api_cloudplayed(midiPlayEnd);
  358. }
  359. });
  360. onUnmounted(() => {
  361. api_remove_cloudplayed(midiPlayEnd);
  362. api_remove_cloudTimeUpdae(midiProgress);
  363. });
  364. // console.log(state.playMode, state.midiUrl);
  365. return () => (
  366. <>
  367. <div class={styles.audioList}>
  368. {state.playMode === "MIDI" && state.speed != 0 && (
  369. <iframe
  370. style={{ display: "none" }}
  371. ref={midiRef}
  372. src={`/midi/index.html`}
  373. onLoad={handleLoad}
  374. />
  375. )}
  376. </div>
  377. </>
  378. );
  379. },
  380. });