Toying around with Java Sound...some problems with KeyEvents

Hi there!

I’m not sure if this is the right forum for the issue, so if it’s not please move

Since I’m studying Media-Informatics I had to do this tiny little Java-Programm based on Java-Sound where I was recommended to implement a synthesizer which made some sounds on keypresses…
So here’s the problem: The Programm works fine on my Desktop PC but not on my Laptop where KeyReleased-Events are generated but no keys are released. The thing is, the sounds should be maintained as long as a key is pressed, so I set up some booleans n stuff, which works quite fine, but on the laptop it just repeats the sound instead of maintaining it. It just goes like pressing a key in a word-processor, for example if I keep holding the “f”-Button
it should be only one “f”, instead of like in word processing “ffffffffffffffffffff…”, if you know what I mean.

So I hope you can help me fix this on my laptop…it’s got SuSe 11.0 running…

Thx

Cya

Rittler

Please, post the code. I’ll try to help.

Here you go… :slight_smile:

Since my Code is too long for one Post I’ll divide it to two…

import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Synthesizer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
/**
 *
 * @author dietzh
 *
 */

public class SynthNote{
	
	//The notes are initialized as static integers
	private static final int C = 24;
	private static final int D = 26;
	private static final int E = 28;
	private static final int F = 29;
	private static final int G = 31;
	private static final int A = 33;
	private static final int B = 35;
	//anything else of need...
	private Synthesizer synth;
	private MidiChannel] channels;
	private static final int nVelocity = 100;
	private KeyboardChannelListener keyboard;
	private JFrame frame;
	private JLabel key;
	private JLabel note;
	private JLabel released;
	
	/**the booleans are there for maintaining the notes, since the Tread.sleep-Thing didn't work out too well.
	Well actually this is workin on my Desktop but the laptop seems to generate Release-Key-Events though keys
	are not released...One can see that through the output of key-released on the JFrame.*/
	private boolean c0C = false , c0D = false, c0E = false, c0F = false, c0G = false, c0A = false, c0B = false;
	private boolean c10C = false, c10D = false, c10E = false, c10F = false, c10G = false, c10A = false, c10B = false;
	private boolean c15C = false, c15D = false, c15E = false, c15F = false, c15G = false, c15A = false, c15B = false;
	
	public SynthNote(){
		
		//standartprocedure for frames...
		frame = new JFrame("SynthNote");
		frame.setSize(300, 200);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	    frame.setLayout(new GridLayout(3, 1));
	    key = new JLabel();
	    note = new JLabel();
	    released = new JLabel();
	    frame.getContentPane().add(key);
	    frame.getContentPane().add(note);
	    frame.getContentPane().add(released);
		
	    //Synthesizer n everything needes is done here...
		synth = null;

		try {
			synth = MidiSystem.getSynthesizer();
			synth.open();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}		
		channels = synth.getChannels();
		
		//Here the implementation of my listener is initialized
		keyboard = new KeyboardChannelListener();
		frame.addKeyListener(keyboard);
		
		frame.setVisible(true);
	}
	
	
	
	public static void main(String] args) {

		new SynthNote();
	}
}
/**
	 * This class implements my KeyListener
	 * @author dietzh
	 *
	 */
	class KeyboardChannelListener implements KeyListener{
		
		//a lot of copy-paste...
		public void keyPressed(KeyEvent e){
			if(c0C == false && e.getKeyCode() == KeyEvent.VK_Y){ //check which key is pressed and if it was pressed before...
				channels[0].noteOn(C, nVelocity); //play the note
				c0C = true; //set the flag
				key.setText("Key pressed: " + e.getKeyChar());//output on the JFrame
				note.setText("Note playing: C");//output on the Jframe
				frame.repaint();//repaint JFrame
				}
			
			else if(!c0D && e.getKeyCode() == KeyEvent.VK_X){
				channels[0].noteOn(D, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: D");
				frame.repaint();
				c0D = true;
			}
			else if(!c0E && e.getKeyCode() == KeyEvent.VK_C){
				channels[0].noteOn(E, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: E");
				frame.repaint();
				c0D = true;
			}
			else if(!c0F && e.getKeyCode() == KeyEvent.VK_V){
				channels[0].noteOn(F, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: F");
				frame.repaint();
				c0F = true;
			}
			else if(!c0G && e.getKeyCode() == KeyEvent.VK_B){
				channels[0].noteOn(G, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: G");
				frame.repaint();
				c0G = true;
			}
			else if(!c0A && e.getKeyCode() == KeyEvent.VK_N){
				channels[0].noteOn(A, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: A");
				frame.repaint();
				c0A = true;
			}
			else if(!c0B && e.getKeyCode() == KeyEvent.VK_M){
				channels[0].noteOn(B, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: B");
				frame.repaint();
				c0B = true;
			}
			else if(c10C == false && e.getKeyCode() == KeyEvent.VK_A){
				channels[10].noteOn(C, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: C");
				frame.repaint();
				c10C = true;
			}
			else if(!c10D && e.getKeyCode() == KeyEvent.VK_S){
				channels[10].noteOn(D, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: D");
				frame.repaint();
				c10D = true;
			}
			else if(!c10E && e.getKeyCode() == KeyEvent.VK_D){
				channels[10].noteOn(E, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: E");
				frame.repaint();
				c10E = true;
			}
			else if(!c10F && e.getKeyCode() == KeyEvent.VK_F){
				channels[10].noteOn(F, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: F");
				frame.repaint();
				c10F = true;
			}
			else if(!c10G && e.getKeyCode() == KeyEvent.VK_G){
				channels[10].noteOn(G, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: G");
				frame.repaint();
				c10G = true;
			}
			else if(!c10A && e.getKeyCode() == KeyEvent.VK_H){
				channels[10].noteOn(A, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: A");
				frame.repaint();
				c10A = true;
			}
			else if(!c10B && e.getKeyCode() == KeyEvent.VK_J){
				channels[10].noteOn(B, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: B");
				frame.repaint();
				c10B = true;
			}
			else if(!c15C && e.getKeyCode() == KeyEvent.VK_Q){
				channels[15].noteOn(C, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: C");
				frame.repaint();
				c15C = true;
			}
			else if(!c15D && e.getKeyCode() == KeyEvent.VK_W){
				channels[15].noteOn(D, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: D");
				frame.repaint();
				c15D = true;
			}
			else if(!c15E && e.getKeyCode() == KeyEvent.VK_E){
				channels[15].noteOn(E, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: E");
				frame.repaint();
				c15E = true;
			}
			else if(!c15F && e.getKeyCode() == KeyEvent.VK_R){
				channels[15].noteOn(F, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: F");
				frame.repaint();
				c15F = true;
			}
			else if(!c15G && e.getKeyCode() == KeyEvent.VK_T){
				channels[15].noteOn(G, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: G");
				frame.repaint();
				c15G = true;
			}
			else if(!c15A && e.getKeyCode() == KeyEvent.VK_Z){
				channels[15].noteOn(A, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: A");
				frame.repaint();
				c15A = true;
			}
			else if(!c15B && e.getKeyCode() == KeyEvent.VK_U){
				channels[15].noteOn(B, nVelocity);
				key.setText("Key pressed: " + e.getKeyChar());
				note.setText("Note playing: B");
				frame.repaint();
				c15B = true;
			}
		}
		public void keyReleased(KeyEvent e){
			
			//a lot of copy-paste as well
			if(e.getKeyCode() == KeyEvent.VK_Y){
				channels[0].noteOff(C, nVelocity); //stop the note
				released.setText("Key released: " + e.getKeyChar());//output on the JFrame
				frame.repaint();//repaint the JFrame
				c0C = false;//set the flag
			}
			else if(e.getKeyCode() == KeyEvent.VK_X){
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				channels[0].noteOff(D, nVelocity);
				c0D = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_C){
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				channels[0].noteOff(E, nVelocity);
				c0E = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_V){
				channels[0].noteOff(F, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c0F = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_B){
				channels[0].noteOff(G, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c0G = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_N){
				channels[0].noteOff(A, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c0A = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_M){
				channels[0].noteOff(B, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c0B = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_A){
				channels[10].noteOff(C, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c10C = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_S){
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				channels[10].noteOff(D, nVelocity);
				c10D = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_D){
				channels[10].noteOff(E, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c10E = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_F){
				channels[10].noteOff(F, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c10F = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_G){
				channels[10].noteOff(G, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c10G = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_H){
				channels[10].noteOff(A, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c10A = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_J){
				channels[10].noteOff(B, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c10B = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_Q){
				channels[15].noteOff(C, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c15C = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_W){
				channels[15].noteOff(D, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c15D = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_E){
				channels[15].noteOff(E, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c15E = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_R){
				channels[15].noteOff(F, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c15F = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_T){
				channels[15].noteOff(G, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c15G = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_Z){
				channels[15].noteOff(A, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c15A = false;
			}
			else if(e.getKeyCode() == KeyEvent.VK_U){
				channels[15].noteOff(B, nVelocity);
				released.setText("Key released: " + e.getKeyChar());
				frame.repaint();
				c15B = false;
			}
		}

		public void keyTyped(KeyEvent arg0) {
			//no need for that one...
			
		}
	}

I found only one problem. Here’s the relevant part corrected:


        public void keyPressed(KeyEvent e){

            ...
            else if(!c0D && e.getKeyCode() == KeyEvent.VK_X){
                channels[0].noteOn(D, nVelocity);
                key.setText("Key pressed: " + e.getKeyChar());
                note.setText("Note playing: D");
                frame.repaint();
                c0D = true;
            }
            else if(!c0E && e.getKeyCode() == KeyEvent.VK_C){
                channels[0].noteOn(E, nVelocity);
                key.setText("Key pressed: " + e.getKeyChar());
                note.setText("Note playing: E");
                frame.repaint();
                **c0E** = true; //was C0D = true;
            }
            else if(!c0F && e.getKeyCode() == KeyEvent.VK_V){
                channels[0].noteOn(F, nVelocity);
                key.setText("Key pressed: " + e.getKeyChar());
                note.setText("Note playing: F");
                frame.repaint();
                c0F = true;
            }
            ...

So far, it seems to be working fine here at work (win xp). I’ll try it at home in both my laptop and desktop computer (both openSUSE 10.3).

[Edit:] You also seem to have inverted VK_Z and VK_Y (but that shouldn’t be the real problem, should it?)

Hey there…

Since I live in Germany I didn’t invert VK_Y and VK_Z, cause on german keyboards those actually are inverted compared to american standarts…

Thx for your corrections concerning that flag though, although it doesn’t seem to affect the actual problem in any way… :wink:

Today I tested it on my laptop. I get the same results sa you do: holding down any key fires quite a number of keyPressed/keyReleased one after the other, until one actually releases that key. Weird!

I do not know what to say: if I were you I would contact someone at Sun and ask them if it is supposed to work that way on laptops. Maybe it is a known bug.

Sorry I couldn’t help you more.

Well, I think it’s not a bug of sun’s java but of SuSe, since it works very well on a friends laptop using Windows XP…