Jim Lawless' Blog


A Simple Media Control Interface Script Processor

Originally published on: Sat, 29 Aug 2009 12:18:17 +0000

In my recent posts A Command Line CD Tray Opener and A Command Line MP3 Player for Windows, I wrote code to send simple command strings to the Media Control Interface via the mciSendString() API function.

I wanted to toy more with the Media Control Interface, so I wrote another short program, mciscript.c, which reads a text file and sends commands from the text file directly to the MCI. The purpose of mciscript is to provide a simple testbed for MCI commands, but it could be used as a practical utility for media control.

mciscript.c


// MCIScript
// Pass commands from a file to the Media Control Interface
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include <windows.h>
#include <stdio.h>
#include <string.h>

#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"kernel32.lib")

void sendCommand(char *);

int main(int argc,char **argv) {
   FILE *fp;
   char *p;
   
   char cmdBuff[1024];
   printf("MCIScript\n");
   printf("Send a batch of MCI commands to the Media Control Interface\n");
   printf("by Jim Lawless\n\n");
   
   if(argc<2) {
      fprintf(stderr,"Syntax:\n\tmciscript text-filename\n");
      return 1;
   }
   fp=fopen(argv[1],"r");
   if(fp==NULL) {
      fprintf(stderr,"Cannot open input file %s\n",argv[1]);
      return 1;
   }
   while(fgets(cmdBuff,sizeof(cmdBuff)-2,fp)!=NULL) {
         // # in the first position indicates a comment
      if(*cmdBuff=='#')
         continue;
         // see if we've got a blank line
      for(p=cmdBuff;*p;p++) {
         if( (*p!=' ')&&(*p!='\t')&&(*p!='\r')&&(*p!='\n'))
            break;
      }
      if(!(*p))
         continue;
            // chop off newline
      cmdBuff[strlen(cmdBuff)-1]=0;
      printf("%s\n",cmdBuff);
      sendCommand(cmdBuff);
   }
   fclose(fp);
   return 0;
}

   // Send a string to the Media Control Interface
   // If an error occurs, display it and the string
   // that produced the error.
void sendCommand(char *s) {
   int i;
   i=mciSendString(s,NULL,0,0);
   if(i) {
         fprintf(stderr,"Error %d when sending %s\n",i,s);
   }
}

The script format for mciscript is simple. If a '#' character is detected in the first position of a line, that line is ignored by the input processor. This allows mciscript to have a crude commenting ability.

Blank lines are also ignored by the input processor. Please note that execution of the script does not terminate if an error occurs on a particular line of input.

All other lines of text are sent to the Media Control Interface. Please consider the following file:

mci.txt


# play an MP3 file starting at 11 seconds
play some.mp3 from 11000 wait


# play an MPEG movie at fullscreen and repeat
# viewing when done.
play some.mpg fullscreen repeat wait

# open the CD tray
set CDAudio door open wait


# close the CD tray
set CDAudio door closed wait

One may invoke the above script by entering the command: mciscript mci.txt

The above script will cause some.mp3 to begin playing at 11 seconds ( 11000 milliseconds ). Then, it will play some.mpg and will repeat forever. Because of the inclusion of repeat and wait, the script will never reach the last two sections which open and close the CD tray door respectively.

At the time of this writing, a list of MCI command can be found here:

http://msdn.microsoft.com/en-us/library/ms710815(VS.85).aspx

The source and EXE files for mciscript can be found here.

http://www.mailsend-online.com/wp/mciscript.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless

del_icio_us Save to del.icio.us
stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks



Previous post: Scrolling GIF Banners with PerlMagick
Next post:Changing the C64 Text Color in C


Search this Blog (and site)

Search this Site with PicoSearch


Subscribe to this Blog

 Subscribe!


Contact Me

Email: jimbo@radiks.net


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

Mad Schemes : Learning Lisp via SICP

Auto Save Clipboard Images Redux

Extending SpiderMonkey JavaScript on Windows

Rhino JavaScript to EXE with launch4j

Compiling Rhino JavaScript to Java

Directory Traversal in Rhino JavaScript

Taking Shape

We've Moved!


Popular Posts

A Command-Line MP3 Player for Windows

Auto Save Images from the Clipboard

Java in a Windows EXE with launch4j

An Interview with Tom Zimmer: Forth System Developer

Setting Windows Console Text Colors in C


Random Posts

An Interview with the Creator of the BDS C Compiler

A Command-Line CD Tray Opener

Jim Butterfield : The Commodore Guru

Scott Ballantyne: Blazin' Into Forth

Preventing Windows Screen-Saver Activation

Checking Shift States with DEBUG

Open Source Licenses

A Simple ROT13 Macro

Locking a Windows Session

BBS Fun in the Eighties


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Blogroll

MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer