Search in sources :

Example 26 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class NativeVorbisLoader method loadBuffer.

private static AudioBuffer loadBuffer(AssetInfo assetInfo) throws IOException {
    AndroidAssetInfo aai = (AndroidAssetInfo) assetInfo;
    AssetFileDescriptor afd = null;
    NativeVorbisFile file = null;
    try {
        afd = aai.openFileDescriptor();
        int fd = afd.getParcelFileDescriptor().getFd();
        file = new NativeVorbisFile(fd, afd.getStartOffset(), afd.getLength());
        ByteBuffer data = BufferUtils.createByteBuffer(file.totalBytes);
        file.readFully(data);
        AudioBuffer ab = new AudioBuffer();
        ab.setupFormat(file.channels, 16, file.sampleRate);
        ab.updateData(data);
        return ab;
    } finally {
        if (file != null) {
            file.close();
        }
        if (afd != null) {
            afd.close();
        }
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) AudioBuffer(com.jme3.audio.AudioBuffer) AndroidAssetInfo(com.jme3.asset.plugins.AndroidLocator.AndroidAssetInfo) ByteBuffer(java.nio.ByteBuffer)

Example 27 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class DesktopAssetManager method loadAsset.

@Override
public <T> T loadAsset(AssetKey<T> key) {
    if (key == null)
        throw new IllegalArgumentException("key cannot be null");
    for (AssetEventListener listener : eventListeners) {
        listener.assetRequested(key);
    }
    AssetCache cache = handler.getCache(key.getCacheType());
    AssetProcessor proc = handler.getProcessor(key.getProcessorType());
    Object obj = cache != null ? cache.getFromCache(key) : null;
    if (obj == null) {
        // Asset not in cache, load it from file system.
        AssetInfo info = handler.tryLocate(key);
        if (info == null) {
            if (handler.getParentKey() != null) {
                // that something went wrong.
                for (AssetEventListener listener : eventListeners) {
                    listener.assetDependencyNotFound(handler.getParentKey(), key);
                }
            }
            throw new AssetNotFoundException(key.toString());
        }
        obj = loadLocatedAsset(key, info, proc, cache);
    }
    T clone = (T) obj;
    if (obj instanceof CloneableSmartAsset) {
        clone = registerAndCloneSmartAsset(key, clone, proc, cache);
    }
    return clone;
}
Also used : AssetCache(com.jme3.asset.cache.AssetCache)

Example 28 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class Context method createProgramFromSourceFilesWithInclude.

/**
     * Creates a program object from the provided source code and files.
     * The source code is made up from the specified include string first, 
     * then all files specified by the resource array (array of asset paths)
     * are loaded by the provided asset manager and appended to the source code.
     * <p>
     * The typical use case is:
     * <ul>
     *  <li>The include string contains some compiler constants like the grid size </li>
     *  <li>Some common OpenCL files used as libraries (Convention: file names end with {@code .clh}</li>
     *  <li>One main OpenCL file containing the actual kernels (Convention: file name ends with {@code .cl})</li>
     * </ul>
     * 
     * After the files were combined, additional include statements are resolved
     * by {@link #createProgramFromSourceCodeWithDependencies(java.lang.String, com.jme3.asset.AssetManager) }.
     * 
     * @param assetManager the asset manager used to load the files
     * @param include an additional include string
     * @param resources an array of asset paths pointing to OpenCL source files
     * @return the new program objects
     * @throws AssetNotFoundException if a file could not be loaded
     */
public Program createProgramFromSourceFilesWithInclude(AssetManager assetManager, String include, List<String> resources) {
    StringBuilder str = new StringBuilder();
    str.append(include);
    for (String res : resources) {
        AssetInfo info = assetManager.locateAsset(new AssetKey<String>(res));
        if (info == null) {
            throw new AssetNotFoundException("Unable to load source file \"" + res + "\"");
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(info.openStream()))) {
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                str.append(line).append('\n');
            }
        } catch (IOException ex) {
            LOG.log(Level.WARNING, "unable to load source file '" + res + "'", ex);
        }
    }
    return createProgramFromSourceCodeWithDependencies(str.toString(), assetManager);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) IOException(java.io.IOException) AssetInfo(com.jme3.asset.AssetInfo)

Example 29 with AssetInfo

use of com.jme3.asset.AssetInfo in project jmonkeyengine by jMonkeyEngine.

the class Context method buildSourcesRec.

private void buildSourcesRec(BufferedReader reader, StringBuilder builder, AssetManager assetManager) throws IOException {
    String ln;
    while ((ln = reader.readLine()) != null) {
        if (ln.trim().startsWith("#import ")) {
            ln = ln.trim().substring(8).trim();
            if (ln.startsWith("\"")) {
                ln = ln.substring(1);
            }
            if (ln.endsWith("\"")) {
                ln = ln.substring(0, ln.length() - 1);
            }
            AssetInfo info = assetManager.locateAsset(new AssetKey<String>(ln));
            if (info == null) {
                throw new AssetNotFoundException("Unable to load source file \"" + ln + "\"");
            }
            try (BufferedReader r = new BufferedReader(new InputStreamReader(info.openStream()))) {
                builder.append("//-- begin import ").append(ln).append(" --\n");
                buildSourcesRec(r, builder, assetManager);
                builder.append("//-- end import ").append(ln).append(" --\n");
            }
        } else {
            builder.append(ln).append('\n');
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) AssetInfo(com.jme3.asset.AssetInfo)

Example 30 with AssetInfo

use of com.jme3.asset.AssetInfo 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)

Aggregations

InputStream (java.io.InputStream)17 TextureKey (com.jme3.asset.TextureKey)12 IOException (java.io.IOException)9 Image (com.jme3.texture.Image)7 AssetInfo (com.jme3.asset.AssetInfo)6 Texture (com.jme3.texture.Texture)6 AssetLoadException (com.jme3.asset.AssetLoadException)5 Test (org.junit.Test)5 AssetKey (com.jme3.asset.AssetKey)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)4 ModelKey (com.jme3.asset.ModelKey)4 InputStreamReader (java.io.InputStreamReader)4 BlenderKey (com.jme3.asset.BlenderKey)3 AudioStream (com.jme3.audio.AudioStream)3 MaterialDef (com.jme3.material.MaterialDef)3 DataInputStream (java.io.DataInputStream)3 AssetFileDescriptor (android.content.res.AssetFileDescriptor)2 AssetManager (com.jme3.asset.AssetManager)2 AndroidAssetInfo (com.jme3.asset.plugins.AndroidLocator.AndroidAssetInfo)2 AudioBuffer (com.jme3.audio.AudioBuffer)2