 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
jballi
Joined: 01 Oct 2005 Posts: 537 Location: Texas, USA
|
Posted: Sun Aug 31, 2008 11:37 am Post subject: [Library] MCI v0.3.1 - Play/Control media files |
|
|
Introduction
In July 2007, Fincs released a popular library of functions to control Media Control Interface (MCI) devices. He called it Sound.ahk. The library is a conversion of the AutoIt standard library Sound.au3, originally provided by RazerM.
A month later, Fincs released an enhanced version of the library as Media.ahk. Because of the unfortunate title of the post ("I am gone for 15 days..."), many forum visitors may have missed this update.
Shortly after posting the last update, forum traffic from Fincs went quiet. I sent him an email about posting an update to his library a while back but I haven't received a response (so far).
After doing some MCI research on msdn, I started making some changes to the library for my personal use. After a while there were enough modifications to share the changes. So as not to confuse this release with any of the previous libraries, I named this library MCI.ahk. Although there are significant differences here and there, the library maintains the same basic structure as originally released by Fincs. Credit and thanks to Fincs for providing the original AutoHotkey libraries and to RazerM for providing the original AutoIt library.
What's New/Different
The following are some of the new or enhanced features:
- Support for Audio CDs. The MCI_OpenCDAudio function has been added to open CDAudio devices. In addition, other library functions have been added or enhanced to support CDAudio and similar devices.
- Notify Callback. The MCI_Play function has been enhanced to support the MCI "notify" flag which will call a developer-defined function when the device completes (or fails to complete) the requested operation.
- Wait flag. With the exception of the MCI_Play and MCI_Record functions, all library functions that manage media (Open, Close, Pause, Resume, Seek, etc.) have been modified to include the MCI "wait" flag by default. This small but important change insures that the library functions do not step on each other by forcing the mciSendString DLL function to complete the requested operation before returning control to the calling function.
- Improved Error Handling/Reporting. Several important changes:
- The MCI_Open and the new MCI_OpenCDAudio functions have been significantly beefed-up to trap for errors. Where possible, a developer-friendly error message is dumped to the debugger when an error occurs.
- All calls to the mciExecute DLL function have been removed. This obsolete (according to msdn) function is a bit easier to use than the mciSendString DLL function but it displays a message dialog when errors occur. A message dialog is tolerable when developing/testing a script but can be catastrophic in a live script especially if the message dialog is displayed in lieu of returning the appropriate error code.
- The MCI_SendString function has been enhanced to collect the associated message text when a non-zero error code is returned from the mciGetErrorString DLL function. Although the error code is still returned by the MCI_SendString function, the developer-friendly message text is dumped to the to the debugger (via outputdebug) for easy review.
The Code
The pertinent files are as follows:
Issues/Considerations/Bugs
- Preview release. Since this a preview release of this library, some of the code has not been optimized and there may be some residual debug code lying around. If there is any interest, these issues will be corrected in future releases.
- MCI devices. A few considerations:
- Not all MCI devices are created equal. If you write a routine that works for one MCI device, don't assume that it will work for other device types. Be sure to test your script using all of the target device types. If necessary, use the MCI_DeviceType function to identify which MCI device type you're using.
- Setup/Configuration. Sometimes a media file will not open, play, or operate correctly because the file's extension is not registered as a valid MCI extension or the MCI extension is assigned to the wrong device type/driver. Although I'm sure they exist, I don't know of any tools to identify and fix a bad MCI configuration. However, additions and changes to the MCI installation can be made in the registry. Be careful! See the documentation in the MCI_Open function for registry locations.
- Bugs. Although it's relatively rare, MCI devices do have bugs. Sometime media doesn't play or does something freaky because of bugs in the device driver.
- Seek. The MCI_Seek function presents unique challenges. It was designed to position the media to a specific location but because the core Seek command stops the media after it has been positioned, the function has been enhanced to return the device back to the state it was in before the function was called if necessary (Play or Pause). Although this methodology works for most devices, it does not work as expected for some devices (CDAudio for example) and it is probable that a call to this function will interrupt the "Notify" option of the MCI_Play function.
- Debugging. Checking the return codes from the MCI_Open and MCI_OpenCDAudio functions is critical. These functions establish a connection to a MCI device and until a valid connection is made, none of the other library functions will work correctly.
Checking the return codes from the rest of the library functions brings into account the law of diminishing returns. The reason: 99% of the MCI errors occur because of programming and/or logic errors. These include errors that occur because of attempts to access a device that hasn't been opened or using a command, flag, or flag option that is not supported by the device.
There are two tools that are necessary for proper debugging:
- MCI Reference Guide. This is a "must have" reference to find out what you can and cannot do with/to a MCI device. See the References section for the link.
- Debugger. When a non-zero error code is returned from the mciGetErrorString DLL function, the associated error message text is dumped to the debugger in an easy-to-read format. Errors trapped by the MCI_Open and MCI_OpenCDAudio functions are also sent to the debugger in a easy-to-read format.
- Documentation. The internal library documentation is mostly complete but there is no external documentation (yet).
References
The following are forum posts and links that I used to research this project.
Sound.ahk - Sound functions for use with AutoHotkey
http://www.autohotkey.com/forum/viewtopic.php?t=20666
I am gone for 15 days...
http://www.autohotkey.com/forum/viewtopic.php?t=22662
MCI Reference
http://tinyurl.com/67sce3
Final Thoughts
I hope that someone finds this useful.
---------------------------------------------------------------------------
v0.1 (Preview)
Original release.
v0.2 (Preview)
- Added the MCI_Record and MCI_Save functions. (thanks heresy)
- Added "Record" example. (thanks heresy)
- Enhanced the MCI_Open function to support the "new" device name (used when recording with WaveAudio device).
- Removed the p_Track parameter from the MCI_Seek function. For devices that support tracks, this code did not work as expected and is not really needed. May be added back (with enhancements) in the future.
- Minor updates to examples.
- Minor documentation changes.
- Minor changes to the forum post body.
v0.3 (Preview)
- Fixed bug with notify feature of MCI_Play function when the p_hwndCallback parameter is not specified. (thanks skwire)
v0.3.1
- Fixed bug in the MCI_Open function for Windows 95/98/ME users. (thanks Drugwash)
Last edited by jballi on Mon Oct 20, 2008 3:35 am; edited 5 times in total |
|
| Back to top |
|
 |
heresy
Joined: 11 Mar 2008 Posts: 291
|
Posted: Sun Aug 31, 2008 12:02 pm Post subject: |
|
|
wow what a coincidence!
i was reading exactly same part of msdn to accomplish native ahk sound recording function and i was even the first reader of this thread!
seems you've wrapped the entire MCI functions.
Thanks for posting this! _________________ Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com |
|
| Back to top |
|
 |
heresy
Joined: 11 Mar 2008 Posts: 291
|
Posted: Sun Aug 31, 2008 3:30 pm Post subject: |
|
|
MCI_Record() / MCI_StopRecord() prototypes
can record from any audio input device (microphone, steromix, etc)
| Code: | MCI_Record(){
MCI_SendString("open new Type waveaudio Alias MCI_RECORD", Dummy)
Return MCI_SendString("record MCI_RECORD", Dummy)
}
MCI_StopRecord(SavePath){
if FileExist(SavePath)
return false
SavePath:= """" SavePath """"
MCI_SendString("stop MCI_RECORD", Dummy)
l_Return := MCI_SendString("save MCI_RECORD " SavePath, Dummy)
MCI_SendString("close MCI_RECORD", Dummy)
Return l_Return
} |
Example
| Code: | Gui, Add, Button, gStart, Start Recording
Gui, Add, Button, gStop, Stop Recording
Gui, Add, Progress, wp +0x8 hwndhwnd
Gui, Show
Return
Start:
FileSelectFile, SavePath, S8, .wav,,*.wav
if !Errorlevel
{
MCI_Record()
SendMessage, 0x40A,1,,, ahk_id %hwnd%
}
Return
Stop:
SendMessage, 0x40A,0,,, ahk_id %hwnd%
MCI_StopRecord(SavePath)
Return |
_________________ Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com |
|
| Back to top |
|
 |
jballi
Joined: 01 Oct 2005 Posts: 537 Location: Texas, USA
|
Posted: Sun Aug 31, 2008 5:44 pm Post subject: |
|
|
| heresy wrote: | MCI_Record() / MCI_StopRecord() prototypes
can record from any audio input device (microphone, steromix, etc)
|
This actually works! Way cool.
After looking at the code, I think, in my humblest opinion, that all of the commands except for the "record" and "save" commands should be removed from the individual functions. The reason: The MCI library already supports commands to Open (needs enhancement to support the "new" media type), Stop, and Close a device. In addition, the Pause and Resume commands can be used with this device while it is opened to record.
If you're willing, I would like to PM (or email) you an example of what I'm talking about so that you can review it and verify that it works and make sense. After we're done and with your permission, I would like to add these functions and the record example to the library and examples download. Let me know.
Thank you for your feedback. |
|
| Back to top |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4114 Location: Belgrade
|
Posted: Mon Sep 01, 2008 9:09 am Post subject: |
|
|
Great job!
Thanks  _________________
 |
|
| Back to top |
|
 |
jballi
Joined: 01 Oct 2005 Posts: 537 Location: Texas, USA
|
Posted: Wed Sep 03, 2008 3:41 am Post subject: |
|
|
| Library updated to v0.2. Added the MCI_Record and MCI_Save functions (thanks heresy) and other minor changes. See the bottom of the first post for a complete list of changes. |
|
| Back to top |
|
 |
jballi
Joined: 01 Oct 2005 Posts: 537 Location: Texas, USA
|
Posted: Sat Sep 06, 2008 10:47 pm Post subject: |
|
|
| Library bumped to v0.3 -- minor bug fix. See the bottom of the first post for a description of the change. |
|
| Back to top |
|
 |
..:: Free Radical ::..
Joined: 20 Sep 2006 Posts: 47
|
Posted: Sun Sep 07, 2008 8:02 am Post subject: |
|
|
| Wow. Thanks. i will use this |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7176
|
Posted: Wed Sep 10, 2008 3:14 pm Post subject: |
|
|
Great presentation and nice package of examples.
Thanks.  |
|
| Back to top |
|
 |
rani
Joined: 18 Mar 2008 Posts: 144
|
Posted: Wed Sep 10, 2008 3:54 pm Post subject: recognize voice |
|
|
recognize voice
can you go 1 step more,
and make voice to text ?
or to find some word in .wav file ? |
|
| Back to top |
|
 |
jballi
Joined: 01 Oct 2005 Posts: 537 Location: Texas, USA
|
Posted: Wed Sep 10, 2008 7:59 pm Post subject: Re: recognize voice |
|
|
| rani wrote: | recognize voice
can you go 1 step more,
and make voice to text ?
or to find some word in .wav file ? |
Unfortunately, the Media Control Interface (MCI) is older technology and Microsoft has no plans to enhance it to include voice recognition (aka "speech-to-text"). Since this library is based entirely on MCI, voice recognition is out of scope.
However, quite a bit of work has been done on text-to-speech and speech-to-text technology. These are just a couple of links to give you a taste:
Here are a few AutoHotkey posts on the topic:
For more AutoHotkey posts, do a search using one or more of the following keywords: Speech, Voice, Talk
I hope this helps. |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Sat Oct 18, 2008 12:51 pm Post subject: |
|
|
@ jballi:
Standard example keeps throwing 'Error opening media file' on any mp3 I try to open. The files work fine with fincs' original library + player.
The problem is I'm running Win98SE and the registry path specified in MCI_Open ( HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\MCI Extensions ) does not exist in this version of Windows.
Instead, an MCI Extensions section can be found in %windir%\win.ini. Mine reads as follows:
| Code: | [mci extensions]
mid=Sequencer
rmi=Sequencer
wav=waveaudio
avi=AVIVideo
cda=CDAudio
aif=MPEGVideo
aifc=MPEGVideo
aiff=MPEGVideo
au=MPEGVideo
m1v=MPEGVideo
m3u=MPEGVideo
midi=Sequencer
mov=MPEGVideo
mp2=MPEGVideo
mp3=MPEGVideo
mpa=MPEGVideo
mpe=MPEGVideo
mpeg=MPEGVideo
mpg=MPEGVideo
mpv2=MPEGVideo
qt=MPEGVideo
snd=MPEGVideo
asf=MPEGVideo2
asx=MPEGVideo2
ivf=MPEGVideo2
mp2v=MPEGVideo
wax=MPEGVideo2
wvx=MPEGVideo2
wm=MPEGVideo2
wmx=MPEGVideo2
wma=MPEGVideo2
wmp=MPEGVideo2
wmv=MPEGVideo2
kar=Sequencer
ka2=Sequencer
dat=MPEGVideo |
I could modify the function to include a win.ini section parser but I'd rather not mess with your code. For personal use I have simply commented out the return false line in the type check, but I'd appreciate if you'd do things right yourself. Thank you. |
|
| Back to top |
|
 |
jballi
Joined: 01 Oct 2005 Posts: 537 Location: Texas, USA
|
Posted: Sat Oct 18, 2008 4:41 pm Post subject: |
|
|
| Drugwash wrote: | Standard example keeps throwing 'Error opening media file' on any mp3 I try to open. The files work fine with fincs' original library + player.
The problem is I'm running Win98SE and the registry path specified in MCI_Open ( HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\MCI Extensions ) does not exist in this version of Windows.
Instead, an MCI Extensions section can be found in %windir%\win.ini. Mine reads as follows:
<snip>
I could modify the function to include a win.ini section parser but I'd rather not mess with your code. For personal use I have simply commented out the return false line in the type check, but I'd appreciate if you'd do things right yourself. Thank you. |
Thanks for reporting the problem.
You're right. The MCI_Open function assumes (incorrectly unfortunately) that you are running a 32-bit version of Windows by assuming that the MCI extensions are loaded in the registry. This test was added to this library because attempting to run an unregistered extension can produce catastrophic results (lock up, crash, etc.).
This should be an easy fix. Because of the new error trap code added to this library, the correct fix might be to remove the test entirely. I'll do some research.
I have a Window 98 machine (somewhere?) but I haven't booted it up in 4 years. Would you be willing to test the fix on your Win98 machine before I do a general release? Let me know. Thanks.
Last edited by jballi on Sat Oct 18, 2008 5:00 pm; edited 1 time in total |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Sat Oct 18, 2008 4:59 pm Post subject: |
|
|
| Sure, no problem. You may send the script to my GMail or post in a PM; unfortunately resources are at 12% now, can't open up my messenger. |
|
| Back to top |
|
 |
jballi
Joined: 01 Oct 2005 Posts: 537 Location: Texas, USA
|
Posted: Mon Oct 20, 2008 3:39 am Post subject: |
|
|
| Library bumped to v0.3.1 -- minor bug fix for Windows 9x/ME users. See the bottom of the first post for a description of the change. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|