All Downloads are FREE. Search and download functionalities are using the official Maven repository.

be.yildizgames.module.audio.openal.OpenAlAudioEngine Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of the Yildiz-Engine project, licenced under the MIT License  (MIT)
 *
 *  Copyright (c) 2019 Grégory Van den Borre
 *
 *  More infos available: https://engine.yildiz-games.be
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 *  documentation files (the "Software"), to deal in the Software without restriction, including without
 *  limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 *  of the Software, and to permit persons to whom the Software is furnished to do so,
 *  subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all copies or substantial
 *  portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 *  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 *  OR COPYRIGHT  HOLDERS BE LIABLE FOR ANY CLAIM,
 *  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  SOFTWARE.
 *
 */

package be.yildizgames.module.audio.openal;

import be.yildizgames.common.exception.technical.NativeException;
import be.yildizgames.common.file.FileResource;
import be.yildizgames.common.file.ResourcePath;
import be.yildizgames.common.file.exception.FileMissingException;
import be.yildizgames.common.geometry.Point3D;
import be.yildizgames.common.jni.Native;
import be.yildizgames.common.jni.NativePointer;
import be.yildizgames.common.libloader.NativeResourceLoader;
import be.yildizgames.common.os.SystemUtil;
import be.yildizgames.module.audio.AudioFile;
import be.yildizgames.module.audio.BaseAudioEngine;
import be.yildizgames.module.audio.SoundCreationException;
import be.yildizgames.module.audio.SoundSource;
import jni.OpenAlSoundEngineNative;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/**
 * OpenAL implementation for the audio engine.
 *
 * @author Grégory Van den Borre
 */
public final class OpenAlAudioEngine extends BaseAudioEngine implements Native {

    private static final System.Logger LOGGER = System.getLogger(OpenAlAudioEngine.class.getName());

    /**
     * Object native pointer address.
     */
    private final NativePointer pointer;

    /**
     * List of existing buffers.
     */
    private final Map bufferList = new HashMap<>();

    private final List paths = new ArrayList<>();

    private boolean vfsAdded;

    /**
     * Simple constructor, load all libraries and initialize the engine.
     * @param loader Loader for the native libraries.
     * @throws AssertionError if loader is null.
     */
    private OpenAlAudioEngine(NativeResourceLoader loader) {
        super();
        LOGGER.log(System.Logger.Level.INFO, "Initializing OpenAL audio engine...");
        if(SystemUtil.isWindows()) {
            loader.loadBaseLibrary( "libFLAC-8", "libsndfile-1", "OpenAL32");
        } else if(SystemUtil.isLinux()) {
            loader.loadLibrary(  "libogg", "libFLAC", "libsndfile", "libopenal");
        }
        loader.loadLibrary("libyildizphysfs", "libyildizopenal");
        this.pointer = NativePointer.create(OpenAlSoundEngineNative.initialize());
        LOGGER.log(System.Logger.Level.INFO, "Initializing OpenAL audio engine complete.");
    }

    /**
     * Create an openal audio engine.
     * @param loader Loader for the native libraries.
     * @return The created openal audio engine.
     * @throws AssertionError if loader is null.
     */
    public static OpenAlAudioEngine create(NativeResourceLoader loader) {
        return new OpenAlAudioEngine(loader);
    }

    private void setListenerPosition(final Point3D pos) {
        OpenAlSoundEngineNative.setListenerPosition(this.pointer.getPointerAddress(), pos.x, pos.y, pos.z);
    }

    private void setListenerOrientation(final Point3D dir) {
        OpenAlSoundEngineNative.setListenerOrientation(this.pointer.getPointerAddress(), dir.x, dir.y, dir.z);
    }

    @Override
    public void update() {
        this.setListenerPosition(this.listener.getAbsolutePosition());
        this.setListenerOrientation(this.listener.getAbsoluteDirection());
    }

    @Override
    public SoundSource createSound(final String file) {
        Optional path = this.paths
                .stream()
                .filter(p -> p.exists(file))
                .findFirst();
        String toLoad = path.map(r -> r.getPath().isEmpty() ? file : r.getPath() + File.separator + file).orElse(file);
        FileResource.FileType type = path.isPresent() ? FileResource.FileType.DIRECTORY : FileResource.FileType.VFS;
        if(type == FileResource.FileType.VFS && !this.vfsAdded) {
            throw new SoundCreationException("Trying to load a file from VFS while none had been mounted " +
                    "or trying to load a non existing file on file system:" + toLoad);
        }
        try {
            if (!this.bufferList.containsKey(toLoad)) {
                this.bufferList.put(toLoad, new ALBuffer(type == FileResource.FileType.VFS ? AudioFile.vfs(toLoad): AudioFile.file(toLoad)));
            }
            return this.bufferList.get(toLoad).createSource();
        } catch (NativeException e) {
            throw new SoundCreationException(e);
        }
    }

    @Override
    public OpenAlAudioEngine addResourcePath(ResourcePath path) {
        if(Files.notExists(Paths.get(path.getPath()).toAbsolutePath())) {
            throw new FileMissingException(path.getPath() + " Cannot be found.");
        }
        if(path.getType() == FileResource.FileType.VFS) {
            OpenAlSoundEngineNative.registerVfsContainer(this.pointer.getPointerAddress(), path.getPath());
            this.vfsAdded = true;
        } else if (path.getType() == FileResource.FileType.DIRECTORY){
            this.paths.add(path);
        }
        return this;
    }

    @Override
    protected void closeImpl() {
        OpenAlSoundEngineNative.close(this.pointer.getPointerAddress());
    }

    @Override
    public void delete() {
        OpenAlSoundEngineNative.delete(this.pointer.getPointerAddress());
        this.pointer.delete();
    }

    @Override
    public NativePointer getPointer() {
        return pointer;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy