Login  Register

JOAL - sound does not want to ''move''

Posted by Jangi2 on Mar 15, 2016; 12:53pm
URL: https://forum.jogamp.org/JOAL-sound-does-not-want-to-move-tp4036491.html

Hello,
I am using JOAL in a university project to insert sound.
I want to prepare a little test program to learn JOAL.
I have a very simple model that uses change listeners (from Swing) to notify a change. Then I have a small controller that handles sound control acording to the model.
And I have the main class that implements the app controller which shall modify the model acording to certain key pressed.

So, to sum up: the user hit a key to move the sound in the 3D audio environment. The app controller modifies the model in consequence. The model notifies the sound controller described above, to move it acording to these changes.

But, there is a problem... The sound denies to move and to update its position.
I am really blocked on this problem...
Can someone help me please?
You will find all my code below.
Bee is the model. SoundingBee is the small controller for the sound rendering of the model. Test is the main class.

Huge thank in advance.
Regards.



CODE:

-- class Bee

package test;

import java.awt.event.*;
import javax.swing.event.*;

/**
 * A Bee is represented by its position in a 3D space...
*/

public class Bee {
  // ATTRIBUTS
 
  private int x;
  private int y;
  private int z;
  private final EventListenerList listeners;
 
  // CONSTRUCTEURS
 
  public Bee() {
    this(0, 0, 0);
  }
 
  public Bee(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
    listeners = new EventListenerList();
  }
 
  // REQUETES
 
  public int getX() {
    return x;
  }
 
  public int getY() {
    return y;
  }
 
  public int getZ() {
    return z;
  }
 
  @Override
  public String toString() {
    return "(" + x + "," + y + "," + z + ")";
  }
 
  // COMMANDES
 
  public void moveLeft() {
    --x;
    fireStateChanged();
  }
 
  public void moveRight() {
    ++x;
    fireStateChanged();    
  }
 
  public void moveBackward() {
    --z;
    fireStateChanged();
  }
 
  public void moveForward() {
    ++z;
    fireStateChanged();
  }
 
  public void moveDown() {
    --y;
    fireStateChanged();
  }
 
  public void moveUp() {
    ++y;
    fireStateChanged();
  }
 
  public void addChangeListener(ChangeListener l) {
    if (l == null) {
      throw new AssertionError();
    }
    listeners.add(ChangeListener.class, l);
  }
 
  public void removeChangeListener(ChangeListener l) {
    if (l == null) {
      throw new AssertionError();
    }
    listeners.remove(ChangeListener.class, l);
  }
 
  // OUTILS
 
  private void fireStateChanged() {
    for (ChangeListener l : listeners.getListeners(ChangeListener.class)) {
      l.stateChanged(new ChangeEvent(this));
    }
  }
}


-- class SoundingBee

package test;

import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;

import com.jogamp.openal.*;
import com.jogamp.openal.util.*;
import com.jogamp.openal.sound3d.*;

import java.io.IOException;

public class SoundingBee extends JComponent {
  // CONSTANTES
 
  private static final float SCALE_FACTOR = 1 / 10;
 
  // ATTRIBUTS
 
  private Bee model;
  private final Source sound;
  private ChangeListener changeListener;
 
  // CONSTRUCTEURS
 
  public SoundingBee(Bee m, String soundPath) throws IOException, UnsupportedAudioFileException {
    if (m == null) {
      throw new AssertionError();
    }
    model = m;
    sound = AudioSystem3D.loadSource(soundPath);
    changeListener = new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        update();
      }
    };
    model.addChangeListener(changeListener);
    sound.setLooping(true);
    update();
  }
 
  @Override
  public String toString() {
    Vec3f p = sound.getPosition();
    return ("(" + p.v1 + "," + p.v2 + "," + p.v3 + ")");
  }
 
  // OUTILS
 
  private void update() {
    sound.setPosition((float) model.getX() * SCALE_FACTOR, (float) model.getY() * SCALE_FACTOR, (float) model.getZ() * SCALE_FACTOR);
  }
 
  protected void begin() {
    if (!sound.isPlaying()) {
      sound.play();
    }
  }
 
  @Override
  protected void finalize() throws Throwable {
    sound.stop();
    sound.delete();
    super.finalize();
  }
}


-- class Test

package test;

import com.jogamp.openal.*;
import com.jogamp.openal.sound3d.*;
import com.jogamp.openal.util.*;

import java.io.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public final class Test {  
  // ATTRIBUTS
  private final String sound = "./sounds/bee.wav";
  private Bee model;
  private SoundingBee bee;
  private JFrame frame;
 
  // CONSTRUCTEURS
  private Test() throws IOException, UnsupportedAudioFileException {
    model = new Bee();
    createView();
    createController();
  }
 
  // COMMANDES
 
  public void test() throws IOException, UnsupportedAudioFileException {
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.requestFocusInWindow();
   
    initAudioEnvironment();
    try {
      bee = new SoundingBee(model, sound);
    } catch (IOException e) {
    } catch (UnsupportedAudioFileException e) {
    }
    bee.begin();
  }
 
  // OUTILS
 
  private void initAudioEnvironment() {
    ALut.alutInit();
    AudioSystem3D.init();
  }
 
  private static void releaseAudioEnvironment() {
    ALut.alutExit();
  }
 
  private void createView() {
    frame = new JFrame("Abeille");
    frame.setPreferredSize(new Dimension(300, 300));
  }
 
  private void createController() {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        releaseAudioEnvironment();
      }
      public void windowLostFocus(WindowEvent e) {
        frame.requestFocusInWindow();
      }
    });
    frame.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
          switch (e.getKeyCode()) {
            case (KeyEvent.VK_UP) :
              model.moveForward();
              System.out.println(e.getKeyCode() + " : " + model.toString() + " / " + bee.toString());
              break;
            case (KeyEvent.VK_DOWN) :
              model.moveBackward();
              break;
            case (KeyEvent.VK_LEFT) :
              model.moveLeft();
              break;
            case (KeyEvent.VK_RIGHT) :
              model.moveRight();
              break;
              case (KeyEvent.VK_HOME) :
              model.moveUp();
              break;
            case (KeyEvent.VK_END) :
              model.moveDown();
              break;
            default :
              break;
          }
        }
    });
  }
 
  // POINT D'ENTREE
 
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        try {
          new Test().test();
        } catch (IOException e) {
          releaseAudioEnvironment();
        } catch (UnsupportedAudioFileException e) {
          releaseAudioEnvironment();
        }
      }
    });
  }
}