Skip to content

Commit

Permalink
Fixed a bug + added additional author info
Browse files Browse the repository at this point in the history
  • Loading branch information
urish committed May 15, 2012
1 parent 5679b68 commit 1744e86
Showing 1 changed file with 113 additions and 119 deletions.
232 changes: 113 additions & 119 deletions src/com/synthbot/audioplugin/vst/vst2/JVstPersistence.java
Expand Up @@ -74,82 +74,79 @@ public static void loadPreset(JVstHost2 vst, File file) throws DataFormatExcepti


DataInputStream fxp = new DataInputStream(new FileInputStream(file)); DataInputStream fxp = new DataInputStream(new FileInputStream(file));


byte[] fourBytes = new byte[4]; try {
byte[] fourBytes = new byte[4];


fxp.read(fourBytes); fxp.read(fourBytes);
if (!CHUNK_MAGIC.equals(new String(fourBytes))) { if (!CHUNK_MAGIC.equals(new String(fourBytes))) {
fxp.close(); throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag.");
throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag."); }
}

// fileLength is read an assigned to a variable for debugging purposes only
@SuppressWarnings("unused")
int fileLength = fxp.readInt();


fxp.read(fourBytes); // fileLength is read an assigned to a variable for debugging purposes only
String chunkDataType = new String(fourBytes); @SuppressWarnings("unused")
boolean isRegularChunk = true; int fileLength = fxp.readInt();
if (REGULAR_PRESET_MAGIC.equals(chunkDataType)) {
isRegularChunk = true; fxp.read(fourBytes);
} else if (OPAQUE_PRESET_MAGIC.equals(chunkDataType)) { String chunkDataType = new String(fourBytes);
if (!vst.acceptsProgramsAsChunks()) { boolean isRegularChunk = true;
fxp.close(); if (REGULAR_PRESET_MAGIC.equals(chunkDataType)) {
throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks."); isRegularChunk = true;
} else if (OPAQUE_PRESET_MAGIC.equals(chunkDataType)) {
if (!vst.acceptsProgramsAsChunks()) {
throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks.");
} else {
isRegularChunk = false;
}
} else { } else {
isRegularChunk = false; throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks.");
} }
} else {
throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks.");
}


int fileVersion = fxp.readInt(); int fileVersion = fxp.readInt();
if (fileVersion > 1) { if (fileVersion > 1) {
fxp.close(); throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported.");
throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported."); }
}


// unique id // unique id
fxp.read(fourBytes); fxp.read(fourBytes);
String uniqueId = new String(fourBytes); String uniqueId = new String(fourBytes);
if (!vst.getUniqueId().equals(uniqueId)) { if (!vst.getUniqueId().equals(uniqueId)) {
fxp.close(); throw new DataFormatException("Unique plugin ID in file does not match given plugin. " +
throw new DataFormatException("Unique plugin ID in file does not match given plugin. " + "Is this file really for " + vst.getEffectName() + "?");
"Is this file really for " + vst.getEffectName() + "?"); }
}


int filePluginVersion = fxp.readInt(); int filePluginVersion = fxp.readInt();
if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) { if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) {
fxp.close(); throw new DataFormatException("This file contains data for a later plugin version " +
throw new DataFormatException("This file contains data for a later plugin version " + Integer.toString(filePluginVersion) + ", and the given plugin is only version " +
Integer.toString(filePluginVersion) + ", and the given plugin is only version " + Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin.");
Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin."); }
}


int numParameters = fxp.readInt(); int numParameters = fxp.readInt();


byte[] programNameBytes = new byte[28]; byte[] programNameBytes = new byte[28];
fxp.read(programNameBytes); fxp.read(programNameBytes);
String programName = new String(programNameBytes); String programName = new String(programNameBytes);
vst.setProgramName(programName); vst.setProgramName(programName);


if (isRegularChunk) { if (isRegularChunk) {
for (int i = 0; i < numParameters; i++) { for (int i = 0; i < numParameters; i++) {
vst.setParameter(i, fxp.readFloat()); vst.setParameter(i, fxp.readFloat());
}
} else {
byte[] chunkData = new byte[fxp.readInt()];
fxp.read(chunkData);
vst.setProgramChunk(chunkData);
} }
} else { } finally {
byte[] chunkData = new byte[fxp.readInt()]; fxp.close();
fxp.read(chunkData);
vst.setProgramChunk(chunkData);
} }

fxp.close();
} }


/** /**
* Loads a preset bank from file to the current bank of the plugin. * Loads a preset bank from file to the current bank of the plugin.
* @param vst The plugin to load the data into. * @param vst The plugin to load the data into.
* @param file The file to read which contains the preset data. * @param file The file to read which contains the preset data.
* @author Uri Shaked * @author Uri Shaked <uri@urish.org>
*/ */
public static void loadBank(JVstHost2 vst, File file) throws DataFormatException, IOException { public static void loadBank(JVstHost2 vst, File file) throws DataFormatException, IOException {
if (vst == null) { if (vst == null) {
Expand All @@ -167,78 +164,75 @@ public static void loadBank(JVstHost2 vst, File file) throws DataFormatException


DataInputStream fxp = new DataInputStream(new FileInputStream(file)); DataInputStream fxp = new DataInputStream(new FileInputStream(file));


byte[] fourBytes = new byte[4]; try {
byte[] fourBytes = new byte[4];


fxp.read(fourBytes); fxp.read(fourBytes);
if (!CHUNK_MAGIC.equals(new String(fourBytes))) { if (!CHUNK_MAGIC.equals(new String(fourBytes))) {
fxp.close(); throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag.");
throw new DataFormatException("File does not contain required Chunk Magic, \"" + CHUNK_MAGIC + "\", flag."); }
}

// fileLength is read an assigned to a variable for debugging purposes only
@SuppressWarnings("unused")
int fileLength = fxp.readInt();


fxp.read(fourBytes); // fileLength is read an assigned to a variable for debugging purposes only
String chunkDataType = new String(fourBytes); @SuppressWarnings("unused")
boolean isRegularChunk = true; int fileLength = fxp.readInt();
if (REGULAR_BANK_MAGIC.equals(chunkDataType)) {
isRegularChunk = true; fxp.read(fourBytes);
} else if (OPAQUE_BANK_MAGIC.equals(chunkDataType)) { String chunkDataType = new String(fourBytes);
if (!vst.acceptsProgramsAsChunks()) { boolean isRegularChunk = true;
fxp.close(); if (REGULAR_BANK_MAGIC.equals(chunkDataType)) {
throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks."); isRegularChunk = true;
} else if (OPAQUE_BANK_MAGIC.equals(chunkDataType)) {
if (!vst.acceptsProgramsAsChunks()) {
throw new DataFormatException("File contains opaque data but plugin claims not to accept programs as chunks.");
} else {
isRegularChunk = false;
}
} else { } else {
isRegularChunk = false; throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks.");
} }
} else {
throw new DataFormatException("File reports that is contains neither regular nor opqaue chunks.");
}

int fileVersion = fxp.readInt();
if (fileVersion > 2) {
fxp.close();
throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported.");
}

// unique id
fxp.read(fourBytes);
String uniqueId = new String(fourBytes);
if (!vst.getUniqueId().equals(uniqueId)) {
fxp.close();
throw new DataFormatException("Unique plugin ID in file does not match given plugin. " +
"Is this file really for " + vst.getEffectName() + "?");
}


int filePluginVersion = fxp.readInt(); int fileVersion = fxp.readInt();
if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) { if (fileVersion > 2) {
fxp.close(); throw new DataFormatException("File version " + Integer.toString(fileVersion) + " is not supported.");
throw new DataFormatException("This file contains data for a later plugin version " + }
Integer.toString(filePluginVersion) + ", and the given plugin is only version " +
Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin.");
}

int numPrograms = fxp.readInt();


int currentProgram = fxp.readInt(); // unique id
fxp.read(fourBytes);
String uniqueId = new String(fourBytes);
if (!vst.getUniqueId().equals(uniqueId)) {
throw new DataFormatException("Unique plugin ID in file does not match given plugin. " +
"Is this file really for " + vst.getEffectName() + "?");
}


// reserved (zero) int filePluginVersion = fxp.readInt();
fxp.read(new byte[124]); if (!ignorePluginVersion && (vst.getPluginVersion() < filePluginVersion)) {
throw new DataFormatException("This file contains data for a later plugin version " +
Integer.toString(filePluginVersion) + ", and the given plugin is only version " +
Integer.toString(vst.getPluginVersion()) + ". Get a newer version of the plugin.");
}


if (isRegularChunk) { int numPrograms = fxp.readInt();
for (int i = currentProgram; i < currentProgram + numPrograms; i++) {
vst.setProgram(i); int currentProgram = fxp.readInt();
for (int j = 0; j < vst.numParameters(); j++) {
vst.setParameter(i, fxp.readFloat()); // reserved (zero)
fxp.read(new byte[124]);

if (isRegularChunk) {
for (int i = currentProgram; i < currentProgram + numPrograms; i++) {
vst.setProgram(i);
for (int j = 0; j < vst.numParameters(); j++) {
vst.setParameter(i, fxp.readFloat());
}
} }
} else {
byte[] chunkData = new byte[fxp.readInt()];
fxp.read(chunkData);
vst.setBankChunk(chunkData);
} }
} else { } finally {
byte[] chunkData = new byte[fxp.readInt()]; fxp.close();
fxp.read(chunkData);
vst.setBankChunk(chunkData);
} }

fxp.close();
} }


/** /**
Expand Down

0 comments on commit 1744e86

Please sign in to comment.