com.jme3.audio.AudioNode Java Examples

The following examples show how to use com.jme3.audio.AudioNode. 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: AbstractSceneFileEditor.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@Override
@FxThread
public void notifyFxRemovedChild(@NotNull final Object parent, @NotNull final Object removed) {

    final MA editor3DPart = getEditor3DPart();
    final ModelNodeTree modelNodeTree = getModelNodeTree();
    modelNodeTree.notifyRemoved(parent, removed);

    if (removed instanceof Light) {
        editor3DPart.removeLight((Light) removed);
    } else if (removed instanceof AudioNode) {
        editor3DPart.removeAudioNode((AudioNode) removed);
    } else if (removed instanceof Spatial) {
        handleRemovedObject((Spatial) removed);
    }
}
 
Example #2
Source File: TestDoppler.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
    public void simpleInitApp() {
        flyCam.setMoveSpeed(10);

        Torus torus = new Torus(10, 6, 1, 3);
        Geometry g = new Geometry("Torus Geom", torus);
        g.rotate(-FastMath.HALF_PI, 0, 0);
        g.center();

        g.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
//        rootNode.attachChild(g);

        ufoNode = new AudioNode(assetManager, "Sound/Effects/Beep.ogg", AudioData.DataType.Buffer);
        ufoNode.setLooping(true);
        ufoNode.setPitch(0.5f);
        ufoNode.setRefDistance(1);
        ufoNode.setMaxDistance(100000000);
        ufoNode.setVelocityFromTranslation(true);
        ufoNode.play();

        Geometry ball = new Geometry("Beeper", new Sphere(10, 10, 0.1f));
        ball.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
        ufoNode.attachChild(ball);

        rootNode.attachChild(ufoNode);
    }
 
Example #3
Source File: CreateAudioNodeAction.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@Override
@FxThread
protected void process() {
    super.process();

    final NodeTree<?> nodeTree = getNodeTree();

    final AudioNode node = new AudioNode();
    node.setName("New audio");

    final TreeNode<?> treeNode = getNode();
    final Node parent = (Node) treeNode.getElement();

    final ChangeConsumer changeConsumer = notNull(nodeTree.getChangeConsumer());
    changeConsumer.execute(new AddChildOperation(node, parent));
}
 
Example #4
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void pauseSource(AudioNode src) {
    checkDead();
    synchronized (threadLock){
        while (!threadLock.get()){
            try {
                threadLock.wait();
            } catch (InterruptedException ex) {
            }
        }
        if (audioDisabled)
            return;
        
        if (src.getStatus() == Status.Playing){
            assert src.getChannel() != -1;

            alSourcePause(channels[src.getChannel()]);
            src.setStatus(Status.Paused);
        }
    }
}
 
Example #5
Source File: AudioViewer3DPart.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Play the current audio.
 */
@JmeThread
private void playImpl() {

    final AudioNode currentAudioNode = getAudioNode();

    if (currentAudioNode != null) {

        final Status status = currentAudioNode.getStatus();

        if (status == Status.Paused) {
            currentAudioNode.play();
            return;
        } else if (status == Status.Playing) {
            return;
        }
    }

    loadImpl(getAudioData(), getAudioKey());

    final AudioNode audioNode = getAudioNode();
    audioNode.play();
}
 
Example #6
Source File: AudioViewer3DPart.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Remove the current audio node.
 */
@JmeThread
private void removeAudioNode() {

    final AudioNode currentAudioNode = getAudioNode();
    if (currentAudioNode == null) {
        return;
    }

    final Node stateNode = getStateNode();
    final Status status = currentAudioNode.getStatus();

    if (status == Status.Playing || status == Status.Paused) {
        currentAudioNode.stop();
    }

    stateNode.detachChild(currentAudioNode);

    setAudioNode(null);
    setPrevStatus(null);
}
 
Example #7
Source File: SoundPerformer.java    From OpenRTS with MIT License 6 votes vote down vote up
@Override
public void perform(Actor a) {
	SoundActor actor = (SoundActor)a;
	AudioNode audio = actorDrawer.getAudioNode(actor.soundPath);
	audio.setPositional(actor.positional);
	if(actor.positional)
		audio.setLocalTranslation(TranslateUtil.toVector3f(actor.getParentModelActor().getPos()));
	
	audio.setLooping(actor.looping);
	audio.setVolume((float)(actor.volume));
	
	audio.setRefDistance(4);
	audio.setReverbEnabled(false);
	
	audio.playInstance();
	a.stopActing();
}
 
Example #8
Source File: AbstractSceneFileEditor.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@Override
@FxThread
public void notifyFxAddedChild(@NotNull final Object parent, @NotNull final Object added, final int index,
                               final boolean needSelect) {

    final MA editor3DPart = getEditor3DPart();
    final ModelNodeTree modelNodeTree = getModelNodeTree();
    modelNodeTree.notifyAdded(parent, added, index);

    if (added instanceof Light) {
        editor3DPart.addLight((Light) added);
    } else if (added instanceof AudioNode) {
        editor3DPart.addAudioNode((AudioNode) added);
    } else if (added instanceof Spatial) {
        handleAddedObject((Spatial) added);
    }

    if (needSelect) {
        EXECUTOR_MANAGER.addJmeTask(() -> EXECUTOR_MANAGER.addFxTask(() -> modelNodeTree.selectSingle(added)));
    }
}
 
Example #9
Source File: AbstractSceneEditor3DPart.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * The process of removing the audio node.
 */
@JmeThread
private void removeAudioNodeImpl(@NotNull final AudioNode audio) {

    final ObjectDictionary<AudioNode, EditorAudioNode> cachedAudioNodes = getCachedAudioNodes();
    final EditorAudioNode audioModel = cachedAudioNodes.get(audio);
    if (audioModel == null) {
        return;
    }

    audioModel.setAudioNode(null);

    final Node audioNode = getAudioNode();
    audioNode.detachChild(audioModel);
    audioNode.detachChild(notNull(audioModel.getModel()));

    getAudioNodes().fastRemove(audioModel);
}
 
Example #10
Source File: AbstractSceneEditor3DPart.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * The process of adding the audio node.
 */
@JmeThread
private void addAudioNodeImpl(@NotNull final AudioNode audio) {

    final ObjectDictionary<AudioNode, EditorAudioNode> cachedAudioNodes = getCachedAudioNodes();

    final EditorAudioNode audioModel = notNull(cachedAudioNodes.get(audio, () -> {

        final Node model = (Node) AUDIO_NODE_MODEL.clone();
        model.setLocalScale(0.01F);

        final EditorAudioNode result = new EditorAudioNode(getCamera());
        result.setModel(model);

        return result;
    }));

    audioModel.setAudioNode(audio);
    audioModel.sync();

    final Node audioNode = getAudioNode();
    audioNode.attachChild(audioModel);
    audioNode.attachChild(audioModel.getModel());

    getAudioNodes().add(audioModel);
}
 
Example #11
Source File: HelloAudio.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** We create two audio nodes. */
private void initAudio() {
  /* gun shot sound is to be triggered by a mouse click. */
  audio_gun = new AudioNode(assetManager, "Sound/Effects/Gun.wav", false);
  audio_gun.setLooping(false);
  audio_gun.setVolume(2);
  rootNode.attachChild(audio_gun);

  /* nature sound - keeps playing in a loop. */
  audio_nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", true);
  audio_nature.setLooping(true);  // activate continuous playing
  audio_nature.setPositional(true);
  audio_nature.setLocalTranslation(Vector3f.ZERO.clone());
  audio_nature.setVolume(3);
  rootNode.attachChild(audio_nature);
  audio_nature.play(); // play continuously!
}
 
Example #12
Source File: AudioTreeNode.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
@Override
@FxThread
public void fillContextMenu(@NotNull final NodeTree<?> nodeTree, @NotNull final ObservableList<MenuItem> items) {
    if (!(nodeTree instanceof ModelNodeTree)) return;

    final AudioNode element = getElement();
    final AudioData audioData = element.getAudioData();
    final AudioSource.Status status = element.getStatus();

    if (audioData != null && status != AudioSource.Status.Playing) {
        items.add(new PlayAudioNodeAction(nodeTree, this));
    } else if (audioData != null) {
        items.add(new StopAudioNodeAction(nodeTree, this));
    }

    super.fillContextMenu(nodeTree, items);
}
 
Example #13
Source File: SceneComposerToolController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void addMarkers(Node parent) {
    
    for (Light light : parent.getLocalLightList())
        addLightMarker(light);
    
    if (parent instanceof AudioNode) {
        addAudioMarker((AudioNode)parent);
    }
    
    for (Spatial s : parent.getChildren()) {
        if (s instanceof Node)
            addMarkers((Node)s);
        else {
            //TODO later if we include other types of non-spatial markers
        }
    }
}
 
Example #14
Source File: NodeUtils.java    From jmonkeybuilder with Apache License 2.0 6 votes vote down vote up
/**
 * Collect all audio nodes.
 *
 * @param spatial   the spatial.
 * @param container the container.
 */
@FromAnyThread
public static void addAudioNodes(@NotNull Spatial spatial, @NotNull Array<AudioNode> container) {

    if (!(spatial instanceof Node)) {
        return;
    }

    var node = (Node) spatial;

    for (var children : node.getChildren()) {
        if (children instanceof AudioNode) {
            container.add((AudioNode) children);
        }
        addAudioNodes(children, container);
    }
}
 
Example #15
Source File: AndroidAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onCompletion(MediaPlayer mp) {
    for (AudioNode src : musicPlaying.keySet()) {
        if (musicPlaying.get(src) == mp) {
            mp.seekTo(0);
            mp.stop();
            src.setStatus(Status.Stopped);
            break;
        }
    }

}
 
Example #16
Source File: AndroidAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void update(float tpf) {
    float distance;
    float volume;

    // Loop over all mediaplayers
    for (AudioNode src : musicPlaying.keySet()) {

        MediaPlayer mp = musicPlaying.get(src);
        {
            // Calc the distance to the listener
            distanceVector.set(listenerPosition);
            distanceVector.subtractLocal(src.getLocalTranslation());
            distance = FastMath.abs(distanceVector.length());

            if (distance < src.getRefDistance()) {
                distance = src.getRefDistance();
            }
            if (distance > src.getMaxDistance()) {
                distance = src.getMaxDistance();
            }
            volume = src.getRefDistance() / distance;

            AndroidAudioData audioData = (AndroidAudioData) src.getAudioData();

            if (FastMath.abs(audioData.getCurrentVolume() - volume) > FastMath.FLT_EPSILON) {
                // Left / Right channel get the same volume by now, only positional
                mp.setVolume(volume, volume);

                audioData.setCurrentVolume(volume);
            }


        }
    }
}
 
Example #17
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void stopSource(AudioNode src) {
    synchronized (threadLock){
        while (!threadLock.get()){
            try {
                threadLock.wait();
            } catch (InterruptedException ex) {
            }
        }
        if (audioDisabled)
            return;
        
        if (src.getStatus() != Status.Stopped){
            int chan = src.getChannel();
            assert chan != -1; // if it's not stopped, must have id

            src.setStatus(Status.Stopped);
            src.setChannel(-1);
            clearChannel(chan);
            freeChannel(chan);
            
            if (src.getAudioData() instanceof AudioStream) {
                AudioStream stream = (AudioStream)src.getAudioData();
                if (stream.isOpen()) {
                    stream.close();
                }
            
                // And free the audio since it cannot be
                // played again anyway.
                deleteAudioData(src.getAudioData());
            }                    
        }
    }
}
 
Example #18
Source File: TestAmbient.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void simpleInitApp()
{     
    /*
    footsteps  = new AudioNode(audioRenderer, assetManager, "Sound/Effects/Foot steps.ogg", true);
    
    footsteps.setPositional(true);
    footsteps.setLocalTranslation(new Vector3f(4, -1, 30));
    footsteps.setMaxDistance(5);
    footsteps.setRefDistance(1);
    footsteps.setLooping(true);

    beep = new AudioNode(audioRenderer, assetManager, "Sound/Effects/Beep.ogg", true);
    beep.setVolume(3);
    beep.setLooping(true);
    
    audioRenderer.playSourceInstance(footsteps);
    audioRenderer.playSource(beep);
    */
    
    waves  = new AudioNode(assetManager, "Sound/Environment/Ocean Waves.ogg", true);
    waves.setPositional(true);

    nature = new AudioNode(assetManager, "Sound/Environment/Nature.ogg", true);
    
    waves.setLocalTranslation(new Vector3f(4, -1, 30));
    waves.setMaxDistance(5);
    waves.setRefDistance(1);
    
    nature.setVolume(3);
    audioRenderer.playSourceInstance(waves);
    audioRenderer.playSource(nature);
}
 
Example #19
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 #20
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 #21
Source File: JmeAudioNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected Sheet createSheet() {
    //TODO: multithreading..
    Sheet sheet = super.createSheet();
    Sheet.Set set = Sheet.createPropertiesSet();
    set.setDisplayName("AudioNode");
    set.setName(AudioNode.class.getName());
    AudioNode obj = node;//getLookup().lookup(Spatial.class);
    if (obj == null) {
        return sheet;
    }

    set.put(new AudioDataProperty(obj));
    set.put(makeProperty(obj, Vector3f.class, "getDirection", "setDirection", "Direction"));
    set.put(makeProperty(obj, boolean.class, "isDirectional", "setDirectional", "Directional"));
    set.put(makeProperty(obj, float.class, "getInnerAngle", "setInnerAngle", "Inner Angle"));
    set.put(makeProperty(obj, float.class, "getOuterAngle", "setOuterAngle", "Outer Angle"));
    set.put(makeProperty(obj, Filter.class, "getDryFilter", "setDryFilter", "Dry Filter"));
    set.put(makeProperty(obj, boolean.class, "isLooping", "setLooping", "Looping"));
    set.put(makeProperty(obj, float.class, "getMaxDistance", "setMaxDistance", "Max Distance"));

    set.put(makeProperty(obj, float.class, "getPitch", "setPitch", "Audio Pitch"));
    set.put(makeProperty(obj, boolean.class, "isPositional", "setPositional", "Positional"));

    set.put(makeProperty(obj, boolean.class, "isReverbEnabled", "setReverbEnabled", "Reverb"));
    set.put(makeProperty(obj, Filter.class, "getReverbFilter", "setReverbFilter", "Reverb Filter"));
    set.put(makeProperty(obj, float.class, "getRefDistance", "setRefDistance", "Ref Distance"));
    set.put(makeProperty(obj, float.class, "getTimeOffset", "setTimeOffset", "Time Offset"));

    set.put(makeProperty(obj, Status.class, "getStatus", "setStatus", "Status"));

    set.put(makeProperty(obj, float.class, "getVolume", "setVolume", "Volume"));
    set.put(makeProperty(obj, Vector3f.class, "getVelocity", "setVelocity", "Velocity"));
    sheet.put(set);
    return sheet;

}
 
Example #22
Source File: AudioTrack.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * sets the audio node to be used for this track
 *
 * @param audio
 */
public void setAudio(AudioNode audio) {
    if (this.audio != null) {
        TrackInfo data = (TrackInfo) audio.getUserData("TrackInfo");
        data.getTracks().remove(this);
    }
    this.audio = audio;
    setUserData(this);
}
 
Example #23
Source File: AndroidAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void playSource(AudioNode src) {
    if (audioDisabled) {
        return;
    }

    //assert src.getStatus() == Status.Stopped || src.getChannel() == -1;

    if (src.getStatus() == Status.Playing) {
        return;
    } else if (src.getStatus() == Status.Stopped) {
        playSourceInstance(src);
    }


}
 
Example #24
Source File: AndroidAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void pauseSource(AudioNode src) {
    if (audioDisabled) {
        return;
    }

    if (src.getStatus() == Status.Playing) {
        if (src.getAudioData() instanceof AndroidAudioData) {
            AndroidAudioData audioData = (AndroidAudioData) src.getAudioData();
            if (audioData.getAssetKey() instanceof AudioKey) {
                AudioKey assetKey = (AudioKey) audioData.getAssetKey();

                if (assetKey.isStream()) {
                    MediaPlayer mp;
                    if (musicPlaying.containsKey(src)) {
                        mp = musicPlaying.get(src);
                        mp.pause();
                        src.setStatus(Status.Paused);
                    }
                } else {
                    assert src.getChannel() != -1;

                    if (src.getChannel() > 0) {
                        soundPool.pause(src.getChannel());
                        src.setStatus(Status.Paused);
                    }
                }
            }
        }

    }

}
 
Example #25
Source File: ActorDrawer.java    From OpenRTS with MIT License 5 votes vote down vote up
protected AudioNode getAudioNode(String soundPath) {
	if (!sounds.containsKey(soundPath)) {
		sounds.put(soundPath, new AudioNode(assetManager, "sounds/" + soundPath));
		mainNode.attachChild(sounds.get(soundPath));
	}
	return sounds.get(soundPath);
}
 
Example #26
Source File: AudioTrack.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Internal use only serialization
 *
 * @param im importer
 * @throws IOException Exception
 */
@Override
public void read(JmeImporter im) throws IOException {
    InputCapsule in = im.getCapsule(this);
    audio = (AudioNode) in.readSavable("audio", null);
    length = in.readFloat("length", length);
    startOffset = in.readFloat("startOffset", 0);
}
 
Example #27
Source File: TestMusicStreaming.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp(){
    assetManager.registerLocator("https://web.archive.org/web/20170625151521if_/http://www.vorbis.com/music/", UrlLocator.class);
    AudioNode audioSource = new AudioNode(assetManager, "Lumme-Badloop.ogg",
            AudioData.DataType.Stream);
    audioSource.setPositional(false);
    audioSource.setReverbEnabled(false);
    audioSource.play();
}
 
Example #28
Source File: NewSpatialPopup.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    SceneApplication.getApplication().enqueue(new Callable<Void>() {

        public Void call() throws Exception {
            AudioNode newItem = new AudioNode();
            newItem.setName("AudioNode");
            node.attachChild(newItem);
            addSpatialUndo(node, newItem);
            setModified();
            return null;
        }
    });
}
 
Example #29
Source File: TestReverb.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
  audioSource = new AudioNode(assetManager, "Sound/Effects/Bang.wav",
          AudioData.DataType.Buffer);

  float[] eax = new float[]{15, 38.0f, 0.300f, -1000, -3300, 0,
    1.49f, 0.54f, 1.00f, -2560, 0.162f, 0.00f, 0.00f, 0.00f,
    -229, 0.088f, 0.00f, 0.00f, 0.00f, 0.125f, 1.000f, 0.250f,
    0.000f, -5.0f, 5000.0f, 250.0f, 0.00f, 0x3f};
  audioRenderer.setEnvironment(new Environment(eax));
  Environment env = Environment.Cavern;
  audioRenderer.setEnvironment(env);
}
 
Example #30
Source File: TestReverb.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void simpleInitApp() {
  audioSource = new AudioNode(assetManager, "Sound/Effects/Bang.wav");

  float[] eax = new float[]{15, 38.0f, 0.300f, -1000, -3300, 0,
    1.49f, 0.54f, 1.00f, -2560, 0.162f, 0.00f, 0.00f, 0.00f,
    -229, 0.088f, 0.00f, 0.00f, 0.00f, 0.125f, 1.000f, 0.250f,
    0.000f, -5.0f, 5000.0f, 250.0f, 0.00f, 0x3f};
  audioRenderer.setEnvironment(new Environment(eax));
  Environment env = Environment.Cavern;
  audioRenderer.setEnvironment(env);
}