setting.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { reactive } from 'vue';
  2. import { handleInitTick } from './tick';
  3. import { handleInitBeat } from './beat-tick';
  4. import deepClone from '@/helpers/deep-clone';
  5. export const setting = reactive({
  6. element: 'jianpu' as 'jianpu' | 'staff', // 元素
  7. beat: '4-4' as '4-2' | '4-3' | '4-4' | '8-3' | '8-6', // 拍号
  8. barLine: '1' as '1' | '2' | '4', // 小节数
  9. tempo: ['1', '2', '3'] as any[], // 节奏形筛选
  10. scorePart: [] as any, // 生成谱面
  11. playState: 'pause' as 'pause' | 'play',
  12. playType: 'tempo' as 'beat' | 'tempo',
  13. speed: 60 // 默认速度
  14. } as any);
  15. /**
  16. * 临时设置的数据
  17. */
  18. export const setting_modal = reactive({
  19. element: 'jianpu' as 'jianpu' | 'staff', // 元素
  20. beat: '4-4' as '4-2' | '4-3' | '4-4' | '8-3' | '8-6', // 拍号
  21. barLine: '1' as '1' | '2' | '4', // 小节数
  22. tempo: ['1', '2', '3'] as any[], // 节奏形筛选
  23. scorePart: [] as any, // 生成谱面
  24. playState: 'pause' as 'pause' | 'play',
  25. playType: 'tempo' as 'beat' | 'tempo',
  26. speed: 60 // 默认速度
  27. } as any);
  28. /** 元素 */
  29. export const elementList = {
  30. jianpu: '简谱',
  31. staff: '五线谱'
  32. } as any;
  33. /** 拍号 */
  34. export const beatList = {
  35. '4-2': '2/4',
  36. '4-3': '3/4',
  37. '4-4': '4/4',
  38. '8-3': '3/8',
  39. '8-6': '6/8'
  40. } as any;
  41. /** 每页小节数量 */
  42. export const barLineList = {
  43. 1: 1,
  44. 2: 2,
  45. 4: 4
  46. } as any;
  47. /** 节奏型筛选 */
  48. // 简谱
  49. const temp: any = {};
  50. const tempNum = [] as any
  51. for (let i = 1; i <= 14; i++) {
  52. temp[i] = i + '.png';
  53. tempNum.push(i)
  54. }
  55. export const tempo4 = temp;
  56. export const tempo4Num = tempNum
  57. // 五线谱
  58. const temp2: any = {};
  59. const tempNum2 = [] as any
  60. for (let i = 15; i <= 31; i++) {
  61. temp2[i] = i + '.png';
  62. tempNum2.push(i)
  63. }
  64. export const tempo8 = temp2;
  65. export const tempo8Num = tempNum2
  66. export const getTempList = (element: 'jianpu' | 'staff') => {
  67. //
  68. let temp: any = {};
  69. let tempNum = [] as any
  70. for (let i = 1; i <= 14; i++) {
  71. temp[i] = i + '.png';
  72. tempNum.push(i)
  73. }
  74. let temp2: any = {};
  75. let tempNum2 = [] as any
  76. for (let i = 15; i <= 31; i++) {
  77. temp2[i] = i + '.png';
  78. tempNum2.push(i)
  79. }
  80. if(element === "jianpu") {
  81. temp = {}
  82. tempNum = []
  83. temp2 = {}
  84. tempNum2 = []
  85. for (let i = 1; i <= 14; i++) {
  86. temp[i] = i + '.png';
  87. tempNum.push(i)
  88. if(i === 5) {
  89. temp[32] = '32.png'
  90. tempNum.push(32)
  91. }
  92. }
  93. for (let i = 15; i <= 31; i++) {
  94. temp2[i] = i + '.png';
  95. tempNum2.push(i)
  96. }
  97. }
  98. return {
  99. tempo4: temp,
  100. tempo4Num: tempNum,
  101. tempo8: temp2,
  102. tempo8Num: tempNum2
  103. }
  104. }
  105. /** 随机生成元素 */
  106. export const randomScoreElement = (element?: string) => {
  107. // const tempoList = setting.tempo;
  108. let tempoList = getTempList(setting_modal.element).tempo4Num || [] as any
  109. if (['4-2', '4-3', '4-4'].includes(setting_modal.beat)) {
  110. tempoList = getTempList(setting_modal.element).tempo4Num;
  111. } else if (['8-3', '8-6'].includes(setting_modal.beat)) {
  112. tempoList = getTempList(setting_modal.element).tempo8Num;
  113. }
  114. const prefix = setting_modal.element === 'jianpu' ? 'j-' : 'f-';
  115. if (element) {
  116. const newArr = tempoList.filter((item: any) => item !== element);
  117. // 生成一个0到newArr长度之间的随机索引
  118. const randomIndex = Math.floor(Math.random() * newArr.length);
  119. return {
  120. url: prefix + newArr[randomIndex] + '.png',
  121. index: newArr[randomIndex]
  122. };
  123. } else {
  124. // 如果只有一个就直接返回
  125. if (tempoList.length === 1) {
  126. return {
  127. url: prefix + tempoList[0] + '.png',
  128. index: tempoList[0]
  129. };
  130. } else {
  131. const randomIndex = Math.floor(Math.random() * tempoList.length);
  132. const randomItem = tempoList[randomIndex];
  133. return {
  134. url: prefix + randomItem + '.png',
  135. index: randomItem
  136. };
  137. }
  138. }
  139. };
  140. /** 设置元素方向 */
  141. export const elementDirection = (type: string, index: number) => {
  142. const prefix = setting.element === 'jianpu' ? 'j-' : 'f-';
  143. let ele = '';
  144. let i = 0;
  145. // const tempoList = setting.tempo;
  146. let tempoList = getTempList(setting.element).tempo4Num || [] as any
  147. if (['4-2', '4-3', '4-4'].includes(setting_modal.beat)) {
  148. tempoList = getTempList(setting.element).tempo4Num;
  149. } else if (['8-3', '8-6'].includes(setting_modal.beat)) {
  150. tempoList = getTempList(setting.element).tempo8Num;
  151. }
  152. const toIndex = tempoList.findIndex((t: any) => Number(t) === index);
  153. if (type === 'up') {
  154. if (toIndex <= 0) {
  155. ele = tempoList[tempoList.length - 1];
  156. i = tempoList.length - 1;
  157. } else {
  158. ele = tempoList[toIndex - 1];
  159. i = toIndex - 1;
  160. }
  161. } else if (type === 'down') {
  162. if (toIndex >= tempoList.length - 1) {
  163. ele = tempoList[0];
  164. i = 0;
  165. } else {
  166. ele = tempoList[toIndex + 1];
  167. i = toIndex + 1;
  168. }
  169. }
  170. return {
  171. url: prefix + ele + '.png',
  172. index: Number(ele)
  173. };
  174. };
  175. /**
  176. * 生成谱面
  177. * @param {temp} 是否临时生成
  178. * */
  179. export const renderScore = () => {
  180. const barLine = Number(setting.barLine);
  181. const beatA = setting.beat.split('-').map((i: any) => Number(i));
  182. let beat = beatA[1];
  183. if (beatA[0] === 8) {
  184. beat = beat / 3;
  185. }
  186. const tempBeat: any = [];
  187. for (let i = 0; i < barLine; i++) {
  188. tempBeat[i] = [];
  189. for (let j = 0; j < beat; j++) {
  190. tempBeat[i][j] = {
  191. ...randomScoreElement()
  192. };
  193. }
  194. }
  195. setting.scorePart = tempBeat;
  196. const beatLengthInMilliseconds = (60 / setting.speed) * 1000;
  197. if (setting.playType === 'beat') {
  198. handleInitTick(
  199. beatLengthInMilliseconds,
  200. Number(beatA[1]) || 4,
  201. Number(beatA[0])
  202. );
  203. } else {
  204. handleInitBeat(beatLengthInMilliseconds, Number(beatA[1]) || 4);
  205. }
  206. initSelectScorePart();
  207. // 初始化设置里面的数据
  208. setting_modal.scorePart = deepClone(setting.scorePart)
  209. };
  210. /** 初始化选中状态 */
  211. export const initSelectScorePart = (i?: number, j?: number) => {
  212. setting.scorePart.forEach((part: Array<any>) => {
  213. part.forEach((item: any) => {
  214. item.selected = false;
  215. });
  216. });
  217. if (i !== undefined && j !== undefined && setting.scorePart[i][j]) {
  218. setting.scorePart[i][j].selected = true;
  219. }
  220. };
  221. /** 随机生成元素 设置中 */
  222. export const randomScoreElementModal = (element?: string) => {
  223. let tempoList = getTempList(setting_modal.element).tempo4Num || [] as any
  224. if (['4-2', '4-3', '4-4'].includes(setting_modal.beat)) {
  225. tempoList = getTempList(setting_modal.element).tempo4Num;
  226. } else if (['8-3', '8-6'].includes(setting_modal.beat)) {
  227. tempoList = getTempList(setting_modal.element).tempo8Num;
  228. }
  229. const prefix = setting_modal.element === 'jianpu' ? 'j-' : 'f-';
  230. if (element) {
  231. const newArr = tempoList.filter((item: any) => item !== element);
  232. // 生成一个0到newArr长度之间的随机索引
  233. const randomIndex = Math.floor(Math.random() * newArr.length);
  234. return {
  235. url: prefix + newArr[randomIndex] + '.png',
  236. index: newArr[randomIndex]
  237. };
  238. } else {
  239. // 如果只有一个就直接返回
  240. if (tempoList.length === 1) {
  241. return {
  242. url: prefix + tempoList[0] + '.png',
  243. index: tempoList[0]
  244. };
  245. } else {
  246. const randomIndex = Math.floor(Math.random() * tempoList.length);
  247. const randomItem = tempoList[randomIndex];
  248. return {
  249. url: prefix + randomItem + '.png',
  250. index: randomItem
  251. };
  252. }
  253. }
  254. };
  255. /*** 生成谱面 设置中 */
  256. export const renderScoreModal = () => {
  257. const barLine = Number(setting_modal.barLine);
  258. const beatA = setting_modal.beat.split('-').map((i: any) => Number(i));
  259. let beat = beatA[1];
  260. if (beatA[0] === 8) {
  261. beat = beat / 3;
  262. }
  263. const tempBeat: any = [];
  264. for (let i = 0; i < barLine; i++) {
  265. tempBeat[i] = [];
  266. for (let j = 0; j < beat; j++) {
  267. tempBeat[i][j] = {
  268. ...randomScoreElementModal()
  269. };
  270. }
  271. }
  272. setting_modal.scorePart = tempBeat;
  273. const beatLengthInMilliseconds = (60 / setting.speed) * 1000;
  274. if (setting_modal.playType === 'beat') {
  275. handleInitTick(
  276. beatLengthInMilliseconds,
  277. Number(beatA[1]) || 4,
  278. Number(beatA[0])
  279. );
  280. } else {
  281. handleInitBeat(beatLengthInMilliseconds, Number(beatA[1]) || 4);
  282. }
  283. initSelectScorePartModal();
  284. };
  285. /** 初始化选中状态 设置中 */
  286. export const initSelectScorePartModal = (i?: number, j?: number) => {
  287. setting_modal.scorePart.forEach((part: Array<any>) => {
  288. part.forEach((item: any) => {
  289. item.selected = false;
  290. });
  291. });
  292. if (i !== undefined && j !== undefined && setting_modal.scorePart[i][j]) {
  293. setting_modal.scorePart[i][j].selected = true;
  294. }
  295. };