Many people cannot afford musical instruments although they might want to play one. My aim for this project is to turn a PC keyboard into a piano.
I've mapped the keys in a zig-zag order which at first looks rather odd - like a twisted piano keyboard - but this allows for many more keys than a layout that appears more traditional and is not a difficult adjustment for the player. My mapping allows for a total of 44 keys, not bad considering that's exactly half as many as the 88 keys on a grand piano.
Here is my program so far:
You'll need to download my ding.wav to run, or replace it with one of your own sounds.
[UPDATED]
rem Program Name: Piano
rem Author: OBese87
rem Date: 5th April 2009
rem ==================
set display mode 640,480,32
disable systemkeys
hide mouse
sync on:sync rate 0
gosub backdrop
rem Constants
TRUE=1:FALSE=0
white= rgb(255,255,255)
grey= rgb(125,125,125)
red= 255*65536
sample_file_path$ = "tones\ding.wav" : rem *** TRY USING YOUR OWN SOUNDS ***
gosub setup_keyboard
rem === MAIN ===
Do
gosub note_press
gosub pitch_alter
gosub display
sync:copy bitmap 1,0
Loop
End
rem === SUBROUTINES ===
backdrop:
sw = screen width()
sh = screen height()
create bitmap 1,sw,sh
for i = 0 to 255
ink i,0
box 0,i*(sh/255),sw-1,(i+1)*3
next i
set text font "arial",1
set text size 14
set text to italic
ink -2,0
text sw-50,sh-16,"OBese87"
set current bitmap 0
set text to normal
return
`//
display:
rem show key status
for note = 1 to maxkeys
if sound playing(note) then ink white,0 else ink grey,0
if keystate(keymap(note))=TRUE then ink red,0
if note<22 then col = 0 else col = 1
text 40+col*250,(note-col*21)*18,key$(note)
text 80+col*250,(note-col*21)*18,str$(keyfreq(note))
text 140+col*250,(note-col*21)*18,str$(keyfreq(note)*pitch)
next note
`text
ink white,0
for col= 0 to 250 step 250
text 0+col,0,"Key" : text 40+col,0,"Note" : text 80+col,0,"Freq." : text 140+col,0,"Pitch/Freq."
next col
text 0,460,"Pitch ("+str$(pitch)+"). To alter hold RETURN + UP/DOWN"
return
`//
note_press:
`check keys
for n = 1 to maxkeys
`key pressed
if keystate(keymap(n))=TRUE
if keyrelease(n)=TRUE
play sound n : keyrelease(n)=FALSE
else
rem note hold effects here
endif
endif
`key released
if keystate(keymap(n))=FALSE then keyrelease(n) = TRUE
next n
return
`//
pitch_alter:
oldu=newu : oldd=newd
newu=upkey() : newd=downkey()
if returnkey()
if newu>oldu and (pitch+5)*keyfreq(maxkeys)<100000 then pitch=pitch+5
if newd>oldd then pitch=pitch-5 : if pitch<1 then pitch=1
for n= 1 to maxkeys
if keyfreq(n)*pitch<100 then keyfreq(n)=100
if keyfreq(n)*pitch>100000 then keyfreq(n)=100000/pitch
set sound speed n,keyfreq(n)*pitch
next n
endif
return
`//
setup_keyboard:
`note tuning data
` C C# D Eb E F F# G Ab A Bb B
data 262,278,294,312,330,350,374,392,419,440,466,497
`key mapping data
` C C# D Eb E F F# G Ab A Bb B C C# D Eb E F F# G Ab
`key a z s x d c f v g b h n j m k , l . ; / '
data 30,44,31,45,32,46,33,47,34,48,35,49,36,50,37,51,38,52,39,53,40
` A Bb B C C# D Eb E F F# G Ab A Bb B C C# D Eb E F F# G
`key q 2 w 3 e 4 r 5 t 6 y 7 u 8 i 9 o 0 p - [ = ]
data 16,3, 17,4, 18,5, 19,6, 20,7, 21,8, 22,9, 23,10,24,11,25,12,26,13,27
`note string data
data "C","C#","D","Eb","E","F","F#","G","Ab","A","Bb","B"
`prepare keys
maxkeys=44
Dim keyfreq(maxkeys)
Dim keymap(maxkeys)
Dim key$(maxkeys)
Dim keyrelease(maxkeys)
`load tone
load sound sample_file_path$,1
for s = 2 to maxkeys
clone sound s,1
next s
`tune keys
pitch=20
for n= 1 to maxkeys
if n<13
read keyfreq(n)
else
keyfreq(n) = keyfreq(n-12)*2
endif
set sound speed n,keyfreq(n)*pitch
next n
`map keys to keyboard
for n= 1 to maxkeys
read map
keymap(n)= map
keyrelease(n)=TRUE
next n
`store key names
for n= 1 to maxkeys
if n<13 then read key$(n) else key$(n)=key$(n-12)
next n
remstart
SET EAX
This command will set the environmental audio effect that all sounds will use.
The Effect Value must be a value between 0 and 26. A value of zero deactivates EAX,
where one of the following values activates one of the preset effects.
1=GENERIC 2=PADDEDCELL 3=ROOM 4=BATHROOM 5=LIVINGROOM 6=STONEROOM 7=AUDITORIUM 8=CONCERTHALL
9=CAVE 10=ARENA 11=HANGAR 12=CARPETEDHALLWAY 13=HALLWAY 14=STONECORRIDOR 15=ALLEY 16=FOREST
17=CITY 18=MOUNTAINS 19=QUARRY 20=PLAIN 21=PARKINGLOT 22=SEWERPIPE 23=UNDERWATER 24=DRUGGED 25=DIZZY 26=PSYCHOTIC
remend
Set EAX 8
return
`//
I've run into two problems that I need help with.
1. I'm having trouble getting the note frequencies right, is there a formula for working these out or a reference somewhere that has lots of frequencies. I could only find frequencies for one scale.
2. When pressing some keys together nothing happens. Is there a work-around so that all keys can be pressed at the same time?
Does anyone know of a site where you can download instrument sounds?