Search in sources :

Example 1 with OutputStream

use of com.xenoage.utils.io.OutputStream in project Zong by Xenoage.

the class OggScoreFileOutput method writeOgg.

/**
 * Writes the given score as a OGG Vorbis file into the given stream.
 */
public static void writeOgg(Score score, OutputStream stream) throws IOException {
    // save temporary WAVE file first
    File tempFile = File.createTempFile(OggScoreFileOutput.class.getName(), ".wav");
    try (OutputStream tempStream = new JseOutputStream(tempFile)) {
        WavScoreFileOutput.writeWav(score, tempStream);
    }
    // convert to ogg
    VorbisEncoder.convert(tempFile.getAbsolutePath(), new JseOutputStream(stream));
    tempFile.delete();
}
Also used : OutputStream(com.xenoage.utils.io.OutputStream) JseOutputStream(com.xenoage.utils.jse.io.JseOutputStream) JseOutputStream(com.xenoage.utils.jse.io.JseOutputStream) File(java.io.File)

Example 2 with OutputStream

use of com.xenoage.utils.io.OutputStream in project Zong by Xenoage.

the class Mp3ScoreFileOutput method writeMp3.

/**
 * Writes the given score as a MP3 into the given stream.
 */
public static void writeMp3(Score score, OutputStream stream) throws IOException {
    // look if LAME is installed
    try {
        Runtime.getRuntime().exec("lame");
    } catch (Exception ex) {
        handle(Companion.warning(Lang.get(Voc.CouldNotFindLAME, Zong.INSTANCE.getWebsite() + "/lame")));
    }
    // save temporary WAVE file first
    File tempWAVFile = File.createTempFile(Mp3ScoreFileOutput.class.getName(), ".wav");
    try (OutputStream tempStream = new JseOutputStream(tempWAVFile)) {
        WavScoreFileOutput.writeWav(score, tempStream);
    }
    // create temporary MP3 file
    File tempMP3File = File.createTempFile(Mp3ScoreFileOutput.class.getName(), ".mp3");
    // convert to MP3
    try {
        Process process = new ProcessBuilder("lame", tempWAVFile.getAbsolutePath(), tempMP3File.getAbsolutePath()).start();
        if (debugToConsole) {
            // forward LAME error output to console
            try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            }
        }
        process.waitFor();
        int exitValue = process.exitValue();
        if (exitValue != 0)
            throw new IOException("LAME process returned: " + exitValue);
        tempWAVFile.delete();
        // copy MP3 file to output stream
        try (JseOutputStream mp3Stream = new JseOutputStream(stream)) {
            JseFileUtils.copyFileToStream(tempMP3File, mp3Stream);
        }
        tempMP3File.delete();
    } catch (Exception ex) {
        tempWAVFile.delete();
        tempMP3File.delete();
        throw new IOException(ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(com.xenoage.utils.io.OutputStream) JseOutputStream(com.xenoage.utils.jse.io.JseOutputStream) JseOutputStream(com.xenoage.utils.jse.io.JseOutputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Aggregations

OutputStream (com.xenoage.utils.io.OutputStream)2 JseOutputStream (com.xenoage.utils.jse.io.JseOutputStream)2 File (java.io.File)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1