snd-1.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. // abc2svg - ABC to SVG translator
  2. // @source: https://chiselapp.com/user/moinejf/repository/abc2svg
  3. // Copyright (C) 2014-2023 Jean-Francois Moine - LGPL3+
  4. //snd-1.js-file to include in html pages with abc2svg-1.js for playing
  5. function AbcPlay(i_conf) {
  6. var conf = i_conf, init = {}, audio = ToAudio(), audio5, midi5, current, abcplay = {
  7. clear: audio.clear, add: audio.add, set_sfu: function (v) {
  8. if (v == undefined)
  9. return conf.sfu
  10. conf.sfu = v
  11. }, set_speed: function (v) {
  12. if (v == undefined)
  13. return conf.speed
  14. conf.new_speed = v
  15. }, set_vol: function (v) {
  16. if (v == undefined)
  17. return conf.gain; conf.gain = v
  18. if (current && current.set_vol)
  19. current.set_vol(v)
  20. }, play: play, stop: vf
  21. }
  22. function vf() { }
  23. function play(istart, i_iend, a_e) {
  24. init.istart = istart; init.i_iend = i_iend; init.a_e = a_e
  25. if (midi5)
  26. midi5.get_outputs(play2)
  27. else
  28. play2()
  29. }
  30. function play2(out) {
  31. var o
  32. if (!out)
  33. out = []
  34. o = audio5.get_outputs()
  35. if (o)
  36. Array.prototype.push.apply(out, o)
  37. if (out.length == 0) {
  38. if (conf.onend)
  39. conf.onend()
  40. return
  41. }
  42. if (out.length == 1) { o = 0 } else {
  43. o = -1
  44. var pr = "Use"
  45. for (var i = 0; i < out.length; i++)
  46. pr += "\n " + i + ": " + out[i]
  47. var res = window.prompt(pr, '0')
  48. if (res) {
  49. o = Number(res)
  50. if (isNaN(o) || o < 0 || o >= out.length)
  51. o = -1
  52. }
  53. if (!res || o < 0) {
  54. if (conf.onend)
  55. conf.onend()
  56. return
  57. }
  58. }
  59. current = out[o] == 'sf2' ? audio5 : midi5; abcplay.play = current.play; abcplay.stop = current.stop
  60. if (current.set_output)
  61. current.set_output(out[o]); abcplay.play(init.istart, init.i_iend, init.a_e)
  62. }
  63. conf.gain = 0.7; conf.speed = 1; (function () {
  64. var v
  65. try {
  66. if (!localStorage)
  67. return
  68. } catch (e) { return }
  69. if (!conf.sfu) {
  70. v = localStorage.getItem("sfu")
  71. if (v)
  72. conf.sfu = v
  73. }
  74. v = localStorage.getItem("volume")
  75. if (v)
  76. conf.gain = Number(v)
  77. })()
  78. if (typeof Midi5 == "function")
  79. midi5 = Midi5(conf)
  80. if (typeof Audio5 == "function")
  81. audio5 = Audio5(conf); return abcplay
  82. }
  83. if (typeof module == 'object' && typeof exports == 'object')
  84. exports.AbcPlay = AbcPlay
  85. if (!abc2svg)
  86. var abc2svg = {}
  87. function ToAudio() {
  88. return {
  89. add: function (first, voice_tb, cfmt) {
  90. var toaud = this, C = abc2svg.C, p_time = 0, abc_time = 0, play_fac = C.BLEN / 4 * 120 / 60, i, n, dt, d, v, s = first, rst = s, rst_fac, rsk = [], b_tim, b_typ
  91. function get_beat() {
  92. var s = first.p_v.meter
  93. if (!s.a_meter[0])
  94. return C.BLEN / 4
  95. if (!s.a_meter[0].bot)
  96. return (s.a_meter[1] && s.a_meter[1].top == '|') ? C.BLEN / 2 : C.BLEN / 4
  97. if (s.a_meter[0].bot == "8" && s.a_meter[0].top % 3 == 0)
  98. return C.BLEN / 8 * 3
  99. return C.BLEN / s.a_meter[0].bot | 0
  100. }
  101. function def_beats() {
  102. var i, s2, s3, tim, last_d, beat = get_beat(), d = first.p_v.meter.wmeasure, nb = d / beat | 0, v = voice_tb.length, p_v = { id: "_beats", v: v, sym: { type: C.BLOCK, v: v, subtype: "midiprog", chn: 9, instr: 16384, ts_prev: first } }, s = { type: C.NOTE, v: v, p_v: p_v, dur: beat, nhd: 0, notes: [{ midi: 37 }] }
  103. for (s2 = first; s2; s2 = s2.ts_next) {
  104. if (s2.bar_type && s2.time) {
  105. nb = (2 * d - s2.time) / beat | 0
  106. last_d = beat - s2.time
  107. break
  108. }
  109. }
  110. s2 = p_v.sym
  111. for (s3 = first; s3 && !s3.time; s3 = s3.ts_next) {
  112. if (s3.type == C.TEMPO) {
  113. s3 = Object.create(s3)
  114. s3.v = v
  115. s3.p_v = p_v
  116. s3.prev = s3.ts_prev = s2
  117. s2.next = s2.ts_next = s3
  118. s2 = s3
  119. play_fac = set_tempo(s2)
  120. break
  121. }
  122. }
  123. voice_tb[v] = p_v
  124. p_v.sym.p_v = p_v
  125. tim = abc_time = -d
  126. first.time = s2.time = tim
  127. if (s3)
  128. p_v.sym.time = tim
  129. for (i = 0; i < nb; i++) {
  130. s3 = Object.create(s)
  131. s3.time = tim
  132. s3.prev = s2
  133. s2.next = s3
  134. s3.ts_prev = s2
  135. s2.ts_next = s3
  136. s2 = s3
  137. if (last_d && i == nb - 1) { s3.dur = s3.dur_orig = s3.notes[0].dur = last_d }
  138. tim += beat
  139. }
  140. s2.ts_next = first.ts_next
  141. s2.ts_next.ts_prev = s2
  142. first.ts_next = p_v.sym
  143. }
  144. function build_parts(first) {
  145. var i, j, c, n, v, s = first, p = s.parts, st = [], r = ""
  146. for (i = 0; i < p.length; i++) {
  147. c = p[i]
  148. switch (c) {
  149. case '.': continue
  150. case '(': st.push(r.length)
  151. continue
  152. case ')': j = st.pop()
  153. if (j == undefined)
  154. j = r.length
  155. continue
  156. }
  157. if (c >= 'A' && c <= 'Z') {
  158. j = r.length
  159. r += c
  160. continue
  161. }
  162. n = Number(c)
  163. if (isNaN(n))
  164. break
  165. v = r.slice(j)
  166. if (r.length + v.length * n > 128)
  167. continue
  168. while (--n > 0)
  169. r += v
  170. }
  171. s.parts = r
  172. s.p_s = []
  173. while (1) {
  174. if (!s.ts_next) {
  175. s.part1 = first
  176. break
  177. }
  178. s = s.ts_next
  179. if (s.part) {
  180. s.part1 = first
  181. v = s.part.text[0]
  182. for (i = 0; i < first.parts.length; i++) {
  183. if (first.parts[i] == v)
  184. first.p_s[i] = s
  185. }
  186. }
  187. }
  188. }
  189. function gen_grace(s) {
  190. var g, i, n, t, d, s2, next = s.next
  191. if (s.sappo) { d = C.BLEN / 16 } else if ((!next || next.type != C.NOTE) && s.prev && s.prev.type == C.NOTE) { d = s.prev.dur / 2 } else {
  192. d = next.dur / 12
  193. if (!(d & (d - 1)))
  194. d = next.dur / 2
  195. else
  196. d = next.dur / 3
  197. if (s.p_v.key.k_bagpipe)
  198. d /= 2
  199. next.time += d
  200. next.dur -= d
  201. }
  202. n = 0
  203. for (g = s.extra; g; g = g.next)
  204. n++
  205. d /= n * play_fac
  206. t = p_time
  207. for (g = s.extra; g; g = g.next) {
  208. g.ptim = t
  209. g.pdur = d
  210. t += d
  211. }
  212. }
  213. function set_tempo(s) {
  214. var i, d = 0, n = s.tempo_notes.length
  215. for (i = 0; i < n; i++)
  216. d += s.tempo_notes[i]
  217. return d * s.tempo / 60
  218. }
  219. function set_variant(s) {
  220. var d, n = s.text.match(/[1-8]-[2-9]|[1-9,.]|[^\s]+$/g)
  221. while (1) {
  222. d = n.shift()
  223. if (!d)
  224. break
  225. if (d[1] == '-')
  226. for (i = d[0]; i <= d[2]; i++)
  227. rsk[i] = s
  228. else if (d >= '1' && d <= '9')
  229. rsk[Number(d)] = s
  230. else if (d != ',')
  231. rsk.push(s)
  232. }
  233. }
  234. if (cfmt.chord)
  235. abc2svg.chord(first, voice_tb, cfmt)
  236. if (cfmt.playbeats)
  237. def_beats()
  238. if (s.parts)
  239. build_parts(s)
  240. rst_fac = play_fac
  241. while (s) {
  242. if (s.noplay) {
  243. s = s.ts_next
  244. continue
  245. }
  246. dt = s.time - abc_time
  247. if (dt != 0) {
  248. p_time += dt / play_fac
  249. abc_time = s.time
  250. }
  251. s.ptim = p_time
  252. if (s.part) {
  253. rst = s
  254. rst_fac = play_fac
  255. }
  256. switch (s.type) {
  257. case C.BAR: if (s.time != b_tim) {
  258. b_tim = s.time
  259. b_typ = 0
  260. }
  261. if (s.text && rsk.length > 1 && s.text[0] != '1') {
  262. if (b_typ & 1)
  263. break
  264. b_typ |= 1
  265. set_variant(s)
  266. play_fac = rst_fac
  267. rst = rsk[0]
  268. }
  269. if (s.bar_type[0] == ':') {
  270. if (b_typ & 2)
  271. break
  272. b_typ |= 2
  273. s.rep_p = rst
  274. if (rst == rsk[0])
  275. s.rep_v = rsk
  276. }
  277. if (s.text) {
  278. if (s.text[0] == '1') {
  279. if (b_typ & 1)
  280. break
  281. b_typ |= 1
  282. s.rep_s = rsk = [rst]
  283. if (rst.bar_type && rst.bar_type.slice(-1) != ':')
  284. rst.bar_type += ':'
  285. set_variant(s)
  286. rst_fac = play_fac
  287. }
  288. } else if (s.bar_type.slice(-1) == ':') {
  289. if (b_typ & 4)
  290. break
  291. b_typ |= 4
  292. rst = s
  293. rst_fac = play_fac
  294. } else if (s.rbstop == 2) {
  295. if (b_typ & 8)
  296. break
  297. b_typ |= 8
  298. rst = s
  299. rst_fac = play_fac
  300. }
  301. break
  302. case C.GRACE: if (s.time == 0 && abc_time == 0) {
  303. dt = 0
  304. if (s.sappo)
  305. dt = C.BLEN / 16
  306. else if (!s.next || s.next.type != C.NOTE)
  307. dt = d / 2
  308. abc_time -= dt
  309. }
  310. gen_grace(s)
  311. break
  312. case C.REST: case C.NOTE: d = s.dur
  313. if (s.next && s.next.type == C.GRACE) {
  314. dt = 0
  315. if (s.next.sappo)
  316. dt = C.BLEN / 16
  317. else if (!s.next.next || s.next.next.type != C.NOTE)
  318. dt = d / 2
  319. s.next.time -= dt
  320. d -= dt
  321. }
  322. d /= play_fac
  323. s.pdur = d
  324. v = s.v
  325. break
  326. case C.TEMPO: if (s.tempo)
  327. play_fac = set_tempo(s)
  328. break
  329. }
  330. s = s.ts_next
  331. }
  332. }
  333. }
  334. }
  335. abc2svg.play_next = function (po) {
  336. function do_tie(not_s, d) {
  337. var i, s = not_s.s, C = abc2svg.C, v = s.v, end_time = s.time + s.dur, repv = po.repv
  338. while (1) {
  339. s = s.ts_next
  340. if (!s || s.time > end_time)
  341. break
  342. if (s.type == C.BAR) {
  343. if (s.rep_p) {
  344. if (!po.repn) {
  345. s = s.rep_p
  346. end_time = s.time
  347. }
  348. }
  349. if (s.rep_s) {
  350. if (!s.rep_s[repv])
  351. break
  352. s = s.rep_s[repv++]
  353. end_time = s.time
  354. }
  355. while (s.ts_next && !s.ts_next.dur)
  356. s = s.ts_next
  357. continue
  358. }
  359. if (s.time < end_time || !s.ti2)
  360. continue
  361. i = s.notes.length
  362. while (--i >= 0) {
  363. note = s.notes[i]
  364. if (note.tie_s == not_s) {
  365. d += s.pdur / po.conf.speed
  366. return note.tie_e ? do_tie(note, d) : d
  367. }
  368. }
  369. }
  370. return d
  371. }
  372. function set_ctrl(po, s2, t) {
  373. var i, p_v = s2.p_v, s = { subtype: "midictl", p_v: p_v, v: s2.v }
  374. for (i in p_v.midictl) {
  375. s.ctrl = Number(i)
  376. s.val = p_v.midictl[i]
  377. po.midi_ctrl(po, s, t)
  378. }
  379. for (s = p_v.sym; s != s2; s = s.next) {
  380. if (s.subtype == "midictl")
  381. po.midi_ctrl(po, s, t)
  382. else if (s.subtype == 'midiprog')
  383. po.midi_prog(po, s)
  384. }
  385. i = po.v_c[s2.v]
  386. if (i == undefined)
  387. po.v_c[s2.v] = i = s2.v < 9 ? s2.v : s2.v + 1
  388. if (po.c_i[i] == undefined)
  389. po.c_i[i] = 0
  390. po.p_v[s2.v] = true
  391. }
  392. function play_cont(po) {
  393. var d, i, st, m, note, g, s2, t, maxt, now, C = abc2svg.C, s = po.s_cur
  394. function var_end(s) {
  395. var i, s2, s3, a = s.rep_v || s.rep_s
  396. ti = 0
  397. for (i = 1; i < a.length; i++) {
  398. s2 = a[i]
  399. if (s2.time > ti) {
  400. ti = s2.time
  401. s3 = s2
  402. }
  403. }
  404. for (s = s3; s != po.s_end; s = s.ts_next) {
  405. if (s.time == ti)
  406. continue
  407. if (s.rbstop == 2)
  408. break
  409. }
  410. po.repv = 1
  411. return s
  412. }
  413. if (po.stop) {
  414. if (po.onend)
  415. po.onend(po.repv)
  416. return
  417. }
  418. while (s.noplay) {
  419. s = s.ts_next
  420. if (!s || s == po.s_end) {
  421. if (po.onend)
  422. po.onend(po.repv)
  423. return
  424. }
  425. }
  426. t = po.stim + s.ptim / po.conf.speed
  427. now = po.get_time(po)
  428. if (po.conf.new_speed) {
  429. po.stim = now - (now - po.stim) * po.conf.speed / po.conf.new_speed
  430. po.conf.speed = po.conf.new_speed
  431. po.conf.new_speed = 0
  432. t = po.stim + s.ptim / po.conf.speed
  433. }
  434. maxt = t + po.tgen
  435. po.timouts = []
  436. while (1) {
  437. if (!po.p_v[s.v])
  438. set_ctrl(po, s, t)
  439. switch (s.type) {
  440. case C.BAR: s2 = null
  441. if (s.rep_p) {
  442. po.repv++
  443. if (!po.repn && (!s.rep_v || po.repv <= s.rep_v.length)) {
  444. s2 = s.rep_p
  445. po.repn = true
  446. } else {
  447. if (s.rep_v)
  448. s2 = var_end(s)
  449. po.repn = false
  450. }
  451. }
  452. if (s.rep_s) {
  453. s2 = s.rep_s[po.repv]
  454. if (s2) {
  455. po.repn = false
  456. if (s2 == s)
  457. s2 = null
  458. } else {
  459. s2 = var_end(s)
  460. if (s2 == po.s_end)
  461. break
  462. }
  463. }
  464. if (s.bar_type.slice(-1) == ':' && s.bar_type[0] != ':')
  465. po.repv = 1
  466. if (s2) {
  467. po.stim += (s.ptim - s2.ptim) / po.conf.speed
  468. s = s2
  469. while (s && !s.dur)
  470. s = s.ts_next
  471. if (!s)
  472. break
  473. t = po.stim + s.ptim / po.conf.speed
  474. break
  475. }
  476. if (!s.part1) {
  477. while (s.ts_next && !s.ts_next.seqst) {
  478. s = s.ts_next
  479. if (s.part1)
  480. break
  481. }
  482. if (!s.part1)
  483. break
  484. }
  485. default: if (s.part1 && po.i_p != undefined) {
  486. s2 = s.part1.p_s[++po.i_p]
  487. if (s2) {
  488. po.stim += (s.ptim - s2.ptim) / po.conf.speed
  489. s = s2
  490. t = po.stim + s.ptim / po.conf.speed
  491. } else { s = po.s_end }
  492. po.repv = 1
  493. }
  494. break
  495. }
  496. if (s && s != po.s_end) {
  497. switch (s.type) {
  498. case C.BAR: break
  499. case C.BLOCK: if (s.subtype == "midictl")
  500. po.midi_ctrl(po, s, t)
  501. else if (s.subtype == 'midiprog')
  502. po.midi_prog(po, s)
  503. break
  504. case C.GRACE: for (g = s.extra; g; g = g.next) {
  505. d = g.pdur / po.conf.speed
  506. for (m = 0; m <= g.nhd; m++) {
  507. note = g.notes[m]
  508. if (!note.noplay)
  509. po.note_run(po, g, note.midi, t + g.ptim - s.ptim, d)
  510. }
  511. }
  512. break
  513. case C.NOTE: case C.REST: d = s.pdur / po.conf.speed
  514. if (s.type == C.NOTE) {
  515. for (m = 0; m <= s.nhd; m++) {
  516. note = s.notes[m]
  517. if (note.tie_s || note.noplay)
  518. continue
  519. po.note_run(po, s, note.midi, t, note.tie_e ? do_tie(note, d) : d)
  520. }
  521. }
  522. if (po.onnote && s.istart) {
  523. i = s.istart
  524. st = (t - now) * 1000
  525. po.timouts.push(setTimeout(po.onnote, st, i, true))
  526. if (d > 2)
  527. d -= .1
  528. setTimeout(po.onnote, st + d * 1000, i, false)
  529. }
  530. break
  531. }
  532. }
  533. while (1) {
  534. if (!s || s == po.s_end || !s.ts_next) {
  535. if (po.onend)
  536. setTimeout(po.onend, (t - now + d) * 1000, po.repv)
  537. po.s_cur = s
  538. return
  539. }
  540. s = s.ts_next
  541. if (!s.noplay)
  542. break
  543. }
  544. t = po.stim + s.ptim / po.conf.speed
  545. if (t > maxt)
  546. break
  547. }
  548. po.s_cur = s
  549. po.timouts.push(setTimeout(play_cont, (t - now) * 1000
  550. - 300, po))
  551. }
  552. function get_part(po) {
  553. var s, i, s_p
  554. for (s = po.s_cur; s; s = s.ts_prev) {
  555. if (s.parts) {
  556. po.i_p = -1
  557. return
  558. }
  559. s_p = s.part1
  560. if (!s_p || !s_p.p_s)
  561. continue
  562. for (i = 0; i < s_p.p_s.length; i++) {
  563. if (s_p.p_s[i] == s) {
  564. po.i_p = i
  565. return
  566. }
  567. }
  568. }
  569. }
  570. get_part(po)
  571. po.stim = po.get_time(po) + .3
  572. - po.s_cur.ptim * po.conf.speed
  573. po.p_v = []
  574. if (!po.repv)
  575. po.repv = 1
  576. play_cont(po)
  577. }
  578. if (typeof module == 'object' && typeof exports == 'object')
  579. exports.ToAudio = ToAudio
  580. var abcsf2 = []
  581. function Audio5(i_conf) {
  582. var po, conf = i_conf, empty = function () { }, errmsg, ac, gain, model, parser, presets, instr = [], params = [], rates = [], w_instr = 0
  583. var b64d = []
  584. function init_b64d() {
  585. var b64l = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', l = b64l.length
  586. for (var i = 0; i < l; i++)
  587. b64d[b64l[i]] = i
  588. b64d['='] = 0
  589. }
  590. function b64dcod(s) {
  591. var i, t, dl, a, l = s.length, j = 0
  592. dl = l * 3 / 4
  593. if (s[l - 1] == '=') {
  594. if (s[l - 2] == '=')
  595. dl--
  596. dl--
  597. l -= 4
  598. }
  599. a = new Uint8Array(dl)
  600. for (i = 0; i < l; i += 4) {
  601. t = (b64d[s[i]] << 18) +
  602. (b64d[s[i + 1]] << 12) +
  603. (b64d[s[i + 2]] << 6) +
  604. b64d[s[i + 3]]
  605. a[j++] = (t >> 16) & 0xff
  606. a[j++] = (t >> 8) & 0xff
  607. a[j++] = t & 0xff
  608. }
  609. if (l != s.length) {
  610. t = (b64d[s[i]] << 18) +
  611. (b64d[s[i + 1]] << 12) +
  612. (b64d[s[i + 2]] << 6) +
  613. b64d[s[i + 3]]
  614. a[j++] = (t >> 16) & 0xff
  615. if (j < dl)
  616. a[j++] = (t >> 8) & 0xff
  617. }
  618. return a
  619. }
  620. function sample_cp(b, s) {
  621. var i, n, a = b.getChannelData(0)
  622. for (i = 0; i < s.length; i++)
  623. a[i] = s[i] / 196608
  624. }
  625. function sf2_create(instr, sf2par, sf2pre) {
  626. function get_instr(i) {
  627. var instrument = sf2par.instrument, zone = sf2par.instrumentZone, j = instrument[i].instrumentBagIndex, jl = instrument[i + 1] ? instrument[i + 1].instrumentBagIndex : zone.length, info = []
  628. while (j < jl) {
  629. instrumentGenerator = sf2par.createInstrumentGenerator_(zone, j)
  630. info.push({ generator: instrumentGenerator.generator, })
  631. j++
  632. }
  633. return { info: info }
  634. }
  635. var i, j, k, sid, gen, parm, gparm, sample, infos, sampleRate, scale, b = instr >> 7, p = instr % 128, pr = sf2pre
  636. rates[instr] = []
  637. for (i = 0; i < pr.length; i++) {
  638. gen = pr[i].header
  639. if (gen.preset == p && gen.bank == b)
  640. break
  641. }
  642. pr = pr[i]
  643. if (!pr) {
  644. errmsg('unknown instrument ' + b + ':' + p)
  645. return
  646. }
  647. pr = pr.info
  648. for (k = 0; k < pr.length; k++) {
  649. if (!pr[k].generator.instrument)
  650. continue
  651. gparm = null
  652. infos = get_instr(pr[k].generator.instrument.amount).info
  653. for (i = 0; i < infos.length; i++) {
  654. gen = infos[i].generator
  655. if (!gparm) { parm = gparm = { attack: .001, hold: .001, decay: .001, sustain: 0 } } else {
  656. parm = Object.create(gparm)
  657. if (!gen.sampleID)
  658. gparm = parm
  659. }
  660. if (gen.attackVolEnv)
  661. parm.attack = Math.pow(2, gen.attackVolEnv.amount / 1200)
  662. if (gen.holdVolEnv)
  663. parm.hold = Math.pow(2, gen.holdVolEnv.amount / 1200)
  664. if (gen.decayVolEnv)
  665. parm.decay = Math.pow(2, gen.decayVolEnv.amount / 1200) / 3
  666. if (gen.sustainVolEnv)
  667. parm.sustain = gen.sustainVolEnv.amount / 1000
  668. if (gen.sampleModes && gen.sampleModes.amount & 1)
  669. parm.sm = 1
  670. if (!gen.sampleID)
  671. continue
  672. sid = gen.sampleID.amount
  673. sampleRate = sf2par.sampleHeader[sid].sampleRate
  674. sample = sf2par.sample[sid]
  675. parm.buffer = ac.createBuffer(1, sample.length, sampleRate)
  676. parm.hold += parm.attack
  677. parm.decay += parm.hold
  678. if (parm.sustain >= .4)
  679. parm.sustain = 0.01
  680. else
  681. parm.sustain = 1 - parm.sustain / .4
  682. sample_cp(parm.buffer, sample)
  683. if (parm.sm) {
  684. parm.loopStart = sf2par.sampleHeader[sid].startLoop / sampleRate
  685. parm.loopEnd = sf2par.sampleHeader[sid].endLoop / sampleRate
  686. }
  687. scale = (gen.scaleTuning ? gen.scaleTuning.amount : 100) / 100, tune = (gen.coarseTune ? gen.coarseTune.amount : 0) +
  688. (gen.fineTune ? gen.fineTune.amount : 0) / 100 +
  689. sf2par.sampleHeader[sid].pitchCorrection / 100 -
  690. (gen.overridingRootKey ? gen.overridingRootKey.amount : sf2par.sampleHeader[sid].originalPitch)
  691. for (j = gen.keyRange.lo; j <= gen.keyRange.hi; j++) {
  692. rates[instr][j] = Math.pow(Math.pow(2, 1 / 12), (j + tune) * scale)
  693. params[instr][j] = parm
  694. }
  695. }
  696. }
  697. }
  698. function load_instr(instr) {
  699. w_instr++
  700. abc2svg.loadjs(conf.sfu + '/' + instr + '.js', function () {
  701. var sf2par = new sf2.Parser(b64dcod(abcsf2[instr]))
  702. sf2par.parse()
  703. var sf2pre = sf2par.getPresets()
  704. sf2_create(instr, sf2par, sf2pre)
  705. if (--w_instr == 0)
  706. play_start()
  707. }, function () {
  708. errmsg('could not find the instrument ' +
  709. ((instr / 128) | 0).toString() + '-' +
  710. (instr % 128).toString())
  711. if (--w_instr == 0)
  712. play_start()
  713. })
  714. }
  715. function def_instr(s, f, sf2par, sf2pre) {
  716. var i, bk = [], nv = -1, vb = 0
  717. s = s.p_v.sym
  718. while (s.ts_prev)
  719. s = s.ts_prev
  720. for (; s; s = s.ts_next) {
  721. if (s.v > nv) {
  722. nv = s.v
  723. bk[nv] = 0
  724. if (s.p_v.midictl) {
  725. if (s.p_v.midictl[0])
  726. bk[s.v] = (bk[s.v] & ~0x1fc000)
  727. + (s.p_v.midictl[0] << 14)
  728. if (s.p_v.midictl[32])
  729. bk[s.v] = (bk[s.v] & ~0x3f80)
  730. + (s.p_v.midictl[32] << 7)
  731. }
  732. }
  733. switch (s.subtype) {
  734. case "midiprog": break
  735. case "midictl": if (s.ctrl != 0 && s.ctrl != 32)
  736. continue
  737. if (bk[s.v] == undefined)
  738. bk[s.v] = 0
  739. if (s.ctrl == 0)
  740. bk[s.v] = (bk[s.v] & ~0x1fc000)
  741. + (s.val << 14)
  742. else
  743. bk[s.v] = (bk[s.v] & ~0x3f80)
  744. + (s.val << 7)
  745. default: continue
  746. }
  747. vb |= 1 << s.v
  748. i = s.instr
  749. if (i == undefined) {
  750. if (s.chn != 9)
  751. continue
  752. i = bk[s.v] ? 0 : 128 * 128
  753. }
  754. if (bk[s.v])
  755. i += bk[s.v]
  756. if (!params[i]) {
  757. params[i] = []
  758. f(i, sf2par, sf2pre)
  759. }
  760. }
  761. nv = (2 << nv) - 1
  762. if (nv != vb && !params[0]) {
  763. params[0] = []
  764. f(0, sf2par, sf2pre)
  765. }
  766. }
  767. function load_res(s) {
  768. if (abc2svg.sf2 || conf.sfu.slice(-4) == ".sf2" || conf.sfu.slice(-3) == ".js") {
  769. if (abc2svg.sf2) {
  770. if (!parser) {
  771. parser = new sf2.Parser(b64dcod(abc2svg.sf2))
  772. parser.parse()
  773. presets = parser.getPresets()
  774. }
  775. } else if (!parser) {
  776. w_instr++
  777. if (conf.sfu.slice(-3) == ".js") {
  778. abc2svg.loadjs(conf.sfu, function () {
  779. load_res(s)
  780. if (--w_instr == 0)
  781. play_start()
  782. }, function () {
  783. errmsg('could not load the sound file '
  784. + conf.sfu)
  785. if (--w_instr == 0)
  786. play_start()
  787. })
  788. return
  789. }
  790. var r = new XMLHttpRequest()
  791. r.open('GET', conf.sfu, true)
  792. r.responseType = "arraybuffer"
  793. r.onload = function () {
  794. if (r.status === 200) {
  795. parser = new sf2.Parser(new Uint8Array(r.response))
  796. parser.parse()
  797. presets = parser.getPresets()
  798. load_res(s)
  799. if (--w_instr == 0)
  800. play_start()
  801. } else {
  802. errmsg('could not load the sound file '
  803. + conf.sfu)
  804. if (--w_instr == 0)
  805. play_start()
  806. }
  807. }
  808. r.onerror = function () {
  809. errmsg('could not load the sound file '
  810. + conf.sfu)
  811. if (--w_instr == 0)
  812. play_start()
  813. }
  814. r.send()
  815. return
  816. }
  817. def_instr(s, sf2_create, parser, presets)
  818. } else { def_instr(s, load_instr) }
  819. }
  820. function get_time(po) { return po.ac.currentTime }
  821. function midi_ctrl(po, s, t) {
  822. switch (s.ctrl) {
  823. case 0: if (po.v_b[s.v] == undefined)
  824. po.v_b[s.v] = 0
  825. po.v_b[s.v] = (po.v_b[s.v] & ~0x1fc000)
  826. + (s.val << 14)
  827. break
  828. case 7: s.p_v.vol = s.val / 127
  829. break
  830. case 32: if (po.v_b[s.v] == undefined)
  831. po.v_b[s.v] = 0
  832. po.v_b[s.v] = (po.v_b[s.v] & ~0x3f80)
  833. + (s.val << 7)
  834. break
  835. }
  836. }
  837. function midi_prog(po, s) {
  838. var i = s.instr
  839. po.v_c[s.v] = s.chn
  840. if (i == undefined) {
  841. if (s.chn != 9)
  842. return
  843. i = po.v_b[s.v] ? 0 : 128 * 128
  844. }
  845. if (po.v_b[s.v])
  846. i += po.v_b[s.v]
  847. po.c_i[s.chn] = i
  848. }
  849. function note_run(po, s, key, t, d) {
  850. var g, st, c = po.v_c[s.v], instr = po.c_i[c], k = key | 0, parm = params[instr][k], o = po.ac.createBufferSource(), v = s.p_v.vol == undefined ? 1 : s.p_v.vol
  851. if (!v || !parm)
  852. return
  853. o.buffer = parm.buffer
  854. if (parm.loopStart) {
  855. o.loop = true
  856. o.loopStart = parm.loopStart
  857. o.loopEnd = parm.loopEnd
  858. }
  859. if (o.detune) {
  860. var dt = (key * 100) % 100
  861. if (dt)
  862. o.detune.value = dt
  863. }
  864. o.playbackRate.value = po.rates[instr][k]
  865. g = po.ac.createGain()
  866. if (parm.hold < 0.002) { g.gain.setValueAtTime(v, t) } else {
  867. if (parm.attack < 0.002) { g.gain.setValueAtTime(v, t) } else {
  868. g.gain.setValueAtTime(0, t)
  869. g.gain.linearRampToValueAtTime(v, t + parm.attack)
  870. }
  871. g.gain.setValueAtTime(v, t + parm.hold)
  872. }
  873. g.gain.exponentialRampToValueAtTime(parm.sustain * v, t + parm.decay)
  874. o.connect(g)
  875. g.connect(po.gain)
  876. o.start(t)
  877. o.stop(t + d)
  878. }
  879. function play_start() {
  880. if (po.stop) {
  881. po.onend(repv)
  882. return
  883. }
  884. gain.connect(ac.destination)
  885. abc2svg.play_next(po)
  886. }
  887. init_b64d()
  888. if (!conf.sfu)
  889. conf.sfu = "Scc1t2"
  890. if (navigator.userAgentData && navigator.userAgentData.getHighEntropyValues)
  891. navigator.userAgentData.getHighEntropyValues(['model']).then(function (ua) { model = ua.model })
  892. else
  893. model = navigator.userAgent
  894. return {
  895. get_outputs: function () { return (window.AudioContext || window.webkitAudioContext) ? ['sf2'] : null }, play: function (i_start, i_end, i_lvl) {
  896. errmsg = conf.errmsg || alert
  897. function play_unlock() {
  898. var buf = ac.createBuffer(1, 1, 22050), src = ac.createBufferSource()
  899. src.buffer = buf
  900. src.connect(ac.destination)
  901. src.start(0)
  902. }
  903. if (!gain) {
  904. ac = conf.ac
  905. if (!ac) {
  906. conf.ac = ac = new (window.AudioContext || window.webkitAudioContext)
  907. if (/iPad|iPhone|iPod/.test(model))
  908. play_unlock()
  909. }
  910. gain = ac.createGain()
  911. gain.gain.value = conf.gain
  912. }
  913. while (i_start.noplay)
  914. i_start = i_start.ts_next
  915. po = { conf: conf, onend: conf.onend || empty, onnote: conf.onnote || empty, s_end: i_end, s_cur: i_start, repv: i_lvl || 0, tgen: 2, get_time: get_time, midi_ctrl: midi_ctrl, midi_prog: midi_prog, note_run: note_run, timouts: [], v_c: [], c_i: [], v_b: [], ac: ac, gain: gain, rates: rates }
  916. w_instr++
  917. load_res(i_start)
  918. if (--w_instr == 0)
  919. play_start()
  920. }, stop: function () {
  921. po.stop = true
  922. po.timouts.forEach(function (id) { clearTimeout(id) })
  923. abc2svg.play_next(po)
  924. if (gain) {
  925. gain.disconnect()
  926. gain = null
  927. }
  928. }, set_vol: function (v) {
  929. if (gain)
  930. gain.gain.value = v
  931. }
  932. }
  933. }
  934. (function (root, factory) { if (typeof exports === "object") { root.sf2 = exports; factory(exports) } else if (typeof define === "function" && define.amd) { define(["exports"], function (exports) { root.sf2 = exports; return (root.sf2, factory(exports)) }) } else { root.sf2 = {}; factory(root.sf2) } }(this, function (sf2) {
  935. "use strict"; sf2.Parser = function (input, options) { options = options || {}; this.input = input; this.parserOptions = options.parserOptions }; sf2.Parser.prototype.parse = function () {
  936. var parser = new sf2.Riff.Parser(this.input, this.parserOptions), chunk; parser.parse(); if (parser.chunkList.length !== 1)
  937. throw new Error('wrong chunk length'); chunk = parser.getChunk(0); if (chunk === null)
  938. throw new Error('chunk not found'); this.parseRiffChunk(chunk); this.input = null
  939. }; sf2.Parser.prototype.parseRiffChunk = function (chunk) {
  940. var parser, data = this.input, ip = chunk.offset, signature; if (chunk.type !== 'RIFF')
  941. throw new Error('invalid chunk type:' + chunk.type); signature = String.fromCharCode(data[ip++], data[ip++], data[ip++], data[ip++]); if (signature !== 'sfbk')
  942. throw new Error('invalid signature:' + signature); parser = new sf2.Riff.Parser(data, { 'index': ip, 'length': chunk.size - 4 }); parser.parse(); if (parser.getNumberOfChunks() !== 3)
  943. throw new Error('invalid sfbk structure'); this.parseInfoList(parser.getChunk(0)); this.parseSdtaList(parser.getChunk(1)); this.parsePdtaList(parser.getChunk(2))
  944. }; sf2.Parser.prototype.parseInfoList = function (chunk) {
  945. var parser, data = this.input, ip = chunk.offset, signature; if (chunk.type !== 'LIST')
  946. throw new Error('invalid chunk type:' + chunk.type); signature = String.fromCharCode(data[ip++], data[ip++], data[ip++], data[ip++]); if (signature !== 'INFO')
  947. throw new Error('invalid signature:' + signature); parser = new sf2.Riff.Parser(data, { 'index': ip, 'length': chunk.size - 4 }); parser.parse()
  948. }; sf2.Parser.prototype.parseSdtaList = function (chunk) {
  949. var parser, data = this.input, ip = chunk.offset, signature; if (chunk.type !== 'LIST')
  950. throw new Error('invalid chunk type:' + chunk.type); signature = String.fromCharCode(data[ip++], data[ip++], data[ip++], data[ip++]); if (signature !== 'sdta')
  951. throw new Error('invalid signature:' + signature); parser = new sf2.Riff.Parser(data, { 'index': ip, 'length': chunk.size - 4 }); parser.parse(); if (parser.chunkList.length !== 1)
  952. throw new Error('TODO'); this.samplingData = parser.getChunk(0)
  953. }; sf2.Parser.prototype.parsePdtaList = function (chunk) {
  954. var parser, data = this.input, ip = chunk.offset, signature; if (chunk.type !== 'LIST')
  955. throw new Error('invalid chunk type:' + chunk.type); signature = String.fromCharCode(data[ip++], data[ip++], data[ip++], data[ip++]); if (signature !== 'pdta')
  956. throw new Error('invalid signature:' + signature); parser = new sf2.Riff.Parser(data, { 'index': ip, 'length': chunk.size - 4 }); parser.parse(); if (parser.getNumberOfChunks() !== 9)
  957. throw new Error('invalid pdta chunk'); this.parsePhdr((parser.getChunk(0))); this.parsePbag((parser.getChunk(1))); this.parsePmod((parser.getChunk(2))); this.parsePgen((parser.getChunk(3))); this.parseInst((parser.getChunk(4))); this.parseIbag((parser.getChunk(5))); this.parseImod((parser.getChunk(6))); this.parseIgen((parser.getChunk(7))); this.parseShdr((parser.getChunk(8)))
  958. }; sf2.Parser.prototype.parsePhdr = function (chunk) {
  959. var data = this.input, ip = chunk.offset, presetHeader = this.presetHeader = [], size = chunk.offset + chunk.size; if (chunk.type !== 'phdr')
  960. throw new Error('invalid chunk type:' + chunk.type); while (ip < size) { presetHeader.push({ presetName: String.fromCharCode.apply(null, data.subarray(ip, ip += 20)), preset: data[ip++] | (data[ip++] << 8), bank: data[ip++] | (data[ip++] << 8), presetBagIndex: data[ip++] | (data[ip++] << 8), library: (data[ip++] | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24)) >>> 0, genre: (data[ip++] | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24)) >>> 0, morphology: (data[ip++] | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24)) >>> 0 }) }
  961. }; sf2.Parser.prototype.parsePbag = function (chunk) {
  962. var data = this.input, ip = chunk.offset, presetZone = this.presetZone = [], size = chunk.offset + chunk.size; if (chunk.type !== 'pbag')
  963. throw new Error('invalid chunk type:' + chunk.type); while (ip < size) { presetZone.push({ presetGeneratorIndex: data[ip++] | (data[ip++] << 8), presetModulatorIndex: data[ip++] | (data[ip++] << 8) }) }
  964. }; sf2.Parser.prototype.parsePmod = function (chunk) {
  965. if (chunk.type !== 'pmod')
  966. throw new Error('invalid chunk type:' + chunk.type); this.presetZoneModulator = this.parseModulator(chunk)
  967. }; sf2.Parser.prototype.parsePgen = function (chunk) {
  968. if (chunk.type !== 'pgen')
  969. throw new Error('invalid chunk type:' + chunk.type); this.presetZoneGenerator = this.parseGenerator(chunk)
  970. }; sf2.Parser.prototype.parseInst = function (chunk) {
  971. var data = this.input, ip = chunk.offset, instrument = this.instrument = [], size = chunk.offset + chunk.size; if (chunk.type !== 'inst')
  972. throw new Error('invalid chunk type:' + chunk.type); while (ip < size) { instrument.push({ instrumentName: String.fromCharCode.apply(null, data.subarray(ip, ip += 20)), instrumentBagIndex: data[ip++] | (data[ip++] << 8) }) }
  973. }; sf2.Parser.prototype.parseIbag = function (chunk) {
  974. var data = this.input, ip = chunk.offset, instrumentZone = this.instrumentZone = [], size = chunk.offset + chunk.size; if (chunk.type !== 'ibag')
  975. throw new Error('invalid chunk type:' + chunk.type); while (ip < size) { instrumentZone.push({ instrumentGeneratorIndex: data[ip++] | (data[ip++] << 8), instrumentModulatorIndex: data[ip++] | (data[ip++] << 8) }) }
  976. }; sf2.Parser.prototype.parseImod = function (chunk) {
  977. if (chunk.type !== 'imod')
  978. throw new Error('invalid chunk type:' + chunk.type); this.instrumentZoneModulator = this.parseModulator(chunk)
  979. }; sf2.Parser.prototype.parseIgen = function (chunk) {
  980. if (chunk.type !== 'igen')
  981. throw new Error('invalid chunk type:' + chunk.type); this.instrumentZoneGenerator = this.parseGenerator(chunk)
  982. }; sf2.Parser.prototype.parseShdr = function (chunk) {
  983. var data = this.input, ip = chunk.offset, samples = this.sample = [], sampleHeader = this.sampleHeader = [], size = chunk.offset + chunk.size, sampleName, start, end, startLoop, endLoop, sampleRate, originalPitch, pitchCorrection, sampleLink, sampleType; if (chunk.type !== 'shdr')
  984. throw new Error('invalid chunk type:' + chunk.type); while (ip < size) {
  985. sampleName = String.fromCharCode.apply(null, data.subarray(ip, ip += 20)); start = (data[ip++] << 0) | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24); end = (data[ip++] << 0) | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24); startLoop = (data[ip++] << 0) | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24); endLoop = (data[ip++] << 0) | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24); sampleRate = (data[ip++] << 0) | (data[ip++] << 8) | (data[ip++] << 16) | (data[ip++] << 24); originalPitch = data[ip++]; pitchCorrection = (data[ip++] << 24) >> 24; sampleLink = data[ip++] | (data[ip++] << 8); sampleType = data[ip++] | (data[ip++] << 8); var sample = new Int16Array(new Uint8Array(data.subarray(this.samplingData.offset + start * 2, this.samplingData.offset + end * 2)).buffer); startLoop -= start; endLoop -= start; if (sampleRate > 0) { var adjust = this.adjustSampleData(sample, sampleRate); sample = adjust.sample; sampleRate *= adjust.multiply; startLoop *= adjust.multiply; endLoop *= adjust.multiply }
  986. samples.push(sample); sampleHeader.push({ sampleName: sampleName, startLoop: startLoop, endLoop: endLoop, sampleRate: sampleRate, originalPitch: originalPitch, pitchCorrection: pitchCorrection, sampleLink: sampleLink, sampleType: sampleType })
  987. }
  988. }; sf2.Parser.prototype.adjustSampleData = function (sample, sampleRate) {
  989. var newSample, i, il, j, multiply = 1; while (sampleRate < 22050) {
  990. newSample = new Int16Array(sample.length * 2); for (i = j = 0, il = sample.length; i < il; ++i) { newSample[j++] = sample[i]; newSample[j++] = sample[i] }
  991. sample = newSample; multiply *= 2; sampleRate *= 2
  992. }
  993. return { sample: sample, multiply: multiply }
  994. }; sf2.Parser.prototype.parseModulator = function (chunk) {
  995. var data = this.input, ip = chunk.offset, size = chunk.offset + chunk.size, code, key, output = []; while (ip < size) {
  996. ip += 2; code = data[ip++] | (data[ip++] << 8); key = sf2.Parser.GeneratorEnumeratorTable[code]; if (key === undefined) { output.push({ type: key, value: { code: code, amount: data[ip] | (data[ip + 1] << 8) << 16 >> 16, lo: data[ip++], hi: data[ip++] } }) } else { switch (key) { case 'keyRange': case 'velRange': case 'keynum': case 'velocity': output.push({ type: key, value: { lo: data[ip++], hi: data[ip++] } }); break; default: output.push({ type: key, value: { amount: data[ip++] | (data[ip++] << 8) << 16 >> 16 } }); break } }
  997. ip += 2; ip += 2
  998. }
  999. return output
  1000. }; sf2.Parser.prototype.parseGenerator = function (chunk) {
  1001. var data = this.input, ip = chunk.offset, size = chunk.offset + chunk.size, code, key, output = []; while (ip < size) {
  1002. code = data[ip++] | (data[ip++] << 8); key = sf2.Parser.GeneratorEnumeratorTable[code]; if (key === undefined) { output.push({ type: key, value: { code: code, amount: data[ip] | (data[ip + 1] << 8) << 16 >> 16, lo: data[ip++], hi: data[ip++] } }); continue }
  1003. switch (key) { case 'keynum': case 'keyRange': case 'velRange': case 'velocity': output.push({ type: key, value: { lo: data[ip++], hi: data[ip++] } }); break; default: output.push({ type: key, value: { amount: data[ip++] | (data[ip++] << 8) << 16 >> 16 } }); break }
  1004. }
  1005. return output
  1006. }; sf2.Parser.prototype.getPresets = function () {
  1007. var preset = this.presetHeader, zone = this.presetZone, output = [], bagIndex, bagIndexEnd, zoneInfo, presetGenerator, presetModulator, i, il, j, jl
  1008. for (i = 0, il = preset.length; i < il; ++i) {
  1009. j = preset[i].presetBagIndex
  1010. jl = preset[i + 1] ? preset[i + 1].presetBagIndex : zone.length
  1011. zoneInfo = []; for (; j < jl; ++j) { presetGenerator = this.createPresetGenerator_(zone, j); presetModulator = this.createPresetModulator_(zone, j); zoneInfo.push({ generator: presetGenerator.generator, modulator: presetModulator.modulator, }) }
  1012. output.push({ info: zoneInfo, header: preset[i], })
  1013. }
  1014. return output
  1015. }; sf2.Parser.prototype.createInstrumentGenerator_ = function (zone, index) { var modgen = this.createBagModGen_(zone, zone[index].instrumentGeneratorIndex, zone[index + 1] ? zone[index + 1].instrumentGeneratorIndex : this.instrumentZoneGenerator.length, this.instrumentZoneGenerator); return { generator: modgen.modgen, } }; sf2.Parser.prototype.createInstrumentModulator_ = function (zone, index) { var modgen = this.createBagModGen_(zone, zone[index].presetModulatorIndex, zone[index + 1] ? zone[index + 1].instrumentModulatorIndex : this.instrumentZoneModulator.length, this.instrumentZoneModulator); return { modulator: modgen.modgen } }; sf2.Parser.prototype.createPresetGenerator_ = function (zone, index) { var modgen = this.createBagModGen_(zone, zone[index].presetGeneratorIndex, zone[index + 1] ? zone[index + 1].presetGeneratorIndex : this.presetZoneGenerator.length, this.presetZoneGenerator); return { generator: modgen.modgen, } }; sf2.Parser.prototype.createPresetModulator_ = function (zone, index) { var modgen = this.createBagModGen_(zone, zone[index].presetModulatorIndex, zone[index + 1] ? zone[index + 1].presetModulatorIndex : this.presetZoneModulator.length, this.presetZoneModulator); return { modulator: modgen.modgen, } }; sf2.Parser.prototype.createBagModGen_ = function (zone, indexStart, indexEnd, zoneModGen) {
  1016. var modgen = { unknown: [], 'keyRange': { hi: 127, lo: 0 } }; var info, i, il; for (i = indexStart, il = indexEnd; i < il; ++i) {
  1017. info = zoneModGen[i]; if (info.type === 'unknown')
  1018. modgen.unknown.push(info.value); else
  1019. modgen[info.type] = info.value
  1020. }
  1021. return { modgen: modgen }
  1022. }; sf2.Parser.GeneratorEnumeratorTable = ['startAddrsOffset', 'endAddrsOffset', 'startloopAddrsOffset', 'endloopAddrsOffset', 'startAddrsCoarseOffset', 'modLfoToPitch', 'vibLfoToPitch', 'modEnvToPitch', 'initialFilterFc', 'initialFilterQ', 'modLfoToFilterFc', 'modEnvToFilterFc', 'endAddrsCoarseOffset', 'modLfoToVolume', undefined, 'chorusEffectsSend', 'reverbEffectsSend', 'pan', undefined, undefined, undefined, 'delayModLFO', 'freqModLFO', 'delayVibLFO', 'freqVibLFO', 'delayModEnv', 'attackModEnv', 'holdModEnv', 'decayModEnv', 'sustainModEnv', 'releaseModEnv', 'keynumToModEnvHold', 'keynumToModEnvDecay', 'delayVolEnv', 'attackVolEnv', 'holdVolEnv', 'decayVolEnv', 'sustainVolEnv', 'releaseVolEnv', 'keynumToVolEnvHold', 'keynumToVolEnvDecay', 'instrument', undefined, 'keyRange', 'velRange', 'startloopAddrsCoarseOffset', 'keynum', 'velocity', 'initialAttenuation', undefined, 'endloopAddrsCoarseOffset', 'coarseTune', 'fineTune', 'sampleID', 'sampleModes', undefined, 'scaleTuning', 'exclusiveClass', 'overridingRootKey']; sf2.Riff = {}; sf2.Riff.Parser = function (input, options) { options = options || {}; this.input = input; this.ip = options.index || 0; this.length = options.length || input.length - this.ip; this.offset = this.ip; this.padding = options.padding !== undefined ? options.padding : true; this.bigEndian = options.bigEndian !== undefined ? options.bigEndian : false }; sf2.Riff.Chunk = function (type, size, offset) { this.type = type; this.size = size; this.offset = offset }; sf2.Riff.Parser.prototype.parse = function () {
  1023. var length = this.length + this.offset; this.chunkList = []; while (this.ip < length)
  1024. this.parseChunk()
  1025. }; sf2.Riff.Parser.prototype.parseChunk = function () {
  1026. var input = this.input, ip = this.ip, size; this.chunkList.push(new sf2.Riff.Chunk(String.fromCharCode(input[ip++], input[ip++], input[ip++], input[ip++]), (size = this.bigEndian ? ((input[ip++] << 24) | (input[ip++] << 16) | (input[ip++] << 8) | (input[ip++])) : ((input[ip++]) | (input[ip++] << 8) | (input[ip++] << 16) | (input[ip++] << 24))), ip)); ip += size; if (this.padding && ((ip - this.offset) & 1) === 1)
  1027. ip++; this.ip = ip
  1028. }; sf2.Riff.Parser.prototype.getChunk = function (index) {
  1029. var chunk = this.chunkList[index]; if (chunk === undefined)
  1030. return null; return chunk
  1031. }; sf2.Riff.Parser.prototype.getNumberOfChunks = function () { return this.chunkList.length }; return sf2
  1032. })); function Midi5(i_conf) {
  1033. var po, conf = i_conf, empty = function () { }, rf, op
  1034. function get_time(po) { return window.performance.now() / 1000 }
  1035. function note_run(po, s, k, t, d) {
  1036. var j, a = (k * 100) % 100, c = po.v_c[s.v], i = po.c_i[c]
  1037. k |= 0
  1038. t *= 1000
  1039. d *= 1000
  1040. if (a && Midi5.ma.sysexEnabled) { po.op.send(new Uint8Array([0xf0, 0x7f, 0x7f, 0x08, 0x02, i & 0x7f, 0x01, k, k, a / .78125, 0, 0xf7]), t) }
  1041. po.op.send(new Uint8Array([0x90 + c, k, 127]), t)
  1042. po.op.send(new Uint8Array([0x80 + c, k, 0]), t + d - 20)
  1043. }
  1044. function midi_ctrl(po, s, t) { po.op.send(new Uint8Array([0xb0 + po.v_c[s.v], s.ctrl, s.val]), t * 1000) }
  1045. function midi_prog(po, s) {
  1046. var i, c = s.chn
  1047. po.v_c[s.v] = c
  1048. if (po.c_i[c] == undefined) {
  1049. po.op.send(new Uint8Array([0xb0 + c, 121, 0]))
  1050. if (0) {
  1051. if (s.p_v.midictl) {
  1052. for (i in s.p_v.midictl)
  1053. po.op.send(new Uint8Array([0xb0 + c, i, s.p_v.midictl[i]]))
  1054. }
  1055. }
  1056. }
  1057. i = s.instr
  1058. if (i != undefined) {
  1059. po.c_i[c] = i
  1060. po.op.send(new Uint8Array([0xc0 + c, i & 0x7f]))
  1061. }
  1062. }
  1063. function send_outputs(access) {
  1064. var o, os, out = []
  1065. Midi5.ma = access
  1066. if (access && access.outputs.size > 0) {
  1067. os = access.outputs.values()
  1068. while (1) {
  1069. o = os.next()
  1070. if (!o || o.done)
  1071. break
  1072. out.push(o.value.name)
  1073. }
  1074. }
  1075. rf(out)
  1076. }
  1077. return {
  1078. get_outputs: function (f) {
  1079. if (!navigator.requestMIDIAccess) {
  1080. f()
  1081. return
  1082. }
  1083. rf = f
  1084. navigator.requestMIDIAccess({ sysex: true }).then(send_outputs, function (msg) { navigator.requestMIDIAccess().then(send_outputs, function (msg) { rf() }) })
  1085. }, set_output: function (name) {
  1086. if (!Midi5.ma)
  1087. return
  1088. var o, os = Midi5.ma.outputs.values()
  1089. while (1) {
  1090. o = os.next()
  1091. if (!o || o.done)
  1092. break
  1093. if (o.value.name == name) {
  1094. op = o.value
  1095. break
  1096. }
  1097. }
  1098. }, play: function (i_start, i_end, i_lvl) {
  1099. po = { conf: conf, onend: conf.onend || empty, onnote: conf.onnote || empty, s_end: i_end, s_cur: i_start, repv: i_lvl || 0, tgen: 2, get_time: get_time, midi_ctrl: midi_ctrl, midi_prog: midi_prog, note_run: note_run, timouts: [], op: op, v_c: [], c_i: [] }
  1100. if (0) { op.send(new Uint8Array([0xf0, 0x7f, 0x7f, 0x08, 0x02, 0x00, 0x01, 0x69, 0x69, 0x00, 0, 0xf7]), t) }
  1101. abc2svg.play_next(po)
  1102. }, stop: function () {
  1103. po.stop = true
  1104. po.timouts.forEach(function (id) { clearTimeout(id) })
  1105. abc2svg.play_next(po)
  1106. if (op && op.clear)
  1107. op.clear()
  1108. }
  1109. }
  1110. }
  1111. function follow(abc, user, playconf) {
  1112. var keep_types = { note: true, rest: true }
  1113. user.anno_stop = function (type, start, stop, x, y, w, h) {
  1114. if (!keep_types[type])
  1115. return
  1116. abc.out_svg('<rect class="abcr _' + start + '_" x="'); abc.out_sxsy(x, '" y="', y); abc.out_svg('" width="' + w.toFixed(2) + '" height="' + abc.sh(h).toFixed(2) + '"/>\n')
  1117. }
  1118. playconf.onnote = function (i, on) {
  1119. var b, i, e, elts, x = 0, y = 0
  1120. if (abc2svg.mu)
  1121. elts = abc2svg.mu.d.getElementsByClassName('_' + i + '_')
  1122. else
  1123. elts = document.getElementsByClassName('_' + i + '_')
  1124. if (!elts || !elts.length)
  1125. return
  1126. e = elts[0]
  1127. e.style.fillOpacity = on ? 0.4 : 0
  1128. if (on && !window.no_scroll) {
  1129. b = e.getBoundingClientRect()
  1130. if (b.top < 0 || b.bottom > window.innerHeight * .8)
  1131. y = b.top - window.innerHeight * .3
  1132. if (b.left < 0 || b.right > window.innerWidth * .8)
  1133. x = b.left - window.innerWidth * .3
  1134. if (x || y)
  1135. window.scrollBy({ top: y, left: x, behavior: (x < 0 || y) ? 'instant' : 'smooth' })
  1136. }
  1137. }
  1138. }
  1139. (function () {
  1140. var sty = document.createElement("style")
  1141. sty.innerHTML = ".abcr {fill: #d00000; fill-opacity: 0; z-index: 15}"
  1142. document.head.appendChild(sty)
  1143. })()
  1144. abc2svg.ch_names = { '': ["C-E G C+", "E-C G C+", "G-C E G "], m: ["C-e G C+", "e-C G C+", "G-C e G "], '7': ["C-b-E G ", "E-C G b ", "G-E b C+", "b-E G C+"], m7: ["C-b-e G ", "e-C G b ", "G-e b C+", "b-e G C+"], m7b5: ["C-b-e g ", "e-C g b ", "g-e b C+", "b-e g C+"], M7: ["C-B-E G ", "E-C G B ", "G-E B C+", "B-E G C+"], '6': ["C-A-E G ", "E-C A B ", "A-E B C+", "B-E A C+"], m6: ["C-A-e G ", "e-C A B ", "A-e B C+", "B-e A C+"], aug: ["C-E a C+", "E-C a C+", "a-C E a "], aug7: ["C-b-E a ", "E-C a b ", "a-E b C+", "b-E a C+"], dim: ["C-E g C+", "E-C g C+", "g-C E g "], dim7: ["C-e g A ", "e-C g A ", "g-e A C+", "A-C e G "], '9': ["C-b-E G D+", "E-C G b D+", "G-E b C+D+", "b-E G C+D+", "D-G-C E b "], m9: ["C-b-e G D+", "e-C G b D+", "G-e b C+D+", "b-e G C+D+", "D-G-C e b "], maj9: ["C-B-E G D+", "E-C G B D+", "G-E B C+D+", "B-E G C+D+", "D-G-C E B "], M9: ["C-B-E G D+", "E-C G B D+", "G-C E B D+", "B-E G C+D+", "D-G-C E B "], '11': ["C-b-E G D+F+", "E-C G b D+F+", "G-E b C+D+F+", "b-E G C+D+F+", "D-G-C E b F+", "F-D-G-C E b D+"], dim9: ["C-A-e g d+", "e-C g A d+", "g-C e A d+", "A-C e g d+", "D-g-C e A "], sus4: ["C-F G C+", "F-C G C+", "G-C F G "], sus9: ["C-D G C+", "D-C G C+", "G-C D G "], '7sus4': ["C-b-F G ", "F-C G b ", "G-F b C+", "b-C F G "], '7sus9': ["C-b-D G ", "D-C G b ", "G-D b C+", "b-C D G "], '5': ["C-G C+", "G-G C+"] }
  1145. abc2svg.midlet = "CdDeEFgGaAbB"
  1146. abc2svg.letmid = { C: 0, d: 1, D: 2, e: 3, E: 4, F: 5, g: 6, G: 7, a: 8, A: 9, b: 10, B: 11 }
  1147. abc2svg.chord = function (first, voice_tb, cfmt) {
  1148. var chnm, i, k, vch, s, gchon, C = abc2svg.C, trans = 48 + (cfmt.chord.trans ? cfmt.chord.trans * 12 : 0)
  1149. function chcr(b, ch) {
  1150. var i, v, r = []
  1151. b = abc2svg.midlet[b]
  1152. i = ch.length
  1153. while (--i > 0) {
  1154. if (ch[i][0] == b)
  1155. break
  1156. }
  1157. ch = ch[i]
  1158. for (i = 0; i < ch.length; i += 2) {
  1159. v = abc2svg.letmid[ch[i]]
  1160. switch (ch[i + 1]) {
  1161. case '+': v += 12; break
  1162. case '-': v -= 12; break
  1163. }
  1164. r.push(v)
  1165. }
  1166. return r
  1167. }
  1168. function filter(a_cs) {
  1169. var i, cs, t
  1170. for (i = 0; i < a_cs.length; i++) {
  1171. cs = a_cs[i]
  1172. if (cs.type != 'g')
  1173. continue
  1174. t = cs.otext
  1175. if (t.slice(-1) == ')')
  1176. t = t.replace(/\(.*/, '')
  1177. return t.replace(/\(|\)|\[|\]/g, '')
  1178. }
  1179. }
  1180. function gench(sb) {
  1181. var r, ch, b, m, n, not, a = filter(sb.a_gch), s = { v: vch.v, p_v: vch, type: C.NOTE, time: sb.time, notes: [] }
  1182. if (!a)
  1183. return
  1184. a = a.match(/([A-GN])([#♯b♭]?)([^/]*)\/?(.*)/)
  1185. if (!a)
  1186. return
  1187. r = abc2svg.letmid[a[1]]
  1188. if (r == undefined) {
  1189. if (a[1] != "N")
  1190. return
  1191. s.type = C.REST
  1192. ch = [0]
  1193. r = 0
  1194. } else {
  1195. switch (a[2]) {
  1196. case "#": case "♯": r++; break
  1197. case "b": case "♭": r--; break
  1198. }
  1199. if (!a[3]) { ch = chnm[""] } else {
  1200. ch = abc2svg.ch_alias[a[3]]
  1201. if (ch == undefined)
  1202. ch = a[3]
  1203. ch = chnm[ch]
  1204. if (!ch)
  1205. ch = a[3][0] == 'm' ? chnm.m : chnm[""]
  1206. }
  1207. if (a[4]) {
  1208. b = a[4][0].toUpperCase()
  1209. b = abc2svg.letmid[b]
  1210. if (b != undefined) {
  1211. switch (a[4][1]) {
  1212. case "#": case "♯": b++; if (b >= 12) b = 0; break
  1213. case "b": case "♭": b--; if (b < 0) b = 11; break
  1214. }
  1215. }
  1216. }
  1217. }
  1218. if (b == undefined)
  1219. b = 0
  1220. ch = chcr(b, ch)
  1221. n = ch.length
  1222. r += trans
  1223. if (sb.p_v.tr_snd)
  1224. r += sb.p_v.tr_snd
  1225. for (m = 0; m < n; m++) {
  1226. not = { midi: r + ch[m] }
  1227. s.notes.push(not)
  1228. }
  1229. s.nhd = n - 1
  1230. s.prev = vch.last_sym
  1231. vch.last_sym.next = s
  1232. s.ts_next = sb.ts_next
  1233. sb.ts_next = s
  1234. s.ts_prev = sb
  1235. if (s.ts_next)
  1236. s.ts_next.ts_prev = s
  1237. vch.last_sym = s
  1238. }
  1239. if (cfmt.chord.names) {
  1240. chnm = Object.create(abc2svg.ch_names)
  1241. for (k in cfmt.chord.names) {
  1242. vch = ""
  1243. for (i = 0; i < cfmt.chord.names[k].length; i++) {
  1244. s = cfmt.chord.names[k][i]
  1245. vch += abc2svg.midlet[s % 12]
  1246. vch += i == 0 ? "-" : (s >= 12 ? "+" : " ")
  1247. }
  1248. chnm[k] = [vch]
  1249. }
  1250. } else { chnm = abc2svg.ch_names }
  1251. k = 0
  1252. for (i = 0; i < voice_tb.length; i++) {
  1253. if (k < voice_tb[i].chn)
  1254. k = voice_tb[i].chn
  1255. }
  1256. if (k == 8)
  1257. k++
  1258. vch = { v: voice_tb.length, id: "_chord", time: 0, sym: { type: C.BLOCK, subtype: "midiprog", chn: k + 1, instr: cfmt.chord.prog || 0, time: 0, ts_prev: first, ts_next: first.ts_next }, vol: cfmt.chord.vol || .6 }
  1259. vch.sym.p_v = vch
  1260. vch.sym.v = vch.v
  1261. vch.last_sym = vch.sym
  1262. voice_tb.push(vch)
  1263. first.ts_next = vch.sym
  1264. gchon = cfmt.chord.gchon
  1265. s = first
  1266. while (1) {
  1267. if (!s.ts_next) {
  1268. if (gchon)
  1269. vch.last_sym.dur = s.time - vch.last_sym.time
  1270. break
  1271. }
  1272. s = s.ts_next
  1273. if (!s.a_gch) {
  1274. if (s.subtype == "midigch") {
  1275. if (gchon && !s.on)
  1276. vch.last_sym.dur = s.time - vch.last_sym.time
  1277. gchon = s.on
  1278. }
  1279. continue
  1280. }
  1281. if (!gchon)
  1282. continue
  1283. for (i = 0; i < s.a_gch.length; i++) {
  1284. gch = s.a_gch[i]
  1285. if (gch.type != 'g')
  1286. continue
  1287. vch.last_sym.dur = s.time - vch.last_sym.time
  1288. gench(s)
  1289. break
  1290. }
  1291. }
  1292. }