index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { computed, defineComponent, ref, watch } from "vue";
  2. import { Switch, Slider } from "vant";
  3. import styles from "./index.module.less"
  4. import { headData } from "../index"
  5. import { headImg } from "../image";
  6. import state, { handleSetSpeed, resetBaseRate } from "../../../state";
  7. import { metronomeData } from "../../../helpers/metronome";
  8. import { getQuery } from "/src/utils/queryString";
  9. import { api_createMusicPlayer, api_updateMusicPlayer, api_checkSocketStatus } from "/src/helpers/communication";
  10. import { storeData } from "/src/store";
  11. import { data as workData } from "/src/page-instrument/custom-plugins/work-index";
  12. export default defineComponent({
  13. name: "speed",
  14. setup() {
  15. const speed = ref(state.speed);
  16. const switchLoading = ref(false);
  17. const query: any = getQuery();
  18. const minusSpeed = () => {
  19. let canSpeed = Math.max(speed.value - 1, 45);
  20. canSpeed = Math.min(canSpeed, 270);
  21. speed.value = canSpeed;
  22. };
  23. const plusSpeed = () => {
  24. let canSpeed = Math.min(speed.value + 1, 270);
  25. canSpeed = Math.max(canSpeed, 45);
  26. speed.value = canSpeed;
  27. };
  28. // 重置当前小节的速度
  29. const resetCurrentSpeed = () => {
  30. resetBaseRate(state.activeNoteIndex);
  31. };
  32. watch(
  33. () => speed.value,
  34. () => {
  35. // handleSetSpeed(speed.value);
  36. // state.speed = Math.floor(speed.value);
  37. state.speed = speed.value
  38. // handleSetSpeed(speed.value);
  39. if (state.playState === 'paused') {
  40. // const currentItem: any = (state.sectionStatus && state.section.length === 2) ? state.sectionFirst || state.section[0] : state.times[state.activeNoteIndex];
  41. const currentItem: any = state.times[state.activeNoteIndex];
  42. state.basePlayRate = currentItem?.measureSpeed ? state.speed / currentItem.measureSpeed : state.speed / state.originSpeed;
  43. }
  44. }
  45. );
  46. watch(
  47. () => state.speed,
  48. () => {
  49. if (speed.value !== state.speed) {
  50. speed.value = state.speed;
  51. }
  52. }
  53. );
  54. const metronomeDisable = computed({
  55. get(){
  56. return !metronomeData.disable
  57. },
  58. set(val){
  59. metronomeData.disable = !val
  60. }
  61. })
  62. // 切换节拍器
  63. const toggleSwitch = async (res: any) => {
  64. metronomeDisable.value = res;
  65. return
  66. switchLoading.value = true;
  67. try {
  68. if (storeData.isApp && state.enableEvaluation) {
  69. // 加载弹窗的开始时间
  70. let startTime = +new Date();
  71. state.loadingText = res ? '节拍器准备中,请稍等…' : '节拍器关闭中,请稍等…';
  72. state.isLoading = true;
  73. const targetSrc = res ? state.beatSong.accompany || state.beatSong.music : state.accompany || state.music;
  74. const resData = await api_updateMusicPlayer({
  75. musicSrc: targetSrc || state.accompany || state.music, // 曲谱音频url,有可能含节拍器的音频不存在
  76. tuneSrc: "https://oss.dayaedu.com/MECMP/1722593665681.mp3", //效音音频url
  77. checkFrequence: 496,
  78. })
  79. // console.log('切换节拍器音频',resData)
  80. if (resData?.content) {
  81. // 返回结果的时间
  82. let intervalTime = +new Date() - startTime;
  83. // 超过1秒直接关闭加载弹窗,小于1秒则最短显示1秒加载弹窗
  84. if (intervalTime >= 1000) {
  85. state.isLoading = false;
  86. metronomeDisable.value = res;
  87. switchLoading.value = false;
  88. } else {
  89. const continueTime = 1000 - intervalTime;
  90. setTimeout(() => {
  91. state.isLoading = false;
  92. metronomeDisable.value = res;
  93. switchLoading.value = false;
  94. }, 1200);
  95. }
  96. }
  97. // api_checkSocketStatus();
  98. } else {
  99. metronomeDisable.value = res;
  100. switchLoading.value = false;
  101. }
  102. } catch (error) {
  103. console.log(error)
  104. } finally {
  105. // state.isLoading = false;
  106. // switchLoading.value = false;
  107. }
  108. };
  109. return () => (
  110. <div class={[styles.speedContainer, !(state.isMixBeat && state.modeType !== "evaluating") && styles.isHideBeat]}>
  111. <div class={[styles.head, "top_draging"]}>
  112. <img class={styles.headTit} src={headImg("headTit.png")} />
  113. <img class={styles.closeImg} src={headImg("closeImg.png")} onClick={()=>{ headData.speedShow = false }} />
  114. </div>
  115. <div class={styles.content}>
  116. <div class={styles.conBox}>
  117. <div class={styles.tit}>速度</div>
  118. <div class={[styles.spendCon, (workData.trainingType === "PRACTICE" || workData.trainingType === "EVALUATION") && styles.disableSpend]}>
  119. <img src={headImg("cutImg.png")} class={[styles.btn]} onClick={minusSpeed} />
  120. <div class={styles.sliderCon}>
  121. <Slider class={styles.slider} max={270} min={speed.value < 45 ? speed.value : 45} v-model={speed.value}>
  122. {{
  123. button: () =>
  124. <div class={styles.customButton}>
  125. <div class={styles.speedVal}>{ Math.floor(speed.value) }</div>
  126. <div class={styles.speedBtn}></div>
  127. </div>
  128. }}
  129. </Slider>
  130. </div>
  131. <img src={headImg("addImg.png")} class={[styles.btn]} onClick={plusSpeed} />
  132. </div>
  133. <div class={[styles.speedSel, (workData.trainingType === "PRACTICE" || workData.trainingType === "EVALUATION") && styles.disableSpend]}>
  134. <div onClick={resetCurrentSpeed}>原速</div>
  135. {[60,70,80,90,100,110,120,130,140,150,160].map((item) => (
  136. <div onClick={()=>{ speed.value = item }}>{item}</div>
  137. ))}
  138. </div>
  139. {
  140. state.isMixBeat && state.modeType !== "evaluating" && !state.isAppPlay &&
  141. <div class={styles.metronome}>
  142. <div class={styles.tit}>节拍器</div>
  143. <Switch
  144. class={switchLoading.value ? styles.switchLoading : ''}
  145. v-model:modelValue={metronomeDisable.value}
  146. loading={switchLoading.value}
  147. onChange={toggleSwitch}
  148. ></Switch>
  149. </div>
  150. }
  151. </div>
  152. </div>
  153. </div>
  154. );
  155. },
  156. });