| 96 | | } |
| 97 | | |
| 98 | | public static Map<String, List<String>> decode(Track tr) throws Exception{ |
| 99 | | List<Byte> v = new ArrayList<Byte>(); |
| 100 | | for(int i = 1; i < tr.size(); i++){ |
| 101 | | MidiMessage m = tr.get(i).getMessage(); |
| 102 | | if(m instanceof MetaMessage){ |
| 103 | | byte b[] = ((MetaMessage)m).getData(); |
| 104 | | if(b.length == 0){ |
| 105 | | continue; |
| 106 | | } |
| 107 | | int h = 1; |
| 108 | | String mmheader; |
| 109 | | do{ |
| 110 | | h++; |
| 111 | | mmheader = new String(b, 0, h * 4, WIN31J); |
| 112 | | }while(!MMHEADER.matcher(mmheader).matches()); |
| 113 | | for(int k = h * 4; k < b.length; k++){ |
| 114 | | if(b[k] != 0x00){ |
| 115 | | v.add(new Byte(b[k])); |
| 116 | | } |
| 117 | | } |
| 118 | | } |
| 119 | | } |
| 120 | | byte b[] = new byte[v.size()]; |
| 121 | | for(int i = 0; i < v.size(); i++){ |
| 122 | | b[i] = v.get(i); |
| 123 | | } |
| 124 | | String textevent = new String(b, WIN31J); |
| 125 | | |
| 126 | | Map<String, List<String>> sections = new LinkedHashMap<String, List<String>>(); |
| 127 | | BufferedReader br = new BufferedReader(new StringReader(textevent)); |
| 128 | | String bf; |
| 129 | | String sn = ""; |
| 130 | | List<String> sv = new ArrayList<String>(); |
| 131 | | do{ |
| 132 | | bf = br.readLine(); |
| 133 | | if(bf == null || SECTIONLINE.matcher(bf).matches()){ |
| 134 | | if(sn.length() > 0 && !sections.containsKey(sn)){ |
| 135 | | sections.put(sn.substring(1, sn.length() - 1), sv); |
| 136 | | } |
| 137 | | sn = bf; |
| 138 | | sv = new ArrayList<String>(); |
| 139 | | } |
| 140 | | else if(bf.length() > 0){ |
| 141 | | sv.add(bf); |
| 142 | | } |
| 143 | | }while(bf != null); |
| 144 | | |
| 145 | | return sections; |
| 146 | | } |
| 147 | | |
| 148 | | public static List<MidiEvent> encode(List<String> trackSections) throws Exception{ |
| 149 | | String te = ""; |
| 150 | | for(int i = 0; i < trackSections.size(); i++){ |
| 151 | | String vts = trackSections.get(i); |
| 152 | | te += vts + "\n"; |
| 153 | | } |
| 154 | | |
| 155 | | List<MidiEvent> tme = new ArrayList<MidiEvent>(); |
| 156 | | byte b[] = te.getBytes(WIN31J); |
| 157 | | for(int j = 0; j <= b.length / TEXTLENGTH; j++){ |
| 158 | | int h = (int)StrictMath.ceil((double)Integer.toString(j).length() / 4.0); |
| 159 | | String mef = "DM:%0" + h * 4 + "d:"; |
| 160 | | byte header[] = String.format(mef, j).getBytes(WIN31J); |
| 161 | | List<Byte> mbl = new ArrayList<Byte>(); |
| 162 | | for(int k = 0; k < header.length; k++){ |
| 163 | | mbl.add(header[k]); |
| 164 | | } |
| 165 | | for(int k = 0; k < TEXTLENGTH; k++){ |
| 166 | | int bi = j * TEXTLENGTH + k; |
| 167 | | if(bi >= b.length){ |
| 168 | | break; |
| 169 | | } |
| 170 | | mbl.add(b[bi]); |
| 171 | | } |
| 172 | | byte mb[] = new byte[mbl.size()]; |
| 173 | | for(int k = 0; k < mbl.size(); k++){ |
| 174 | | mb[k] = mbl.get(k); |
| 175 | | } |
| 176 | | MetaMessage mm = new MetaMessage(); |
| 177 | | mm.setMessage(1, mb, mb.length); |
| 178 | | tme.add(new MidiEvent(mm, 0L)); |
| 179 | | } |
| 180 | | |
| 181 | | return tme; |