views:

143

answers:

1

Hello,

I am writing program in Java will have to do with music input. I want to use my computer keyboard as instrument.Pretty much it will be as button accordion which I play. I will do the mapping between keys and notes through 2 octaves for beginning. I was thinking what would be good design in timing how much the key was pressed. I am using JFugue framework to generate music strings from what was played, and then it can be easily exported to midi. In order to generate these strings I will have to measure how much the tone was pressed and then give it some musical note length. This information about note length is also appended to the string. If you think I don't even need this please let me know, because I am Java beginner.

A: 

You can implement a KeyListener and rely on the the two keyPressed and keyReleased methods to time your notes.

Your KeyListener implementation would need to maintain state, perhaps in the form of a Map of VK_ codes -> event times. You could populate the Map with a start time in keyPressed processing, identifying the key in the KeyEvent through its getKeyCode(). You can then get the duration of the note once the keyReleased event fires by pulling the start time out of the Map with the key code.

If you haven't found it yet, here is Sun's tutorial on KeyListeners

akf
Hey I managed to write some code. So far it is good. I set the measure, tempo and give the JFugue player the string sequence it has to play. If I turn on metronome on and play with same tempo I send to player, it does sampling of tone lengths really well. But...but....I have little problem with KeyEvents. If I press a key for a longer time, it generates one KeyEvent, than waits some time and starts to generate many same KeyEvents.This behavior might be due to OS. If I press and hold the key it starts typing really fast...What to do...
ZeKoU
This does sound OS-specific. Are the repeated `KeyEvent` actions all coming in `keyPressed`/`keyReleased` pairs or are they just repeating `keyPressed`? If the latter, you have hope. You can test whether your collection of key start times already has an entry for the key in question, and if so, do not overwrite the start time held as the value.
akf