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. // handleSetSpeed(speed.value);
  38. if (state.playState === 'paused') {
  39. const currentItem: any = (state.sectionStatus && state.section.length === 2) ? state.sectionFirst || state.section[0] : state.times[state.activeNoteIndex];
  40. state.basePlayRate = currentItem?.measureSpeed ? state.speed / currentItem.measureSpeed : state.speed / state.originSpeed;
  41. }
  42. }
  43. );
  44. watch(
  45. () => state.speed,
  46. () => {
  47. if (speed.value !== state.speed) {
  48. speed.value = state.speed;
  49. }
  50. }
  51. );
  52. const metronomeDisable = computed({
  53. get(){
  54. return !metronomeData.disable
  55. },
  56. set(val){
  57. metronomeData.disable = !val
  58. }
  59. })
  60. // 切换节拍器
  61. const toggleSwitch = async (res: any) => {
  62. metronomeDisable.value = res;
  63. return
  64. switchLoading.value = true;
  65. try {
  66. if (storeData.isApp && state.enableEvaluation) {
  67. // 加载弹窗的开始时间
  68. let startTime = +new Date();
  69. state.loadingText = res ? '节拍器准备中,请稍等…' : '节拍器关闭中,请稍等…';
  70. state.isLoading = true;
  71. const targetSrc = res ? state.beatSong.accompany || state.beatSong.music : state.accompany || state.music;
  72. const resData = await api_updateMusicPlayer({
  73. musicSrc: targetSrc || state.accompany || state.music, // 曲谱音频url,有可能含节拍器的音频不存在
  74. tuneSrc: "https://oss.dayaedu.com/MECMP/1722593665681.mp3", //效音音频url
  75. checkFrequence: 496,
  76. })
  77. // console.log('切换节拍器音频',resData)
  78. if (resData?.content) {
  79. // 返回结果的时间
  80. let intervalTime = +new Date() - startTime;
  81. // 超过1秒直接关闭加载弹窗,小于1秒则最短显示1秒加载弹窗
  82. if (intervalTime >= 1000) {
  83. state.isLoading = false;
  84. metronomeDisable.value = res;
  85. switchLoading.value = false;
  86. } else {
  87. const continueTime = 1000 - intervalTime;
  88. setTimeout(() => {
  89. state.isLoading = false;
  90. metronomeDisable.value = res;
  91. switchLoading.value = false;
  92. }, 1200);
  93. }
  94. }
  95. // api_checkSocketStatus();
  96. } else {
  97. metronomeDisable.value = res;
  98. switchLoading.value = false;
  99. }
  100. } catch (error) {
  101. console.log(error)
  102. } finally {
  103. // state.isLoading = false;
  104. // switchLoading.value = false;
  105. }
  106. };
  107. return () => (
  108. <div class={[styles.speedContainer, !(state.isMixBeat && state.modeType !== "evaluating") && styles.isHideBeat]}>
  109. <div class={[styles.head, "top_draging"]}>
  110. <img class={styles.headTit} src={headImg("headTit.png")} />
  111. <img class={styles.closeImg} src={headImg("closeImg.png")} onClick={()=>{ headData.speedShow = false }} />
  112. </div>
  113. <div class={styles.content}>
  114. <div class={styles.conBox}>
  115. <div class={styles.tit}>速度</div>
  116. <div class={[styles.spendCon, (workData.trainingType === "PRACTICE" || workData.trainingType === "EVALUATION") && styles.disableSpend]}>
  117. <img src={headImg("cutImg.png")} class={[styles.btn]} onClick={minusSpeed} />
  118. <div class={styles.sliderCon}>
  119. <Slider class={styles.slider} max={270} min={speed.value < 45 ? speed.value : 45} v-model={speed.value}>
  120. {{
  121. button: () =>
  122. <div class={styles.customButton}>
  123. <div class={styles.speedVal}>{ Math.floor(speed.value) }</div>
  124. <div class={styles.speedBtn}></div>
  125. </div>
  126. }}
  127. </Slider>
  128. </div>
  129. <img src={headImg("addImg.png")} class={[styles.btn]} onClick={plusSpeed} />
  130. </div>
  131. <div class={[styles.speedSel, (workData.trainingType === "PRACTICE" || workData.trainingType === "EVALUATION") && styles.disableSpend]}>
  132. <div onClick={resetCurrentSpeed}>原速</div>
  133. <div onClick={()=>{ speed.value = 70 }}>70</div>
  134. <div onClick={()=>{ speed.value = 80 }}>80</div>
  135. <div onClick={()=>{ speed.value = 90 }}>90</div>
  136. <div onClick={()=>{ speed.value = 100 }}>100</div>
  137. <div onClick={()=>{ speed.value = 110 }}>110</div>
  138. </div>
  139. {
  140. state.isMixBeat && state.modeType !== "evaluating" &&
  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. });