state.ts 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. import { closeToast, showToast } from "vant";
  2. import { nextTick, reactive } from "vue";
  3. import { OpenSheetMusicDisplay } from "../osmd-extended/src";
  4. import { metronomeData } from "./helpers/metronome";
  5. import { GradualNote, GradualTimes, GradualVersion } from "./type";
  6. import { handleEndEvaluat, handleStartEvaluat } from "./view/evaluating";
  7. import { IFingering, mappingVoicePart, subjectFingering, matchVoicePart } from "/src/view/fingering/fingering-config";
  8. import { handleStartTick } from "./view/tick";
  9. import { audioListStart, getAudioCurrentTime, getAudioDuration, setAudioCurrentTime, setAudioPlaybackRate } from "./view/audio-list";
  10. import { toggleFollow } from "./view/follow-practice";
  11. import { browser, setStorageSpeed, setGlobalData } from "./utils";
  12. import { api_cloudGetMediaStatus, api_createMusicPlayer, api_cloudChangeSpeed } from "./helpers/communication";
  13. import { verifyCanRepeat, getDuration } from "./helpers/formateMusic";
  14. import { getMusicSheetDetail } from "./utils/baseApi"
  15. import { getQuery } from "/src/utils/queryString";
  16. import { followData } from "/src/view/follow-practice/index"
  17. const query: any = getQuery();
  18. /** 入门 | 进阶 | 大师 */
  19. export type IDifficulty = "BEGINNER" | "ADVANCED" | "PERFORMER";
  20. /** 渲染类型: 五线谱,简谱 */
  21. export enum EnumMusicRenderType {
  22. /** 五线谱 */
  23. staff = "staff",
  24. /** 简谱 */
  25. firstTone = "firstTone",
  26. /** 固定音高 */
  27. fixedTone = "fixedTone",
  28. }
  29. export const musicscoresettingKey = "musicscoresetting";
  30. /** 有声音的是那个音源 */
  31. export type IPlayState = "music" | "background";
  32. /** 播放状态 */
  33. export type IAudioState = "play" | "paused";
  34. /** 来源 */
  35. export enum IPlatform {
  36. APP = "APP",
  37. PC = "PC",
  38. }
  39. export type ISonges = {
  40. background?: string
  41. music?: string
  42. }
  43. /**
  44. * 特殊教材分类id
  45. */
  46. const classids = [1,2,3,4,6,7,8,9,10,11,12,13,14,15,16,17,30,31,35,36,38,108,150,151,152,153,154,155,156,157,158,178,179,180,181,182]; // 大雅金唐, 竖笛教程, 声部训练展开的分类ID
  47. // 乐器code码
  48. export const musicalInstrumentCodeInfo = [
  49. {
  50. name: '长笛',
  51. code: 'Flute',
  52. id: 1
  53. },
  54. {
  55. name: '短笛',
  56. code: 'Piccolo',
  57. id: 2
  58. },
  59. {
  60. name: '单簧管',
  61. code: 'Clarinet',
  62. id: 3
  63. },
  64. {
  65. name: '低音单簧管',
  66. code: 'Bass Clarinet',
  67. id: 4
  68. },
  69. {
  70. name: '中音萨克斯',
  71. code: 'Alto Saxophone',
  72. id: 5
  73. },
  74. {
  75. name: '次中音萨克斯',
  76. code: 'Tenor Saxophone',
  77. id: 6
  78. },
  79. {
  80. name: '高音萨克斯',
  81. code: 'Soprano Saxophone',
  82. id: 7
  83. },
  84. {
  85. name: '上低音萨克斯',
  86. code: 'Baritone Saxophone',
  87. id: 8
  88. },
  89. {
  90. name: '双簧管',
  91. code: 'Oboe',
  92. id: 9
  93. },
  94. {
  95. name: '大管',
  96. code: 'Bassoon',
  97. id: 10
  98. },
  99. {
  100. name: '小号',
  101. code: 'Trumpet',
  102. id: 11
  103. },
  104. {
  105. name: '圆号',
  106. code: 'Horn',
  107. id: 12
  108. },
  109. {
  110. name: '长号',
  111. code: 'Trombone',
  112. id: 13
  113. },
  114. {
  115. name: '上低音号',
  116. code: 'Baritone',
  117. id: 14
  118. },
  119. {
  120. name: '次中音号',
  121. code: 'Euphonium',
  122. id: 15
  123. },
  124. {
  125. name: '大号',
  126. code: 'Tuba',
  127. id: 16
  128. },
  129. {
  130. name: '钢琴',
  131. code: 'Piano',
  132. id: 17
  133. },
  134. {
  135. name: '电钢琴',
  136. code: 'Electronical Piano',
  137. id: 18
  138. },
  139. {
  140. name: '钢片琴',
  141. code: 'Glockenspiel',
  142. id: 19
  143. },
  144. {
  145. name: '小提琴',
  146. code: 'Violin',
  147. id: 20
  148. },
  149. {
  150. name: '中提琴',
  151. code: 'Viola',
  152. id: 21
  153. },
  154. {
  155. name: '大提琴',
  156. code: 'Violoncello',
  157. id: 22
  158. },
  159. {
  160. name: '低音提琴',
  161. code: 'Contrabass',
  162. id: 23
  163. },
  164. {
  165. name: '架子鼓',
  166. code: 'Drum Set',
  167. id: 24
  168. },
  169. {
  170. name: '小鼓',
  171. code: 'Snare Drum',
  172. id: 25
  173. },
  174. {
  175. name: '马林巴',
  176. code: 'Marimba',
  177. id: 26
  178. },
  179. {
  180. name: '颤音琴',
  181. code: 'Vibraphone',
  182. id: 27
  183. },
  184. {
  185. name: '钟琴',
  186. code: 'Chimes',
  187. id: 28
  188. },
  189. {
  190. name: '木琴',
  191. code: 'Xylophone',
  192. id: 29
  193. },
  194. {
  195. name: '管钟',
  196. code: 'Tubular Bells',
  197. id: 30
  198. },
  199. {
  200. name: '定音鼓',
  201. code: 'Timpani',
  202. id: 31
  203. },
  204. {
  205. name: '键盘',
  206. code: 'Mallets',
  207. id: 32
  208. },
  209. {
  210. name: '排箫',
  211. code: 'Panpipes',
  212. id: 33
  213. },
  214. {
  215. name: '陶笛',
  216. code: 'Ocarina',
  217. id: 34
  218. },
  219. {
  220. name: '葫芦丝',
  221. code: 'Woodwind',
  222. id: 35
  223. },
  224. {
  225. name: '口风琴',
  226. code: 'Nai',
  227. id: 36
  228. },
  229. {
  230. name: '德式竖笛',
  231. code: 'Tenor Recorder',
  232. id: 37
  233. },
  234. {
  235. name: '英式竖笛',
  236. code: 'Baroque Recorder',
  237. id: 38
  238. },
  239. ]
  240. const state = reactive({
  241. /** 来源 : PC , app */
  242. platform: "" as IPlatform,
  243. appName: "" as "GYM" | "COLEXIU",
  244. musicRenderType: EnumMusicRenderType.staff as EnumMusicRenderType,
  245. /**曲谱是否渲染完成 */
  246. musicRendered: false,
  247. /** 当前曲谱数据ID, 和曲谱ID不一致 */
  248. detailId: "",
  249. /** 曲谱资源URL */
  250. xmlUrl: "",
  251. /** 声部ID */
  252. subjectId: 0 as number,
  253. trackId: 0 as string | number,
  254. /** 分类ID */
  255. categoriesId: 0,
  256. /** 分类名称 */
  257. categoriesName: "",
  258. /** 是否支持评测 */
  259. enableEvaluation: true,
  260. /** 是否支持转谱 */
  261. enableNotation: false,
  262. /** 曲谱ID */
  263. examSongId: "",
  264. /** 内容平台的曲谱ID,可能会和业务端的id不一样 */
  265. cbsExamSongId: "",
  266. /** 曲谱名称 */
  267. examSongName: "",
  268. /** 曲谱封面 */
  269. coverImg: "",
  270. /** 扩展字段 */
  271. extConfigJson: {} as any,
  272. /** 扩展样式字段 */
  273. extStyleConfigJson: {} as any,
  274. /** 是否开启节拍器(mp3节拍器) */
  275. isOpenMetronome: false,
  276. /** 是否显示指法 */
  277. isShowFingering: false,
  278. /** 原音 */
  279. music: "",
  280. /** 伴奏 */
  281. accompany: "",
  282. /** midiURL */
  283. midiUrl: "",
  284. /** 父分ID */
  285. parentCategoriesId: 0,
  286. /** 分类ID */
  287. musicSheetCategoriesId: 0,
  288. /** 各产品端的分类ID,(管乐迷、管乐团、酷乐秀、课堂乐器) */
  289. bizMusicCategoryId: 0,
  290. /** 资源类型: mp3 | midi */
  291. playMode: "MP3" as "MP3" | "MIDI",
  292. /** 设置的速度 */
  293. speed: 0,
  294. /** 曲谱音频正常的速度 */
  295. originSpeed: 0,
  296. /** 播放过程中显示的速度 */
  297. playIngSpeed: 0,
  298. /** 分轨名称 */
  299. track: "",
  300. /** 当前显示声部索引 */
  301. partIndex: 0,
  302. /** 是否需要节拍器 */
  303. needTick: false,
  304. /** 曲谱实例 */
  305. osmd: null as unknown as OpenSheetMusicDisplay,
  306. /**是否是特殊乐谱类型, 主要针对管乐迷 */
  307. isSpecialBookCategory: false,
  308. /** 播放状态 */
  309. playState: "paused" as IAudioState,
  310. /** 播放结束状态 */
  311. playEnd: false,
  312. /** 播放那个: 原音,伴奏 */
  313. playSource: "music" as IPlayState,
  314. /** 播放进度 */
  315. playProgress: 0,
  316. /** 激活的note index */
  317. activeNoteIndex: 0,
  318. /** 激活的小节 */
  319. activeMeasureIndex: 0,
  320. /** 选段状态 */
  321. sectionStatus: false,
  322. /** 选段数据 */
  323. section: [] as any[],
  324. /** 选段背景 */
  325. sectionBoundingBoxs: [] as any[],
  326. /** 开启选段预备 */
  327. isOpenPrepare: false,
  328. /** 选段预备 */
  329. sectionFirst: null as any,
  330. /** 音符数据 */
  331. times: [] as any[],
  332. /** 播放模式 */
  333. modeType: "practise" as "practise" | "follow" | "evaluating",
  334. /** 设置 */
  335. setting: {
  336. /** 效音提醒 */
  337. soundEffect: true,
  338. /** 护眼模式 */
  339. eyeProtection: false,
  340. /** 摄像头 */
  341. camera: false,
  342. /** 摄像头透明度 */
  343. cameraOpacity: 70,
  344. /** 循环播放 */
  345. repeatAutoPlay: true,
  346. /** 显示指法 */
  347. displayFingering: true,
  348. /** 显示光标 */
  349. displayCursor: true,
  350. /** 频率 */
  351. frequency: 0,
  352. /** 评测难度 */
  353. evaluationDifficulty: "ADVANCED" as IDifficulty,
  354. /** 保存到相册 */
  355. saveToAlbum: true,
  356. /** 开启伴奏 */
  357. enableAccompaniment: true,
  358. /** 反应时间 */
  359. reactionTimeMs: 0,
  360. /** 节拍器音量 */
  361. beatVolume: 50,
  362. },
  363. /** 后台设置的基准评测频率 */
  364. baseFrequency: 440,
  365. /** mp3节拍器的时间,统计拍数、速度计算得出 */
  366. fixtime: 0,
  367. /** 指法信息 */
  368. fingeringInfo: {} as IFingering,
  369. /** 滚动容器的ID */
  370. scrollContainer: "musicAndSelection",
  371. /** 是否是打击乐 */
  372. isPercussion: false,
  373. /** 评测标准 */
  374. evaluationStandard: '',
  375. /** 是否重复节拍器的时间 */
  376. repeatedBeats: 0,
  377. /**当前曲谱中所有声部名字 */
  378. partListNames: [] as any,
  379. /** 渐变速度信息 */
  380. gradual: [] as GradualNote[],
  381. /** 渐变速度版本 */
  382. gradualVersion: GradualVersion.BASE as GradualVersion,
  383. /** 渐变时间信息 */
  384. gradualTimes: null as GradualTimes,
  385. /** 单声部多声轨 */
  386. multitrack: 0,
  387. /** 缩放 */
  388. zoom: 0.8,
  389. /** 渲染曲谱比例 */
  390. musicZoom: 1,
  391. /** 练习,评测是否是选段模式 */
  392. isSelectMeasureMode: false,
  393. /** 是否是评分显示 */
  394. isReport: false,
  395. /** 是否隐藏评测报告弹窗,保存演奏按钮,默认不隐藏 */
  396. isHideEvaluatReportSaveBtn: false,
  397. /** 是否是合奏 */
  398. isConcert: false,
  399. /** 用户选择的结束小节数 */
  400. userChooseEndIndex: 0,
  401. /** 重播小节集合信息 */
  402. repeatInfo: [],
  403. /** 多分轨的曲子,可支持筛选的分轨 */
  404. canSelectTracks: [] as any,
  405. /** 声部codeId */
  406. subjectCodeId: 0 as number,
  407. /** 乐器codeId,用于匹配乐器指法、声部转调、特殊声部处理等 */
  408. musicalCodeId: 0 as number,
  409. /** 乐器code,用于评测传参 */
  410. musicalCode: '' as any,
  411. /** 合奏曲目是否合并展示 */
  412. isCombineRender: false,
  413. /** 小节的持续时长,以后台设置的播放速度计算 */
  414. measureTime: 0,
  415. /** 跟练模式,节拍器播放的时间 */
  416. beatStartTime: 0,
  417. /** 是否为详情预览模式 */
  418. isPreView: false,
  419. /** 是否为评测报告模式 */
  420. isEvaluatReport: false,
  421. /** midi播放器是否初始化中 */
  422. midiPlayIniting: false,
  423. /** 曲目信息 */
  424. songs: {} as ISonges,
  425. isAppPlay: false, // 是否是app播放
  426. /** 音频播放器实例 */
  427. audiosInstance: null as any,
  428. /** midi音频的时长 */
  429. durationNum: 0,
  430. });
  431. const browserInfo = browser();
  432. let offset_duration = 0;
  433. /** 自定义数据 */
  434. export const customData = reactive({
  435. /** 自定义音符时值 */
  436. customNoteRealValue: [] as any,
  437. /** 自定义音符按读取到的时值 */
  438. customNoteCurrentTime: false,
  439. });
  440. /** 在渲染前后计算光标应该走到的音符 */
  441. const setStep = () => {
  442. if (state.playState !== "play") {
  443. console.log("暂停播放");
  444. return;
  445. }
  446. let startTime = Date.now();
  447. requestAnimationFrame(() => {
  448. const endTime = Date.now();
  449. // 渲染时间大于16.6,就会让页面卡顿, 如果渲染时间大与16.6就下一个渲染帧去计算
  450. if (endTime - startTime < 16.7) {
  451. handlePlaying();
  452. setStep();
  453. } else {
  454. setTimeout(() => {
  455. handlePlaying();
  456. setStep();
  457. }, 16.7);
  458. }
  459. });
  460. };
  461. /** 开始播放 */
  462. export const onPlay = () => {
  463. console.log("开始播放");
  464. state.playEnd = false;
  465. // offset_duration = browserInfo.xiaomi ? 0.2 : 0.08;
  466. offset_duration = 0.2;
  467. setStep();
  468. };
  469. /** 播放模式结束自动重播 */
  470. const autoResetPlay = () => {
  471. if (state.modeType !== "practise") return;
  472. skipNotePlay(0, true);
  473. // 没有开启自动重播, 不是练习模式
  474. if (!state.setting.repeatAutoPlay) return;
  475. scrollViewNote();
  476. setTimeout(() => {
  477. togglePlay("play");
  478. }, 1000);
  479. };
  480. /** 播放完成事件 */
  481. export const onEnded = () => {
  482. console.log("音频播放结束");
  483. // 修改状态为结束
  484. state.playEnd = true;
  485. state.playState = "paused";
  486. // 结束播放
  487. audioListStart(state.playState);
  488. // 调用结束评测
  489. handleEndEvaluat(true);
  490. // 调用自动重复播放
  491. autoResetPlay();
  492. };
  493. /**
  494. * 播放一直触发的事件
  495. */
  496. const handlePlaying = () => {
  497. const currentTime = getAudioCurrentTime();
  498. const duration = getAudioDuration();
  499. state.playProgress = (currentTime / duration) * 100;
  500. let item = getNote(currentTime);
  501. // console.log(11111,currentTime,duration,state.playSource, item.i)
  502. // console.log(item.i,item.noteId,item.measureSpeed)
  503. // 练习模式下,实时刷新小节速度
  504. if (item && state.modeType === "practise" && state.playState === "play" && item.measureSpeed && item.measureSpeed !== state.playIngSpeed) {
  505. const ratio = state.speed / state.originSpeed
  506. state.playIngSpeed = Math.ceil(ratio * item.measureSpeed) || state.speed
  507. } else if (state.modeType === "practise" && state.playState === "play" && item && !item.measureSpeed) {
  508. state.playIngSpeed = state.speed
  509. }
  510. if (item) {
  511. // 选段状态下
  512. if (state.sectionStatus && state.section.length === 2) {
  513. // 如果开启了预备拍
  514. const selectStartItem = state.sectionFirst ? state.sectionFirst : state.section[0];
  515. const selectEndItem = state.section[1];
  516. /**
  517. * #9374,反复小节的曲目播放错误, bug修复
  518. * 曲目:噢!苏珊娜-排箫-人音
  519. * 现象:重播小节为2-9,选段为4-12,当播完第9小节后会回到第2小节重播2-8,再播放10-12
  520. * 4-12,不符合重播规则,所以播完第9小节后,音频需要跳转到第10小节播放
  521. */
  522. if (state.repeatInfo.length) {
  523. const canRepeatInfo = verifyCanRepeat(state.section[0].MeasureNumberXML, state.section[1].MeasureNumberXML)
  524. const repeatIdx = canRepeatInfo.repeatIdx == -1 ? 0 : canRepeatInfo.repeatIdx
  525. if (state.modeType === "practise" && !canRepeatInfo.canRepeat && state.section[1].MeasureNumberXML > state.repeatInfo[repeatIdx].end) {
  526. const preItem = state.times[item.i - 1]
  527. if (preItem && preItem.MeasureNumberXML > item.MeasureNumberXML) {
  528. const skipItem = state.times.find((item: any) => item.MeasureNumberXML === preItem.MeasureNumberXML + 1)
  529. if (skipItem) {
  530. // 跳转到指定的音频位置
  531. setAudioCurrentTime(skipItem.time, skipItem.i);
  532. gotoNext(skipItem);
  533. return
  534. }
  535. }
  536. }
  537. }
  538. // if (Math.abs(selectEndItem.endtime - currentTime) < offset_duration) {
  539. if (currentTime - selectEndItem.endtime > offset_duration) {
  540. console.log("选段播放结束");
  541. // 如果为选段评测模式
  542. if (state.modeType === "evaluating" && state.isSelectMeasureMode) {
  543. onEnded();
  544. return;
  545. }
  546. // #8698 bug修复
  547. if (state.modeType === "practise" && state.sectionStatus && !state.setting.repeatAutoPlay) {
  548. onEnded();
  549. resetPlaybackToStart();
  550. return;
  551. }
  552. item = selectStartItem;
  553. setAudioCurrentTime(selectStartItem.time, selectStartItem.i);
  554. }
  555. }
  556. gotoNext(item);
  557. }
  558. // 评测不播放叮咚节拍器
  559. // if (state.modeType !== "evaluating") {
  560. // metronomeData.metro?.sound(currentTime);
  561. // }
  562. metronomeData.metro?.sound(currentTime);
  563. };
  564. /** 跳转到指定音符开始播放 */
  565. export const skipNotePlay = (itemIndex: number, isStart = false) => {
  566. const item = state.times[itemIndex];
  567. let itemTime = item.time;
  568. if (isStart) {
  569. itemTime = 0;
  570. }
  571. if (item) {
  572. setAudioCurrentTime(itemTime, itemIndex);
  573. gotoNext(item);
  574. metronomeData.metro?.sound(itemTime);
  575. }
  576. };
  577. /**
  578. * 切换曲谱播放状态
  579. * @param playState 可选: 默认 undefined, 需要切换的状态 play:播放, paused: 暂停
  580. */
  581. export const togglePlay = async (playState?: "play" | "paused") => {
  582. // midi播放
  583. if (state.isAppPlay) {
  584. await api_cloudChangeSpeed({
  585. speed: state.speed,
  586. originalSpeed: state.originSpeed,
  587. songID: state.examSongId,
  588. });
  589. const cloudGetMediaStatus = await api_cloudGetMediaStatus();
  590. const status = cloudGetMediaStatus?.content.status
  591. state.playState = status
  592. } else {
  593. state.playState = playState ? playState : state.playState === "paused" ? "play" : "paused";
  594. }
  595. if (state.playState === "play" && state.sectionStatus && state.section.length == 2 && state.playProgress === 0) {
  596. resetPlaybackToStart();
  597. }
  598. // 设置为开始播放时, 如果需要节拍,先播放节拍器
  599. if (state.playState === "play" && state.needTick) {
  600. const tickend = await handleStartTick();
  601. // console.log("🚀 ~ tickend:", tickend)
  602. // 节拍器返回false, 取消播放
  603. if (!tickend) {
  604. state.playState = "paused";
  605. return false;
  606. }
  607. }
  608. // 如果选段没有结束, 直接开始播放,清空选段状态
  609. if (state.playState == "play") {
  610. if (state.sectionStatus && state.section.length < 2) {
  611. clearSelection();
  612. }
  613. }
  614. audioListStart(state.playState);
  615. return true;
  616. };
  617. /** 结束播放 */
  618. export const handleStopPlay = () => {
  619. state.playState = "paused";
  620. audioListStart(state.playState);
  621. };
  622. /** 重置播放为开始 */
  623. export const resetPlaybackToStart = () => {
  624. // 如果为选段状态
  625. if (state.sectionStatus && state.section.length === 2) {
  626. state.section = formateSelectMearure(state.section);
  627. return;
  628. }
  629. skipNotePlay(0, true);
  630. };
  631. /** 跳转到指定音符 */
  632. export const gotoCustomNote = (index: number) => {
  633. try {
  634. state.osmd.cursor.reset();
  635. } catch (error) {}
  636. for (let i = 0; i < index; i++) {
  637. state.osmd.cursor.next();
  638. }
  639. };
  640. const setCursorPosition = (note: any, cursor: any) => {
  641. if (state.musicRenderType === EnumMusicRenderType.firstTone || state.musicRenderType === EnumMusicRenderType.fixedTone) {
  642. nextTick(() => {
  643. let bbox = note.bbox;
  644. if (!bbox) {
  645. const musicContainer = document.getElementById("musicAndSelection")?.getBoundingClientRect() || {
  646. x: 0,
  647. y: 0,
  648. };
  649. const parentLeft = musicContainer.x || 0;
  650. const noteEle = document.querySelector(`#vf-${note.svgElement?.attrs?.id}`);
  651. if (noteEle) {
  652. const noteHead = noteEle.querySelector(".vf-numbered-note-head");
  653. const noteHeadBbox = noteHead?.getBoundingClientRect?.();
  654. if (noteHeadBbox) {
  655. note.bbox = {
  656. left: noteHeadBbox.x - parentLeft - noteHeadBbox.width / 4,
  657. width: noteHeadBbox.width * 1.5,
  658. };
  659. bbox = note.bbox;
  660. }
  661. }
  662. }
  663. if (!bbox) return;
  664. const baseW = state.platform === IPlatform.PC ? 29 : 18;
  665. const width = (bbox.width - baseW) / 3;
  666. // console.log(555555,bbox.left,width)
  667. cursor.cursorElement.style.left = bbox.left + "px";
  668. cursor.cursorElement.style.transform = `translateX(${width}px)`;
  669. });
  670. }
  671. };
  672. /** 跳转到下一个音符 */
  673. export const gotoNext = (note: any) => {
  674. // console.log(33333333333,state.activeNoteIndex,note.i)
  675. const num = note.i;
  676. if (state.activeNoteIndex === note.i) {
  677. try {
  678. setCursorPosition(note, state.osmd.cursor);
  679. } catch (error) {
  680. console.log(error);
  681. }
  682. return;
  683. }
  684. const osmd = state.osmd;
  685. let prev = state.activeNoteIndex;
  686. state.activeNoteIndex = num;
  687. state.activeMeasureIndex = note.MeasureNumberXML;
  688. if (prev && num - prev === 1) {
  689. osmd.cursor.next();
  690. } else if (prev && num - prev > 0) {
  691. while (num - prev > 0) {
  692. prev++;
  693. osmd.cursor.next();
  694. }
  695. } else {
  696. gotoCustomNote(num);
  697. }
  698. try {
  699. setCursorPosition(note, state.osmd.cursor);
  700. } catch (error) {
  701. console.log(error);
  702. }
  703. scrollViewNote();
  704. };
  705. /** 获取指定音符 */
  706. export const getNote = (currentTime: number) => {
  707. const times = state.times;
  708. const len = state.times.length;
  709. /** 播放超过了最后一个音符的时间,直接结束, 2秒误差 */
  710. if (currentTime > times[len - 1].endtime + 2) {
  711. onEnded();
  712. return;
  713. }
  714. let _item = null as any;
  715. for (let i = state.activeNoteIndex; i < len; i++) {
  716. const item = times[i];
  717. const prevItem = times[i - 1];
  718. if (currentTime >= item.time) {
  719. if (!prevItem || item.time != prevItem.time) {
  720. _item = item;
  721. }
  722. } else {
  723. break;
  724. }
  725. }
  726. // console.log("activeNoteIndex", currentTime, state.activeNoteIndex, _item.i);
  727. return _item;
  728. };
  729. /** 重播 */
  730. export const handleResetPlay = () => {
  731. resetPlaybackToStart();
  732. // 如果是暂停, 直接播放
  733. togglePlay("play");
  734. };
  735. /** 设置速度 */
  736. export const handleSetSpeed = (speed: number) => {
  737. setStorageSpeed(state.examSongId, speed);
  738. state.speed = speed;
  739. };
  740. /** 清除选段状态 */
  741. export const clearSelection = () => {
  742. state.sectionStatus = false;
  743. state.section = [];
  744. closeToast();
  745. };
  746. /** 开启选段 */
  747. export const handleChangeSection = () => {
  748. // 如果开启了选段,再次点击取消选段
  749. if (state.sectionStatus) {
  750. togglePlay("paused");
  751. skipNotePlay(0, true);
  752. clearSelection();
  753. return;
  754. }
  755. state.sectionStatus = true;
  756. // 开启
  757. if (state.sectionStatus) {
  758. togglePlay("paused");
  759. }
  760. showToast({
  761. message: "请选择开始小节",
  762. duration: 0,
  763. position: "top",
  764. className: "selectionToast",
  765. });
  766. };
  767. /** 效验并格式化选段小节 */
  768. const formateSelectMearure = (_list: any[]): any[] => {
  769. if (!_list.length) return [];
  770. const list = _list.sort((a, b) => a.time - b.time);
  771. const startXml = list[0]?.measureOpenIndex;
  772. const endXml = list.last()?.measureOpenIndex;
  773. const selectStartMeasure = state.times.filter((n: any) => startXml === n.measureOpenIndex) || [];
  774. const selectEndMeasure = state.times.filter((n: any) => endXml === n.measureOpenIndex) || [];
  775. // 没有找到选段小节
  776. if (!selectStartMeasure.length || !selectEndMeasure.length) {
  777. clearSelection();
  778. return [];
  779. }
  780. list[0] = selectStartMeasure[0];
  781. list[1] = selectEndMeasure.last();
  782. let startItemINdex = list[0].i;
  783. // 开启预备拍
  784. if (state.isOpenPrepare) {
  785. const startXmlIndex = list[0].MeasureNumberXML;
  786. state.sectionFirst = state.times.find((n: any) => startXmlIndex - n.MeasureNumberXML === 1);
  787. startItemINdex = state.sectionFirst ? state.sectionFirst.i : startItemINdex;
  788. }
  789. skipNotePlay(startItemINdex, startItemINdex === 0);
  790. return list;
  791. };
  792. /** 选择选段 */
  793. export const handleSelection = (item: any) => {
  794. if (!state.sectionStatus || state.section.length > 1) return;
  795. if (state.section.length !== 2 && item) {
  796. state.section.push(item);
  797. if (state.section.length === 2) {
  798. setSection(state.section[0].MeasureNumberXML,state.section[1].MeasureNumberXML)
  799. //state.section = formateSelectMearure(state.section);
  800. closeToast();
  801. }
  802. }
  803. if (state.section.length === 1) {
  804. showToast({
  805. message: "请选择结束小节",
  806. duration: 0,
  807. position: "top",
  808. className: "selectionToast",
  809. });
  810. }
  811. };
  812. /** 阶段练习、阶段评测设置选段小节 */
  813. export const setSection = (start: number, end: number, userSpeed?: number) => {
  814. const startNotes = state.times.filter(
  815. (n: any) => n.noteElement.sourceMeasure.MeasureNumberXML == start
  816. )
  817. const endNotes = state.times.filter(
  818. (n: any) => n.noteElement.sourceMeasure.MeasureNumberXML == end
  819. )
  820. const lastEndId = endNotes[endNotes.length - 1].noteId
  821. let lastEndNotes = endNotes.filter((n: any) => n.noteId === lastEndId)
  822. // 是否符合重播规则
  823. const canRepeatInfo = verifyCanRepeat(start, end)
  824. console.log(22222,canRepeatInfo)
  825. const isCanRepeat = canRepeatInfo.canRepeat
  826. // 如果符合重播规则,但是lastEndNotes长度为1,则需要向前找,直到找到lastEndNotes长度为2
  827. let currentEndNum: number = end
  828. const lastEndIndex = state.repeatInfo[canRepeatInfo.repeatIdx]?.end || 0
  829. while (isCanRepeat && lastEndNotes.length === 1 && lastEndNotes[0].MeasureNumberXML <= lastEndIndex) {
  830. currentEndNum = currentEndNum - 1
  831. const newEndNotes = state.times.filter(
  832. (n: any) => n.noteElement.sourceMeasure.MeasureNumberXML == currentEndNum
  833. )
  834. const newLastEndId = newEndNotes[newEndNotes.length - 1].noteId
  835. lastEndNotes = newEndNotes.filter((n: any) => n.noteId === newLastEndId)
  836. }
  837. const endIdx: any = (isCanRepeat && canRepeatInfo.repeatIdx == state.repeatInfo.length - 1) ? lastEndNotes.length - 1 : 0
  838. const startNote = startNotes[0]
  839. // const endNote = endNotes[endNotes.length - 1]
  840. const endNote = lastEndNotes[endIdx]
  841. if (startNote && endNote) {
  842. state.isSelectMeasureMode = true;
  843. // 设置小节
  844. hanldeDirectSelection([startNote, endNote]);
  845. //设置速度
  846. if (userSpeed) {
  847. handleSetSpeed(userSpeed);
  848. }
  849. }
  850. }
  851. /** 直接设置选段 */
  852. export const hanldeDirectSelection = (list: any[]) => {
  853. if (!Array.isArray(list) || list.length !== 2) return;
  854. state.sectionStatus = true;
  855. setTimeout(() => {
  856. state.section = formateSelectMearure(list);
  857. //console.log(333333333,state.section)
  858. }, 500);
  859. };
  860. let offsetTop = 0;
  861. /**
  862. * 窗口内滚动到音符的区域
  863. * @param isScroll 可选: 强制滚动到顶部, 默认: false
  864. * @returns void
  865. */
  866. export const scrollViewNote = () => {
  867. const cursorElement = document.getElementById("cursorImg-0")!;
  868. const musicAndSelection = document.getElementById(state.scrollContainer)!;
  869. if (!cursorElement || !musicAndSelection || offsetTop === cursorElement.offsetTop) return;
  870. offsetTop = cursorElement.offsetTop;
  871. if (offsetTop > 50) {
  872. musicAndSelection.scrollTo({
  873. top: (offsetTop - 50) * state.musicZoom,
  874. behavior: "smooth",
  875. });
  876. } else {
  877. musicAndSelection.scrollTo({
  878. top: 0,
  879. behavior: "smooth",
  880. });
  881. }
  882. };
  883. /** 检测是否是节奏练习 */
  884. export const isRhythmicExercises = () => {
  885. return state.examSongName.indexOf("节奏练习") > -1;
  886. };
  887. /** 重置状态 */
  888. export const handleRessetState = () => {
  889. // 切换模式,清除选段
  890. skipNotePlay(0, true);
  891. clearSelection();
  892. if (state.modeType === "evaluating") {
  893. handleStartEvaluat();
  894. } else if (state.modeType === "practise") {
  895. togglePlay("paused");
  896. } else if (state.modeType === "follow") {
  897. toggleFollow(false);
  898. }
  899. };
  900. export default state;
  901. /** 初始化评测音频 */
  902. export const evaluatCreateMusicPlayer = () => {
  903. return api_createMusicPlayer({
  904. musicSrc: state.accompany || state.music, // 曲谱音频url
  905. tuneSrc: "https://oss.dayaedu.com/cloud-coach/1686725501654check_music1_(1).mp3", //效音音频url
  906. });
  907. };
  908. /** 获取内容平台的接口详情并初始化state信息 */
  909. export const getMusicDetail = async (id: string) => {
  910. const res = await getMusicSheetDetail(id);
  911. if (res?.code === 200) {
  912. getMusicInfo(res)
  913. }
  914. };
  915. const getMusicInfo = (res: any) => {
  916. const index = query["part-index"] ? parseInt(query["part-index"] as string) : 0;
  917. // 原音声轨
  918. const musicData = res.data.musicSheetSoundList?.[index] || {};
  919. // 伴奏声轨
  920. const accompanyData = res.data.musicSheetAccompanimentList?.[0] || {}
  921. const musicInfo = {
  922. ...res.data,
  923. music: musicData.audioFileUrl || '',
  924. accompany: accompanyData.audioFileUrl || '',
  925. musicSheetId: musicData.musicSheetId || res.data.bizId,
  926. track: musicData.track || '',
  927. };
  928. console.log("🚀 ~ musicInfo:", musicInfo);
  929. setState(musicInfo, index);
  930. };
  931. const setState = (data: any, index: number) => {
  932. state.appName = "COLEXIU";
  933. state.detailId = data.bizId;
  934. state.xmlUrl = data.xmlFileUrl;
  935. state.partIndex = index;
  936. state.trackId = data.track;
  937. state.subjectId = data.subjectIds ? data.subjectIds.split(',')?.[0] : 0;
  938. // 声部code
  939. const subjectCode = data.subjectCodes ? data.subjectCodes.split(',')?.[0] : '';
  940. // 乐器code
  941. let musicalCode = data.musicalInstrumentIdCodes ? data.musicalInstrumentIdCodes.split(',')?.[0] : '';
  942. const pitchSubject = musicalInstrumentCodeInfo.find((n) => n.code.toLocaleLowerCase() === subjectCode.toLocaleLowerCase())
  943. const pitchMusical = musicalInstrumentCodeInfo.find((n) => n.code.toLocaleLowerCase() === musicalCode.toLocaleLowerCase())
  944. state.subjectCodeId = pitchSubject ? pitchSubject.id : 0
  945. state.musicalCodeId = pitchMusical ? pitchMusical.id : 0
  946. state.categoriesId = data.musicCategoryId;
  947. state.categoriesName = data.musicTagNames;
  948. // state.enableEvaluation = data.isEvaluated ? true : false;
  949. state.examSongId = data.bizId + "";
  950. state.cbsExamSongId = data.id + "";
  951. state.examSongName = data.name;
  952. state.coverImg = data.musicCover ?? "";
  953. state.isCombineRender = data.musicSheetType === "SINGLE" && data.musicSheetSoundList?.length > 1
  954. setCustom(state.isCombineRender ? data.musicSheetSoundList?.length : 0);
  955. // 解析扩展字段
  956. if (data.extConfigJson) {
  957. try {
  958. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  959. } catch (error) {
  960. console.error("解析扩展字段错误:", error);
  961. }
  962. }
  963. state.gradualTimes = state.extConfigJson.gradualTimes;
  964. state.repeatedBeats = state.extConfigJson.repeatedBeats || 0;
  965. // 曲子包含节拍器,就不开启节拍器
  966. state.needTick = data.isUseSystemBeat && data.isPlayBeat ? true : false;
  967. // state.isOpenMetronome = data.isUseSystemBeat ? false : true;
  968. state.isOpenMetronome = data.isPlayBeat && !data.isUseSystemBeat ? true : false
  969. state.isShowFingering = data.isShowFingering ? true : false;
  970. // 设置曲谱的播放模式, APP播放(midi音频是app播放) | h5播放
  971. state.isAppPlay = data.playMode === 'MIDI';
  972. state.music = data.music;
  973. state.accompany = data.accompany;
  974. state.midiUrl = data.midiFileUrl;
  975. state.parentCategoriesId = data.musicTag;
  976. state.musicSheetCategoriesId = data.musicCategoryId;
  977. state.bizMusicCategoryId = data.bizMusicCategoryId
  978. state.playMode = data.playMode === "MP3" ? "MP3" : "MIDI";
  979. state.originSpeed = state.speed = data.playSpeed;
  980. // state.playIngSpeed = data.playSpeed;
  981. const track = data.code || data.track;
  982. state.track = track ? track.replace(/ /g, "").toLocaleLowerCase() : "";
  983. // 能否评测,根据当前声轨有无伴奏判断
  984. state.enableEvaluation = state.accompany ? true : false
  985. state.isConcert = data.musicSheetType === "CONCERT" ? true : false;
  986. // multiTracksSelection 返回为空,默认代表全部分轨
  987. state.canSelectTracks = data.multiTracksSelection === "null" || data.multiTracksSelection === "" || data.multiTracksSelection === null ? [] : data.multiTracksSelection?.split(',');
  988. // 开启预备小节
  989. state.isOpenPrepare = true;
  990. state.extStyleConfigJson = data.extStyleConfigJson || {}
  991. // console.log("🚀 ~ state.subjectId:", state.subjectId, state.track as any , state.subjectId)
  992. // 是否打击乐
  993. /**
  994. * 是否打击乐:AMPLITUDE & 节奏练习:DECIBELS
  995. * evaluationStandard:("评测标准 节奏 AMPLITUDE 音准 FREQUENCY 分贝 DECIBELS")
  996. * 打击乐&节奏练习,没有跟练模式
  997. */
  998. // state.isPercussion = isRhythmicExercises();
  999. state.isPercussion = data.evaluationStandard === "AMPLITUDE" || data.evaluationStandard === "DECIBELS";
  1000. state.evaluationStandard = data.evaluationStandard?.toLocaleLowerCase() || ''
  1001. // 设置是否特殊曲谱, 是特殊曲谱取反(不理解之前的思考逻辑), 使用后台设置的速度
  1002. state.isSpecialBookCategory = !classids.includes(Number(data.musicCategoryId));
  1003. // 设置指法
  1004. // const code = state.isConcert ? mappingVoicePart(state.trackId, "ENSEMBLE") : mappingVoicePart(state.subjectId, "INSTRUMENT");
  1005. /**
  1006. * 各平台的乐器声部id不统一,为了兼容处理老的数据,加上乐器code码,此码唯一
  1007. * 获取指法code
  1008. */
  1009. const code = state.isConcert ? matchVoicePart(state.trackId, "CONCERT") : matchVoicePart(state.musicalCodeId, "SINGLE");
  1010. state.fingeringInfo = subjectFingering(code);
  1011. console.log("🚀 ~ state.fingeringInfo:", code, state.fingeringInfo, state.trackId, state.track);
  1012. state.musicalCodeId = state.fingeringInfo?.id || 0
  1013. state.musicalCode = musicalInstrumentCodeInfo.find(item => item.id === state.musicalCodeId)?.code || state.trackId
  1014. ;(window as any).DYSubjectId = state.musicalCodeId
  1015. // 开启自定义每行显示的小节数
  1016. ;(window as any).customSectionAmount = true
  1017. // 如果切换的声轨没有指法,择指法开关置灰并且不可点击
  1018. if (!state.fingeringInfo.name && state.setting.displayFingering) {
  1019. state.setting.displayFingering = false
  1020. }
  1021. // 检测是否原音和伴奏都有
  1022. if (!state.music || !state.accompany) {
  1023. state.playSource = state.music ? "music" : "background";
  1024. }
  1025. // 如果是PC端,放大曲谱
  1026. state.platform = query.platform?.toLocaleUpperCase() || "";
  1027. if (state.platform === IPlatform.PC) {
  1028. state.zoom = query.zoom || 1.5;
  1029. }
  1030. /**
  1031. * 默认渲染什么谱面类型 & 能否转谱逻辑
  1032. * 渲染类型:首先取url参数musicRenderType,没有该参数则取musicalInstruments字段匹配的当前分轨的defaultScore,没有匹配到则取默认值('firstTone')
  1033. * 能否转谱:先取isConvertibleScore字段,如果isConvertibleScore为true,则取musicalInstruments字段匹配的当前分轨的transferFlag,都为true则可以转谱
  1034. *
  1035. */
  1036. let pitchTrack = null
  1037. if (state.isConcert) {
  1038. musicalCode = musicalInstrumentCodeInfo.find((item: any) => item.id === state.musicalCodeId)?.code
  1039. pitchTrack = data.musicalInstruments?.find((item: any) => item.code === musicalCode)
  1040. } else {
  1041. pitchTrack = data.musicalInstruments?.find((item: any) => item.code === musicalCode)
  1042. }
  1043. let musicalRenderType = ''
  1044. if (pitchTrack?.defaultScore) {
  1045. musicalRenderType = pitchTrack?.defaultScore === 'STAVE' ? 'staff' : pitchTrack?.defaultScore === 'JIAN' ? 'fixedTone' : pitchTrack?.defaultScore === 'FIRST' ? 'firstTone' : ''
  1046. }
  1047. state.musicRenderType = query.musicRenderType || musicalRenderType || EnumMusicRenderType.firstTone;
  1048. state.enableNotation = pitchTrack ? data.isConvertibleScore && pitchTrack.transferFlag : data.isConvertibleScore
  1049. console.log("state对象", state);
  1050. // 评测基准频率
  1051. state.baseFrequency = data.evaluationFrequency ? data.evaluationFrequency.split(",")[0] : 440
  1052. state.baseFrequency = Number(state.baseFrequency)
  1053. // 用户上次的频率和基准频率误差超过10,则重置
  1054. if (Math.abs(state.setting.frequency - state.baseFrequency) > 10) {
  1055. state.setting.frequency = state.baseFrequency >= 0 ? state.baseFrequency : 440
  1056. } else {
  1057. state.setting.frequency = state.setting.frequency || state.baseFrequency
  1058. }
  1059. };
  1060. // 多分轨合并显示标示
  1061. const setCustom = (trackNum?: number) => {
  1062. if (trackNum || state.extConfigJson.multitrack) {
  1063. setGlobalData("multitrack", trackNum || state.extConfigJson.multitrack);
  1064. }
  1065. };
  1066. /** 跟练模式播放节拍器(叮咚) */
  1067. export const followBeatPaly = () => {
  1068. let metroTimer: any = null;
  1069. if (!followData.start) {
  1070. clearTimeout(metroTimer)
  1071. metroTimer = null
  1072. return;
  1073. }
  1074. const time = state.measureTime*1000 / metronomeData.totalNumerator
  1075. requestAnimationFrame(() => {
  1076. const endTime = Date.now();
  1077. if (endTime - state.beatStartTime < time) {
  1078. followBeatPaly();
  1079. } else {
  1080. // metroTimer = setTimeout(() => {
  1081. // metronomeData.metro?.simulatePlayAudio()
  1082. // startTime = Date.now();
  1083. // followBeatPaly();
  1084. // }, time);
  1085. metronomeData.metro?.simulatePlayAudio()
  1086. state.beatStartTime = Date.now();
  1087. followBeatPaly();
  1088. }
  1089. });
  1090. };