|
Key Trigger - Play notes
from the qwerty keyboard |
Reload
page to restart example
|
In this example keys from the
computer keyboard are mapped to the pitch of a note and trigger a note
to sound when pressed.
To
hear the example you may need to click on the example area (grey square
top left) to provide the Applet with focus. The reliability of this
working varies with different browsers : (
Importing the SoundCipher library is done in line 1.
Line 2 creates an instance of the SoundCipher class called sc, for use
in the program.
The keyPressed() method is called each time a user types on the qwerty
keyboard. In this method the playNote() command from SoundCipher is
called with three arguments; pitch, dynamic and duration. Pitch is
calculated by subtracting 40 from the key number. For example 'a' is key
number 97 resulting in MIDI pitch 57 (the A below middle C) and
each sucessive letter of the alphabet increases by one. A more useful
mapping of qwerty to piano keyboard could be done using a case
statement with multiple conditions - as there is no simple keyboard layout correlation - but we leave this as an exercise
for you.
The draw() method is required to keep Processing 'alive' even though it is empty and does not do anything. |