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();
}
}
}
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;
}
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);
}
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');
}
}
}
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;
}
}
}
Aggregations