I am developing a drum metronome. So I have .wav sound files and I need to play them with using minimum computer memory and CPU because it's very important for the metronome to play the sample on the exact time. Currently I use the code from this link
How to play .wav files with java
class MakeSound {
private final int BUFFER_SIZE = 128000;
private File soundFile;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
/**
* #param filename the name of the file that is going to be played
*/
public void playSound(String filename){
String strFilename = filename;
try {
soundFile = new File(strFilename);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
#SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
}
}
This code works but it's too slow for using it in a metronome app. So which is the fastest way of playing wav sound files. Please take into account that sometimes I need to play them simultaneously so the sound should play as a separate thread I think.
Thanks
i am assuming that you have a working method that looks like this:
example #1: (don't do this)
public void startMetronome(){
boolean abort = false;
String audoFileName = new String("myAudioFile);
do{
playSound(audoFileName );
while(abort != false);
}
or maybe you've done some better implementation like this:
exapmle#2: (don't do this either)
Runnable r = new Runnable(){
boolean abort = false;
String audoFileName = new String("myAudioFile);
do{
playSound(audoFileName );
try{
Thread.sleep(500);
}catch (Exception e);
while(abort != false);
}
new Thread(r).start();
in any case you're doing a big mistake: you initialize every time you play a sound the sound line and every time you load the file again and again.
but thats a toally wrong approach, you have to load the file once, open the line once and play the sound repetially on the line!
(cheap) solution #3:
adjust your playSoundmethode like this:
public void playSound(String filename){
//same as above
String strFilename = filename;
soundFile = new File(strFilename); //i've shorened the catch/throws, to make it more readable
audioStream = AudioSystem.getAudioInputStream(soundFile);
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
sourceLine.start();
//right now, the line is set up and all data is availible
boolean isRunning = true;
while(isRunning){ //this is an endless loop!
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
nBytesRead = audioStream.read(abData, 0, abData.length);
//and your timing here
try{
Thread.sleep(500);
}catch (Exception e);
}
sourceLine.drain();
sourceLine.close();
}
and best practice would be to play the whole thing in an seperate thread... (as example #2)
Related
This is my sound class.
public class Sounds extends Thread {
private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE = 524288;
enum Position {
LEFT, RIGHT, NORMAL
};
public Sounds(String wavFile) {
filename = wavFile;
curPosition = Position.NORMAL;
}
public Sounds(String wavfile, Position p) {
filename = wavfile;
curPosition = p;
}
public void run() {
File soundFile = new File(filename);
if (!soundFile.exists()) {
System.err.println(".wav file not found: " + filename);
return;
}
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline
.getControl(FloatControl.Type.PAN);
if (curPosition == Position.RIGHT)
pan.setValue(1.0f);
else if (curPosition == Position.LEFT)
pan.setValue(-1.0f);
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
And this is my ActionListener that I want to stop the music when the user clicks a button. The music plays on start up.
btnSound.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
btnSound.setVisible(false);
btnNoSound.setVisible(true);
new Sounds("Sounds/MenuMusic.wav").end();
}
});
I can get the music to play just fine, but I can't figure out how to stop it. I tried using .stop() and .end() but I was told they were deprecated. Anyone have any idea how I can do this? thanks.
You could use the clip class. This is my code for reading a .wav file and playing it:
try {
File file = new File(src); // src is the location of the file
AudioInputStream stream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(stream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
That was, clip.stop() will stop the clip. To reset the clip, use clip.setFramePosition(0). Note that clip does not need to be closed, because if clip.close() is called after clip.start(), the clip will not play.
Hope that's what you were looking for!
EDIT: Scoping
Using scoping in Java, if you declare a local variable in a method, you cannot access it outside of the method. If you want to access the clip outside of the main method, extract it to a field:
class Main {
static Clip clip;
public static void main (String... args) {
try {
File file...
clip = AudioSystem.getClip();
...
} catch...
}
}
I'm making anti-phase sound with java.(anti-phase is reflected wave. x-coordinate is not changed, but y-coordinate is upside down.)
Before reflecting sound wave, I have to get byte array(or int array) from sound.
I get sound from microphone of my laptop.
Here is my CODE(I got original code, which record sound as file, in web. I modified it little)
public class NoiseController extends Thread{
private TargetDataLine line;
private AudioInputStream audioInputStream;
public NoiseController(TargetDataLine line) {
this.line = line;
this.audioInputStream = new AudioInputStream(line);
}
public void start() {
line.start();
super.start();
}
public void stopRecording() {
line.stop();
line.close();
}
public void run() {
try {
int packet;
while((packet = audioInputStream.read()) != -1)
System.out.println(packet);
}
catch(IOException ioe) {
ioe.getStackTrace();
}
}
public static void main(String[] args) {
AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine)AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
}
catch(LineUnavailableException lue) {
out("unable to get a recording line");
lue.printStackTrace();
System.exit(-1);
}
AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
NoiseController recorder = new NoiseController(targetDataLine);
System.out.println(targetDataLine);
System.out.println(targetType);
out("Press ENTER to start the recording.");
try {
System.in.read();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
recorder.start();
out("Recording...");
out("Press ENTER to stop the recording.");
try {
System.in.read();
System.in.read();
}
catch(IOException ioe) {
ioe.getStackTrace();
}
recorder.stopRecording();
out("Recording stopped.");
}
private static void out(String msg) {
System.out.println(msg);
}
}
However, Console doesn't print anything while recording...
It shows just
com.sun.media.sound.DirectAudioDevice$DirectTDL#25154f_
WAVE
Press ENTER to start the recording.
Recording...
Press ENTER to stop the recording.
Recording stopped.
If I edit run() like AudioSystem.write(stream, fileType, out);
instead of
int packet;
while((packet = audioInputStream.read()) != -1)
System.out.println(packet);
program saves wav file.
What is wrong in my program?
you are not printing the Exception as Uwe Allner said.
I´ve also try to correct it and I think the result must be like this:
public class NoiseController extends Thread {
private final TargetDataLine line;
private final AudioInputStream audioInputStream;
public NoiseController(final TargetDataLine line) {
this.line = line;
this.audioInputStream = new AudioInputStream(line);
}
#Override
public void start() {
line.start();
super.start();
}
public void stopRecording() {
line.stop();
line.close();
try {
audioInputStream.close();
} catch (final IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
try {
final int bufferSize = 1024;
int read = 0;
final byte[] frame = new byte[bufferSize];
while ((read = audioInputStream.read(frame)) != -1 && line.isOpen()) {
// only the first read bytes are valid
System.out.println(Arrays.toString(frame));
}
} catch (final IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(final String[] args) {
final AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100.0F, 16, 2, 4, 44100.0F, false);
final DataLine.Info info = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
} catch (final LineUnavailableException lue) {
out("unable to get a recording line");
lue.printStackTrace();
System.exit(-1);
}
final AudioFileFormat.Type targetType = AudioFileFormat.Type.WAVE;
final NoiseController recorder = new NoiseController(targetDataLine);
System.out.println(targetDataLine);
System.out.println(targetType);
out("Press ENTER to start the recording.");
try {
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.start();
out("Recording...");
out("Press ENTER to stop the recording.");
try {
System.in.read();
System.in.read();
} catch (final IOException ioe) {
ioe.printStackTrace();
}
recorder.stopRecording();
out("Recording stopped.");
}
private static void out(final String msg) {
System.out.println(msg);
}
}
I am trying to write a little program that reads a wav file and sends the output as if it would come from my microphone. Unfortunately I do not have much experience with the sound API.
Background: What I am basically trying to realize is a program that plays a sound while I am in a voicechat (i.e Teamspeak, Ventrilo). To get that to work now I would have to switch the recording device to "What you hear", play the sound and then switch back to microphone. The program should simulate input from the microphone.
So far I could not get any further than just playing the sound. I guess I just need a different SourceLine?
public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
public void playSound(File soundFile) {
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(infoIn);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
#SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
}
}
Solution:
Install VB Audio Cable (http://vb-audio.pagesperso-orange.fr/Cable/). It's donationware. Make VB-Output standard recording device. In the microphone properties choose VB-Input as playback.
public class Player {
private final int BUFFER_SIZE = 128000;
private AudioInputStream audioStream;
private AudioFormat audioFormat;
private SourceDataLine sourceLine;
public void playSound(File soundFile) {
try {
audioStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
audioFormat = audioStream.getFormat();
DataLine.Info infoIn = new DataLine.Info(SourceDataLine.class,
audioFormat);
try {
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
Mixer mixer = null;
for (int i = 0; i < mixerInfos.length; i++) {
System.out.println(mixerInfos[i].getName());
if (mixerInfos[i].getName().equals(
"CABLE Input (VB-Audio Virtual Cable)")) {
mixer = AudioSystem.getMixer(mixerInfos[i]);
break;
}
}
sourceLine = (SourceDataLine) mixer.getLine(infoIn);
sourceLine.open(audioFormat);
} catch (LineUnavailableException e) {
e.printStackTrace();
System.exit(1);
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
#SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
}
}
You can install some program like Virtual Audio Cable and from your music player redirect playing sound to virtual input. In your program you must listen that virtual input source and you can for test redirect to normal headphones or speaker received signal or saved it to file.
This code does not work. I added some System.out.println("Start capturing...3"); statements to understand where the bug is, and I saw that the bug is in the line.open(format); command. Why am I getting a bug?
import javax.sound.sampled.*;
import java.io.*;
public class JavaSoundRecorder {
// record duration, in milliseconds
static final long RECORD_TIME = 4000;
File wavFile = new File("C:\\Users\\kecia\\R\\RecordAudio.wav");
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
TargetDataLine line;
AudioFormat getAudioFormat()
{
float sampleRate = 16000;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 8;
//8,16
int channels = 2;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = true;
//true,false
return new AudioFormat(
sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
void start() {
try {
System.out.println("Start capturing...1");
AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
System.out.println("Start capturing...2");
// checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported");
System.exit(0);
}
System.out.println("Start capturing...3");
line = (TargetDataLine) AudioSystem.getLine(info);
System.out.println("Start capturing...4");
////////////////////////////////////////////////////////////////////////
line.open(format);
////////////////////////////////////////////////////////////////////////
System.out.println("Start capturing...5");
line.start(); // start capturing
System.out.println("Start capturing...6");
AudioInputStream ais = new AudioInputStream(line);
System.out.println("Start recording...");
// start recording
AudioSystem.write(ais, fileType, wavFile);
} catch (LineUnavailableException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
void finish() {
line.stop();
line.close();
System.out.println("END");
}
public static void main(String[] args) {
final JavaSoundRecorder recorder = new JavaSoundRecorder();
// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.finish();
}
});
stopper.start();
recorder.start();
}
}
See my response to a similar question here:
Sound recording not working in java
That works perfectly for me.It captures and saves sound from mic, into a file.
And it's easy to use
First of all, I love you guys! This is THE best site for finding answers to weird and difficult programming questions. This is the first problem I have not been able to find a solution for on this site, so thanks for that.
So, I have a runnable Game.class and a runnable OggStreamer.class, to allow the music to run in its own separate thread, which I send as a parameter for the Game-class to use. When I run the game from my IDE the OggStreamer always works, but when I've exported it to a .jar-file, it only works 1/3 of the times I start it up. And it's not like the first piece of music doesn't start, and then the next piece of music started will play..it doesn't work at all, until I've started the game a few times.
Has any of you good people had a similar problem? I could understand it if it didn't work at all, which could indicate there was something wrong with the file-references to the music inside the jar-file...but it DOES work, just not consistently.
NOTE: This is my first attempt at Game-programming, and I know it's not very pretty and that I am a total newbie :) There are many things I'd change about the general design, but I'm using this as a project to help me understand the problems I'll encounter when I sit down to design a real framework for my next game.
Start.class
public static void main(String[] args) throws InterruptedException{
ExecutorService threadExecutor = Executors.newCachedThreadPool();
Game game = new Game(musicStreamer);
game.init(); // loads stuff
threadExecutor.execute( musicStreamer ); // start task1
threadExecutor.execute( game ); // start task2
threadExecutor.shutdown();
}
OggStreamer.class
public class OggStreamer implements Runnable{
private URL url;
private AudioInputStream stream;
private AudioInputStream decodedStream;
private AudioFormat format;
private AudioFormat decodedFormat;
private boolean stop, running;
String filename = "";
SourceDataLine line = null;
public OggStreamer() {
this.stop = true;
this.running = true;
this.url = null;
}
public void run() {
while(running){
while (!this.stop) {
System.out.println("Playing Loop");
try {
// Get AudioInputStream from given file.
this.stream = AudioSystem.getAudioInputStream(this.url);
this.decodedStream = null;
if (this.stream != null) {
this.format = this.stream.getFormat();
this.decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
this.format.getSampleRate(), 16,
this.format.getChannels(),
this.format.getChannels() * 2,
this.format.getSampleRate(), false);
// Get AudioInputStream that will be decoded by underlying
// VorbisSPI
this.decodedStream = AudioSystem.getAudioInputStream(
this.decodedFormat, this.stream);
}else{
JOptionPane.showMessageDialog(null, "Stream = null!");
}
} catch (Exception e) {
// Do nothing
System.out.println("Could not get or decode audiostream");
}
line = null;
try {
line = this.getSourceDataLine(this.decodedFormat);
FloatControl volume = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(1);
} catch (LineUnavailableException lue) {
// Do nothing
JOptionPane.showMessageDialog(null, "Line is unavailable!");
}
if (line != null) {
try {
byte[] data = new byte[4096];
// Start
line.start();
int nBytesRead = 0;
while (nBytesRead != -1) {
nBytesRead = this.decodedStream.read(data, 0,
data.length);
if (nBytesRead != -1) {
line.write(data, 0, nBytesRead);
}
if (this.stop) {
break;
}
}
// Stop
line.drain();
line.stop();
line.close();
} catch (IOException io) {
// Do nothing
JOptionPane.showMessageDialog(null, "Line cannot start!");
}
}
}
}
}
private SourceDataLine getSourceDataLine(AudioFormat audioFormat)
throws LineUnavailableException {
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}
public void startLoop(String filenameString) {
this.filename = filenameString;
System.out.println("Starting loop with: "+filenameString);
this.url = this.getClass().getResource(filenameString);
this.stop = false;
}
public void stopLoop() {
System.out.println("Stopping loop");
try {
if(this.decodedStream!=null)this.decodedStream.close();
if(this.stream!=null)this.stream.close();
} catch (IOException e) {
}
this.stop = true;
this.url = null;
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public URL getUrl() {
return url;
}
public void setUrl(String string) {
this.filename = string;
this.url = this.getClass().getResource(string);
}
public String getFilename() {
return filename;
}
}
Game.class uses the OggStreamer-class as such:
if(musicStreamer!=null && !musicStreamer.getFilename().equals("/snm/sound/oggs/Prelude.ogg")){
if(!musicStreamer.isStop())musicStreamer.stopLoop();
musicStreamer.startLoop("/snm/sound/oggs/Prelude.ogg");
}