1// abc2svg - ABC to SVG translator
2// @source: https://chiselapp.com/user/moinejf/repository/abc2svg
3// Copyright (C) 2014-2020 Jean-Francois Moine - LGPL3+
4//chord.js-generation of accompaniment
5abc2svg.chordnames={'':["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+"]}
6abc2svg.midlet="CdDeEFgGaAbB"
7abc2svg.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}
8abc2svg.chord=function(first,voice_tb,cfmt){var chnm,i,k,vch,s,gchon,C=abc2svg.C,trans=48+(cfmt.chord.trans?cfmt.chord.trans*12:0)
9function chcr(b,ch){var i,v,r=[]
10b=abc2svg.midlet[b]
11i=ch.length
12while(--i>0){if(ch[i][0]==b)
13break}
14ch=ch[i]
15for(i=0;i<ch.length;i+=2){v=abc2svg.letmid[ch[i]]
16switch(ch[i+1]){case'+':v+=12;break
17case'-':v-=12;break}
18r.push(v)}
19return r}
20function gench(sb,gch){var r,ch,b,m,n,not,a=gch.otext.match(/([A-G])([#♯b♭]?)([^/]*)\/?(.*)/),s={v:vch.v,p_v:vch,type:C.NOTE,time:sb.time,notes:[]}
21if(a)
22r=abc2svg.letmid[a[1]]
23if(r==undefined){s.type=C.REST
24ch=[0]
25r=0}else{switch(a[2]){case"#":case"♯":r++;break
26case"b":case"♭":r--;break}
27if(!a[3]){ch=chnm[""]}else{ch=abc2svg.ch_alias[a[3]]
28if(ch==undefined)
29ch=a[3]
30ch=chnm[ch]
31if(!ch)
32ch=a[3][0]=='m'?chnm.m:chnm[""]}
33if(a[4]){b=abc2svg.letmid[a[4][0]]
34if(b!=undefined){switch(a[4][1]){case"#":case"♯":b++;if(b>=12)b=0;break
35case"b":case"♭":b--;if(b<0)b=11;break}}}}
36if(b==undefined)
37b=0
38ch=chcr(b,ch)
39n=ch.length
40r+=trans
41for(m=0;m<n;m++){not={midi:r+ch[m]}
42s.notes.push(not)}
43s.nhd=n-1
44s.prev=vch.last_sym
45vch.last_sym.next=s
46s.ts_next=sb.ts_next
47sb.ts_next=s
48s.ts_prev=sb
49if(s.ts_next)
50s.ts_next.ts_prev=s
51vch.last_sym=s}
52if(cfmt.chord.names){chnm=Object.create(abc2svg.chordnames)
53for(k in cfmt.chord.names){vch=""
54for(i=0;i<cfmt.chord.names[k].length;i++){s=cfmt.chord.names[k][i]
55vch+=abc2svg.midlet[s%12]
56vch+=i==0?"-":(s>=12?"+":" ")}
57chnm[k]=[vch]}}else{chnm=abc2svg.chordnames}
58vch={v:voice_tb.length,id:"_chord",time:0,sym:{type:C.REST,time:0},instr:cfmt.chord.prog||0,vol:cfmt.chord.vol||.8}
59vch.last_sym=vch.sym
60voice_tb.push(vch)
61k=0
62for(i=0;i<vch.v;i++){if(k<voice_tb[i].chn)
63k=voice_tb[i].chn}
64if(k==8)
65k++
66vch.chn=k+1
67gchon=cfmt.chord.gchon
68s=first
69while(1){if(!s.ts_next){if(gchon)
70vch.last_sym.dur=s.time-vch.last_sym.time
71break}
72s=s.ts_next
73if(!s.a_gch){if(s.subtype=="midigch"){if(gchon&&!s.on)
74vch.last_sym.dur=s.time-vch.last_sym.time
75gchon=s.on}
76continue}
77if(!gchon)
78continue
79for(i=0;i<s.a_gch.length;i++){gch=s.a_gch[i]
80if(gch.type!='g')
81continue
82vch.last_sym.dur=s.time-vch.last_sym.time
83gench(s,gch)
84break}}}
85