- 更新日時:
- 2010/05/11 20:15:55 (2 年 前)
- パス:
- trunk/src/org/rayflood/mikuvat/io/vsq
- ファイル:
-
- 1 追加
- 1 移動
-
. (追加)
-
VSQFile.java (移動) (移動元: trunk/src/org/rayflood/mikuvat/io/VSQFile.java) (17 diff)
凡例:
- 変更なし
- 追加
- 削除
-
trunk/src/org/rayflood/mikuvat/io/vsq/VSQFile.java
r2 r4 1 package org.rayflood.mikuvat.io; 2 3 import static org.rayflood.mikuvat.io.VSQDecoder.decode; 4 import static org.rayflood.mikuvat.io.VSQDecoder.encode; 5 import static org.rayflood.mikuvat.io.VSQDecoder.setSectionBracket; 6 1 package org.rayflood.mikuvat.io.vsq; 2 3 import static org.rayflood.mikuvat.Utilities.setSectionBracket; 4 5 import java.io.BufferedReader; 7 6 import java.io.File; 7 import java.io.FileNotFoundException; 8 import java.io.IOException; 9 import java.io.StringReader; 10 import java.nio.charset.Charset; 8 11 import java.util.ArrayList; 12 import java.util.LinkedHashMap; 9 13 import java.util.List; 10 14 import java.util.Map; 11 12 import javax.sound.midi.MidiEvent; 13 import javax.sound.midi. MidiSystem;15 import java.util.regex.Pattern; 16 17 import javax.sound.midi.InvalidMidiDataException; 14 18 import javax.sound.midi.Sequence; 15 import javax.sound.midi.Track; 16 17 public class VSQFile extends ImmutableObject{ 19 20 import org.rayflood.mikuvat.io.BaseObject; 21 import org.rayflood.mikuvat.io.InvalidBaseObjectException; 22 import org.rayflood.mikuvat.io.midi.MidiData; 23 import org.rayflood.mikuvat.io.midi.MidiFile; 24 import org.rayflood.mikuvat.io.midi.MidiMeta; 25 import org.rayflood.mikuvat.io.midi.MidiTrack; 26 27 public class VSQFile extends MidiFile{ 18 28 private static final long serialVersionUID = -6314152290164983853L; 29 public static final Pattern MMHEADER = Pattern.compile("^DM:\\d+:"); 30 public static final Charset WIN31J = Charset.forName("Windows-31J"); 31 public static final Pattern SECTIONLINE = Pattern.compile("^\\[.+\\]$"); 32 public static final int TEXTLENGTH = 119; 19 33 public static final String COMMON = "Common"; 20 34 public static final String MASTER = "Master"; … … 22 36 public static final String EVENTLIST = "EventList"; 23 37 24 private File vsqFile;25 private Integer fileType;26 private Float divisionType;27 private Integer resolution;28 private MidiTrack conductor;29 38 private MasterSection master; 30 39 private MixerSection mixer; 31 40 private ArrayList<VSQTrack> tracks; 32 41 33 public VSQFile(String vsq) throws Exception{42 public VSQFile(String vsq) throws IOException, InvalidMidiDataException{ 34 43 this(); 35 readVSQFile(vsq); 36 } 37 38 public VSQFile(File vsq) throws Exception{ 44 readMidiFile(vsq); 45 setVSQSequence(getMidiTracks()); 46 } 47 48 public VSQFile(File vsq) throws IOException, InvalidMidiDataException{ 39 49 this(); 40 readVSQFile(vsq); 41 } 42 43 public VSQFile(Sequence sq) throws Exception{ 50 readMidiFile(vsq); 51 setVSQSequence(getMidiTracks()); 52 } 53 54 public VSQFile(Sequence sq) throws IOException{ 44 55 this(); 45 setVSQSequence(sq); 46 } 47 48 public void readVSQFile(String vsq) throws Exception{ 56 setMidiSequence(sq); 57 setVSQSequence(getMidiTracks()); 58 } 59 60 public void readVSQFile(String vsq) throws IOException, InvalidMidiDataException{ 49 61 readVSQFile(new File(vsq)); 50 62 } 51 63 52 public void readVSQFile(File vsq) throws Exception{53 s etVsqFile(vsq);54 setVSQSequence( MidiSystem.getSequence(getVsqFile()));55 } 56 57 public void writeVSQFile() throws Exception{64 public void readVSQFile(File vsq) throws IOException, InvalidMidiDataException{ 65 super.readMidiFile(vsq); 66 setVSQSequence(getMidiTracks()); 67 } 68 69 public void writeVSQFile() throws IOException, InvalidMidiDataException, InvalidBaseObjectException{ 58 70 writeVSQFile(getVsqFile()); 59 71 } 60 72 61 public void writeVSQFile(String vsq) throws Exception{73 public void writeVSQFile(String vsq) throws IOException, InvalidMidiDataException, InvalidBaseObjectException{ 62 74 writeVSQFile(new File(vsq)); 63 75 } 64 76 65 public void writeVSQFile(File vsq) throws Exception{ 66 MidiSystem.write(getVSQSequence(), getFileType(), vsq); 67 } 68 69 public void setVSQSequence(Sequence sq) throws Exception{ 70 setFileType(MidiSystem.getMidiFileTypes(sq)[0]); 71 setDivisionType(sq.getDivisionType()); 72 setResolution(sq.getResolution()); 73 Track t[] = sq.getTracks(); 74 setConductor(t[0]); 75 76 Map<String, List<String>> sections = decode(t[1]); 77 public void writeVSQFile(File vsq) throws IOException, InvalidMidiDataException, InvalidBaseObjectException{ 78 setMidiTracks(getVSQSequence()); 79 super.writeMidiFile(vsq); 80 } 81 82 public Map<String, List<String>> decode(List<MidiData> md) throws IOException{ 83 List<Byte> v = new ArrayList<Byte>(); 84 for(int i = 0; i < md.size(); i++){ 85 MidiData m = md.get(i); 86 if(m instanceof MidiMeta){ 87 byte b[] = ((MidiMeta)m).getData(); 88 if(b.length == 0){ 89 continue; 90 } 91 String mmheader = new String(b); 92 String[] ss = MMHEADER.split(mmheader, 2); 93 if(ss.length > 1){ 94 for(int k = mmheader.indexOf(ss[1]); k < b.length; k++){ 95 if(b[k] != 0x00){ 96 v.add(b[k]); 97 } 98 } 99 } 100 } 101 } 102 byte b[] = new byte[v.size()]; 103 for(int i = 0; i < v.size(); i++){ 104 b[i] = v.get(i); 105 } 106 String textevent = new String(b, WIN31J); 107 System.out.println(textevent); 108 109 Map<String, List<String>> sections = new LinkedHashMap<String, List<String>>(); 110 BufferedReader br = new BufferedReader(new StringReader(textevent)); 111 String bf; 112 String sn = ""; 113 List<String> sv = new ArrayList<String>(); 114 do{ 115 bf = br.readLine(); 116 if(bf == null || SECTIONLINE.matcher(bf).matches()){ 117 if(sn.length() > 0 && !sections.containsKey(sn)){ 118 sections.put(sn.substring(1, sn.length() - 1), sv); 119 } 120 sn = bf; 121 sv = new ArrayList<String>(); 122 } 123 else if(bf.length() > 0){ 124 sv.add(bf); 125 } 126 }while(bf != null); 127 128 return sections; 129 } 130 131 public List<MidiData> encode(List<String> trackSections){ 132 String te = ""; 133 for(int i = 0; i < trackSections.size(); i++){ 134 String vts = trackSections.get(i); 135 te += vts + "\n"; 136 } 137 138 List<MidiData> tme = new ArrayList<MidiData>(); 139 byte b[] = te.getBytes(WIN31J); 140 for(int j = 0; j <= b.length / TEXTLENGTH; j++){ 141 int h = (int)StrictMath.ceil((double)Integer.toString(j).length() / 4.0); 142 String mef = "DM:%0" + h * 4 + "d:"; 143 byte header[] = String.format(mef, j).getBytes(WIN31J); 144 List<Byte> mbl = new ArrayList<Byte>(); 145 for(int k = 0; k < header.length; k++){ 146 mbl.add(header[k]); 147 } 148 for(int k = 0; k < TEXTLENGTH; k++){ 149 int bi = j * TEXTLENGTH + k; 150 if(bi >= b.length){ 151 break; 152 } 153 mbl.add(b[bi]); 154 } 155 byte mb[] = new byte[mbl.size()]; 156 for(int k = 0; k < mbl.size(); k++){ 157 mb[k] = mbl.get(k); 158 } 159 tme.add(new MidiMeta(1, mb)); 160 } 161 162 return tme; 163 } 164 165 public void setVSQSequence(ArrayList<MidiTrack> t) throws IOException{ 166 Map<String, List<String>> sections = decode(t.get(0).getMidiData(0L)); 77 167 setMaster(new MasterSection(sections.get(MASTER))); 78 168 setMixer(new MixerSection(sections.get(MIXER))); 79 addTrack(new VSQTrack(t[1].get(0), getMixerSetting(0), sections)); 80 for(int i = 2; i < t.length; i++){ 81 addTrack(new VSQTrack(t[i].get(0), getMixerSetting(i - 1), decode(t[i]))); 82 } 83 } 84 85 public Sequence getVSQSequence() throws Exception{ 86 Sequence sq = new Sequence(getDivisionType(), getResolution()); 87 Track mt = sq.createTrack(); 88 getConductor(mt); 169 addVSQTrack(new VSQTrack(getMixerSetting(0), sections)); 170 for(int i = 1; i < t.size(); i++){ 171 addVSQTrack(new VSQTrack(getMixerSetting(i), decode(t.get(i).getMidiData(0L)))); 172 } 173 } 174 175 public ArrayList<MidiTrack> getVSQSequence() throws InvalidBaseObjectException{ 176 ArrayList<MidiTrack> t = new ArrayList<MidiTrack>(); 89 177 90 178 List<List<String>> tsections = getVSQText(); 91 for(int i = 0; i < getTrackSize(); i++){ 92 VSQTrack vt = getTrack(i); 93 Track t = sq.createTrack(); 94 t.add(vt.getTrackNameME()); 95 List<MidiEvent> me = encode(tsections.get(i)); 96 for(int j = 0; j < me.size(); j++){ 97 t.add(me.get(j)); 98 } 99 } 100 101 return sq; 102 } 103 104 public List<List<String>> getVSQText() throws Exception{ 105 if(!isReadied()){ 106 throw new Exception(); 107 } 179 for(int i = 0; i < getVSQTrackSize(); i++){ 180 MidiTrack mt = new MidiTrack(); 181 mt.setMidiData(0L, encode(tsections.get(i))); 182 t.add(mt); 183 } 184 185 return t; 186 } 187 188 public List<List<String>> getVSQText() throws InvalidBaseObjectException{ 189 validate(); 108 190 109 191 List<List<String>> tcommons = new ArrayList<List<String>>(); 110 192 List<List<String>> ttimelines = new ArrayList<List<String>>(); 111 for(int i = 0; i < get TrackSize(); i++){112 VSQTrack vt = get Track(i);193 for(int i = 0; i < getVSQTrackSize(); i++){ 194 VSQTrack vt = getVSQTrack(i); 113 195 tcommons.add(vt.getCommon().getSection()); 114 196 ttimelines.add(vt.getTimeLine().getSectionFast()); … … 128 210 vsqtext.add(sections); 129 211 130 for(int i = 1; i < get TrackSize(); i++){212 for(int i = 1; i < getVSQTrackSize(); i++){ 131 213 sections = new ArrayList<String>(); 132 214 sections.add(setSectionBracket(COMMON)); … … 164 246 } 165 247 166 public int get TrackSize(){248 public int getVSQTrackSize(){ 167 249 return tracks.size(); 168 250 } 169 251 170 public void add Track(VSQTrack vt){252 public void addVSQTrack(VSQTrack vt){ 171 253 if(vt == null || vt.isDeleted()){ 172 254 return; … … 175 257 } 176 258 177 public void add Track(int index, VSQTrack vt){259 public void addVSQTrack(int index, VSQTrack vt){ 178 260 if(vt == null || vt.isDeleted()){ 179 261 return; 180 262 } 181 if(index < get TrackSize()){263 if(index < getVSQTrackSize()){ 182 264 tracks.add(index, vt); 183 265 } 184 266 else{ 185 add Track(vt);186 } 187 } 188 189 public void set Track(int index, VSQTrack vt){267 addVSQTrack(vt); 268 } 269 } 270 271 public void setVSQTrack(int index, VSQTrack vt){ 190 272 if(vt == null || vt.isDeleted()){ 191 remove Track(index);192 return; 193 } 194 if(index < get TrackSize()){273 removeVSQTrack(index); 274 return; 275 } 276 if(index < getVSQTrackSize()){ 195 277 tracks.set(index, vt); 196 278 } 197 279 else{ 198 add Track(vt);199 } 200 } 201 202 public VSQTrack get Track(int index){203 if(index < get TrackSize()){280 addVSQTrack(vt); 281 } 282 } 283 284 public VSQTrack getVSQTrack(int index){ 285 if(index < getVSQTrackSize()){ 204 286 return tracks.get(index); 205 287 } … … 209 291 } 210 292 211 public VSQTrack remove Track(int index){212 if(index < get TrackSize()){293 public VSQTrack removeVSQTrack(int index){ 294 if(index < getVSQTrackSize()){ 213 295 return tracks.remove(index); 214 296 } … … 223 305 sl.add("VsqFile=" + getVsqFileS()); 224 306 } 225 if(hasFileType()){226 sl.add("FileType=" + getFileType());227 }228 if(hasDivisionType()){229 sl.add("DivisionType=" + getDivisionType());230 }231 if(hasResolution()){232 sl.add("Resolution=" + getResolution());233 }234 if(hasConductor()){235 sl.add("Conductor={" + getConductor().toString() + "}");236 }237 307 if(hasMaster()){ 238 308 sl.add("Master={" + getMaster().toString() + "}"); … … 241 311 sl.add("Mixer={" + getMixer().toString() + "}"); 242 312 } 243 if(has Tracks()){244 StringBu ffer sb = new StringBuffer();245 sb.append(get Track(0).toString());246 for(int i = 1; i < get TrackSize(); i++){247 sb.append("," + get Track(i).toString());313 if(hasVSQTracks()){ 314 StringBuilder sb = new StringBuilder(); 315 sb.append(getVSQTrack(0).toString()); 316 for(int i = 1; i < getVSQTrackSize(); i++){ 317 sb.append("," + getVSQTrack(i).toString()); 248 318 } 249 319 sl.add("Tracks=[" + sb.toString() + "]"); 250 320 } 251 321 252 StringBuffer sb = new StringBuffer(); 322 StringBuilder sb = new StringBuilder(); 323 sb.append(super.toString()); 324 253 325 sb.append("VSQFile={"); 254 326 if(!sl.isEmpty()){ … … 262 334 } 263 335 264 public boolean isReadied(){ 265 boolean ready = true; 266 ready &= hasFileType(); 267 ready &= hasDivisionType(); 268 ready &= hasResolution(); 269 ready &= hasConductor() && getConductor().isReadied(); 270 ready &= hasMaster() && getMaster().isReadied(); 271 ready &= hasMixer() && getMixer().isReadied(); 272 for(int i = 0; i < getTrackSize(); i++){ 273 ready &= getTrack(i).isReadied(); 336 public boolean validate() throws InvalidBaseObjectException{ 337 boolean ready = super.validate(); 338 ready &= hasMaster() && getMaster().validate(); 339 ready &= hasMixer() && getMixer().validate(); 340 for(int i = 0; i < getVSQTrackSize(); i++){ 341 ready &= getVSQTrack(i).validate(); 342 } 343 if(!ready){ 344 throw new InvalidBaseObjectException(this); 274 345 } 275 346 return ready; 276 347 } 277 348 278 public int compareTo(ImmutableObject obj){ 279 VSQFile vsqFile = (VSQFile)obj; 280 return compare(getVsqFile().getPath(), vsqFile.getVsqFile().getPath()); 281 } 282 283 public VSQFile over(Object obj, boolean fill, boolean has){ 349 public VSQFile over(BaseObject obj, boolean fill, boolean has){ 350 super.over(obj, fill, has); 284 351 VSQFile vsqFile = ((VSQFile)obj).clone(); 285 if(vsqFile.hasFileType() && (fill || has == hasFileType())){286 setFileType(vsqFile.getFileType());287 }288 if(vsqFile.hasDivisionType() && (fill || has == hasDivisionType())){289 setDivisionType(vsqFile.getDivisionType());290 }291 if(vsqFile.hasResolution() && (fill || has == hasResolution())){292 setResolution(vsqFile.getResolution());293 }294 if(vsqFile.hasConductor()){295 MidiTrack conductor = hasConductor() ? getConductor() : new MidiTrack();296 setConductor(conductor.over(vsqFile.getConductor(), fill, has));297 }298 352 if(vsqFile.hasMaster()){ 299 353 MasterSection master = hasMaster() ? getMaster() : new MasterSection(); … … 304 358 setMixer(mixer.over(vsqFile.getMixer(), fill, has)); 305 359 } 306 for(int i = 0; i < vsqFile.getTracks().size(); i++){ 307 VSQTrack vt = getTrack(i); 308 setTrack(i, vt.over(vsqFile.getTrack(i), fill, has)); 309 } 310 if(vsqFile.hasVsqFile() && (fill || has == hasVsqFile())){ 311 setVsqFile(vsqFile.getVsqFile()); 360 for(int i = 0; i < vsqFile.getVSQTracks().size(); i++){ 361 VSQTrack vt = getVSQTrack(i); 362 setVSQTrack(i, vt.over(vsqFile.getVSQTrack(i), fill, has)); 312 363 } 313 364 return this; … … 315 366 316 367 public VSQFile setDefault(){ 317 setFileType(1); 318 setDivisionType(0.0f); 319 setResolution(480); 320 setConductor(new MidiTrack()); 368 super.setDefault(); 321 369 setMaster(MasterSection.getDefault()); 322 370 setMixer(MixerSection.getDefault()); 323 deleteTracks(); 324 addTrack(VSQTrack.getDefault()); 325 deleteVsqFile(); 371 deleteVSQTracks(); 372 addVSQTrack(VSQTrack.getDefault()); 326 373 return this; 327 374 } … … 334 381 335 382 public VSQFile delete(){ 336 deleteFileType(); 337 deleteDivisionType(); 338 deleteResolution(); 339 deleteConductor(); 383 super.delete(); 340 384 deleteMaster(); 341 385 deleteMixer(); 342 deleteTracks(); 343 deleteVsqFile(); 386 deleteVSQTracks(); 344 387 return this; 345 388 } 346 389 347 390 public boolean isDeleted(){ 348 boolean has = false; 349 has |= hasFileType(); 350 has |= hasDivisionType(); 351 has |= hasResolution(); 352 has |= hasConductor(); 391 boolean has = super.isDeleted(); 353 392 has |= hasMaster(); 354 393 has |= hasMixer(); 355 has |= hasTracks(); 356 has |= hasVsqFile(); 394 has |= hasVSQTracks(); 357 395 return !has; 358 396 } 359 397 360 398 public VSQFile(){ 399 super(); 361 400 delete(); 362 401 } 363 402 364 403 public File getVsqFile(){ 365 return vsqFile;404 return getMidiFile(); 366 405 } 367 406 … … 371 410 372 411 public void setVsqFile(File vsqFile){ 373 this.vsqFile = vsqFile;412 setMidiFile(vsqFile); 374 413 } 375 414 … … 379 418 380 419 public void deleteVsqFile(){ 381 vsqFile = null;420 deleteMidiFile(); 382 421 } 383 422 384 423 public boolean hasVsqFile(){ 385 return vsqFile != null; 386 } 387 388 public Integer getFileType(){ 389 return fileType; 390 } 391 392 public void setFileType(Integer fileType){ 393 this.fileType = fileType; 394 } 395 396 public void deleteFileType(){ 397 fileType = null; 398 } 399 400 public boolean hasFileType(){ 401 return fileType != null; 402 } 403 404 public Float getDivisionType(){ 405 return divisionType; 406 } 407 408 public void setDivisionType(Float divisionType){ 409 this.divisionType = divisionType; 410 } 411 412 public void deleteDivisionType(){ 413 divisionType = null; 414 } 415 416 public boolean hasDivisionType(){ 417 return divisionType != null; 418 } 419 420 public Integer getResolution(){ 421 return resolution; 422 } 423 424 public void setResolution(Integer resolution){ 425 this.resolution = resolution; 426 } 427 428 public void deleteResolution(){ 429 resolution = null; 430 } 431 432 public boolean hasResolution(){ 433 return resolution != null; 434 } 435 436 public MidiTrack getConductor(){ 437 return conductor; 438 } 439 440 public void getConductor(Track t) throws Exception{ 441 conductor.getTrack(t); 442 } 443 444 public void setConductor(MidiTrack conductor){ 445 if(conductor == null || conductor.isDeleted()){ 446 deleteConductor(); 447 return; 448 } 449 this.conductor = conductor; 450 } 451 452 public void setConductor(Track t){ 453 this.conductor = new MidiTrack(t); 454 } 455 456 public void deleteConductor(){ 457 conductor = null; 458 } 459 460 public boolean hasConductor(){ 461 return conductor != null; 424 return hasMidiFile(); 462 425 } 463 426 … … 502 465 } 503 466 504 public ArrayList<VSQTrack> get Tracks(){467 public ArrayList<VSQTrack> getVSQTracks(){ 505 468 return tracks; 506 469 } 507 470 508 public void set Tracks(ArrayList<VSQTrack> tracks){471 public void setVSQTracks(ArrayList<VSQTrack> tracks){ 509 472 if(tracks == null){ 510 delete Tracks();473 deleteVSQTracks(); 511 474 return; 512 475 } … … 514 477 } 515 478 516 public void delete Tracks(){517 set Tracks(new ArrayList<VSQTrack>());518 } 519 520 public boolean has Tracks(){479 public void deleteVSQTracks(){ 480 setVSQTracks(new ArrayList<VSQTrack>()); 481 } 482 483 public boolean hasVSQTracks(){ 521 484 return !tracks.isEmpty(); 522 485 } … … 526 489 } 527 490 528 public static VSQFile load(String file) throws Exception{529 return (VSQFile) ImmutableObject.load(file);530 } 531 532 public static VSQFile load(File file) throws Exception{533 return (VSQFile) ImmutableObject.load(file);534 } 535 536 public void store(String file) throws Exception{491 public static VSQFile load(String file) throws FileNotFoundException, IOException, ClassNotFoundException{ 492 return (VSQFile)BaseObject.load(file); 493 } 494 495 public static VSQFile load(File file) throws FileNotFoundException, IOException, ClassNotFoundException{ 496 return (VSQFile)BaseObject.load(file); 497 } 498 499 public void store(String file) throws FileNotFoundException, IOException{ 537 500 super.store(file); 538 501 } 539 502 540 public void store(File file) throws Exception{503 public void store(File file) throws FileNotFoundException, IOException{ 541 504 super.store(file); 542 505 } 543 506 544 public static VSQFile unzip(String file) throws Exception{545 return (VSQFile) ImmutableObject.unzip(file);546 } 547 548 public static VSQFile unzip(File file) throws Exception{549 return (VSQFile) ImmutableObject.unzip(file);550 } 551 552 public void zip(String file) throws Exception{507 public static VSQFile unzip(String file) throws IOException, ClassNotFoundException{ 508 return (VSQFile)BaseObject.unzip(file); 509 } 510 511 public static VSQFile unzip(File file) throws IOException, ClassNotFoundException{ 512 return (VSQFile)BaseObject.unzip(file); 513 } 514 515 public void zip(String file) throws IOException{ 553 516 super.zip(file); 554 517 } 555 518 556 public void zip(File file) throws Exception{519 public void zip(File file) throws IOException{ 557 520 super.zip(file); 558 521 }
