Author Topic: Java volume control  (Read 10761 times)

0 Members and 1 Guest are viewing this topic.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Java volume control
« on: January 23, 2011, 07:46:41 pm »
If there are any java programmers out there who know how to play a sound and modify the volume of that sound while it is being played, that would be very helpful, I am so stuck, googling for the whole day and I still can't find any clues.  I did find a single way to control the volume, but it lags for about 2 seconds and doesn't have near the fidelity that provides any use.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Java volume control
« Reply #1 on: January 23, 2011, 07:56:55 pm »
What exactly are you trying to play?

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Java volume control
« Reply #2 on: January 23, 2011, 07:57:23 pm »
a sound?

Offline Broseph Radson

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 295
  • Rating: +20/-1
  • Its 0x1A4 somewhere
    • View Profile
Re: Java volume control
« Reply #3 on: January 23, 2011, 08:10:22 pm »
Have you looked here?
http://download.oracle.com/javase/1.5.0/docs/guide/sound/programmer_guide/chapter6.html
Im no java expert but this appears to be what youre looking for (unless this is what you tried already)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Java volume control
« Reply #4 on: January 23, 2011, 09:27:00 pm »
sound's a bitch. my classmate in AP computer science downloaded something to play .wav files. you can check the java API for sound, i think there's a class for MIDI and a class for other uncompressed files like .wav.

aren't you using greenfoot? i swear i remember hearing sound in that Nightmare demo. i'll ask my friend tomorrow for that class which played a .wav file if you want.


Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Java volume control
« Reply #5 on: January 26, 2011, 02:46:11 pm »
Hey thanks for your help everybody, I was able to eventually get it to work, but unfortunately specific circumstances have prevented me from using the method I developed.  It worked perfectly, it had volume, pan, and echo control, but it relied on input as a Wav file, and as I'm sure all of you know, wav files can be quite huge.  greenfoot applets have a maximum size requirement, and so I was forced to switch to mp3 files, of which I have no control over whatsoever.  So thanks for all your help, but unfortunately it looks like colume control will not play any part in Nightmare

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Java volume control
« Reply #6 on: January 26, 2011, 03:58:26 pm »
Hey thanks for your help everybody, I was able to eventually get it to work, but unfortunately specific circumstances have prevented me from using the method I developed.  It worked perfectly, it had volume, pan, and echo control, but it relied on input as a Wav file, and as I'm sure all of you know, wav files can be quite huge.  greenfoot applets have a maximum size requirement, and so I was forced to switch to mp3 files, of which I have no control over whatsoever.  So thanks for all your help, but unfortunately it looks like colume control will not play any part in Nightmare

did your code use part of Greenfoot's library? if not, can i see the source code?


Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Java volume control
« Reply #7 on: January 26, 2011, 04:31:52 pm »
Nope, I did some heavy modification of the original code I had tested, creating a dynamic buffer that changed in size based on the frame length of a single frame of the game.  The issue that was preventing me before was that there was too large of a data buffer 128K, and when I changed the volume, it had to finished 128K of sound data before the changes could be seen.  By significantly shortening the buffer and making it dynamic, I was able to get smooth volume and pan control:

here is my Sound class:

Code: [Select]
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.EnumControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import javax.sound.sampled.Control;


public class Sound  extends Actor
{
    AePlayWave sound;
    private String filename;
    SourceDataLine auline;
    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
    int nBytesRead = 0;
    AudioInputStream audioInputStream;
    FloatControl volume;
    FloatControl pan;
    float vol = 0;   
    int count = 0;
    boolean stopped = true;
    boolean paused = false;
    long prev = 0;

    public Sound(){
    }
   
   
    public Sound(String wavfile){
        filename = wavfile;
       
        File soundFile = new File(filename);
        if (!soundFile.exists()) {
            System.err.println("Wave file not found: " + filename);
            return;
        }
 
        audioInputStream = null;
        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) {
            e1.printStackTrace();
            return;
        } catch (IOException e1) {
            e1.printStackTrace();
            return;
        }
 
        AudioFormat format = audioInputStream.getFormat();
        auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
 
        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
            //auline.open();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
 
        if (auline.isControlSupported(FloatControl.Type.PAN)) {
            pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
        }
       
        if (auline.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
            volume = (FloatControl) auline.getControl(FloatControl.Type.MASTER_GAIN);
        }   
    }
   
   

    public void act(){   //must be called every frame of the game for sound to continually play
        if(stopped || paused){
            prev = 0;
            return;
        }
        long time = System.currentTimeMillis();
        long diff = 0;
        if(prev == 0){
            diff = 5000;
        }else{
            diff = time - prev;
            diff *= 200;
        }
           
        try {       
            nBytesRead = audioInputStream.read(abData, 0, (int)diff);
            if (nBytesRead >= 0){
                auline.write(abData, 0, nBytesRead);
            }else{
                stopSound();
            }
           
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }     
        prev = time;   
    }
   
    public void setVolume(float vol){
        volume.setValue(vol);
    }
   
    public void setPan(float p){
        pan.setValue(p);
    }
   
    public void stopSound(){
        auline.stop();
        auline.flush();
        stopped = true;
        paused = false;
    }
   
    public void playSound(){
        //try {       
        //    audioInputStream.reset();
        //} catch (IOException e) {
        //    e.printStackTrace();
        //    return;
        //}   
        auline.start();
        stopped = false;
        paused = false;
    }
   
    public void pauseSound(){
        if(stopped || paused){
            return;
        }
        auline.stop();
        paused = true;
    }
   
    public void resumeSound(){
        if(stopped || !paused){
            return;
        }
        auline.start();
        paused = false;
    }

    public void loopSound(){
       
    }
}

Offline vitorrv

  • LV0 Newcomer (Next: 5)
  • Posts: 1
  • Rating: +0/-0
    • View Profile
Re: Java volume control
« Reply #8 on: November 22, 2011, 07:37:40 pm »
Hey Builderboy, I'm working on a hardware project which is supposed to trigger a sound allowing volume control, i not managing your code to work, could you give me an example of the main class and how do you make a call to play a sound and configure it?

Thx!

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Java volume control
« Reply #9 on: November 22, 2011, 07:44:45 pm »
I actually ended up using a separate library because the above one kept giving me problems.  The library I used was Javazoom, you could check it out and see if you can get that to work for you :)

Also feel free to introduce yourself!  Hope to see you around! ^^