PDA

View Full Version : HELP! Making a drum machine and Can't play sound!


JChop
06-01-2005, 04:07 PM
I am working on a simple drum machine. I have the interface and I am using the audio class. I am trying to get it so when you hit the play button it loops through all the checkboxes and plays if the checkboxes that are checked. The sound comes from the files selected in the panel. I am trying to get it so it loops in 10 seconds. Please please any help would be greatly appreciated.

JCHOPBEATS.COM
coming soon...

here's the interface

import javax.swing.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.JCheckBox;

import javax.swing.Action;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import java.awt.*;

public class Musica extends JFrame implements ActionListener {
public final int SONG_COUNT = 6, CHECKS = 16;
JPanel[] songSelection;
JTextField[] pathField;
JButton[] browse;
JCheckBox[][] checkBoxes = new JCheckBox[SONG_COUNT][CHECKS];
JButton playButton;
JButton stopButton;
Box containerBox;


public Musica()
{
playButton = new JButton("Play");
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
play();
}
});
stopButton = new JButton("Stop");
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
play();
}
});


containerBox = new Box(BoxLayout.Y_AXIS);
songSelection = new JPanel[SONG_COUNT];
pathField = new JTextField[SONG_COUNT];
browse = new JButton[SONG_COUNT];
for (int i = 0; i < songSelection.length; i++) {
songSelection[i] = new JPanel(new FlowLayout(FlowLayout.LEFT));
pathField[i] = new JTextField();
pathField[i].setColumns(20);
browse[i] = new JButton("Browse");
browse[i].addActionListener(this);
browse[i].setActionCommand("BROWSE_" + String.valueOf(i));
songSelection[i].add(pathField[i]);
songSelection[i].add(browse[i]);
for (int j = 0; j < checkBoxes[i].length; j++) {

checkBoxes[i][j] = new JCheckBox();
songSelection[i].add(checkBoxes[i][j]);
}
containerBox.add(songSelection[i]);

}
getContentPane().add(containerBox, BorderLayout.CENTER);
getContentPane().add(playButton, BorderLayout.SOUTH);
getContentPane().add(stopButton, BorderLayout.EAST);
pack();
show();
}
public void play()
{

new AudioPlayer02();
}
public void actionPerformed(ActionEvent e)
{
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(this);
if(e.getActionCommand().equals("BROWSE_0"))
pathField[0].setText(chooser.getSelectedFile().getName());
else if(e.getActionCommand().equals("BROWSE_1"))
pathField[1].setText(chooser.getSelectedFile().getName());
if(e.getActionCommand().equals("BROWSE_2"))
pathField[0].setText(chooser.getSelectedFile().getName());
else if(e.getActionCommand().equals("BROWSE_3"))
pathField[1].setText(chooser.getSelectedFile().getName());
if(e.getActionCommand().equals("BROWSE_4"))
pathField[0].setText(chooser.getSelectedFile().getName());
else if(e.getActionCommand().equals("BROWSE_5"))
pathField[1].setText(chooser.getSelectedFile().getName());




}
public static void main(String[] args) {
new Musica();



}

}




Here's the audio class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;

public class AudioPlayer02 extends JFrame{

AudioFormat audioFormat;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
boolean stopPlayback = false;
final JButton stopBtn = new JButton("Stop");
final JButton playBtn = new JButton("Play");
final JTextField textField =
new JTextField("junk.au");

public static void main(String args[]){
new AudioPlayer02();
}//end main
//-------------------------------------------//

public AudioPlayer02(){//constructor

stopBtn.setEnabled(false);
playBtn.setEnabled(true);

//Instantiate and register action listeners
// on the Play and Stop buttons.
playBtn.addActionListener(
new ActionListener(){
public void actionPerformed(
ActionEvent e){
stopBtn.setEnabled(true);
playBtn.setEnabled(false);
// playAudio();//Play the file
}//end actionPerformed
}//end ActionListener
);//end addActionListener()

stopBtn.addActionListener(
new ActionListener(){
public void actionPerformed(
ActionEvent e){
//Terminate playback before EOF
stopPlayback = true;
}//end actionPerformed
}//end ActionListener
);//end addActionListener()

getContentPane().add(playBtn,"West");
getContentPane().add(stopBtn,"East");
getContentPane().add(textField,"North");

setTitle("Copyright 2003, R.G.Baldwin");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(250,70);
setVisible(true);
}//end constructor
//-------------------------------------------//

//This method plays back audio data from an
// audio file whose name is specified in the
// text field.


public void getLoop(String[] files, JCheckBox[][] checks){

for (int x = 0; x < files.length; x++){
for (int y = 0; y < checks.length; y++){
if(checks[x][y].isSelected() == true){
playAudio(files[x]);
}
}
}
}


private void playAudio(String filename) {
try{
File soundFile =
new File(filename);
audioInputStream = AudioSystem.
getAudioInputStream(soundFile);
audioFormat = audioInputStream.getFormat();
System.out.println(audioFormat);

DataLine.Info dataLineInfo =
new DataLine.Info(
SourceDataLine.class,
audioFormat);

sourceDataLine =
(SourceDataLine)AudioSystem.getLine(
dataLineInfo);

//Create a thread to play back the data and
// start it running. It will run until the
// end of file, or the Stop button is
// clicked, whichever occurs first.
// Because of the data buffers involved,
// there will normally be a delay between
// the click on the Stop button and the
// actual termination of playback.
new PlayThread().start();
}catch (Exception e) {
e.printStackTrace();
System.exit(0);
}//end catch
}//end playAudio


//=============================================//
//Inner class to play back the data from the
// audio file.
class PlayThread extends Thread{
byte tempBuffer[] = new byte[10000];

public void run(){
try{
sourceDataLine.open(audioFormat);
sourceDataLine.start();

int cnt;
//Keep looping until the input read method
// returns -1 for empty stream or the
// user clicks the Stop button causing
// stopPlayback to switch from false to
// true.
while((cnt = audioInputStream.read(
tempBuffer,0,tempBuffer.length)) != -1
&& stopPlayback == false){
if(cnt > 0){
//Write data to the internal buffer of
// the data line where it will be
// delivered to the speaker.
sourceDataLine.write(
tempBuffer, 0, cnt);
}//end if
}//end while
//Block and wait for internal buffer of the
// data line to empty.
sourceDataLine.drain();
sourceDataLine.close();

//Prepare to playback another file
stopBtn.setEnabled(false);
playBtn.setEnabled(true);
stopPlayback = false;
}catch (Exception e) {
e.printStackTrace();
System.exit(0);
}//end catch
}//end run
}//end inner class PlayThread
//===================================//

}//end outer class AudioPlayer02.java

JamieR
06-01-2005, 05:34 PM
1) You posted this in the wrong section - it should be posted under the Computer Programming forum.

2) Please use [ CODE ] [/ CODE ] brackets (without the spaces :)) to wrap your code in code tags, as it makes it easier to read ;)

Scootertaj
06-01-2005, 07:48 PM
Hopefully Aerospace sees this, as I have seen he is very good with this kinda stuff.

_Aerospace_Eng_
06-02-2005, 12:11 AM
Hopefully Aerospace sees this, as I have seen he is very good with this kinda stuff.
If it was html or some other language but java then I might be able to help but seeing as how I don't know java, I'm afraid I can't help in this aspect.

Scootertaj
06-02-2005, 12:21 AM
Ah that's too bad because you did a great job with the embedded Windows Media Player!

_Aerospace_Eng_
06-02-2005, 06:36 AM
Ah that's too bad because you did a great job with the embedded Windows Media Player!
Which I have just updated by the way to be up to standards using the object tag which is understood by most new browsers, and also the more cross browser compatable way using the embed tag only because some browsers can't understand the object tag.