Search in sources :

Example 6 with AudioData

use of com.jme3.audio.AudioData in project jmonkeyengine by jMonkeyEngine.

the class WAVLoader method load.

private AudioData load(AssetInfo info, InputStream inputStream, boolean stream) throws IOException {
    this.in = new ResettableInputStream(info, inputStream);
    inOffset = 0;
    int sig = in.readInt();
    if (sig != i_RIFF)
        throw new IOException("File is not a WAVE file");
    // skip size
    in.readInt();
    if (in.readInt() != i_WAVE)
        throw new IOException("WAVE File does not contain audio");
    inOffset += 4 * 3;
    readStream = stream;
    if (readStream) {
        audioStream = new AudioStream();
        audioData = audioStream;
    } else {
        audioBuffer = new AudioBuffer();
        audioData = audioBuffer;
    }
    while (true) {
        int type = in.readInt();
        int len = in.readInt();
        inOffset += 4 * 2;
        switch(type) {
            case i_fmt:
                readFormatChunk(len);
                inOffset += len;
                break;
            case i_data:
                // Compute duration based on data chunk size
                duration = (float) (len / bytesPerSec);
                if (readStream) {
                    readDataChunkForStream(inOffset, len);
                } else {
                    readDataChunkForBuffer(len);
                }
                return audioData;
            default:
                int skipped = in.skipBytes(len);
                if (skipped <= 0) {
                    return null;
                }
                inOffset += skipped;
                break;
        }
    }
}
Also used : AudioStream(com.jme3.audio.AudioStream) IOException(java.io.IOException) AudioBuffer(com.jme3.audio.AudioBuffer)

Example 7 with AudioData

use of com.jme3.audio.AudioData in project jmonkeyengine by jMonkeyEngine.

the class TestMusicPlayer method btnOpenActionPerformed.

// </editor-fold>//GEN-END:initComponents
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
    //GEN-FIRST:event_btnOpenActionPerformed
    JFileChooser chooser = new JFileChooser();
    chooser.setAcceptAllFileFilterUsed(true);
    chooser.setDialogTitle("Select OGG file");
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    chooser.setMultiSelectionEnabled(false);
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        btnStopActionPerformed(null);
        final File selected = chooser.getSelectedFile();
        AssetLoader loader = null;
        if (selected.getName().endsWith(".wav")) {
            loader = new WAVLoader();
        } else {
            loader = new OGGLoader();
        }
        AudioKey key = new AudioKey(selected.getName(), true, true);
        try {
            musicData = (AudioData) loader.load(new AssetInfo(null, key) {

                @Override
                public InputStream openStream() {
                    try {
                        return new FileInputStream(selected);
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    }
                    return null;
                }
            });
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        musicSource = new AudioNode(musicData, key);
        musicLength = musicData.getDuration();
        updateTime();
    }
}
Also used : OGGLoader(com.jme3.audio.plugins.OGGLoader) AssetInfo(com.jme3.asset.AssetInfo) AssetLoader(com.jme3.asset.AssetLoader) JFileChooser(javax.swing.JFileChooser) WAVLoader(com.jme3.audio.plugins.WAVLoader)

Aggregations

AudioBuffer (com.jme3.audio.AudioBuffer)3 AudioData (com.jme3.audio.AudioData)3 AudioStream (com.jme3.audio.AudioStream)3 AudioKey (com.jme3.audio.AudioKey)2 InputStream (java.io.InputStream)2 AssetInfo (com.jme3.asset.AssetInfo)1 AssetLoader (com.jme3.asset.AssetLoader)1 AssetManager (com.jme3.asset.AssetManager)1 OGGLoader (com.jme3.audio.plugins.OGGLoader)1 WAVLoader (com.jme3.audio.plugins.WAVLoader)1 Texture (com.jme3.texture.Texture)1 LogicalOggStream (de.jarnbjo.ogg.LogicalOggStream)1 VorbisStream (de.jarnbjo.vorbis.VorbisStream)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 JFileChooser (javax.swing.JFileChooser)1