AHK Piano
AHK Piano
Another just for fun script to go along with the A small piano keyboard that can be played with a mouse, keyboard, or on a touchscreen. This again uses the which is included in the download. Using the MCI library instead of "SoundPlay", allows for chords (multiple notes) and no delay when clicking a key.
Script, background pic, sampled piano notes, and the MCI library are included in the zip file.
Script, background pic, sampled piano notes, and the MCI library are included in the zip file.
Last edited by x32 on 06 Mar 2023, 17:55, edited 1 time in total.
Re: AHK Piano
Hi x32,
This looks amazing, just what I was wanting, but unfortunately I can't get the sound to play. I'll start digging into the docs, but I thought I'd ask if you know the kind of thing that's likely to be wrong. I've #included the MCI.ahk library, and tested that the notes play in PotPlayer.
I also un-commented the #Warn command and it reported that "wait" was an undefined global variable. I added a line defining it (guessing at sensible milliseconds, and also trying true), but I imagine it's just undefined because it's set by default in the library and #Warn only gives non-critical error reports?
I also see the docs discuss different options for early and later versions of Windows, but suggest this might only work with 32-bit ones - I'm running Win 7 64-bit. Or maybe I need to discover or set something to do with my MCI extensions?
MCI_Open() checks for OS:
Just guesswork as I'm not very knowledgable about all this!
Cheers,
John
This looks amazing, just what I was wanting, but unfortunately I can't get the sound to play. I'll start digging into the docs, but I thought I'd ask if you know the kind of thing that's likely to be wrong. I've #included the MCI.ahk library, and tested that the notes play in PotPlayer.
I also un-commented the #Warn command and it reported that "wait" was an undefined global variable. I added a line defining it (guessing at sensible milliseconds, and also trying true), but I imagine it's just undefined because it's set by default in the library and #Warn only gives non-critical error reports?
I also see the docs discuss different options for early and later versions of Windows, but suggest this might only work with 32-bit ones - I'm running Win 7 64-bit. Or maybe I need to discover or set something to do with my MCI extensions?
MCI_Open() checks for OS:
Code: Select all
;-- Which OS type?
if A_OSType=WIN32_NT ;-- Windows NT4/2000/XP/2003/Vista/etc.
RegRead
,l_Dummy
,HKEY_LOCAL_MACHINE
,SOFTWARE\Microsoft\Windows NT\CurrentVersion\MCI Extensions
,%l_Extension%
else
{
;-- Windows 95/98/ME
iniRead
,l_Value
,%A_WinDir%\win.ini
,MCI Extensions
,%l_Extension%
if l_Value=ERROR
ErrorLevel=1
}
Cheers,
John
Re: AHK Piano
It could be the codecs on your system and how your system handles the mp3 extension. On mine, Windows 10 64 bit, I am only able to play mp3 and they are treated as a "digitalvideo" file, wav files are seen as "waveaudio" and won't play with the MCI library on my system. You could try converting the mp3s to .wav and see if they play on your system.
Try this script to see how your system treats audio files.
Try this script to see how your system treats audio files.
Code: Select all
#NoEnv
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance, Force
Gui, Add, Button, x10 y10 w100 gload, Load
Gui, Add, Edit, x10 y+5 w100 vtype,
Gui, Show, w200 h75, MCI Device Type
Return
load:
FileSelectFile, selectedtrack, 3, , Select an Audio File, Audio (*.wav; *.mp3)
If ErrorLevel
Return
hMedia:=MCI_Open(selectedtrack)
MCI_Play(hMedia)
mcidevice := MCI_DeviceType(hMedia)
GuiControl, , type, %mcidevice%
Return
Exit:
GuiEscape:
GuiClose:
MCI_Stop(hMedia)
MCI_Close(hMedia)
Gui, Destroy
ExitApp
Return
Re: AHK Piano
I don't understand - the notes are all .wav files, not .mp3.
I'll try your program and see if I discover anything. Thanks!
I'll try your program and see if I discover anything. Thanks!
Re: AHK Piano
OK, I tried that. Nothing plays (mp3 or wav), but I also get no result in the Edit box - so maybe that's useful. Looks like that's failing to get the MCI device type. What to do about that, I don't know.
Re: AHK Piano
For me test script works for mp3 as well as for wav files, however AHK_Piano remains silent (Win10 Home).
Re: AHK Piano
You're right, this is playing wav files. I just downloaded it to be sure I uploaded a working script. It does work on my system with wav files. The sixteen track mixer I released a couple of weeks ago would only work with mp3 files so I have no idea what the difference is.
Getting these scripts to work on my system was stretching my abilities so sorry I can't be more help in troubleshooting. There is mention of mciGetErrorString, at the bottom of the notes section which may be of help but I don't understand how to use it.
Re: AHK Piano
Thanks x32 and rommmcek...all these clues are helpful.
@rommmcek - do you get a string in the Edit when you run the test script?
@x32 - do you? No need to apologise, I'm grateful you posted your AHK Piano, as it's given me a way to do something I want in my script. I thought it would be impossible or a massive undertaking, but it seems more feasible now. I'm mostly in the dark with all this, but I'll try the error function and keep digging on MSDN and other places. I'll post here what I find.
@rommmcek - do you get a string in the Edit when you run the test script?
@x32 - do you? No need to apologise, I'm grateful you posted your AHK Piano, as it's given me a way to do something I want in my script. I thought it would be impossible or a massive undertaking, but it seems more feasible now. I'm mostly in the dark with all this, but I'll try the error function and keep digging on MSDN and other places. I'll post here what I find.
Re: AHK Piano
Cheers rommmcek.
@x32 Can you tell me what script you developed this (or the drum kit) from originally? It might help me debug this.
@x32 Can you tell me what script you developed this (or the drum kit) from originally? It might help me debug this.
Re: AHK Piano
I did everything based off the MCI library. I first made a simple audio player to learn how the MCI functions work. I'll post that if you think it would help.
I started the drum script and this piano script using the SoundPlay command built in AHK but it had a huge amount of lag which made it unusable so I switched to the MCI libary.
I also get digitalvideo for mp3 and waveaudio for wav . I remembered this morning that my system does play both mp3 and wav but only mp3 responds to the MCI_SetVolume function. Wav always plays at 100% volume.
You might need to trouble shoot the small things first if you haven't already. I would put a message box in some of the subroutines to makes sure the text is working as buttons on your system. If not take the "BackgroundTrans" option out of the "Gui, Add, Text" lines and see if that helps.
Second, I would add a message to the "Load" subroutine near the end of the script, to be sure MCI is loading the files correctly. It should return "MCIFile1" or an error message.
The only other thing I could think of is to use the "MCI_SetVolume" function after each "MCI_Open" function to make sure the sounds are not turned all the way down.
Here is a copy of my original piano script which uses buttons instead of background transparent text over an image. It may be easier to troubleshoot the MCI library using this script.
I started the drum script and this piano script using the SoundPlay command built in AHK but it had a huge amount of lag which made it unusable so I switched to the MCI libary.
I also get digitalvideo for mp3 and waveaudio for wav . I remembered this morning that my system does play both mp3 and wav but only mp3 responds to the MCI_SetVolume function. Wav always plays at 100% volume.
You might need to trouble shoot the small things first if you haven't already. I would put a message box in some of the subroutines to makes sure the text is working as buttons on your system. If not take the "BackgroundTrans" option out of the "Gui, Add, Text" lines and see if that helps.
Second, I would add a message to the "Load" subroutine near the end of the script, to be sure MCI is loading the files correctly. It should return "MCIFile1" or an error message.
Code: Select all
Load:
hMedia1:=MCI_Open("notes\a4.wav")
MsgBox, %hMedia1%
Here is a copy of my original piano script which uses buttons instead of background transparent text over an image. It may be easier to troubleshoot the MCI library using this script.
Code: Select all
; #Warn ; Enable warnings to assist with detecting common errors.
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance, Force
Gui, Margin, 5, 5
Gui, Add, Button, x30 y25 w50 h75 gas4, A#4`ns
Gui, Add, Button, x140 y25 w50 h75 gcs5, C#5`nf
Gui, Add, Button, x+5 y25 w50 h75 gds5, D#5`ng
Gui, Add, Button, x310 y25 w50 h75 gfs5, F#5`nj
Gui, Add, Button, x+5 y25 w50 h75 ggs5, G#5`nk
Gui, Add, Button, x+5 y25 w50 h75 gas5, A#5`nl
Gui, Add, Button, x520 y25 w50 h75 gcs6, C#6`n2
Gui, Add, Button, x+5 y25 w50 h75 gds6, D#6`n3
Gui, Add, Button, x690 y25 w50 h75 gfs6, F#6`n5
Gui, Add, Button, x+5 y25 w50 h75 ggs6, G#6`n6
Gui, Add, Button, x+5 y25 w50 h75 gas6, A#6`n7
Gui, Add, Button, x5 y100 w50 h75 ga4, A4`n440`nz
Gui, Add, Button, x+5 y100 w50 h75 gb4, b4`nx
Gui, Add, Button, x+5 y100 w50 h75 gc5, C5`nc
Gui, Add, Button, x+5 y100 w50 h75 gd5, D5`nv
Gui, Add, Button, x+5 y100 w50 h75 ge5, E5`nb
Gui, Add, Button, x+5 y100 w50 h75 gf5, F5`nn
Gui, Add, Button, x+5 y100 w50 h75 gg5, G5`nm
Gui, Add, Button, x+5 y100 w50 h75 ga5, A5`n880`n,
Gui, Add, Button, x+5 y100 w50 h75 gb5, B5`n.
Gui, Add, Button, x+5 y100 w50 h75 gc6, C6`n/ q
Gui, Add, Button, x+5 y100 w50 h75 gd6, D6`nw
Gui, Add, Button, x+5 y100 w50 h75 ge6, E6`ne
Gui, Add, Button, x+5 y100 w50 h75 gf6, F6`nr
Gui, Add, Button, x+5 y100 w50 h75 gg6, G6`nt
Gui, Add, Button, x+5 y100 w50 h75 ga6, A6`n880`ny
Gui, Add, Button, x+5 y100 w50 h75 gb6, B6`nu
Gui, Add, Button, x+5 y100 w50 h75 gc7, C7`ni
Gui, Show, w940 h200 , AHK Piano
GoSub, Load
Return
z::
a4:
MCI_Play(hMedia1,wait)
hMedia1:=MCI_Open("notes\a4.wav")
Return
s::
as4:
MCI_Play(hMedia2,wait)
hMedia2:=MCI_Open("notes\bb4.wav")
Return
x::
b4:
MCI_Play(hMedia3,wait)
hMedia3:=MCI_Open("notes\b4.wav")
Return
c::
c5:
MCI_Play(hMedia4,wait)
hMedia4:=MCI_Open("notes\c5.wav")
Return
f::
cs5:
MCI_Play(hMedia5,wait)
hMedia5:=MCI_Open("notes\db5.wav")
Return
v::
d5:
MCI_Play(hMedia6,wait)
hMedia6:=MCI_Open("notes\d5.wav")
Return
g::
ds5:
MCI_Play(hMedia7,wait)
hMedia7:=MCI_Open("notes\eb5.wav")
Return
b::
e5:
MCI_Play(hMedia8,wait)
hMedia8:=MCI_Open("notes\e5.wav")
Return
n::
f5:
MCI_Play(hMedia9,wait)
hMedia9:=MCI_Open("notes\f5.wav")
Return
j::
fs5:
MCI_Play(hMedia10,wait)
hMedia10:=MCI_Open("notes\gb5.wav")
Return
m::
g5:
MCI_Play(hMedia11,wait)
hMedia11:=MCI_Open("notes\g5.wav")
Return
k::
gs5:
MCI_Play(hMedia12,wait)
hMedia12:=MCI_Open("notes\ab5.wav")
Return
,::
a5:
MCI_Play(hMedia13,wait)
hMedia13:=MCI_Open("notes\a5.wav")
Return
l::
as5:
MCI_Play(hMedia14,wait)
hMedia14:=MCI_Open("notes\bb5.wav")
Return
.::
b5:
MCI_Play(hMedia15,wait)
hMedia15:=MCI_Open("notes\b5.wav")
Return
/::
q::
c6:
MCI_Play(hMedia16,wait)
hMedia16:=MCI_Open("notes\c6.wav")
Return
2::
cs6:
MCI_Play(hMedia17,wait)
hMedia17:=MCI_Open("notes\db6.wav")
Return
w::
d6:
MCI_Play(hMedia18,wait)
hMedia18:=MCI_Open("notes\d6.wav")
Return
3::
ds6:
MCI_Play(hMedia19,wait)
hMedia19:=MCI_Open("notes\eb6.wav")
Return
e::
e6:
MCI_Play(hMedia20,wait)
hMedia20:=MCI_Open("notes\e6.wav")
Return
r::
f6:
MCI_Play(hMedia21,wait)
hMedia21:=MCI_Open("notes\f6.wav")
Return
5::
fs6:
MCI_Play(hMedia22,wait)
hMedia22:=MCI_Open("notes\gb6.wav")
Return
t::
g6:
MCI_Play(hMedia23,wait)
hMedia23:=MCI_Open("notes\g6.wav")
Return
6::
gs6:
MCI_Play(hMedia24,wait)
hMedia24:=MCI_Open("notes\ab6.wav")
Return
y::
a6:
MCI_Play(hMedia25,wait)
hMedia25:=MCI_Open("notes\a6.wav")
Return
7::
as6:
MCI_Play(hMedia26,wait)
hMedia26:=MCI_Open("notes\bb6.wav")
Return
u::
b6:
MCI_Play(hMedia27,wait)
hMedia27:=MCI_Open("notes\b6.wav")
Return
i::
c7:
MCI_Play(hMedia28,wait)
hMedia28:=MCI_Open("notes\c7.wav")
Return
Exit:
GuiEscape:
GuiClose:
Gui, Destroy
ExitApp
Return
Load:
hMedia1:=MCI_Open("notes\a4.wav")
hMedia2:=MCI_Open("notes\bb4.wav")
hMedia3:=MCI_Open("notes\b4.wav")
hMedia4:=MCI_Open("notes\c5.wav")
hMedia5:=MCI_Open("notes\db5.wav")
hMedia6:=MCI_Open("notes\d5.wav")
hMedia7:=MCI_Open("notes\eb5.wav")
hMedia8:=MCI_Open("notes\e5.wav")
hMedia9:=MCI_Open("notes\f5.wav")
hMedia10:=MCI_Open("notes\gb5.wav")
hMedia11:=MCI_Open("notes\g5.wav")
hMedia12:=MCI_Open("notes\ab5.wav")
hMedia13:=MCI_Open("notes\a5.wav")
hMedia14:=MCI_Open("notes\bb5.wav")
hMedia15:=MCI_Open("notes\b5.wav")
hMedia16:=MCI_Open("notes\c6.wav")
hMedia17:=MCI_Open("notes\db6.wav")
hMedia18:=MCI_Open("notes\d6.wav")
hMedia19:=MCI_Open("notes\eb6.wav")
hMedia20:=MCI_Open("notes\e6.wav")
hMedia21:=MCI_Open("notes\f6.wav")
hMedia22:=MCI_Open("notes\gb6.wav")
hMedia23:=MCI_Open("notes\g6.wav")
hMedia24:=MCI_Open("notes\ab6.wav")
hMedia25:=MCI_Open("notes\a6.wav")
hMedia26:=MCI_Open("notes\bb6.wav")
hMedia27:=MCI_Open("notes\b6.wav")
hMedia28:=MCI_Open("notes\c7.wav")
Return
stop:
MCI_Stop(hMedia1)
MCI_Close(hMedia1)
MCI_Stop(hMedia2)
MCI_Close(hMedia2)
MCI_Stop(hMedia3)
MCI_Close(hMedia3)
MCI_Stop(hMedia4)
MCI_Close(hMedia4)
MCI_Stop(hMedia5)
MCI_Close(hMedia5)
MCI_Stop(hMedia6)
MCI_Close(hMedia6)
MCI_Stop(hMedia7)
MCI_Close(hMedia7)
MCI_Stop(hMedia8)
MCI_Close(hMedia8)
MCI_Stop(hMedia9)
MCI_Close(hMedia9)
MCI_Stop(hMedia10)
MCI_Close(hMedia10)
Return
Re: AHK Piano
Many thanks for all that x32, I'll learn a lot I'm sure.
Re: AHK Piano
Button script plays for me.
Didn't like how responds to holding key down. I tried e.g.:But there is noise when releasing the key.
Didn't like how responds to holding key down. I tried e.g.:
Code: Select all
z::
a4:
MCI_Play(hMedia1,wait)
hMedia1:=MCI_Play("notes\a4.wav")
KeyWait z
hMedia1:=MCI_Stop("notes\a4.wav")
Return
Re: AHK Piano
You shouldn't need to reassign the hMedia variable for each command and it looks like this would play the same note twice but simultaneously. Try this,rommmcek wrote: ↑20 Jan 2022, 18:18Button script plays for me.
Didn't like how responds to holding key down. I tried e.g.:But there is noise when releasing the key.Code: Select all
z:: a4: MCI_Play(hMedia1,wait) hMedia1:=MCI_Play("notes\a4.wav") KeyWait z hMedia1:=MCI_Stop("notes\a4.wav") Return
Code: Select all
z::
a4:
MCI_Play(hMedia1,wait) ; hMedia1 is a variable assigned notes\a4.wav on script startup in the load command.
KeyWait z ;
MCI_Stop(hMedia1)
Return
It's possible the noise at the end is an artifact of cutting off the sample
Re: AHK Piano
There is a problem with the MCI library provided with the piano package. I had the v1.1 provided by jballi (see here) and the drum kit played correctly. Installed the piano files complete with the embedded v1.0 MCI script in the same folder, overwriting v1.1, and both drums and piano were silent. Reverted MCI to v1.1 and they both started working correctly again.rommmcek wrote: ↑20 Jan 2022, 02:19For me test script works for mp3 as well as for wav files, however AHK_Piano remains silent (Win10 Home).
Mind you this happens in Linux Mint under Wine 5.14 (32bit profile set as XP), but it may be the same under certain versions of Windows.
Part of my AHK work can be found here.
Re: AHK Piano
Oh yeah, thanks! Didn't pay attention about MCI versions. With v1.0 AHK_Piano works.
@x32: Artefact or not, its annoying.
Your proposal seems to play each note only once...
Re: AHK Piano
Ooooooh, @Drugwash you fixed it for me! I didn't get time to do lots more debugging, and when I came back, there you were with this solution. Thanks.
@x32 Thanks again - this is amazing. I'll have to see what more I can do with that. I see it plays notes together, polyphonic, but only apparently up to some limit, and then it sounds like notes are missed until old ones finish playing. Shouldn't be a problem - maybe testing for a new note and if too many, stop the oldest one and play the new one.
@x32 Thanks again - this is amazing. I'll have to see what more I can do with that. I see it plays notes together, polyphonic, but only apparently up to some limit, and then it sounds like notes are missed until old ones finish playing. Shouldn't be a problem - maybe testing for a new note and if too many, stop the oldest one and play the new one.
Re: AHK Piano
You're both welcome.
I wonder if the old Klavier hero script still works in new Windows... It's quite difficult to get MIDI to work in Linux, and it has its drawbacks.
I wonder if the old Klavier hero script still works in new Windows... It's quite difficult to get MIDI to work in Linux, and it has its drawbacks.
Part of my AHK work can be found here.
Re: AHK Piano
Kalvier Hero plays fine on W10 H x64, even if it still has some bugs.
Re: AHK Piano
Great to hear it still works.
There used to be a few issues back when working on it but couldn't be fixed as far as I remember. Fat chance for me to get them fixed now since Wine has many features unimplemented or incomplete (such as no keyboard layout registry entries).
There used to be a few issues back when working on it but couldn't be fixed as far as I remember. Fat chance for me to get them fixed now since Wine has many features unimplemented or incomplete (such as no keyboard layout registry entries).
Part of my AHK work can be found here.