state.ts 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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, audioData } 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, api_cloudSuspend, api_cloudSetCurrentTime, api_cloudDestroy } 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. name: '高音陶笛',
  241. code: 'Whistling',
  242. id: 39
  243. },
  244. ]
  245. const state = reactive({
  246. /** 来源 : PC , app */
  247. platform: "" as IPlatform,
  248. appName: "" as "GYM" | "COLEXIU",
  249. musicRenderType: EnumMusicRenderType.staff as EnumMusicRenderType,
  250. /**曲谱是否渲染完成 */
  251. musicRendered: false,
  252. /** 当前曲谱数据ID, 和曲谱ID不一致 */
  253. detailId: "",
  254. /** 曲谱资源URL */
  255. xmlUrl: "",
  256. /** 声部ID */
  257. subjectId: 0 as number,
  258. trackId: 0 as string | number,
  259. /** 分类ID */
  260. categoriesId: 0,
  261. /** 分类名称 */
  262. categoriesName: "",
  263. /** 是否支持评测 */
  264. enableEvaluation: true,
  265. /** 是否支持转谱 */
  266. enableNotation: false,
  267. /** 曲谱ID */
  268. examSongId: "",
  269. /** 内容平台的曲谱ID,可能会和业务端的id不一样 */
  270. cbsExamSongId: "",
  271. /** 曲谱名称 */
  272. examSongName: "",
  273. /** 曲谱封面 */
  274. coverImg: "",
  275. /** 扩展字段 */
  276. extConfigJson: {} as any,
  277. /** 扩展样式字段 */
  278. extStyleConfigJson: {} as any,
  279. /** 是否开启节拍器(mp3节拍器) */
  280. isOpenMetronome: false,
  281. /** 是否显示指法 */
  282. isShowFingering: false,
  283. /** 原音 */
  284. music: "",
  285. /** 伴奏 */
  286. accompany: "",
  287. /** midiURL */
  288. midiUrl: "",
  289. /** 父分ID */
  290. parentCategoriesId: 0,
  291. /** 分类ID */
  292. musicSheetCategoriesId: 0,
  293. /** 各产品端的分类ID,(管乐迷、管乐团、酷乐秀、课堂乐器) */
  294. bizMusicCategoryId: 0,
  295. /** 资源类型: mp3 | midi */
  296. playMode: "MP3" as "MP3" | "MIDI",
  297. /** 设置的速度 */
  298. speed: 0,
  299. /** 曲谱音频正常的速度 */
  300. originSpeed: 0,
  301. /** 播放过程中显示的速度 */
  302. playIngSpeed: 0,
  303. /** 分轨名称 */
  304. track: "",
  305. /** 当前显示声部索引 */
  306. partIndex: 0,
  307. /** 是否需要节拍器 */
  308. needTick: false,
  309. /** 曲谱实例 */
  310. osmd: null as unknown as OpenSheetMusicDisplay,
  311. /**是否是特殊乐谱类型, 主要针对管乐迷 */
  312. isSpecialBookCategory: false,
  313. /** 播放状态 */
  314. playState: "paused" as IAudioState,
  315. /** 播放结束状态 */
  316. playEnd: false,
  317. /** 播放类型 演奏 演唱 */
  318. playType: "perform" as "perform" | "sing",
  319. /** 播放那个: 原音,伴奏 */
  320. playSource: "music" as IPlayState,
  321. /** 播放进度 */
  322. playProgress: 0,
  323. /** 激活的note index */
  324. activeNoteIndex: 0,
  325. /** 激活的小节 */
  326. activeMeasureIndex: 0,
  327. /** 选段状态 */
  328. sectionStatus: false,
  329. /** 选段数据 */
  330. section: [] as any[],
  331. /** 选段背景 */
  332. sectionBoundingBoxs: [] as any[],
  333. /** 开启选段预备 */
  334. isOpenPrepare: false,
  335. /** 选段预备 */
  336. sectionFirst: null as any,
  337. /** 音符数据 */
  338. times: [] as any[],
  339. /** 播放模式 */
  340. modeType: "practise" as "practise" | "follow" | "evaluating",
  341. /** 设置 */
  342. setting: {
  343. /** 效音提醒 */
  344. soundEffect: true,
  345. /** 护眼模式 */
  346. eyeProtection: false,
  347. /** 摄像头 */
  348. camera: false,
  349. /** 摄像头透明度 */
  350. cameraOpacity: 70,
  351. /** 循环播放 */
  352. repeatAutoPlay: true,
  353. /** 显示指法 */
  354. displayFingering: true,
  355. /** 显示光标 */
  356. displayCursor: true,
  357. /** 频率 */
  358. frequency: 0,
  359. /** 评测难度 */
  360. evaluationDifficulty: "BEGINNER" as IDifficulty,
  361. /** 保存到相册 */
  362. saveToAlbum: true,
  363. /** 开启伴奏 */
  364. enableAccompaniment: true,
  365. /** 反应时间 */
  366. reactionTimeMs: 0,
  367. /** 节拍器音量 */
  368. beatVolume: 50,
  369. },
  370. /** 后台设置的基准评测频率 */
  371. baseFrequency: 440,
  372. /** mp3节拍器的时间,统计拍数、速度计算得出,evxml通过读取xml元素获取 */
  373. fixtime: 0,
  374. /** evxml等待播放的时间 */
  375. evXmlBeginTime: 0,
  376. /** 第二遍循环evxml等待播放的时间 */
  377. secondEvXmlBeginTime: 0,
  378. /** evxml等待播放的时间集合,多遍反复播放,会有多个timegap(前奏)时间 */
  379. evXmlBeginArr: [] as any,
  380. /** 指法信息 */
  381. fingeringInfo: {} as IFingering,
  382. /** 滚动容器的ID */
  383. scrollContainer: "musicAndSelection",
  384. /** 是否是打击乐 */
  385. isPercussion: false,
  386. /** 评测标准 */
  387. evaluationStandard: '',
  388. /** 是否重复节拍器的时间 */
  389. repeatedBeats: 0,
  390. /**当前曲谱中所有声部名字 */
  391. partListNames: [] as any,
  392. /** 渐变速度信息 */
  393. gradual: [] as GradualNote[],
  394. /** 渐变速度版本 */
  395. gradualVersion: GradualVersion.BASE as GradualVersion,
  396. /** 渐变时间信息 */
  397. gradualTimes: null as GradualTimes,
  398. /** 单声部多声轨 */
  399. multitrack: 0,
  400. /** 缩放 */
  401. zoom: 0.8,
  402. /** 渲染曲谱比例 */
  403. musicZoom: 1,
  404. /** 练习,评测是否是选段模式 */
  405. isSelectMeasureMode: false,
  406. /** 是否是评分显示 */
  407. isReport: false,
  408. /** 是否隐藏评测报告弹窗,保存演奏按钮,默认不隐藏 */
  409. isHideEvaluatReportSaveBtn: false,
  410. /** 是否是合奏 */
  411. isConcert: false,
  412. /** 用户选择的结束小节数 */
  413. userChooseEndIndex: 0,
  414. /** 重播小节集合信息 */
  415. repeatInfo: [] as any,
  416. /** 多分轨的曲子,可支持筛选的分轨 */
  417. canSelectTracks: [] as any,
  418. /** 声部codeId */
  419. subjectCodeId: 0 as number,
  420. /** 乐器codeId,用于匹配乐器指法、声部转调、特殊声部处理等 */
  421. musicalCodeId: 0 as number,
  422. /** 乐器code,用于评测传参 */
  423. musicalCode: '' as any,
  424. /** 合奏曲目是否合并展示 */
  425. isCombineRender: false,
  426. /** 小节的持续时长,以后台设置的播放速度计算 */
  427. measureTime: 0,
  428. /** 跟练模式,节拍器播放的时间 */
  429. beatStartTime: 0,
  430. /** 是否为详情预览模式 */
  431. isPreView: false,
  432. /** 是否为评测报告模式 */
  433. isEvaluatReport: false,
  434. /** midi播放器是否初始化中 */
  435. midiPlayIniting: false,
  436. /** 曲目信息 */
  437. songs: {} as ISonges,
  438. isAppPlay: false, // 是否midi音频,midi是app播放
  439. /** 音频播放器实例 */
  440. audiosInstance: null as any,
  441. /** midi音频的时长 */
  442. durationNum: 0,
  443. midiSectionStart: 0,
  444. /** 音频文件是否加载完成 */
  445. audioDone: false,
  446. /** 谱面svgdom节点 */
  447. osmdSvgDom: null as any,
  448. /** 滚动容器dom */
  449. osdmScrollDom: null as any,
  450. /** 光标dom */
  451. cursorDom: null as any,
  452. fistNoteLeft: 0,
  453. /** 是否为单行谱渲染模式 */
  454. isSingleLine: false,
  455. /** 首尾音符的间距 */
  456. noteDistance: 0,
  457. /** 一行谱运动模式,平滑移动、匀速移动 */
  458. moveType: "smooth" as "smooth" | "uniform",
  459. /** 是否是evxml */
  460. isEvxml: false,
  461. noTimes: [] as any,
  462. /** 老师端:功能按钮布局方向 */
  463. playBtnDirection: "left" as "left" | "right",
  464. /** 云练习按钮方向,如果有指法并且是竖向的指法,为了防止播放按钮把指法挡住,此时云练习播放按钮方向应该取反 */
  465. musicScoreBtnDirection: "right" as "left" | "right",
  466. /** 是否在老师端上课页面 */
  467. isAttendClass: false,
  468. /** 引导页信息 */
  469. guideInfo: null as any,
  470. noteCoords: [] as any,
  471. specialPosInit: false,
  472. /** 资源类型 */
  473. paymentType: null,
  474. /** 播放模式,默认练习模式 */
  475. defaultModeType: 1,
  476. /** 音符最多歌词次数 */
  477. maxLyricNum: 0,
  478. });
  479. const browserInfo = browser();
  480. let offset_duration = 0;
  481. /** 自定义数据 */
  482. export const customData = reactive({
  483. /** 自定义音符时值 */
  484. customNoteRealValue: [] as any,
  485. /** 自定义音符按读取到的时值 */
  486. customNoteCurrentTime: false,
  487. });
  488. /** 在渲染前后计算光标应该走到的音符 */
  489. const setStep = () => {
  490. // console.log('播放状态',state.playState)
  491. if (state.playState !== "play") {
  492. console.log("暂停播放");
  493. return;
  494. }
  495. let startTime = Date.now();
  496. requestAnimationFrame(() => {
  497. const endTime = Date.now();
  498. // 渲染时间大于16.6,就会让页面卡顿, 如果渲染时间大与16.6就下一个渲染帧去计算
  499. if (endTime - startTime < 16.7) {
  500. handlePlaying();
  501. setStep();
  502. } else {
  503. setTimeout(() => {
  504. handlePlaying();
  505. setStep();
  506. }, 16.7);
  507. }
  508. });
  509. };
  510. /** 开始播放 */
  511. export const onPlay = () => {
  512. console.log("开始播放",'音频总时长:',getAudioDuration());
  513. state.playEnd = false;
  514. // offset_duration = browserInfo.xiaomi ? 0.2 : 0.08;
  515. offset_duration = 0.2;
  516. setStep();
  517. };
  518. /** 播放模式结束自动重播 */
  519. const autoResetPlay = () => {
  520. if (state.modeType !== "practise") return;
  521. skipNotePlay(0, true);
  522. // 没有开启自动重播, 不是练习模式
  523. if (!state.setting.repeatAutoPlay) return;
  524. scrollViewNote();
  525. setTimeout(() => {
  526. togglePlay("play");
  527. }, 1000);
  528. };
  529. /** 播放完成事件 */
  530. export const onEnded = () => {
  531. console.log("音频播放结束");
  532. // if (state.isAppPlay) {
  533. // // 销毁播放器
  534. // api_cloudDestroy();
  535. // }
  536. // 修改状态为结束
  537. state.playEnd = true;
  538. state.playState = "paused";
  539. // 结束播放
  540. audioListStart(state.playState);
  541. // 调用结束评测
  542. handleEndEvaluat(true);
  543. // 调用自动重复播放
  544. autoResetPlay();
  545. };
  546. /**
  547. * 播放一直触发的事件
  548. */
  549. const handlePlaying = () => {
  550. const currentTime = getAudioCurrentTime();
  551. const duration = getAudioDuration();
  552. state.playProgress = (currentTime / duration) * 100;
  553. let item = getNote(currentTime);
  554. // console.log(11111,currentTime,duration,state.playSource, item)
  555. // console.log(item?.i,item?.noteId,item?.measureSpeed,'播放')
  556. // 练习模式下,实时刷新小节速度
  557. if (item && state.modeType === "practise" && state.playState === "play" && item.measureSpeed && item.measureSpeed !== state.playIngSpeed) {
  558. const ratio = state.speed / state.originSpeed
  559. state.playIngSpeed = Math.ceil(ratio * item.measureSpeed) || state.speed
  560. } else if (state.modeType === "practise" && state.playState === "play" && item && !item.measureSpeed) {
  561. state.playIngSpeed = state.speed
  562. }
  563. state.playIngSpeed = state.playIngSpeed || state.speed;
  564. if (item) {
  565. // 选段状态下
  566. if (state.sectionStatus && state.section.length === 2) {
  567. // 如果开启了预备拍
  568. const selectStartItem = state.sectionFirst ? state.sectionFirst : state.section[0];
  569. const selectEndItem = state.section[1];
  570. /**
  571. * #9374,反复小节的曲目播放错误, bug修复
  572. * 曲目:噢!苏珊娜-排箫-人音
  573. * 现象:重播小节为2-9,选段为4-12,当播完第9小节后会回到第2小节重播2-8,再播放10-12
  574. * 4-12,不符合重播规则,所以播完第9小节后,音频需要跳转到第10小节播放
  575. */
  576. if (state.repeatInfo.length) {
  577. const canRepeatInfo = verifyCanRepeat(state.section[0].MeasureNumberXML, state.section[1].MeasureNumberXML)
  578. const repeatIdx = canRepeatInfo.repeatIdx == -1 ? 0 : canRepeatInfo.repeatIdx
  579. if (state.modeType === "practise" && !canRepeatInfo.canRepeat && state.section[1].MeasureNumberXML > state.repeatInfo[repeatIdx].end) {
  580. const preItem = state.times[item.i - 1]
  581. if (preItem && preItem.MeasureNumberXML > item.MeasureNumberXML) {
  582. const skipItem = state.times.find((item: any) => item.MeasureNumberXML === preItem.MeasureNumberXML + 1)
  583. if (skipItem) {
  584. // 跳转到指定的音频位置
  585. setAudioCurrentTime(skipItem.time, skipItem.i);
  586. gotoNext(skipItem);
  587. return
  588. }
  589. }
  590. }
  591. }
  592. // if (Math.abs(selectEndItem.endtime - currentTime) < offset_duration) {
  593. // if (currentTime - selectEndItem.endtime > offset_duration) {
  594. //console.log(currentTime,selectEndItem.endtime)
  595. if (currentTime - selectEndItem.endtime >= 0) {
  596. console.log("选段播放结束", state.setting.repeatAutoPlay);
  597. // 如果为选段评测模式
  598. if (state.modeType === "evaluating" && state.isSelectMeasureMode) {
  599. onEnded();
  600. return;
  601. }
  602. // #8698 bug修复
  603. if (state.modeType === "practise" && state.sectionStatus) {
  604. onEnded();
  605. resetPlaybackToStart();
  606. return;
  607. }
  608. item = selectStartItem;
  609. setAudioCurrentTime(selectStartItem.time, selectStartItem.i);
  610. }
  611. }
  612. gotoNext(item);
  613. }
  614. // 评测不播放叮咚节拍器
  615. // if (state.modeType !== "evaluating") {
  616. // metronomeData.metro?.sound(currentTime);
  617. // }
  618. metronomeData.metro?.sound(currentTime);
  619. // 一行谱,需要滚动小节
  620. if (state.isSingleLine) {
  621. if (state.moveType === 'smooth') {
  622. smoothMoveSvgDom();
  623. } else {
  624. uniformMoveSvgDom();
  625. }
  626. }
  627. };
  628. /** 跳转到指定音符开始播放 */
  629. export const skipNotePlay = async (itemIndex: number, isStart = false) => {
  630. const item = state.times[itemIndex];
  631. let itemTime = item.time;
  632. if (isStart) {
  633. itemTime = 0;
  634. }
  635. if (item) {
  636. setAudioCurrentTime(itemTime, itemIndex);
  637. // 一行谱,点击音符,或者播放完成,需要跳转音符位置
  638. gotoNext(item, true);
  639. metronomeData.metro?.sound(itemTime);
  640. if (state.isAppPlay) {
  641. await api_cloudSetCurrentTime({
  642. currentTime: itemTime * 1000,
  643. songID: state.examSongId,
  644. })
  645. audioData.progress = itemTime
  646. state.midiSectionStart = itemTime
  647. }
  648. }
  649. };
  650. /**
  651. * 切换曲谱播放状态
  652. * @param playState 可选: 默认 undefined, 需要切换的状态 play:播放, paused: 暂停
  653. */
  654. export const togglePlay = async (playState?: "play" | "paused", sourceType?: string) => {
  655. // 如果mp3资源还在加载中,给出提示
  656. if (!state.isAppPlay && !state.audioDone) {
  657. if (sourceType !== 'courseware') showToast('音频资源加载中,请稍后')
  658. return
  659. }
  660. // midi播放
  661. if (state.isAppPlay) {
  662. if (playState === "paused") {
  663. await api_cloudSuspend({
  664. songID: state.examSongId,
  665. })
  666. state.playState = 'paused'
  667. return
  668. }
  669. skipNotePlay(state.activeNoteIndex, false);
  670. await api_cloudChangeSpeed({
  671. speed: state.modeType === "evaluating" ? state.originSpeed : state.speed,
  672. originalSpeed: state.originSpeed,
  673. songID: state.examSongId,
  674. });
  675. const cloudGetMediaStatus = await api_cloudGetMediaStatus();
  676. const status = cloudGetMediaStatus?.content.status === "suspend" ? "play" : "paused"
  677. state.playState = status
  678. } else {
  679. state.playState = playState ? playState : state.playState === "paused" ? "play" : "paused";
  680. }
  681. if (state.playState === "play" && state.sectionStatus && state.section.length == 2 && state.playProgress === 0) {
  682. resetPlaybackToStart();
  683. }
  684. // 设置为开始播放时, 如果需要节拍,先播放节拍器
  685. if (state.playState === "play" && state.needTick) {
  686. const tickend = await handleStartTick();
  687. // console.log("🚀 ~ tickend:", tickend)
  688. // 节拍器返回false, 取消播放
  689. if (!tickend) {
  690. state.playState = "paused";
  691. return false;
  692. }
  693. }
  694. // 如果选段没有结束, 直接开始播放,清空选段状态
  695. if (state.playState == "play") {
  696. if (state.sectionStatus && state.section.length < 2) {
  697. clearSelection();
  698. }
  699. }
  700. audioListStart(state.playState);
  701. return true;
  702. };
  703. /** 结束播放 */
  704. export const handleStopPlay = () => {
  705. state.playState = "paused";
  706. audioListStart(state.playState);
  707. };
  708. /** 重置播放为开始 */
  709. export const resetPlaybackToStart = () => {
  710. // 如果为选段状态
  711. if (state.sectionStatus && state.section.length === 2) {
  712. state.section = formateSelectMearure(state.section);
  713. return;
  714. } else {
  715. // 非选段状态,重播需要重置当前选中的小节为第一个小节
  716. metronomeData.activeMetro = metronomeData.metroMeasure[0]?.[0] || {};
  717. }
  718. skipNotePlay(0, true);
  719. };
  720. /** 跳转到指定音符 */
  721. export const gotoCustomNote = (index: number) => {
  722. try {
  723. state.osmd.cursor.reset();
  724. } catch (error) { }
  725. for (let i = 0; i < index; i++) {
  726. state.osmd.cursor.next();
  727. }
  728. };
  729. // 找出离目标元素最近的音符
  730. const computedDistance = (x: number, y: number) => {
  731. let minDistance = -1, minidx = 0;
  732. let a, b, c;
  733. state.noteCoords.forEach((note: any, idx: any) => {
  734. //a,b为直角三角形的两个直角边
  735. a = Math.abs(note.x - x)
  736. b = Math.abs(note.y - y)
  737. //c为直角三角形的斜边
  738. c = Math.sqrt(a * a + b * b) as 0
  739. c = Number(c.toFixed(0)) as 0
  740. if (c !== 0 && (minDistance === - 1 || c < minDistance)) {
  741. //min为元素中离目标元素最近元素的距离
  742. minDistance = c
  743. minidx = idx
  744. }
  745. })
  746. return minidx
  747. };
  748. const customNotePosition = (note: any, cursor: any) => {
  749. const specialIds = ['1788850864767643649', '1788502467554750466', '1788501975122489346'];
  750. if (specialIds.includes(state.cbsExamSongId) && note.multipleRestMeasures === 0) {
  751. const pageLeft = document.getElementById('scrollContainer')?.getBoundingClientRect()?.x || 0;
  752. // 元素的位置
  753. const element = document.getElementById('cursorImg-0')?.getBoundingClientRect?.() || { x: 0, y: 0 };
  754. // 找出距离元素最近的音符
  755. if (element.x && element.y) {
  756. const noteIdx = computedDistance(element.x, element.y);
  757. const targetX = state.noteCoords[noteIdx]?.x - pageLeft;
  758. console.log('音符索引', noteIdx)
  759. cursor.cursorElement.style.left = targetX + "px";
  760. cursor.cursorElement.style.transform = `translateX(0px)`;
  761. }
  762. }
  763. }
  764. const setCursorPosition = (note: any, cursor: any, flag?: string) => {
  765. // console.log('音符',note?.i,state.osmd.Cursor.noteGraphicalId,note.svgElement?.attrs?.id)
  766. if (state.musicRenderType === EnumMusicRenderType.firstTone || state.musicRenderType === EnumMusicRenderType.fixedTone) {
  767. /**
  768. * bug:#9920、#9940
  769. * 简谱选段模式,预备小节为休止小节时,选段播放结束,指针会重置到第一小节位置的初始位置
  770. */
  771. if (state.sectionStatus && state.playState === 'paused' && state.sectionFirst && (note.multipleRestMeasures || note.MeasureNumberXML !== state.sectionFirst?.MeasureNumberXML)) {
  772. return
  773. }
  774. const specialIds = ['1788850864767643649', '1788502467554750466', '1788501975122489346'];
  775. if (specialIds.includes(state.cbsExamSongId) && note.multipleRestMeasures === 0) {
  776. // console.log('音符idx',note?.i,cursor.cursorElement.style.left)
  777. const cursorLeft = cursor?.cursorElement?.style?.left ? parseFloat(cursor.cursorElement.style.left) : 0;
  778. let patchX = 0;
  779. if (state.cbsExamSongId == '1788502467554750466') {
  780. if (state.musicRenderType === EnumMusicRenderType.firstTone) {
  781. patchX = (note.i == 0 || note.i == 60) ? 21 : (note.i == 1 || note.i == 7 || note.i == 23 || note.i == 38 || note.i == 44 || note.i == 52 || note.i == 58) ? -6 : (note.i >= 2 || note.i <= 6) || (note.i >= 8 || note.i <= 22) || (note.i >= 24 || note.i <= 37) || (note.i >= 39 || note.i <= 43) || (note.i >= 45 || note.i <= 51) || (note.i >= 53 || note.i <= 57) || (note.i == 59) ? 6 : 0;
  782. }
  783. if (state.musicRenderType === EnumMusicRenderType.fixedTone) {
  784. patchX = note.i == 0 ? 31 : (note.i == 8 || note.i == 14 || note.i == 30 || note.i == 45 || note.i == 51 || note.i == 59 || note.i == 65) ? -10 : note.i == 67 ? 31 : 0;
  785. }
  786. } else if (state.cbsExamSongId == '1788501975122489346') {
  787. if (state.musicRenderType === EnumMusicRenderType.firstTone) {
  788. patchX = (note.i == 0) ? 21 : (note.i == 1 || note.i == 7 || note.i == 23 || note.i == 38 || note.i == 44 || note.i == 52 || note.i == 58) ? -6 : (note.i == 9 || note.i == 10 || note.i == 12 || note.i == 13) ? 3 : (note.i == 14 || note.i == 30 || note.i == 45 || note.i == 51 || note.i == 59) ? 6 : (note.i == 45) ? -8 : (note.i >= 15 || note.i <= 29) || (note.i >= 31 || note.i <= 36) || (note.i >= 38 || note.i <= 44) || (note.i >= 46 || note.i <= 50) || (note.i >= 52 || note.i <= 58) || (note.i >= 60 || note.i <= 64) || (note.i == 66) ? 4 : 0;
  789. }
  790. if (state.musicRenderType === EnumMusicRenderType.fixedTone) {
  791. patchX = note.i == 0 ? 31 : (note.i == 8 || note.i == 14 || note.i == 30 || note.i == 45 || note.i == 51 || note.i == 59 || note.i == 65) ? -10 : note.i == 67 ? 31 : 0;
  792. }
  793. }
  794. if (flag === 'refresh' || (flag === 'init' && !state.specialPosInit)) {
  795. // console.log('音符idx',note?.i,cursor.cursorElement.style.left)
  796. cursor.cursorElement.style.left = cursorLeft + patchX + "px";
  797. state.specialPosInit = true;
  798. }
  799. } else {
  800. nextTick(() => {
  801. let bbox = note.bbox;
  802. if (!bbox) {
  803. const musicContainer = document.getElementById("musicAndSelection")?.getBoundingClientRect() || {
  804. x: 0,
  805. y: 0,
  806. };
  807. const parentLeft = musicContainer.x || 0;
  808. const noteEle = document.querySelector(`#vf-${note.svgElement?.attrs?.id}`);
  809. if (noteEle) {
  810. const noteHead = noteEle.querySelector(".vf-numbered-note-head");
  811. const noteHeadBbox = noteHead?.getBoundingClientRect?.();
  812. if (noteHeadBbox) {
  813. note.bbox = {
  814. left: noteHeadBbox.x - parentLeft - noteHeadBbox.width / 4,
  815. width: noteHeadBbox.width * 1.5,
  816. };
  817. bbox = note.bbox;
  818. }
  819. }
  820. }
  821. if (!bbox) return;
  822. const baseW = state.platform === IPlatform.PC ? 29 : 18;
  823. const width = (bbox.width - baseW) / 3;
  824. // console.log(555555,bbox.left,width)
  825. cursor.cursorElement.style.left = bbox.left + "px";
  826. cursor.cursorElement.style.transform = `translateX(${width}px)`;
  827. });
  828. }
  829. }
  830. };
  831. /**
  832. * 跳转到下一个音符
  833. * 一行谱,点击音符,或者播放完成,需要跳转音符位置,增加参数skipNote
  834. **/
  835. export const gotoNext = (note: any, skipNote?: boolean) => {
  836. // console.log(33333333333,state.activeNoteIndex,note.i)
  837. const num = note.i;
  838. if (state.activeNoteIndex === note.i) {
  839. try {
  840. setCursorPosition(note, state.osmd.cursor, 'init');
  841. } catch (error) {
  842. console.log(error);
  843. }
  844. return;
  845. }
  846. const osmd = state.osmd;
  847. let prev = state.activeNoteIndex;
  848. state.activeNoteIndex = num;
  849. state.activeMeasureIndex = note.MeasureNumberXML;
  850. if (prev && num - prev === 1) {
  851. // console.log('跳转音符',11111,osmd.cursor)
  852. // if (!note.id && note.multipleRestMeasures === 0) {
  853. // } else {
  854. // osmd.cursor.next();
  855. // }
  856. osmd.cursor.next();
  857. } else if (prev && num - prev > 0) {
  858. while (num - prev > 0) {
  859. prev++;
  860. // console.log('跳转音符',22222)
  861. osmd.cursor.next();
  862. }
  863. } else {
  864. gotoCustomNote(num);
  865. }
  866. try {
  867. setCursorPosition(note, state.osmd.cursor, 'refresh');
  868. } catch (error) {
  869. console.log(error);
  870. }
  871. // 一行谱,需要滚动小节
  872. if (state.isSingleLine) {
  873. moveSvgDom(skipNote);
  874. }
  875. scrollViewNote();
  876. };
  877. /** 获取指定音符 */
  878. export const getNote = (currentTime: number) => {
  879. const times = state.times;
  880. const len = state.times.length;
  881. /** 播放超过了最后一个音符的时间,直接结束, 2秒误差 */
  882. if (currentTime > times[len - 1].endtime + 2 && !state.isAppPlay) {
  883. onEnded();
  884. return;
  885. }
  886. let _item = null as any;
  887. for (let i = state.activeNoteIndex; i < len; i++) {
  888. let item = times[i];
  889. const prevItem = times[i - 1];
  890. // if (state.isEvxml) {
  891. // let diffArr: any[] = [];
  892. // times.forEach((note: any, noteIdx: number) => {
  893. // if (currentTime >= note.time && currentTime <= times[noteIdx+1].time) {
  894. // let diffTime = times[noteIdx+1].time - currentTime;
  895. // diffArr.push({
  896. // diffTime,
  897. // idx: noteIdx
  898. // })
  899. // }
  900. // })
  901. // diffArr.sort((a, b) => a.diffTime - b.diffTime);
  902. // item = diffArr.length ? times[diffArr[0].idx] : item;
  903. // }
  904. if (currentTime >= item.time) {
  905. if (!prevItem || item.time != prevItem.time) {
  906. _item = item;
  907. }
  908. } else {
  909. break;
  910. }
  911. }
  912. // console.log("activeNoteIndex", currentTime, state.activeNoteIndex, _item.i);
  913. return _item;
  914. };
  915. /** 重播 */
  916. export const handleResetPlay = () => {
  917. // 如果是midi需要重置播放进度
  918. if (state.isAppPlay) {
  919. audioData.progress = 0
  920. }
  921. resetPlaybackToStart();
  922. // 如果是暂停, 直接播放
  923. togglePlay("play");
  924. };
  925. /** 设置速度 */
  926. export const handleSetSpeed = (speed: number) => {
  927. setStorageSpeed(state.examSongId, speed);
  928. state.speed = speed;
  929. };
  930. /** 清除选段状态 */
  931. export const clearSelection = () => {
  932. state.sectionStatus = false;
  933. state.section = [];
  934. closeToast();
  935. };
  936. /** 开启选段 */
  937. export const handleChangeSection = () => {
  938. // 如果开启了选段,再次点击取消选段
  939. if (state.sectionStatus) {
  940. togglePlay("paused");
  941. clearSelection();
  942. skipNotePlay(0, true);
  943. state.sectionFirst = null;
  944. return;
  945. }
  946. state.sectionStatus = true;
  947. // 开启
  948. if (state.sectionStatus) {
  949. togglePlay("paused");
  950. }
  951. showToast({
  952. message: "请选择开始小节",
  953. duration: 0,
  954. position: "top",
  955. className: "selectionToast",
  956. });
  957. };
  958. /** 效验并格式化选段小节 */
  959. const formateSelectMearure = (_list: any[]): any[] => {
  960. if (!_list.length) return [];
  961. const list = _list.sort((a, b) => a.time - b.time);
  962. const startXml = list[0]?.measureOpenIndex;
  963. const endXml = list.last()?.measureOpenIndex;
  964. const selectStartMeasure = state.times.filter((n: any) => startXml === n.measureOpenIndex) || [];
  965. const selectEndMeasure = state.times.filter((n: any) => endXml === n.measureOpenIndex) || [];
  966. // 没有找到选段小节
  967. if (!selectStartMeasure.length || !selectEndMeasure.length) {
  968. clearSelection();
  969. return [];
  970. }
  971. list[0] = selectStartMeasure[0];
  972. list[1] = selectEndMeasure.last();
  973. let startItemINdex = list[0].i;
  974. // 开启预备拍
  975. if (state.isOpenPrepare) {
  976. const startXmlIndex = list[0].MeasureNumberXML;
  977. state.sectionFirst = state.times.find((n: any) => startXmlIndex - n.MeasureNumberXML === 1);
  978. startItemINdex = state.sectionFirst ? state.sectionFirst.i : startItemINdex;
  979. }
  980. skipNotePlay(startItemINdex, startItemINdex === 0);
  981. return list;
  982. };
  983. /** 选择选段 */
  984. export const handleSelection = (item: any) => {
  985. if (!state.sectionStatus || state.section.length > 1) return;
  986. if (state.section.length !== 2 && item) {
  987. state.section.push(item);
  988. if (state.section.length === 2) {
  989. setSection(state.section[0].MeasureNumberXML, state.section[1].MeasureNumberXML)
  990. //state.section = formateSelectMearure(state.section);
  991. closeToast();
  992. }
  993. }
  994. if (state.section.length === 1) {
  995. showToast({
  996. message: "请选择结束小节",
  997. duration: 0,
  998. position: "top",
  999. className: "selectionToast",
  1000. });
  1001. }
  1002. };
  1003. /** 阶段练习、阶段评测设置选段小节 */
  1004. export const setSection = (start: number, end: number, userSpeed?: number) => {
  1005. const startNotes = state.times.filter(
  1006. (n: any) => n.noteElement.sourceMeasure.MeasureNumberXML == start
  1007. )
  1008. const endNotes = state.times.filter(
  1009. (n: any) => n.noteElement.sourceMeasure.MeasureNumberXML == end
  1010. )
  1011. state.userChooseEndIndex = end
  1012. const lastEndId = endNotes[endNotes.length - 1].noteId
  1013. let lastEndNotes = endNotes.filter((n: any) => n.noteId === lastEndId)
  1014. // 是否符合重播规则
  1015. const canRepeatInfo = verifyCanRepeat(start, end)
  1016. console.log('能否重播', canRepeatInfo)
  1017. const isCanRepeat = canRepeatInfo.canRepeat
  1018. // 如果符合重播规则,但是lastEndNotes长度为1,则需要向前找,直到找到lastEndNotes长度为2
  1019. /**
  1020. * 如果符合重播规则,但是lastEndNotes长度为1,则需要向前找,直到找到lastEndNotes长度为2
  1021. * 2024.03.28 新增逻辑,如果当前选中的结束小节是跳房子,则不向前找,因为这种场景不符合重播规则(bug:#9921)
  1022. * TODO:通过vf-volta判断是否是跳房子
  1023. */
  1024. let isSkipVolta = false; // 结束小节是否是跳房子小节
  1025. if (lastEndNotes.length === 1) {
  1026. isSkipVolta = lastEndNotes[0]?.stave?.modifiers?.some((item: any) => item.getAttribute('type') === 'Volta')
  1027. }
  1028. let currentEndNum: number = end
  1029. const lastEndIndex: any = state.repeatInfo[canRepeatInfo.repeatIdx]?.end || 0
  1030. while (isCanRepeat && lastEndNotes.length === 1 && lastEndNotes[0].MeasureNumberXML <= lastEndIndex && !isSkipVolta) {
  1031. currentEndNum = currentEndNum - 1
  1032. const newEndNotes = state.times.filter(
  1033. (n: any) => n.noteElement.sourceMeasure.MeasureNumberXML == currentEndNum
  1034. )
  1035. const newLastEndId = newEndNotes[newEndNotes.length - 1].noteId
  1036. lastEndNotes = newEndNotes.filter((n: any) => n.noteId === newLastEndId)
  1037. }
  1038. const endIdx: any = (isCanRepeat && canRepeatInfo.repeatIdx == state.repeatInfo.length - 1) ? lastEndNotes.length - 1 : 0
  1039. const startNote = startNotes[0]
  1040. // const endNote = endNotes[endNotes.length - 1]
  1041. const endNote = lastEndNotes[endIdx]
  1042. if (startNote && endNote) {
  1043. state.isSelectMeasureMode = true;
  1044. // 设置小节
  1045. hanldeDirectSelection([startNote, endNote]);
  1046. //设置速度
  1047. if (userSpeed) {
  1048. handleSetSpeed(userSpeed);
  1049. }
  1050. }
  1051. }
  1052. /** 直接设置选段 */
  1053. export const hanldeDirectSelection = (list: any[]) => {
  1054. if (!Array.isArray(list) || list.length !== 2) return;
  1055. state.sectionStatus = true;
  1056. setTimeout(() => {
  1057. state.section = formateSelectMearure(list);
  1058. console.log('选段小节', state.section)
  1059. }, 500);
  1060. };
  1061. let offsetTop = 0;
  1062. /**
  1063. * 窗口内滚动到音符的区域
  1064. * @param isScroll 可选: 强制滚动到顶部, 默认: false
  1065. * @returns void
  1066. */
  1067. export const scrollViewNote = () => {
  1068. const cursorElement = document.getElementById("cursorImg-0")!;
  1069. const musicAndSelection = document.getElementById(state.scrollContainer)!;
  1070. if (!cursorElement || !musicAndSelection || offsetTop === cursorElement.offsetTop) return;
  1071. offsetTop = cursorElement.offsetTop;
  1072. if (offsetTop > 50) {
  1073. musicAndSelection.scrollTo({
  1074. top: (offsetTop - 50) * state.musicZoom,
  1075. behavior: "smooth",
  1076. });
  1077. } else {
  1078. musicAndSelection.scrollTo({
  1079. top: 0,
  1080. behavior: "smooth",
  1081. });
  1082. }
  1083. };
  1084. /** 检测是否是节奏练习 */
  1085. export const isRhythmicExercises = () => {
  1086. return state.examSongName.indexOf("节奏练习") > -1;
  1087. };
  1088. /** 重置状态 */
  1089. export const handleRessetState = () => {
  1090. // 切换模式,清除选段
  1091. clearSelection();
  1092. skipNotePlay(0, true);
  1093. // midi 重置播放进度
  1094. if (state.isAppPlay) {
  1095. audioData.progress = 0;
  1096. }
  1097. if (state.modeType === "evaluating") {
  1098. handleStartEvaluat();
  1099. } else if (state.modeType === "practise") {
  1100. togglePlay("paused");
  1101. } else if (state.modeType === "follow") {
  1102. toggleFollow(false);
  1103. }
  1104. };
  1105. export default state;
  1106. /** 初始化评测音频 */
  1107. export const evaluatCreateMusicPlayer = () => {
  1108. return api_createMusicPlayer({
  1109. musicSrc: state.accompany || state.music, // 曲谱音频url
  1110. tuneSrc: "https://oss.dayaedu.com/cloud-coach/1686725501654check_music1_(1).mp3", //效音音频url
  1111. });
  1112. };
  1113. /** 获取内容平台的接口详情并初始化state信息 */
  1114. export const getMusicDetail = async (id: string) => {
  1115. const res = await getMusicSheetDetail(id);
  1116. if (res?.code === 200) {
  1117. getMusicInfo(res)
  1118. }
  1119. };
  1120. const getMusicInfo = (res: any) => {
  1121. const index = query["part-index"] ? parseInt(query["part-index"] as string) : 0;
  1122. // 原音声轨
  1123. const musicData = res.data.musicSheetSoundList?.[index] || {};
  1124. // 伴奏声轨
  1125. const accompanyData = res.data.musicSheetAccompanimentList?.[0] || {}
  1126. const musicInfo = {
  1127. ...res.data,
  1128. music: musicData.audioFileUrl || '',
  1129. accompany: accompanyData.audioFileUrl || '',
  1130. musicSheetId: musicData.musicSheetId || res.data.bizId,
  1131. track: musicData.track || '',
  1132. };
  1133. console.log("🚀 ~ musicInfo:", musicInfo);
  1134. setState(musicInfo, index);
  1135. };
  1136. const setState = (data: any, index: number) => {
  1137. state.appName = "COLEXIU";
  1138. state.detailId = data.bizId;
  1139. state.xmlUrl = data.xmlFileUrl;
  1140. state.paymentType = data.paymentType
  1141. state.partIndex = index;
  1142. state.trackId = data.track;
  1143. state.subjectId = data.subjectIds ? data.subjectIds.split(',')?.[0] : 0;
  1144. // 声部code
  1145. const subjectCode = data.subjectCodes ? data.subjectCodes.split(',')?.[0] : '';
  1146. // 乐器code
  1147. let musicalCode = data.musicalInstrumentIdCodes ? data.musicalInstrumentIdCodes.split(',')?.[0] : '';
  1148. const pitchSubject = musicalInstrumentCodeInfo.find((n) => n.code.toLocaleLowerCase() === subjectCode.toLocaleLowerCase())
  1149. const pitchMusical = musicalInstrumentCodeInfo.find((n) => n.code.toLocaleLowerCase() === musicalCode.toLocaleLowerCase())
  1150. state.subjectCodeId = pitchSubject ? pitchSubject.id : 0
  1151. state.musicalCodeId = pitchMusical ? pitchMusical.id : 0
  1152. state.categoriesId = data.musicCategoryId;
  1153. state.categoriesName = data.musicTagNames;
  1154. // state.enableEvaluation = data.isEvaluated ? true : false;
  1155. state.examSongId = data.bizId + "";
  1156. state.cbsExamSongId = data.id + "";
  1157. state.examSongName = data.name;
  1158. state.coverImg = data.musicCover ?? "";
  1159. // 单声部多声轨合并展示
  1160. state.isCombineRender = data.musicSheetType === "SINGLE" && data.musicSheetSoundList?.length > 1
  1161. setCustom(state.isCombineRender ? data.musicSheetSoundList?.length : 0);
  1162. // 解析扩展字段
  1163. if (data.extConfigJson) {
  1164. try {
  1165. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  1166. } catch (error) {
  1167. console.error("解析扩展字段错误:", error);
  1168. }
  1169. }
  1170. state.gradualTimes = state.extConfigJson.gradualTimes;
  1171. state.repeatedBeats = state.extConfigJson.repeatedBeats || 0;
  1172. state.isEvxml = state.extConfigJson.isEvxml == 1 ? true : false;
  1173. // 曲子包含节拍器,就不开启节拍器
  1174. state.needTick = data.isUseSystemBeat && data.isPlayBeat ? true : false;
  1175. // state.isOpenMetronome = data.isUseSystemBeat ? false : true;
  1176. state.isOpenMetronome = data.isPlayBeat && !data.isUseSystemBeat ? true : false
  1177. state.isShowFingering = data.isShowFingering ? true : false;
  1178. // 设置曲谱的播放模式, APP播放(midi音频是app播放) | h5播放
  1179. state.isAppPlay = data.playMode === 'MIDI';
  1180. state.music = data.music;
  1181. state.accompany = data.accompany;
  1182. state.midiUrl = data.midiFileUrl;
  1183. state.parentCategoriesId = data.musicTag;
  1184. state.musicSheetCategoriesId = data.musicCategoryId;
  1185. state.bizMusicCategoryId = data.bizMusicCategoryId
  1186. state.playMode = data.playMode === "MP3" ? "MP3" : "MIDI";
  1187. state.originSpeed = state.speed = parseFloat(data.playSpeed) || 0;
  1188. // state.originSpeed = state.speed = data.playSpeed;
  1189. // state.playIngSpeed = data.playSpeed;
  1190. const track = data.code || data.track;
  1191. state.track = track ? track.replace(/ /g, "").toLocaleLowerCase() : "";
  1192. // 能否评测,根据当前声轨有无伴奏判断
  1193. if (state.isAppPlay) {
  1194. state.enableEvaluation = state.midiUrl ? true : false
  1195. } else {
  1196. state.enableEvaluation = state.accompany ? true : false
  1197. }
  1198. state.isConcert = data.musicSheetType === "CONCERT" ? true : false;
  1199. // multiTracksSelection 返回为空,默认代表全部分轨
  1200. state.canSelectTracks = data.multiTracksSelection === "null" || data.multiTracksSelection === "" || data.multiTracksSelection === null ? [] : data.multiTracksSelection?.split(',');
  1201. // 开启预备小节
  1202. state.isOpenPrepare = true;
  1203. state.extStyleConfigJson = data.extStyleConfigJson || {}
  1204. // console.log("🚀 ~ state.subjectId:", state.subjectId, state.track as any , state.subjectId)
  1205. // 是否打击乐
  1206. /**
  1207. * 是否打击乐:AMPLITUDE & 节奏练习:DECIBELS
  1208. * evaluationStandard:("评测标准 节奏 AMPLITUDE 音准 FREQUENCY 分贝 DECIBELS")
  1209. * 打击乐&节奏练习,没有跟练模式
  1210. */
  1211. // state.isPercussion = isRhythmicExercises();
  1212. state.isPercussion = data.evaluationStandard === "AMPLITUDE" || data.evaluationStandard === "DECIBELS";
  1213. state.evaluationStandard = data.evaluationStandard?.toLocaleLowerCase() || ''
  1214. // 设置是否特殊曲谱, 是特殊曲谱取反(不理解之前的思考逻辑), 使用后台设置的速度
  1215. state.isSpecialBookCategory = !classids.includes(Number(data.musicCategoryId));
  1216. // 设置指法
  1217. // const code = state.isConcert ? mappingVoicePart(state.trackId, "ENSEMBLE") : mappingVoicePart(state.subjectId, "INSTRUMENT");
  1218. /**
  1219. * 各平台的乐器声部id不统一,为了兼容处理老的数据,加上乐器code码,此码唯一
  1220. * 获取指法code
  1221. */
  1222. const code = state.isConcert ? matchVoicePart(state.trackId, "CONCERT") : matchVoicePart(state.musicalCodeId, "SINGLE");
  1223. state.fingeringInfo = subjectFingering(code);
  1224. console.log("🚀 ~ state.fingeringInfo:", code, state.fingeringInfo, state.trackId, state.track);
  1225. state.musicalCodeId = state.fingeringInfo?.id || 0
  1226. state.musicalCode = musicalInstrumentCodeInfo.find(item => item.id === state.musicalCodeId)?.code || state.trackId
  1227. ; (window as any).DYSubjectId = state.musicalCodeId
  1228. // 开启自定义每行显示的小节数
  1229. ; (window as any).customSectionAmount = true
  1230. // 标识是课堂乐器,用于移调时进行区分处理
  1231. ; (window as any).DYProjectName = 'musicScore';
  1232. // 如果切换的声轨没有指法,择指法开关置灰并且不可点击
  1233. if (!state.fingeringInfo.name && state.setting.displayFingering) {
  1234. state.setting.displayFingering = false
  1235. }
  1236. // 检测是否原音和伴奏都有
  1237. if (!state.music || !state.accompany) {
  1238. state.playSource = state.music ? "music" : "background";
  1239. }
  1240. // 如果是PC端,放大曲谱
  1241. state.platform = query.platform?.toLocaleUpperCase() || "";
  1242. if (state.platform === IPlatform.PC) {
  1243. state.zoom = query.zoom || 1.5;
  1244. state.enableEvaluation = false;
  1245. }
  1246. /**
  1247. * 默认渲染什么谱面类型 & 能否转谱逻辑
  1248. * 渲染类型:首先取url参数musicRenderType,没有该参数则取musicalInstruments字段匹配的当前分轨的defaultScore,没有匹配到则取默认值('firstTone')
  1249. * 能否转谱:先取isConvertibleScore字段,如果isConvertibleScore为true,则取musicalInstruments字段匹配的当前分轨的transferFlag,都为true则可以转谱
  1250. *
  1251. */
  1252. let pitchTrack = null
  1253. if (state.isConcert) {
  1254. musicalCode = musicalInstrumentCodeInfo.find((item: any) => item.id === state.musicalCodeId)?.code
  1255. pitchTrack = data.musicalInstruments?.find((item: any) => item.code === musicalCode)
  1256. } else {
  1257. pitchTrack = data.musicalInstruments?.find((item: any) => item.code === musicalCode)
  1258. }
  1259. let musicalRenderType = ''
  1260. if (pitchTrack?.defaultScore) {
  1261. musicalRenderType = pitchTrack?.defaultScore === 'STAVE' ? 'staff' : pitchTrack?.defaultScore === 'JIAN' ? 'fixedTone' : pitchTrack?.defaultScore === 'FIRST' ? 'firstTone' : ''
  1262. }
  1263. state.musicRenderType = query.musicRenderType || musicalRenderType || EnumMusicRenderType.firstTone;
  1264. /**
  1265. * TODO:摇篮曲特殊处理
  1266. */
  1267. if (['1788501975122489346', '1788502467554750466', '1789839575249596417'].includes(state.cbsExamSongId)) {
  1268. if (state.musicRenderType === 'fixedTone') {
  1269. state.musicRenderType = EnumMusicRenderType.firstTone;
  1270. }
  1271. }
  1272. state.enableNotation = pitchTrack ? data.isConvertibleScore && pitchTrack.transferFlag : data.isConvertibleScore
  1273. console.log("state对象", state);
  1274. // 评测基准频率
  1275. state.baseFrequency = data.evaluationFrequency ? data.evaluationFrequency.split(",")[0] : 440
  1276. state.baseFrequency = Number(state.baseFrequency)
  1277. // 用户上次的频率和基准频率误差超过10,则重置
  1278. if (Math.abs(state.setting.frequency - state.baseFrequency) > 10) {
  1279. state.setting.frequency = state.baseFrequency >= 0 ? state.baseFrequency : 440
  1280. } else {
  1281. state.setting.frequency = state.setting.frequency || state.baseFrequency
  1282. }
  1283. };
  1284. // 多分轨合并显示标示
  1285. const setCustom = (trackNum?: number) => {
  1286. if (trackNum || state.extConfigJson.multitrack) {
  1287. state.multitrack = trackNum || 0
  1288. setGlobalData("multitrack", trackNum || state.extConfigJson.multitrack);
  1289. }
  1290. };
  1291. /** 跟练模式播放节拍器(叮咚) */
  1292. export const followBeatPaly = () => {
  1293. let metroTimer: any = null;
  1294. if (!followData.start) {
  1295. clearTimeout(metroTimer)
  1296. metroTimer = null
  1297. return;
  1298. }
  1299. const time = state.measureTime * 1000 / metronomeData.totalNumerator
  1300. requestAnimationFrame(() => {
  1301. const endTime = Date.now();
  1302. if (endTime - state.beatStartTime < time) {
  1303. followBeatPaly();
  1304. } else {
  1305. // metroTimer = setTimeout(() => {
  1306. // metronomeData.metro?.simulatePlayAudio()
  1307. // startTime = Date.now();
  1308. // followBeatPaly();
  1309. // }, time);
  1310. metronomeData.metro?.simulatePlayAudio()
  1311. state.beatStartTime = Date.now();
  1312. followBeatPaly();
  1313. }
  1314. });
  1315. };
  1316. // 音符添加bbox
  1317. export const addNoteBBox = (list: any[]) => {
  1318. const musicContainer = document.getElementById("musicAndSelection")?.getBoundingClientRect() || {
  1319. x: 0,
  1320. y: 0,
  1321. };
  1322. const parentLeft = musicContainer.x || 0;
  1323. let voicesBBox: any = null;
  1324. for (let i = 0; i < list.length; i++) {
  1325. const note = list[i];
  1326. const { svgElement, multipleRestMeasures, totalMultipleRestMeasures, stave } = note;
  1327. /**
  1328. * 兼容合并休止小节没有音符的情况,将合并的小节宽度等分,音符位置就在等分的位置
  1329. */
  1330. let bbox: any = null;
  1331. if (svgElement?.attrs.id) {
  1332. // @ts-ignore
  1333. bbox = document.getElementById(`vf-${svgElement?.attrs?.id}`)?.getBBox();
  1334. const noteBbox = document.getElementById(`vf-${svgElement?.attrs?.id}`)?.getBoundingClientRect?.() || { x: 0, width: 0 };
  1335. bbox = {
  1336. left: noteBbox.x - parentLeft - noteBbox.width / 4, // 用于简谱模式,跳动音符时,设置光标的位置(五线谱:osmd自动设置光标位置,简谱:需要手动设置光标位置)
  1337. x: bbox?.x * state.zoom,
  1338. y: bbox?.y * state.zoom,
  1339. width: bbox?.width * state.zoom,
  1340. height: bbox?.height * state.zoom,
  1341. }
  1342. } else {
  1343. // @ts-ignore
  1344. let currentVoicesBBox: any = document.getElementById(`${stave?.attrs?.id}`)?.nextSibling?.getBBox();
  1345. const svgBodyBBox: any = document.getElementById('musicAndSelection')?.getBoundingClientRect();
  1346. if (!currentVoicesBBox && multipleRestMeasures <= totalMultipleRestMeasures) {
  1347. currentVoicesBBox = voicesBBox;
  1348. }
  1349. let nextIndex = i + 1;
  1350. while (!list[nextIndex]?.id && nextIndex < list.length) {
  1351. nextIndex += 1;
  1352. }
  1353. // 休止小节的开头和下一个音符之间的间距
  1354. let multipleWidth: any = currentVoicesBBox?.width * state.zoom;
  1355. if (list[nextIndex]?.id) {
  1356. // @ts-ignore
  1357. multipleWidth = document.getElementById(`${list[nextIndex]?.stave?.attrs?.id}`)?.getBBox()?.x * state.zoom - currentVoicesBBox?.x * state.zoom;
  1358. }
  1359. const ratioWidth = multipleWidth / totalMultipleRestMeasures || 0;
  1360. bbox = currentVoicesBBox ? {
  1361. bottom: currentVoicesBBox.bottom,
  1362. height: 30,
  1363. left: currentVoicesBBox.x * state.zoom + ratioWidth * (multipleRestMeasures - 1),
  1364. right: currentVoicesBBox.y,
  1365. top: currentVoicesBBox.top,
  1366. width: 1,
  1367. x: currentVoicesBBox.x * state.zoom + ratioWidth * (multipleRestMeasures - 1),
  1368. y: currentVoicesBBox.y,
  1369. svgBodyLeft: svgBodyBBox?.x,
  1370. } : null;
  1371. voicesBBox = currentVoicesBBox;
  1372. }
  1373. note.bbox = bbox;
  1374. }
  1375. }
  1376. // 一行谱模式,创建固定的光标
  1377. export const createFixedCursor = () => {
  1378. if (!state.isSingleLine) return;
  1379. const svg: any = document.getElementById("osmdSvgPage1");
  1380. state.osmdSvgDom = svg;
  1381. const scrollDom = document.getElementById("musicAndSelection");
  1382. const cursorDom = document.getElementById("cursorImg-0");
  1383. state.fistNoteLeft = cursorDom?.getBoundingClientRect()?.left || 0;
  1384. state.osdmScrollDom = scrollDom;
  1385. state.cursorDom = cursorDom;
  1386. let copyCursor: any = cursorDom?.cloneNode(true);
  1387. if (copyCursor) {
  1388. copyCursor.setAttribute('id', 'cursor-copy');
  1389. copyCursor.style.position = 'sticky';
  1390. copyCursor.style.zIndex = '2';
  1391. // if (!state.times[0]?.id) {
  1392. // copyCursor.style.left = state.times[0]?.bbox?.x + state.times[0]?.bbox?.width / 3 + 'px';
  1393. // }
  1394. copyCursor.style.left = state.times[0]?.bbox?.x + state.times[0]?.bbox?.width / 2 - 1 + 'px';
  1395. copyCursor.style.height = parseFloat(copyCursor.style.height) * 3 + 'px';
  1396. copyCursor.style.opacity = state.moveType === 'uniform' ? 0 : 1;
  1397. // copyCursor.style.background = 'red';
  1398. copyCursor && scrollDom?.appendChild(copyCursor);
  1399. // 创建左侧背景dom
  1400. // @ts-ignore
  1401. const firstMeasureBBox: any = document.querySelector('.vf-measure')?.getBBox();
  1402. const leftDom = document.createElement("div");
  1403. leftDom.style.width = state.times[0]?.bbox?.x - firstMeasureBBox?.x * state.zoom + 'px';
  1404. leftDom.style.height = firstMeasureBBox?.height * state.zoom + 'px';
  1405. leftDom.style.left = firstMeasureBBox?.x * state.zoom + 'px';
  1406. leftDom.style.transform = 'translateY(20px)';
  1407. leftDom.classList.add('leftNoteBg');
  1408. // scrollDom?.appendChild(leftDom);
  1409. }
  1410. }
  1411. /** 计算首尾音符的间距 */
  1412. export const calculateDistance = () => {
  1413. const firstNoteBBox = state.times[0]?.bbox;
  1414. const lastNoteBBox = state.times.last()?.bbox;
  1415. if (firstNoteBBox && lastNoteBBox) {
  1416. const noteDistance = lastNoteBBox.x - firstNoteBBox.x + lastNoteBBox.width / 2 - firstNoteBBox.width / 2 - 1;
  1417. console.log('首尾间距', noteDistance)
  1418. state.noteDistance = noteDistance || 0;
  1419. }
  1420. }
  1421. /** 跳动svgdom */
  1422. export const moveSvgDom = (skipNote?: boolean) => {
  1423. // const cursorLeft = state.cursorDom?.getBoundingClientRect()?.left || 0;
  1424. // const leftValue = parseFloat(state.cursorDom.style.left) - 47 - 20;
  1425. // console.log(cursorLeft,leftValue,'光标位置')
  1426. // state.osmdSvgDom.style.transform = `translateX(${-leftValue}px)`;
  1427. // state.osdmScrollDom.scrollLeft = leftValue
  1428. // console.log('当前音符',state.activeNoteIndex)
  1429. state.times.forEach((item: any, idx: number) => {
  1430. const svgEl = document.getElementById(`vf-${state.times[idx]?.svgElement?.attrs?.id}`)
  1431. const stemEl = document.getElementById(`vf-${state.times[idx]?.svgElement?.attrs?.id}-stem`)
  1432. if ((item.i === state.activeNoteIndex || item.id === state.times[state.activeNoteIndex].id) && item.svgElement) {
  1433. svgEl?.classList.add('noteActive')
  1434. stemEl?.classList.add('noteActive')
  1435. } else {
  1436. svgEl?.classList.remove('noteActive')
  1437. stemEl?.classList.remove('noteActive')
  1438. }
  1439. })
  1440. // document.getElementById('cursor-copy')?.classList.add('cursorAnimate');
  1441. /**
  1442. * 计算需要移动的距离
  1443. * 当前选中的音符和第一个音符之间的间距
  1444. */
  1445. if (skipNote) {
  1446. const distance = state.times[state.activeNoteIndex].bbox?.x - state.times[0].bbox?.x + state.times[state.activeNoteIndex].bbox?.width / 2 - state.times[0].bbox?.width / 2;
  1447. state.osdmScrollDom.scrollTo({
  1448. left: distance,
  1449. behavior: "smooth",
  1450. });
  1451. }
  1452. }
  1453. /** 平滑移动svgdom */
  1454. export const smoothMoveSvgDom = () => {
  1455. const currentTime = getAudioCurrentTime();
  1456. const matchNoteIdx = state.times.findIndex((item: any) => Math.abs(item.time - currentTime) * 1000 < 100)
  1457. // if (matchNoteIdx >= 0) {
  1458. // console.log('匹配',matchNoteIdx,currentTime)
  1459. // }
  1460. if (currentTime <= state.fixtime) return;
  1461. if (currentTime > state.times.last()?.time) return;
  1462. // console.log('跳转音符',currentTime)
  1463. const currentBBox = state.times[state.activeNoteIndex]?.bbox;
  1464. let nextIndex = state.activeNoteIndex + 1;
  1465. let nextBBox = state.times[nextIndex]?.bbox;
  1466. while (!nextBBox && nextIndex < state.times.length) {
  1467. nextIndex += 1;
  1468. nextBBox = state.times[nextIndex]?.bbox;
  1469. }
  1470. // 下一个音符和当前播放音符之间的间距
  1471. let noteDistance = nextBBox?.x - state.times[state.activeNoteIndex].bbox?.x + nextBBox?.width / 4 - state.times[state.activeNoteIndex].bbox?.width / 4 || 0
  1472. if (noteDistance) {
  1473. // 当前的音符和下一个音符之间的时值
  1474. const noteDuration = state.times[nextIndex].time - state.times[state.activeNoteIndex]?.time;
  1475. // 当前时值在该区间的占比
  1476. const playProgress = (currentTime - state.times[state.activeNoteIndex]?.time) / noteDuration;
  1477. // 如果当前播放的音符是休止小节的,实际没有音符
  1478. // if (!state.times[state.activeNoteIndex]?.id && state.times[state.activeNoteIndex]?.multipleRestMeasures && state.times[state.activeNoteIndex+1].id) {
  1479. // noteDistance = noteDistance - state.times[state.activeNoteIndex]?.bbox?.svgBodyLeft;
  1480. // }
  1481. const distance = noteDistance * playProgress;
  1482. // 上一个音符和第一个音符的间距
  1483. let preDistance = state.times[state.activeNoteIndex].bbox?.x - state.times[0].bbox?.x + state.times[state.activeNoteIndex].bbox?.width / 4;
  1484. // console.log(state.activeNoteIndex,'滑动', distance, preDistance, state.osdmScrollDom.scrollLeft, noteDistance, nextIndex, currentTime, noteDuration )
  1485. // console.log('当前音符',state.activeNoteIndex,'距离',noteDistance,'比例',playProgress,'上一个距离',preDistance,'时值',currentTime, noteDuration)
  1486. //console.log('滑动','音符:',state.activeNoteIndex,'小节:', state.activeMeasureIndex)
  1487. state.osdmScrollDom.scrollLeft = distance + preDistance;
  1488. } else {
  1489. const playProgress = (currentTime - state.times[0]?.time) / state.times.last()?.time
  1490. const distance = state.noteDistance * playProgress;
  1491. state.osdmScrollDom.scrollLeft = distance;
  1492. }
  1493. }
  1494. // 匀速平移
  1495. export const uniformMoveSvgDom = () => {
  1496. const currentTime = getAudioCurrentTime();
  1497. if (currentTime <= state.fixtime) return;
  1498. if (currentTime > state.times.last()?.time) return;
  1499. const playProgress = (currentTime - state.fixtime) / state.times.last()?.time;
  1500. const distance = playProgress * state.noteDistance || 0;
  1501. state.osdmScrollDom.scrollLeft = distance;
  1502. }