Java Code Examples for com.jme3.audio.AudioNode#getAudioData()

The following examples show how to use com.jme3.audio.AudioNode#getAudioData() . 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: 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 2
Source File: AndroidAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
    AudioNode src = mapLoadingAudioNodes.get(sampleId);
    if (src.getAudioData() instanceof AndroidAudioData) {
        AndroidAudioData audioData = (AndroidAudioData) src.getAudioData();

        if (status == 0) // load was successfull
        {
            int channelIndex;
            channelIndex = soundPool.play(audioData.getId(), 1f, 1f, 1, -1, 1f);
            src.setChannel(channelIndex);
            // Playing started ?
            if (src.getChannel() > 0) {
                src.setStatus(Status.Playing);
            }
        } else {
            src.setChannel(-1);
        }
    } else {
        throw new IllegalArgumentException("AudioData is not of type AndroidAudioData for AudioNode " + src.toString());
    }
}
 
Example 3
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 4
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 5
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void clearChannel(int index){
    // make room at this channel
    if (chanSrcs[index] != null){
        AudioNode src = chanSrcs[index];

        int sourceId = channels[index];
        alSourceStop(sourceId);

        if (src.getAudioData() instanceof AudioStream){
            AudioStream str = (AudioStream) src.getAudioData();
            ib.position(0).limit(STREAMING_BUFFER_COUNT);
            ib.put(str.getIds()).flip();
            alSourceUnqueueBuffers(sourceId, ib);
        }else if (src.getAudioData() instanceof AudioBuffer){
            alSourcei(sourceId, AL_BUFFER, 0);
        }

        if (src.getDryFilter() != null && supportEfx){
            // detach filter
            alSourcei(sourceId, EFX10.AL_DIRECT_FILTER, EFX10.AL_FILTER_NULL);
        }
        if (src.isPositional()){
            AudioNode pas = (AudioNode) src;
            if (pas.isReverbEnabled() && supportEfx) {
                AL11.alSource3i(sourceId, EFX10.AL_AUXILIARY_SEND_FILTER, 0, 0, EFX10.AL_FILTER_NULL);
            }
        }

        chanSrcs[index] = null;
    }
}
 
Example 6
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void playSourceInstance(AudioNode src){
    checkDead();
    synchronized (threadLock){
        while (!threadLock.get()){
            try {
                threadLock.wait();
            } catch (InterruptedException ex) {
            }
        }
        if (audioDisabled)
            return;
        
        if (src.getAudioData() instanceof AudioStream)
            throw new UnsupportedOperationException(
                    "Cannot play instances " +
                    "of audio streams. Use playSource() instead.");

        if (src.getAudioData().isUpdateNeeded()){
            updateAudioData(src.getAudioData());
        }

        // create a new index for an audio-channel
        int index = newChannel();
        if (index == -1)
            return;

        int sourceId = channels[index];

        clearChannel(index);

        // set parameters, like position and max distance
        setSourceParams(sourceId, src, true);
        attachAudioToSource(sourceId, src.getAudioData());
        chanSrcs[index] = src;

        // play the channel
        alSourcePlay(sourceId);
    }
}
 
Example 7
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void playSource(AudioNode src) {
    checkDead();
    synchronized (threadLock){
        while (!threadLock.get()){
            try {
                threadLock.wait();
            } catch (InterruptedException ex) {
            }
        }
        if (audioDisabled)
            return;

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

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

            // allocate channel to this source
            int index = newChannel();
            if (index == -1) {
                logger.log(Level.WARNING, "No channel available to play {0}", src);
                return;
            }
            clearChannel(index);
            src.setChannel(index);

            AudioData data = src.getAudioData();
            if (data.isUpdateNeeded())
                updateAudioData(data);

            chanSrcs[index] = src;
            setSourceParams(channels[index], src, false);
            attachAudioToSource(channels[index], data);
        }

        alSourcePlay(channels[src.getChannel()]);
        src.setStatus(Status.Playing);
    }
}