Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Library] MCI v1.1 - Play/Control media files


  • Please log in to reply
71 replies to this topic
jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
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.

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:Source code: MCI.ahk
Documentation: MCI.html
Examples: MCI - Examples.zip
Issues/Considerations/Bugs
  • 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.
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.autohotke...ith-autohotkey/

I am gone for 15 days...
http://www.autohotke...p-media-player/

MCI Reference
http://tinyurl.com/67sce3Scripts that use the library:
QuickMediaPlayer
http://www.autohotke...pic.php?t=71867---------------------------------------------------------------------------
Release Notes

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)

v1.0
- Cleaned up code. No functional differences from v0.3.1
- Added a few examples.
- Added external documentation.

v1.1
- Minor changes to support Unicode. Should work on all versions of AutoHotkey but has not been tested on a 64-bit build.
- Updated examples.


heresy
  • Members
  • 291 posts
  • Last active: Sep 26 2008 10:47 PM
  • Joined: 11 Mar 2008
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

heresy
  • Members
  • 291 posts
  • Last active: Sep 26 2008 10:47 PM
  • Joined: 11 Mar 2008
MCI_Record() / MCI_StopRecord() prototypes
can record from any audio input device (microphone, steromix, etc)

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
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

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

MCI_Record() / MCI_StopRecord() prototypes
can record from any audio input device (microphone, steromix, etc)

This actually works! Way cool. 8)

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.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
Great job!
Thanks :)
Posted Image

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
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.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Library bumped to v0.3 -- minor bug fix. See the bottom of the first post for a description of the change.

..:: Free Radical ::..
  • Members
  • 74 posts
  • Last active: May 30 2015 12:13 PM
  • Joined: 20 Sep 2006
Wow. Thanks. i will use this

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Great presentation and nice package of examples.
Thanks. :)

rani
  • Members
  • 217 posts
  • Last active: Jul 21 2016 12:53 PM
  • Joined: 18 Mar 2008
recognize voice

can you go 1 step more,
and make voice to text ?

or to find some word in .wav file ?

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

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:
Microsoft Speech API 5.3
http://tinyurl.com/6krxcw

(...which may require the) Speech SDK 5.1
http://tinyurl.com/yptaooHere are a few AutoHotkey posts on the topic:
Voice recognition
http://www.autohotke...pic.php?t=20493

Voice Recognition COM
http://www.autohotke...pic.php?t=26841

Text to Talk
http://www.autohotke...opic.php?t=3719
For more AutoHotkey posts, do a search using one or more of the following keywords: Speech, Voice, Talk

I hope this helps.

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
@ 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:
[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.

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

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.

Drugwash
  • Members
  • 1078 posts
  • Last active: May 24 2016 04:20 PM
  • Joined: 07 Sep 2008
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.

(AHK 1.0.48.05 and Win98SE) forever | My scripts are here


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
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.