use of com.xenoage.utils.exceptions.InvalidFormatException in project Zong by Xenoage.
the class CompressedFileInput method loadScore.
/**
* Loads and returns the {@link com.xenoage.zong.core.Score} at the given path.
*/
public com.xenoage.zong.core.Score loadScore(String path) throws InvalidFormatException, IOException {
BufferedInputStream bis = new BufferedInputStream(zip.openFile(path));
// XML or compressed?
bis.mark();
FileType fileType = FileTypeReader.getFileType(bis);
bis.reset();
bis.unmark();
if (fileType == null)
throw new InvalidFormatException("Score has invalid format: " + path);
com.xenoage.zong.core.Score ret = null;
switch(fileType) {
case Compressed:
ret = loadCompressedScore(path);
break;
case XMLScorePartwise:
ret = new MusicXmlScoreFileInput().read(bis, path);
break;
case XMLScoreTimewise:
throw new IllegalStateException("score-timewise is currently not implemented");
default:
throw new InvalidFormatException("Score has invalid format: " + path);
}
bis.close();
return ret;
}
use of com.xenoage.utils.exceptions.InvalidFormatException in project Zong by Xenoage.
the class MusicXmlScoreFileInput method read.
/**
* Creates a {@link Score} instance from the document
* behind the given {@link InputStream}.
* @param inputStream the input stream with the MusicXML document
* @param filePath file path if known, null otherwise
*/
@Override
public Score read(InputStream stream, @MaybeNull String filePath) throws InvalidFormatException, IOException {
// parse MusicXML file
MxlScorePartwise score;
try {
XmlReader xmlReader = platformUtils().createXmlReader(stream);
MusicXMLDocument doc = MusicXMLDocument.read(xmlReader);
score = doc.getScore();
} catch (XmlDataException ex) {
// no valid MusicXML
throw new InvalidFormatException(ex);
} catch (Exception ex) {
throw new IOException(ex);
}
return read(score, Level.LogErrors);
}
use of com.xenoage.utils.exceptions.InvalidFormatException in project Zong by Xenoage.
the class MusicXmlScoreFileInput method read.
/**
* Builds a {@link Score} entity from a {@link MxlScorePartwise} document.
* @param doc the provided score-partwise document
* @param errorHandling how to deal with errors
*/
public Score read(MxlScorePartwise mxlScore, Level errorHandling) throws InvalidFormatException {
try {
// create new score
Score score = new Score();
// read information about the score
ScoreInfo scoreInfo = new ScoreInfoReader(mxlScore.getScoreHeader()).read();
score.setInfo(scoreInfo);
// read score format
MxlScoreHeader mxlScoreHeader = mxlScore.getScoreHeader();
MxlDefaults mxlDefaults = mxlScoreHeader.getDefaults();
ScoreFormat scoreFormat = new ScoreFormatReader(mxlDefaults).read();
score.setFormat(scoreFormat);
// read layout format
MxlLayout mxlLayout = (mxlDefaults != null ? mxlDefaults.getLayout() : null);
LayoutFormat layoutFormat = new LayoutFormatReader(mxlLayout, scoreFormat.getInterlineSpace() / 10).read();
// TIDY
score.setMetaData("layoutformat", layoutFormat);
// create the list of staves
ErrorHandling mxlErrorHandling = new ErrorHandling(errorHandling);
StavesListReader stavesListReader = new StavesListReader(mxlScore, mxlErrorHandling);
StavesList stavesList = stavesListReader.read();
stavesList.setScore(score);
score.setStavesList(stavesList);
// read the musical contents
new ScoreReader(mxlScore).readToScore(score, mxlErrorHandling);
// remember the XML document for further application-dependend processing
// TIDY
score.setMetaData("mxldoc", mxlScore);
// when errors were collected, log them and save them as metadata
if (mxlErrorHandling.getErrorMessages().size() > 0) {
INSTANCE.log(Companion.warning("The file could be loaded, but the following error(s) were reported: " + mxlErrorHandling.getErrorMessages()));
score.setMetaData("mxlerrors", mxlErrorHandling.getErrorMessages());
}
return score;
} catch (RuntimeException ex) {
// catch runtime exceptions and rethrow them in the expected type
throw new InvalidFormatException(ex);
}
}
use of com.xenoage.utils.exceptions.InvalidFormatException in project Zong by Xenoage.
the class SynthManager method loadSoundbank.
/**
* Loads the soundbank from the given file.
* If it fails, an {@link InvalidFormatException} is thrown.
*/
public void loadSoundbank(File file) throws InvalidFormatException {
try {
Soundbank newSB;
try (FileInputStream fis = new FileInputStream(file)) {
newSB = MidiSystem.getSoundbank(new BufferedInputStream(fis));
}
if (soundbank != null)
synthesizer.unloadAllInstruments(soundbank);
soundbank = newSB;
synthesizer.loadAllInstruments(soundbank);
} catch (Exception ex) {
throw new InvalidFormatException("Invalid soundbank: " + file, ex);
}
}
use of com.xenoage.utils.exceptions.InvalidFormatException in project Zong by Xenoage.
the class SynthManager method init.
/**
* Initializes the manager. Must be called at the beginning one time
* and each time when the audio settings should be reloaded.
* @param readSettings true, to load the settings from the configuration file.
* false, to use default settings (e.g. for an applet)
*/
public static void init(boolean readSettings) throws MidiUnavailableException {
// TIDY method (especially default settings)
if (instance == null) {
instance = new SynthManager();
}
if (readSettings) {
// load settings (or use default ones)
Settings s = Settings.getInstance();
String file = configFile;
String deviceName = s.getSetting("devicename", file);
float sampleRate = s.getSetting("samplerate", file, 44100);
int sampleSizeInBits = s.getSetting("bits", file, 16);
int channels = s.getSetting("channels", file, 2);
int latency = s.getSetting("latency", file, 100);
int polyphony = s.getSetting("polyphony", file, 64);
String interpolation = s.getSetting("interpolation", file, "linear");
String soundbank = s.getSetting("soundbank", file, null);
// init midi and soundbank
instance.initMidi(sampleRate, sampleSizeInBits, channels, latency, polyphony, deviceName, interpolation);
if (soundbank != null && soundbank.length() > 0) {
try {
instance.loadSoundbank(new File(soundbank));
} catch (InvalidFormatException ex) {
// TODO
}
}
} else {
instance.initMidi(44100, 16, 2, 100, 64, null, "linear");
}
}
Aggregations