Search in sources :

Example 1 with InvalidFormatException

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;
}
Also used : Score(com.xenoage.zong.io.musicxml.opus.Score) BufferedInputStream(com.xenoage.utils.io.BufferedInputStream) FileType(com.xenoage.zong.io.musicxml.FileType) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException)

Example 2 with InvalidFormatException

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);
}
Also used : MxlScorePartwise(com.xenoage.zong.musicxml.types.MxlScorePartwise) XmlReader(com.xenoage.utils.xml.XmlReader) MusicXMLDocument(com.xenoage.zong.musicxml.MusicXMLDocument) XmlDataException(com.xenoage.utils.xml.XmlDataException) IOException(java.io.IOException) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException) IOException(java.io.IOException) XmlDataException(com.xenoage.utils.xml.XmlDataException) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException)

Example 3 with InvalidFormatException

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);
    }
}
Also used : ErrorHandling(com.xenoage.zong.io.musicxml.in.util.ErrorHandling) LayoutFormatReader(com.xenoage.zong.io.musicxml.in.readers.LayoutFormatReader) MxlDefaults(com.xenoage.zong.musicxml.types.MxlDefaults) MxlLayout(com.xenoage.zong.musicxml.types.groups.MxlLayout) LayoutFormat(com.xenoage.zong.core.format.LayoutFormat) ScoreReader(com.xenoage.zong.io.musicxml.in.readers.ScoreReader) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException) ScoreFormat(com.xenoage.zong.core.format.ScoreFormat) Score(com.xenoage.zong.core.Score) ScoreInfo(com.xenoage.zong.core.info.ScoreInfo) StavesList(com.xenoage.zong.core.music.StavesList) ScoreFormatReader(com.xenoage.zong.io.musicxml.in.readers.ScoreFormatReader) ScoreInfoReader(com.xenoage.zong.io.musicxml.in.readers.ScoreInfoReader) MxlScoreHeader(com.xenoage.zong.musicxml.types.groups.MxlScoreHeader) StavesListReader(com.xenoage.zong.io.musicxml.in.readers.StavesListReader)

Example 4 with InvalidFormatException

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);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException) FileInputStream(java.io.FileInputStream) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException)

Example 5 with InvalidFormatException

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");
    }
}
Also used : File(java.io.File) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException) Settings(com.xenoage.utils.jse.settings.Settings)

Aggregations

InvalidFormatException (com.xenoage.utils.exceptions.InvalidFormatException)5 BufferedInputStream (com.xenoage.utils.io.BufferedInputStream)1 Settings (com.xenoage.utils.jse.settings.Settings)1 XmlDataException (com.xenoage.utils.xml.XmlDataException)1 XmlReader (com.xenoage.utils.xml.XmlReader)1 Score (com.xenoage.zong.core.Score)1 LayoutFormat (com.xenoage.zong.core.format.LayoutFormat)1 ScoreFormat (com.xenoage.zong.core.format.ScoreFormat)1 ScoreInfo (com.xenoage.zong.core.info.ScoreInfo)1 StavesList (com.xenoage.zong.core.music.StavesList)1 FileType (com.xenoage.zong.io.musicxml.FileType)1 LayoutFormatReader (com.xenoage.zong.io.musicxml.in.readers.LayoutFormatReader)1 ScoreFormatReader (com.xenoage.zong.io.musicxml.in.readers.ScoreFormatReader)1 ScoreInfoReader (com.xenoage.zong.io.musicxml.in.readers.ScoreInfoReader)1 ScoreReader (com.xenoage.zong.io.musicxml.in.readers.ScoreReader)1 StavesListReader (com.xenoage.zong.io.musicxml.in.readers.StavesListReader)1 ErrorHandling (com.xenoage.zong.io.musicxml.in.util.ErrorHandling)1 Score (com.xenoage.zong.io.musicxml.opus.Score)1 MusicXMLDocument (com.xenoage.zong.musicxml.MusicXMLDocument)1 MxlDefaults (com.xenoage.zong.musicxml.types.MxlDefaults)1