• Post Reply Bookmark Topic Watch Topic
  • New Topic

Button Recording to a File  RSS feed

 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stuck.

I have a Frame with 2 panels. The first panel has 127 buttons that play a note when selected. The second panel has 3 buttons (record, stop, and replay). When the record button is pressed the program is to record the (note) buttons until the stop is selected. This is where I am having the problem. I am treating the button presses as an object and using the following code to try to record (at the actionPerformed method):

public void actionPerformed (ActionEvent e) {
int count;
for(count = 0; count < 128; count++) {
if(e.getSource() == buttons[count])
syn.playNote(50, count, 1000);
}

if(e.getSource() == b[0]){
String s = JOptionPane.showInputDialog(null, "Please enter file name: ");

// b[0] is the record button

try {
while (e.getSource() != b[1]) {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(s));

// b[1] is the stop button

out.writeObject(buttons[count]);
continue;
}
}
catch (IOException ex) {
System.out.println(ex);
}

if (e.getSource() == b[1])
out.close();

}

else if (e.getSource() == b[2])

// b[2] is to be the replay button - no code as of yet

;

}

Many questions. Am I correct to try to treat the buttons as objects? How do I loop writing each "note" button without asking for the file name each time? What am I doing wrong with out.close so that it cannot be recognized? etc.?

Any guidance is appreciated. Just to get me started on the correct path. I have been staring at this part of the code (and changing it so often), that I can't realize even what my next step should be?

Thank you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the record button just sets a flag - recordingNow = true - and the code that calls the synthesizer to play the note also records the note. Then there is no loop in your record button code.

What information do you plan to record? Are you going to look at the Sequencer classes for playback or make up your own playback?

Wandering off into style preferences ... There are a million ways to organize this kind of code. See if you can find a style that gets any real work out of the actionPerformed method. It might look like:

Another way to organize things is to use a listener per button for start, stop and playback. Then they don't have to check source at all.

Keep sharing your progress. This look like fun!
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

I am not familiar with the flag set function (new at this Java stuff), and am pretty sure that is not how the program is suppose to work. The information I am trying to record is the sequence of the buttons pushed (which calls the synth class that plays the sound until the "stop" record button is pressed. Then, I am suppose to be able to "replay" the stored file and have it play the sequence/notes recorded. I thought that a "button" in a frame resulted in an button object, therefore I was trying to record this information via ObjectOutputStream. Now I am not to sure.

I have already set up listeners for all the buttons (record, stop, and replay along with the 128 buttons for individual notes - per the synth class). I guess I am still confused about how to proceed. Record objects? I would like to post the whole program and see what you think (by the way, I have already verified the button to note part of the program, now I am trying to add in the I/O part - write to a file and read from a file), but it is 125 lines long and I do not know how readable that would be.

Any additional suggestions are appreciated.

Thank you,
Mitch
 
nabi bux
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear you have mistake that you using int count in actionperformed event
each time when you click any button the int cont iniliazed by the zero
please keep it global and static then performed the action
 
nabi bux
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make int count globel variable and declare it as
static variable actualy the problem is that
you declare it local variable when you click a button each time it is
created and initialized with 0
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the count variable looks fine and I think you said the play buttons work right now. It would be more common Java to use "i" as a throw-away indexing variable. It's just a style thing, but using the common style helps others read your code more quickly:

Your recording problem is in the "while" loop. Trying to loop inside this event won't do what you want because there is no way to process note button events during your loop.

"Flag" is an old-timer term for a boolean member variable declared at the top of the class to indicate whether we are recording right now or not. The Record button sets the variable to true, the Stop button sets it to false.

Now when a note button event comes through, we can tell whether it should be recorded. Back in your note button logic:

Does that make sense?
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the great replys/suggestions. I will review and try to implement your suggestion(s). Unfortunately, I am out on business all week and will not be able to get back to you until Friday. Please don't think I am ignoring you. I will give you a status this weekend.

Don't forget me.

Thank you again for your help.

Mitch
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I am back. I have completely revamped the program. Taking into account your suggestions and others (e.g. treat the buttons as an integer for recording and playback). I am having problems getting the program to write to a file. When I input an existing file or a new file I get "Write error"/"FileNotFound"/"BadFileDescriptor". I am pasting my ActioPerformed method. Any suggestions/help is appreciated.

public void actionPerformed (ActionEvent e) {

if (e.getSource() == b[0]) {
s = JOptionPane.showInputDialog(null, "Please enter file name: ");
recording = true;
try{
dataOut = new DataOutputStream(new FileOutputStream(s));
}
catch(IOException ex) {
System.out.println("Cannot open file." + ex);
return;
}

}

else if (e.getSource() == b[1]) {
recording = false;

}

else if (e.getSource() == b[2]) {

jfc = new JFileChooser();
jfc.showDialog(null, "Select a file to open");

try{
dataIn = new DataInputStream(new FileInputStream(jfc.getSelectedFile()));
}
catch(IOException ex) {
System.out.println("Cannot open file. " + ex);
return;
}
try {
count = dataIn.readInt();
while(count != -1) {
syn.playNote(50, count, 1000);
}
}
catch (IOException exc) {
System.out.println("Read error. " + exc);
}
try {
dataOut.close();
}
catch (IOException ex2) {
System.out.println("Read error. " + ex2);
}


}

for (count = 0; count < 128; count++) {
if (e.getSource() == buttons[count])
syn.playNote(50, count, 1000);
if (recording) {

try{
dataOut.writeInt(count);
}
catch(IOException ex) {
System.out.println("Write error. " + ex);
}
try {
dataOut.close();
}
catch (IOException ex1) {
System.out.println("Output error. " + ex1);
}

}

}

}



Sorry it is so long.

Thank you,
Mitch
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy Easter! Things are going much better with my program. Please ignore my previous posting. Much has been figured out. I kept reading your suggestions and eventually started to simplify my program to meet your suggestions. Most of the program is working as per requirements. Thank you!

Except;
When reading back from the file, at the end of the file I get an IOException. My program for this part is as follows:

else if (e.getSource() == b[2]) {

jfc = new JFileChooser();
jfc.showDialog(null, "Select a file to open");

try{
dataIn = new DataInputStream(new FileInputStream(jfc.getSelectedFile()));
}
catch(IOException ex) {
System.out.println("Cannot open file. " + ex);
return;
}

try {
count = dataIn.readInt();
while(count != -1) {
syn.playNote(50, count, 1000);
count = dataIn.readInt();
}
}
catch (IOException exc) {
System.out.println("Read error. " + exc);
}
try {
dataIn.close();
}
catch (IOException ex2) {
System.out.println("Read error closing. " + ex2);
}
}

Is there something wrong with my while statement? Doesn't the return value = -1 when the end of the file is reached? Any suggestions/help is appreciated. Note the file is being read and the notes/buttons are being played (that were previously recorded). It is at the end of the file that throws the exception.

Thank you.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mitch Krah:
Is there something wrong with my while statement? Doesn't the return value = -1 when the end of the file is reached?
Nope, according to the JavaDocs for DataInput, readInt() throws EOFException when the end of file is reached before reading a full int. This way you can safely write a -1 to the file and be able to read it later.

If you consider -1 to be invalid, you could write a -1 to the file before closing it at the end of recording. This would stop you reading it during playback. Otherwise, catch and swallow the EOFException since it's not an error.
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Yes, I noticed that at the end of the read I get (and catch) the EOFException. However, I do not think this is going to be acceptable. I am recording buttons (with associated notes that have been assigned by the listener) and playing them back from a file. I do not know how to elligantly handle the EOFexception? Currently I get a ugly noise and I print out the exception.

I have tried while and for loops to quit at the end of the file and currently am using the following for loop:

try {
System.out.println("Length of file " + s + " is: " + s.length());

for(i = 0; i < s.length(); i++) {
count = dataIn.readInt();
syn.playNote(50, count, 1000);

}
}
catch (IOException exc) {
System.out.println("Read error. " + exc);
}

However, my s.length() keeps returning 8 even when I have 5 or 10 elements. I can't understand what is going on. I printed "s" and "s.length()" to verify that I am getting the length of the same file I have chosen from JFileChooser. And, they are the same. Also, I have opened the selected file (in notepad) and have verified that the # of elements is what I expect (5 or 10), but s.length() still states "8"???

Any suggestions?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "s" a File or a String? If it's a String, that's not the length of the file. Also, since you're writing ints and the length of a file is measured in bytes, you'll need to divide length by 4 to get the number of ints it contains.

You can use the "write -1 before closing the file" suggestion I made above to solve this problem. Also, why can't you simply catch EOFException and ignore it?
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The light went on! It is a string and I am getting the length of the file name. Wow! That is a relief.

I like your suggestion of catching the EOFException and doing nothing. How do I catch the exception and keep it from DONG!ing me?

Also, I will see about implementing your write -1 at the end of the file.

Thank you,
Mitch

You have provided much, much help!
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mitch Krah:
I like your suggestion of catching the EOFException and doing nothing. How do I catch the exception and keep it from DONG!ing me?
Glad to help! For this, you can slightly modify your code:I've simplified the loop, too, to not check for -1. Now, you've intimated that it makes a noise when the end of file is reached. Is this just my imagination, or does the sound get garbled? If that's the case, there's something else going on.
[ March 27, 2005: Message edited by: David Harkness ]
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The noise is a definate "you messed up" DONG! I'll bet it is a JBuilder thing.

However, I tried your "write a value that is not possible at the end of the recording, then check for that value when reading" suggestion and it worked great (after I updated some of my code that was not compatible in the "for" loop. The program works great.

I like your code. It is much more simple than what I had. Looks good. I will (also) try that option. But, you got me through the tough part. Raelly appreciate the help. Hope you had a great Easter. I am good now.

Thank you,
Mitch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!