com.jme3.audio.AudioKey Java Examples

The following examples show how to use com.jme3.audio.AudioKey. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WAVLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Object load(AssetInfo info) throws IOException {
    AudioData data;
    InputStream inputStream = null;
    try {
        inputStream = info.openStream();
        data = load(inputStream, ((AudioKey)info.getKey()).isStream());
        if (data instanceof AudioStream){
            inputStream = null;
        }
        return data;
    } finally {
        if (inputStream != null){
            inputStream.close();
        }
    }
}
 
Example #2
Source File: OGGLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Object load(AssetInfo info) throws IOException {
    if (!(info.getKey() instanceof AudioKey)){
        throw new IllegalArgumentException("Audio assets must be loaded using an AudioKey");
    }
    
    AudioKey key = (AudioKey) info.getKey();
    boolean readStream = key.isStream();
    boolean streamCache = key.useStreamCache();
    
    InputStream in = null;
    try {
        in = info.openStream();
        AudioData data = load(in, readStream, streamCache);
        if (data instanceof AudioStream){
            // audio streams must remain open
            in = null;
        }
        return data;
    } finally {
        if (in != null){
            in.close();
        }
    }
    
}
 
Example #3
Source File: WAVLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object load(AssetInfo info) throws IOException {
    AudioData data;
    InputStream inputStream = null;
    try {
        inputStream = info.openStream();
        data = load(info, inputStream, ((AudioKey)info.getKey()).isStream());
        if (data instanceof AudioStream){
            inputStream = null;
        }
        return data;
    } finally {
        if (inputStream != null){
            inputStream.close();
        }
    }
}
 
Example #4
Source File: OGGLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Object load(AssetInfo info) throws IOException {
    if (!(info.getKey() instanceof AudioKey)){
        throw new IllegalArgumentException("Audio assets must be loaded using an AudioKey");
    }
    
    AudioKey key = (AudioKey) info.getKey();
    boolean readStream = key.isStream();
    boolean streamCache = key.useStreamCache();
    
    InputStream in = null;
    try {
        in = info.openStream();
        AudioData data = load(in, readStream, streamCache);
        if (readStream && !streamCache) {
            // we still need the stream in this case ..
            in = null;
        }
        return data;
    } finally {
        if (in != null){
            in.close();
        }
    }
    
}
 
Example #5
Source File: PlayAudioNodeAction.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@Override
@FxThread
protected void process() {
    super.process();

    final AudioTreeNode audioModelNode = (AudioTreeNode) getNode();
    final AudioNode audioNode = audioModelNode.getElement();

    final AssetManager assetManager = EditorUtil.getAssetManager();

    EXECUTOR_MANAGER.addJmeTask(() -> {

        final AudioKey audioKey = AudioNodeUtils.getAudioKey(audioNode);
        final AudioData audioData = assetManager.loadAudio(audioKey);

        AudioNodeUtils.updateData(audioNode, audioData, audioKey);

        audioNode.play();
    });
}
 
Example #6
Source File: AudioViewer3DPart.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Load the audio data.
 *
 * @param audioData the audio data.
 * @param audioKey  the audio key.
 */
@JmeThread
private void loadImpl(@NotNull final AudioData audioData, @NotNull final AudioKey audioKey) {
    removeAudioNode();
    setAudioData(audioData);
    setAudioKey(audioKey);

    final Node stateNode = getStateNode();

    final AudioNode audioNode = new AudioNode(audioData, audioKey);
    audioNode.setPositional(false);
    stateNode.attachChild(audioNode);

    setAudioNode(audioNode);
}
 
Example #7
Source File: AndroidAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void deleteAudioData(AudioData ad) {
    if (ad instanceof AndroidAudioData) {
        AndroidAudioData audioData = (AndroidAudioData) ad;
        if (audioData.getAssetKey() instanceof AudioKey) {
            AudioKey assetKey = (AudioKey) audioData.getAssetKey();
            if (assetKey.isStream()) {
                for (AudioNode src : musicPlaying.keySet()) {
                    if (src.getAudioData() == ad) {
                        MediaPlayer mp = musicPlaying.get(src);
                        mp.stop();
                        mp.release();
                        musicPlaying.remove(src);
                        src.setStatus(Status.Stopped);
                        src.setChannel(-1);
                        break;
                    }
                }
            } else {
                if (audioData.getId() > 0) {
                    soundPool.unload(audioData.getId());
                }
                audioData.setId(0);
            }

        }
    } else {
        throw new IllegalArgumentException("AudioData is not of type AndroidAudioData in deleteAudioData");
    }
}
 
Example #8
Source File: NativeVorbisLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Object load(AssetInfo assetInfo) throws IOException {
    AudioKey key = (AudioKey) assetInfo.getKey();
    if (!(assetInfo instanceof AndroidLocator.AndroidAssetInfo)) {
        throw new UnsupportedOperationException("Cannot load audio files from classpath." + 
                                                "Place your audio files in " +
                                                "Android's assets directory");
    }
    
    if (key.isStream()) {
        return loadStream(assetInfo);
    } else {
        return loadBuffer(assetInfo);
    }
}
 
Example #9
Source File: AudioNodeUtils.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Get an audio key from the audio node.
 *
 * @param audioNode the audio node.
 * @return the audio key.
 */
@FromAnyThread
public static @Nullable AudioKey getAudioKey(@NotNull AudioNode audioNode) {
    try {
        return (AudioKey) AUDIO_KEY_FIELD.get(audioNode);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
Source File: AudioNodeUtils.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Update audio data for an audio node.
 *
 * @param audioNode the audio node.
 * @param audioData the audio data.
 * @param audioKey  the audio key.
 */
@JmeThread
public static void updateData(
        @NotNull AudioNode audioNode,
        @Nullable AudioData audioData,
        @Nullable AudioKey audioKey
) {
    try {
        AUDIO_DATA_FIELD.set(audioNode, audioData);
        AUDIO_KEY_FIELD.set(audioNode, audioKey);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example #11
Source File: AudioViewerEditor.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
@Override
@FxThread
public void openFile(@NotNull final Path file) {
    super.openFile(file);

    final Path assetFile = notNull(EditorUtil.getAssetFile(file));
    final String assetPath = EditorUtil.toAssetPath(assetFile);

    final AudioKey audioKey = new AudioKey(assetPath);
    final AssetManager assetManager = EditorUtil.getAssetManager();
    final AudioData audioData = assetManager.loadAudio(audioKey);

    final float duration = audioData.getDuration();
    final int bitsPerSample = audioData.getBitsPerSample();
    final int channels = audioData.getChannels();
    final AudioData.DataType dataType = audioData.getDataType();
    final int sampleRate = audioData.getSampleRate();

    final AudioViewer3DPart editorAppState = getEditorAppState();
    editorAppState.load(audioData, audioKey);

    getChannelsField().setText(String.valueOf(channels));
    getDurationField().setText(String.valueOf(duration));
    getDataTypeField().setText(String.valueOf(dataType));
    getSampleRateField().setText(String.valueOf(sampleRate));
    getBitsPerSampleField().setText(String.valueOf(bitsPerSample));
}
 
Example #12
Source File: AudioKeyPropertyControl.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Add the new audio data.
 *
 * @param file the audio file.
 */
@FxThread
private void addAudioData(@NotNull Path file) {

    var assetFile = notNull(getAssetFile(file));
    var audioKey = new AudioKey(toAssetPath(assetFile));

    changed(audioKey, getPropertyValue());
    setIgnoreListener(true);
    try {
        reload();
    } finally {
        setIgnoreListener(false);
    }
}
 
Example #13
Source File: AudioKeyPropertyControl.java    From jmonkeybuilder with Apache License 2.0 5 votes vote down vote up
public AudioKeyPropertyControl(
        @Nullable AudioKey element,
        @NotNull String paramName,
        @NotNull C changeConsumer
) {
    super(element, paramName, changeConsumer);
    setOnDragOver(this::handleDragOverEvent);
    setOnDragDropped(this::handleDragDroppedEvent);
    setOnDragExited(this::handleDragExitedEvent);
}
 
Example #14
Source File: AudioKeyPropertyControl.java    From jmonkeybuilder with Apache License 2.0 4 votes vote down vote up
private @NotNull String getKeyLabel(@Nullable AudioKey assetKey) {
    return EditorUtil.isEmpty(assetKey) ? NO_AUDIO : assetKey.getName();
}
 
Example #15
Source File: DesktopAssetManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public AudioData loadAudio(AudioKey key){
    return loadAsset(key);
}
 
Example #16
Source File: DesktopAssetManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public AudioData loadAudio(String name){
    return loadAudio(new AudioKey(name, false));
}
 
Example #17
Source File: DesktopAssetManager.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public AudioData loadAudio(AudioKey key){
    return (AudioData) loadAsset(key);
}
 
Example #18
Source File: DesktopAssetManager.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public AudioData loadAudio(String name){
    return loadAudio(new AudioKey(name, false));
}
 
Example #19
Source File: AssetManager.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Load audio file, supported types are WAV or OGG.
 * @param key Asset key of the audio file to load
 * @return The audio data loaded
 *
 * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
 */
public AudioData loadAudio(AudioKey key);
 
Example #20
Source File: AudioViewer3DPart.java    From jmonkeybuilder with Apache License 2.0 2 votes vote down vote up
/**
 * Set the audio key.
 *
 * @param audioKey the audio key.
 */
@JmeThread
private void setAudioKey(@NotNull final AudioKey audioKey) {
    this.audioKey = audioKey;
}
 
Example #21
Source File: AudioViewer3DPart.java    From jmonkeybuilder with Apache License 2.0 2 votes vote down vote up
/**
 * Get the audio key.
 *
 * @return the audio key.
 */
@JmeThread
private @NotNull AudioKey getAudioKey() {
    return notNull(audioKey);
}
 
Example #22
Source File: AudioViewer3DPart.java    From jmonkeybuilder with Apache License 2.0 2 votes vote down vote up
/**
 * Load the audio data.
 *
 * @param audioData the audio data.
 * @param audioKey  the audio key.
 */
@FromAnyThread
public void load(@NotNull final AudioData audioData, @NotNull final AudioKey audioKey) {
    EXECUTOR_MANAGER.addJmeTask(() -> loadImpl(audioData, audioKey));
}
 
Example #23
Source File: AssetManager.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Load audio file, supported types are WAV or OGG.
 * @param key
 * @return The audio data loaded
 *
 * @see AssetManager#loadAsset(com.jme3.asset.AssetKey)
 */
public AudioData loadAudio(AudioKey key);