cd_audio.shtml

来自「mfc资源大全包含MFC编程各个方面的源码」· SHTML 代码 · 共 598 行 · 第 1/2 页

SHTML
598
字号
<HTML>

<!-- Header information-->
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <!-- add your name in the CONTENT field below -->
   <META NAME="Author" CONTENT="Scott Miller">
   <TITLE>Miscellaneous - A Cd-audio class to build a simple CD player</TITLE>
</HEAD>

<!-- Set background properties -->
<body background="/fancyhome/back.gif" bgcolor="#FFFFFF">

<!-- A word from our sponsors... -->
<table WIDTH="100%">
<tr WIDTH="100%"><td align=center><!--#exec cgi="/cgi/ads.cgi"--><td></tr>
</table>



<CENTER><H3><FONT COLOR="#AOAO99">
A Cd-audio class to build a simple CD player
</FONT></H3></CENTER>
<HR>

<!-- Author and contact details -->
This article was contributed by <A HREF="mailto:giancarlo@saria.com">Giancarlo Iovino</A>.

<!-- Sample image - gif or jpg -->
<p><strong><img src="cd_audio_player.gif"
alt="The simple CD player dialog" width="194" height="202"></strong>&nbsp;<a
href="cd_audio.zip">Download Source Code and
Example</a> </p>
<!-- The article... --><strong>Introduction. What is the MCI interface?</strong></p>

<p>The <strong>MCI</strong> (Media Control Interface) is the
Windows API that gives programmer a high-level control on all
multimedia devices and resource files. MCI provides applications
with device-independent capabilities for controlling audio, video
and visual peripherals. This API has two different interface
levels: at the lower level (<em>command-message interface</em>)
the programmer controls devices by calling the <strong>mciSendCommand()</strong>
function. This function requires as its arguments the command to
be sent and some other command-specific parameters. The higher
level (<em>command-string interface</em>) is essentially a
textual version of the first, meaning that each command is
passed, together with its possible arguments, as a text string to
the <strong>mciSendString()</strong> function. As you can easily
imagine, the higher level offers a more friendly interface,
keeping programmers away from flags and structures. Even if MCI
is quite simple to use, I think that it should be a good thing if
it was object-oriented, too.</p>

<p>In this article, I'll try to explain how to build a simple
CD-audio player, using my <strong>CCdAudio</strong> class. This
class encapsulates the MCI command-message interface for
CD-audio. I hope it can be a good starting point to write
analogous classes for other multimedia devices: waveform-audio
devices, MIDI sequencers, etc. Building the player, however,
involves many other interesting issues. In particular, I'll show</p>

<ul>
    <li><a href="#1">How to use the asynchronous mode of MCI
        commands: the MM_MCINOTIFY message</a>.</li>
    <li><a href="#2">How to detect the events of opening and
        closing the CD drawer</a>. </li>
    <li><a href="#3">How to use the MSF and TMSF time formats</a>.</li>
</ul>

<p><strong>Wrapping the MCI</strong></p>

<p><img src="cd_audio_mci_hyer.gif" align="right" hspace="0" width="182"
height="151">Writing a set of classes to encapsulate the whole
MCI interface isn't so simple as you can think. The main
difficulty arises from the fact that it hasn't been thought in an
object-oriented way. The best thing is to start writing a base
class that implement a generic MCI device: this will be the base
class for all the other specific devices (Fig. 1). </p>

<p><strong>CMciDevice overview</strong></p>

<p>I called this base class <strong>CMciDevice</strong>. Its <strong>SendCommand()</strong>
protected member function hides the mciSendCommand() function,
but has also the capability to handle possible errors: when the
device is in the 'error report' mode, MCI errors are
automatically displayed using the MessageBox() function. You can
turn the error report on or off with the <a href="#REPORTERRORS"><strong>ReportErrors</strong></a><strong>()</strong>
member function (the default is no error report). <br>
In the CMciDevice class, as well as in CCdAudio, many of the
functions that implements MCI commands return a DWORD value that
is zero in case of success and a nonzero error code otherwise .
You can use this value to perform your own error handling (if you
want to know more about MCI error codes you can take a look at
the documentation). Other functions, however, don't return any
error code. In these cases, you can use the <a
href="#GETLASTERROR"><strong>GetLastError</strong></a><strong>()</strong>
function to get the code of the last occurred error.<br>
The <a href="#GETDEVCAPS"><strong>GetDevCaps</strong></a><strong>()</strong>
function 'queries' the device to know if it has specific
capabilities (see the table below).<br>
The <a href="#GETDEVCAPS"><strong>GetdevcapsCompound</strong></a>
capability deserves some more explanation. MCI devices are
divided in two categories: <em>simple</em> and <em>compound</em>
devices. The difference is that a compound device needs a <em>media
file</em> to work. Cd-audio is a typical simple device, while
wave-recorder is a compound device since it needs a file (the
waveform) to work. For more details on CMciDevice you can go to
the <a href="#REFERENCE"><strong>reference guide</strong></a>
below.</p>

<p><!--webbot bot="HTMLMarkup" startspan --><A NAME="#1">
<!--webbot
bot="HTMLMarkup" endspan --><strong>Synchronous vs asynchronous
method</strong></p>

<p>MCI supports both the synchronous and asynchronous operating
mode. When a MCI operation is performed synchronously the <strong>mciSendCommand()</strong>
function returns only when its task has been carried out. In the
asynchronous mode, instead, the function returns immediately but
a 'callback' window has to be designated by passing its handle as
a parameter. When the required operation has been performed (with
or without success), MCI notifies the callback window of the
event sending it a <strong>MM_MCINOTIFY</strong> message. This
message has a parameter whose values can be used to understand if
the required operation has been successfully performed, aborted
or failed (see the sample project code for details about how to
do this). Asynchronous method is very convenient for long
operations that might require much time to be completed such as
playback or seek. This is the reason why, in CCdAudio, <a
href="#PLAY"><strong>Play</strong></a><strong>()</strong> and <a
href="#SEEK"><strong>Seek</strong></a><strong>()</strong> are
asynchronous by default.</p>

<p><!--webbot bot="HTMLMarkup" startspan --><A NAME="#2"><!--webbot
bot="HTMLMarkup" endspan --><strong>Detecting the CD drawer
opening and closing</strong></p>

<p>When you open the CD drawer or when you insert a new CD and
close it, Windows sends applications a special
&quot;plug-and-play&quot; message: <strong>WM_DEVICECHANGE</strong>.
This message notifies an application or device driver of a change
to the hardware configuration of a device or the computer. Its
parameters are:</p>

<p>Event = (UINT) wParam; <br>
dwData = (DWORD) lParam;</p>

<p>The opening of the CD drawer is signalled by the operating
system as a DBT_DEVICEREMOVECOMPLETE event. Its closing, instead,
is a DBT_DEVICEARRIVAL event. The dwData parameter points to an
event-specific structure, but DEV_BROADCAST_HDR is a structure
header common to all the structure types that dwData can point.
The dbch_devicetype field distinguishes ports from logical
devices: if it is equal to DBT_DEVTYP_VOLUME then dwData points
to a DEV_BROADCAST_VOLUME structure. In this structure, the
dbcv_flags field shows if the involved drive is a network drive
or a physical device. To verify if it is really a CD we have to
use the dbcv_unitmask field: it is an unsigned long value with a
set bit for each drive that caused the event: the first
(leftmost) bit for A, the second for B, and so on. For instance,
if the CD drive is D: its value will be 8 (= 0...01000) since D
is the fourth letter. Once we have obtained the drive letter,
reading the volume label and verifying if it is really a CD drive
is quite simple. Sample code follows:</p>

<pre><font color="#800000">BOOL CMCISampleDlg::OnDeviceChange( UINT nEventType, DWORD dwData ) {
	DEV_BROADCAST_HDR *pdbch = (DEV_BROADCAST_HDR *) dwData;

	switch(nEventType) {
	case DBT_DEVICEARRIVAL:					// CD drawer was closed
		if (pdbch-&gt;dbch_devicetype == DBT_DEVTYP_VOLUME) {
			PDEV_BROADCAST_VOLUME pdbcv = 
				(PDEV_BROADCAST_VOLUME) dwData;
			if (pdbcv-&gt;dbcv_flags == DBTF_MEDIA)
			{
				TCHAR szDrive[4];
				// pdbcv-&gt;unitmask contains the drive bits
				for (UINT i=0; !(pdbcv-&gt;dbcv_unitmask &amp; (1&lt;&lt;i)); i++);
				wsprintf(szDrive, _T(&quot;%c:\\&quot;), 'A'+i);			
				if (GetDriveType(szDrive) == DRIVE_CDROM) {					
					UpdateControls();					
				}
			}
			else {
				// It is a network drive
			}
			break;
		}
	case DBT_DEVICEREMOVECOMPLETE:				// CD drawer was opened
	break;
	}

	return TRUE;
}</font>
</pre>

<p><!--webbot bot="HTMLMarkup" startspan --><A NAME="#3"><!--webbot
bot="HTMLMarkup" endspan --><strong>The MSF and TMSF time formats</strong></p>

<p>Several MCI commands involve setting or getting a time. For
instance, the <a href="#PLAY"><strong>Play</strong></a>()
function requires two times: the time at which to start the
playback and the time at which to stop it. Both are DWORDs but,
however, MCI can interpret them in different ways, depending on
which time format has been previously set with the MCI_SET
command (or, in he CCdAudio class, with the <a
href="#SETTIMEFORMAT"><strong>SetTimeFormat</strong></a><strong>()</strong>
member function). The allowable time formats for CD-audio are
milliseconds, MSF and TMSF. MSF stands for Minutes/Second/Frame
while TMSF stands for Track/Minute/Second/Frame. MSF and TMSF
times are packed in a single DWORD and special macros are used to
pack/unpack them. To simplify programmer's life, I wrote two
classes (CMsf and CTmsf ) for these time formats to eliminate the
necessity of remembering (and using) these macros. Both have an
overloaded operator DWORD to allow their use with those functions
that require DWORD parameters. Here are the two classes as they
are defined in the 'Mci.h' file:</p>

<pre><font color="#800000">class CMsf {
public:
	CMsf() { m_dwMsf = 0; }

	CMsf(DWORD dwMsf) { m_dwMsf = dwMsf; }

	CMsf(BYTE minute, BYTE second, BYTE frame) {	
		m_dwMsf = MCI_MAKE_MSF(minute, second, frame);
	}

	operator DWORD() const {return m_dwMsf;}

	BYTE GetMinute() const { return MCI_MSF_MINUTE(m_dwMsf); }
	BYTE GetSecond() const { return MCI_MSF_SECOND(m_dwMsf); }
	BYTE GetFrame() const { return MCI_MSF_FRAME(m_dwMsf); }

protected:
	DWORD m_dwMsf;
};

class CTmsf {
public:
	CTmsf() { m_dwTmsf = 0; }

	CTmsf(DWORD dwTmsf) { m_dwTmsf = dwTmsf; }

	CTmsf(BYTE track, BYTE minute, BYTE second, BYTE frame) {	
	m_dwTmsf = MCI_MAKE_TMSF(track, minute, second, frame);
	}

	operator DWORD() const {return m_dwTmsf;}

	BYTE GetTrack() const { return MCI_TMSF_TRACK(m_dwTmsf); }
	BYTE GetMinute() const { return MCI_TMSF_MINUTE(m_dwTmsf); }
	BYTE GetSecond() const { return MCI_TMSF_SECOND(m_dwTmsf); }
	BYTE GetFrame() const { return MCI_TMSF_FRAME(m_dwTmsf); }

protected:
	DWORD m_dwTmsf;
};</font></pre>

<p><strong>How to use CCdAudio</strong></p>

<p>To use CCdAudio you have to copy the 'Mci.h', 'Mci.cpp',
'CdAudio.cpp', and 'CdAudio.h' files in your project folder. Once
you have copied these files, include 'Mci.cpp' and 'CdAudio.cpp'
in your project. #include the CdAudio.h file at the beginning of
the module that needs it. Be aware that you have to link your
program with the 'winmm.lib' static library, or an error will
occur at link-time. </p>

<p>Using any MCI device requires the following steps:</p>

<ol>
    <li>Open the device using <a href="#OPEN"><strong>Open</strong></a><strong>()</strong>.</li>
    <li>Set a callback window for asynchronous operations using <a
        href="#SETCALLBACKWND"><strong>SetCallbackWnd</strong></a><strong>()
        </strong>(optional).</li>
    <li>Set a time format using <a href="#SETTIMEFORMAT"><strong>SetTimeFormat</strong></a><strong>()</strong>
        (optional).</li>
    <li>Perform a sequence of operations: (<a href="#PLAY"><strong>Play</strong></a><strong>()</strong>,
        <a href="#STOP"><strong>Stop</strong></a><strong>()</strong>,
        <a href="#SEEK"><strong>Seek</strong></a><strong>()</strong>,
        ... etc.).</li>
    <li>Close the device using <a href="#CLOSE"><strong>Close</strong></a>().</li>
</ol>

<p><!--webbot bot="HTMLMarkup" startspan --><A NAME="#REFERENCE">
<!--webbot
bot="HTMLMarkup" endspan --><strong>REFERENCE GUIDE</strong></p>

<p><strong>Operations for CMciDevice</strong></p>

<ul>
    <li>void <strong>Attach</strong>(UINT wDeviceID)<br>
        Attaches the device to a MCI device already opened</li>
    <li>MCIDEVICEID <strong>GetDeviceID</strong>() const<br>
        Gets the device ID</li>
    <li><!--webbot bot="HTMLMarkup" startspan --><A NAME="#MCIDEVICEOPEN">
<!--webbot
        bot="HTMLMarkup" endspan -->virtual DWORD <strong>Open</strong>(DWORD

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?