AHK Piano

Post your working scripts, libraries and tools for AHK v1.1 and older
ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: AHK Piano

Post by ahketype » 22 Jan 2022, 19:02

Hi guys - I'm having fun with this. But why does each media file have to be loaded again after the MCI_Play()? Isn't there a way to re-use it from memory?

x32
Posts: 177
Joined: 25 Nov 2016, 16:44

Re: AHK Piano

Post by x32 » 22 Jan 2022, 21:13

ahketype wrote:
22 Jan 2022, 19:02
Hi guys - I'm having fun with this. But why does each media file have to be loaded again after the MCI_Play()? Isn't there a way to re-use it from memory?
I'm not sure why that is. If I remember correctly, reloading the file after playing it was the only way I could find to get it to work. It is the same way in the Drum script. Have you tried removing the extra "MCI_Open" lines and see if it works on your system?

I don't know if it's just how MCI works or if it's something I'm missing but if anyone knows I would like to learn why as well.

Actually I just had a thought. MCI may think the file is still playing since there is not a "MCI_Stop" function ever used. I'll play with it a bit and see if I can figure anything out.

Edit:
And no. Using stop does not work either. The only way I can find to play a file more than once is by reopening it every time.

Edit 2:
Wait, I know why. The file has been played to the end, there is nothing left to play. If the file is 4 seconds long every time you hit play, other than the first, it's trying to start playing from the 4 second mark. Technically, I should be using the function "MCI_Seek(hMedia1,"start")", which rewinds the track to the beginning but there seems to be some latency using that method. Repeatedly opening the track seems to allow the track to be reset without cutting off what is already playing. This could possibly be leaving a lot of open files in memory, I'm not sure.

ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: AHK Piano

Post by ahketype » 23 Jan 2022, 02:41

Yeah, that all fitted with my tests, but I think I've made inroads into it now. As you thought, we have to reset the audio with MCI_Seek. I suspect subsequent Play calls aren't being ignored, it's just it's already playing at position x, and continues playing at position x, so you don't notice unless it's reset somewhere that sounds different. Since the piano notes are about 30 seconds long (some are 25-ish), when we test it we've not left it long enough to complete, and nothing audible changes.

So one fix is to Seek the start before or after a note's played (after, ready for the next time), or always to use, MCI_Play(hmedia, "from 0 to " . n) which seems to do the same anyway!

Incidentally, if n is beyond the end of the audio, it seems to not play it at all.

So this works for me:

Code: Select all

load:
FileSelectFile, selectedtrack, 3, , Select an Audio File, Audio (*.wav; *.mp3)
If ErrorLevel
	Return
hMedia:=MCI_Open(selectedtrack)
MCI_Play(hMedia,"from 0 to 500")
mcidevice := MCI_DeviceType(hMedia)
GuiControl, , type, %mcidevice%
Sleep 1000
MCI_Play(hMedia,"from 0 to 500")
Sleep 1000
MCI_Play(hMedia,"from 0 to 500")
Return
Edited to add - or of course "from 0 to 20000" with these files. The playing doesn't have to finish for the new call to work.

For the piano key routines, I've not discovered which works best, seeking 0 or playing from 0 to n (< file len) ... I think the audio length is another function you can call to check.

ahketype
Posts: 191
Joined: 27 Oct 2016, 15:06
Location: Yorkshire, UK

Re: AHK Piano

Post by ahketype » 23 Jan 2022, 05:36

P.S. Now I've found the higher notes are shorter - about 12 s. Probably best to interrogate MCI_Length() first for the "to" value so as not to exceed it, if you do it that way.

Post Reply

Return to “Scripts and Functions (v1)”