com.jme3.audio.AudioData Java Examples

The following examples show how to use com.jme3.audio.AudioData. 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: 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 #3
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 #4
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 #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: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private int convertFormat(AudioData ad){
    switch (ad.getBitsPerSample()){
        case 8:
            if (ad.getChannels() == 1)
                return AL_FORMAT_MONO8;
            else if (ad.getChannels() == 2)
                return AL_FORMAT_STEREO8;

            break;
        case 16:
            if (ad.getChannels() == 1)
                return AL_FORMAT_MONO16;
            else
                return AL_FORMAT_STEREO16;
    }
    throw new UnsupportedOperationException("Unsupported channels/bits combination: "+
                                            "bits="+ad.getBitsPerSample()+", channels="+ad.getChannels());
}
 
Example #7
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 #8
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 #9
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);
    }
}
 
Example #10
Source File: OGGLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private AudioData load(InputStream in, boolean readStream, boolean streamCache) throws IOException{
        if (readStream && streamCache){
            oggStream = new CachedOggStream(in);
        }else{
            oggStream = new UncachedOggStream(in);
        }

        Collection<LogicalOggStream> streams = oggStream.getLogicalStreams();
        loStream = streams.iterator().next();

//        if (loStream == null){
//            throw new IOException("OGG File does not contain vorbis audio stream");
//        }

        vorbisStream = new VorbisStream(loStream);
        streamHdr = vorbisStream.getIdentificationHeader();
//        commentHdr = vorbisStream.getCommentHeader();
    
        if (!readStream){
            AudioBuffer audioBuffer = new AudioBuffer();
            audioBuffer.setupFormat(streamHdr.getChannels(), 16, streamHdr.getSampleRate());
            audioBuffer.updateData(readToBuffer());
            return audioBuffer;
        }else{
            AudioStream audioStream = new AudioStream();
            audioStream.setupFormat(streamHdr.getChannels(), 16, streamHdr.getSampleRate());
            
            // might return -1 if unknown
            float streamDuration = computeStreamDuration();
            
            audioStream.updateData(readToStream(), streamDuration);
            return audioStream;
        }
    }
 
Example #11
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 #12
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean attachAudioToSource(int sourceId, AudioData data){
    if (data instanceof AudioBuffer){
        return attachBufferToSource(sourceId, (AudioBuffer) data);
    }else if (data instanceof AudioStream){
        return attachStreamToSource(sourceId, (AudioStream) data);
    }
    throw new UnsupportedOperationException();
}
 
Example #13
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 #14
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void updateAudioData(AudioData ad){
    if (ad instanceof AudioBuffer){
        updateAudioBuffer((AudioBuffer) ad);
    }else if (ad instanceof AudioStream){
        updateAudioStream((AudioStream) ad);
    }
}
 
Example #15
Source File: LwjglAudioRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void deleteAudioData(AudioData ad){
    synchronized (threadLock){
        while (!threadLock.get()){
            try {
                threadLock.wait();
            } catch (InterruptedException ex) {
            }
        }
        if (audioDisabled)
            return;
    
        if (ad instanceof AudioBuffer){
            AudioBuffer ab = (AudioBuffer) ad;
            int id = ab.getId();
            if (id != -1){
                ib.put(0,id);
                ib.position(0).limit(1);
                alDeleteBuffers(ib);
                ab.resetObject();
            }
        }else if (ad instanceof AudioStream){
            AudioStream as = (AudioStream) ad;
            int[] ids = as.getIds();
            if (ids != null){
                ib.clear();
                ib.put(ids).flip();
                alDeleteBuffers(ib);
                as.resetObject();
            }
        }
    }            
}
 
Example #16
Source File: TestAbsoluteLocators.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args){
    AssetManager am = new DesktopAssetManager();

    am.registerLoader(AWTLoader.class.getName(), "jpg");
    am.registerLoader(WAVLoader.class.getName(), "wav");

    // register absolute locator
    am.registerLocator("/",  ClasspathLocator.class.getName());

    // find a sound
    AudioData audio = am.loadAudio("Sound/Effects/Gun.wav");

    // find a texture
    Texture tex = am.loadTexture("Textures/Terrain/Pond/Pond.jpg");

    if (audio == null)
        throw new RuntimeException("Cannot find audio!");
    else
        System.out.println("Audio loaded from Sounds/Effects/Gun.wav");

    if (tex == null)
        throw new RuntimeException("Cannot find texture!");
    else
        System.out.println("Texture loaded from Textures/Terrain/Pond/Pond.jpg");

    System.out.println("Success!");
}
 
Example #17
Source File: PlaceholderAssets.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static AudioData getPlaceholderAudio(){
    AudioBuffer audioBuf = new AudioBuffer();
    audioBuf.setupFormat(1, 8, 44100);
    ByteBuffer bb = BufferUtils.createByteBuffer(1);
    bb.put((byte)0).flip();
    audioBuf.updateData(bb);
    return audioBuf;
}
 
Example #18
Source File: PlaceholderAssets.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static AudioData getPlaceholderAudio(){
    AudioBuffer audioBuf = new AudioBuffer();
    audioBuf.setupFormat(1, 8, 44100);
    ByteBuffer bb = BufferUtils.createByteBuffer(1);
    bb.put((byte)0).flip();
    audioBuf.updateData(bb);
    return audioBuf;
}
 
Example #19
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 #20
Source File: TestAbsoluteLocators.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args){
    AssetManager am = JmeSystem.newAssetManager();

    am.registerLoader(AWTLoader.class, "jpg");
    am.registerLoader(WAVLoader.class, "wav");

    // register absolute locator
    am.registerLocator("/",  ClasspathLocator.class);

    // find a sound
    AudioData audio = am.loadAudio("Sound/Effects/Gun.wav");

    // find a texture
    Texture tex = am.loadTexture("Textures/Terrain/Pond/Pond.jpg");

    if (audio == null)
        throw new RuntimeException("Cannot find audio!");
    else
        System.out.println("Audio loaded from Sounds/Effects/Gun.wav");

    if (tex == null)
        throw new RuntimeException("Cannot find texture!");
    else
        System.out.println("Texture loaded from Textures/Terrain/Pond/Pond.jpg");

    System.out.println("Success!");
}
 
Example #21
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 #22
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 #23
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 #24
Source File: OGGLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("unchecked")
    private AudioData load(InputStream in, boolean readStream, boolean streamCache) throws IOException{
        if (readStream && streamCache){
            oggStream = new CachedOggStream(in);
        }else{
            oggStream = new UncachedOggStream(in);
        }

        Collection<LogicalOggStream> streams = oggStream.getLogicalStreams();
        loStream = streams.iterator().next();

//        if (loStream == null){
//            throw new IOException("OGG File does not contain vorbis audio stream");
//        }

        vorbisStream = new VorbisStream(loStream);
        streamHdr = vorbisStream.getIdentificationHeader();
//        commentHdr = vorbisStream.getCommentHeader();
    
        if (!readStream){
            AudioBuffer audioBuffer = new AudioBuffer();
            audioBuffer.setupFormat(streamHdr.getChannels(), 16, streamHdr.getSampleRate());
            audioBuffer.updateData(readToBuffer());
            return audioBuffer;
        }else{
            AudioStream audioStream = new AudioStream();
            audioStream.setupFormat(streamHdr.getChannels(), 16, streamHdr.getSampleRate());
            
            // might return -1 if unknown
            float streamDuration = computeStreamDuration();
            
            audioStream.updateData(readToStream(oggStream.isSeekable()), streamDuration);
            return audioStream;
        }
    }
 
Example #25
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 #26
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 #27
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 #28
Source File: SoundDeviceJme.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public SoundHandle loadSound(SoundSystem soundSystem, String filename) {
    AudioNode an = new AudioNode(assetManager, filename, AudioData.DataType.Buffer);
    an.setPositional(false);
    return new SoundHandleJme(ar, an);
}
 
Example #29
Source File: TestWav.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void simpleInitApp() {
  audioSource = new AudioNode(assetManager, "Sound/Effects/Gun.wav",
          AudioData.DataType.Buffer);
  audioSource.setLooping(false);
}
 
Example #30
Source File: WAVLoader.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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 = 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;
        }
    }
}