Click here to Skip to main content
15,891,567 members
Articles / Desktop Programming / MFC

How to play and record sounds

Rate me:
Please Sign up or sign in to vote.
4.64/5 (53 votes)
12 Mar 2001 519.5K   14.7K   142  
A simple application that shows how to play and record sounds under Windows
<!--------------------------------------------------------------------------->  
<!--                           INTRODUCTION                                

 The Code Project article submission template (HTML version)

 Using this template will help us post your article sooner. We are using MS 
 IIS and ASP pages on the server, allowing us to simplify the templates used 
 to create the articles.

 To fill in this template, just follow the 3 easy steps below:

   1. Fill in the article description details
   2. Add links to your images and downloads
   3. Include the main article text

 That's all there is to it! All formatting will be done by the ASP script 
 engine.
-->  

<!--------------------------------------------------------------------------->  
<!--                        IGNORE THIS SECTION                            -->
<html>
<head>
<title>The Code Project</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<Style>
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
</style>
</head>
<body bgcolor="#FFFFFF" color=#000000>
<!--------------------------------------------------------------------------->  


<!-------------------------------     STEP 1      --------------------------->
<!--  Fill in the details (CodeProject will reformat this section for you) -->

<pre>
Basename:    ?? please check the spelling for me!
Title:       HOWTO play and record sound
Author:      Thomas.Holme 
Email:       thomas.holme@openmpeg4.org
Environment: VC++ 6.0, win2k
Keywords:    mmsystem, sound, play, record
Level:       Beginner
Description: Howto record and play sound
Section      Multimedia
SubSection   Audio
</pre>


<!-------------------------------     STEP 2      --------------------------->
<!--  Include download and sample image information. All included files    --> 
<!--  should be in /my_cool_code/                                          -->

<ul>
<li class=download><a href="fister.zip">Download source + demo
  200 Kb</a></li>

<p><img border="0" src="wpe5.gif" width="152" height="136"></p>


<!-------------------------------     STEP 3      --------------------------->
<!--  Add the article text. Please use simple formatting (<h2>, <p> etc)   --> 

<h2>Introduction</h2>
  <p>This is a simple application that show you how to play and record sound under windows. 
  <br>It uses the old multimedia API. A better solution may be to use DirectSound.
  </p>

  <p><img border="0" src="UML.gif" width="427" height="327">
  </p>

  <h3>HOWTO read the code fast
  </h3>

  <p>Start with the two functions in <code>CFisterDlg</code> called <i>OnPlay</i>
  and <i>OnRecord</i>. Follow them down to the depth you need to use the
  classes.
  </p>

  <h3>Short description</h3>
  <p><code>CSoundIn</code> is a wrapper class that will let you retreive sound
  from the soundcard. Main functions is Start() and Stop()<p><code>CSoundOut</code>
  is a wrapper class that will let you play sound on the soundcard. Main
  functions is Start() and Stop()<p><code>CSoundFile</code> is a wrapper class
  of a single wave file, it can either be a file reader or a file writer object.
  See the constructor.<p><code>CSoundBase</code> is a very small class that
  encapsulate the wave format.<p><code>CBuffer</code> is a very small class that
  encapsulates a simple one dimentional buffer.<p>The project has a number of
  different callback functions:
</ul>
<blockquote>
  <ul>
    <li>One callback function make the Play button change it's lable to stop
      when it has finished playing the file. <code>CDialogDlg </code>enherits <code>CPipe
      </code>and overload a function that <code>CPipe </code>can call when it
      has finished playing the wave file. <code> </code></li>
    <li>Another callback function make it possible for <code>CSoundIn</code> to
      callback to <code>CPipe</code> when it has filled the input buffer. Thus <code>CPipe</code>
      can give <code>CSoundIn</code> a new buffer to fill. </li>
    <li>A clone of the above principle is also used in <code>CSoundOut</code>,
      which enables it to callback to the owner when it is finished playing the
      sound in a given buffer. </li>
  </ul>
</blockquote>
<ul>
  <h3>Problems that I encountered</h3>
  <p>I have spend almost 2 days debugging the following stupid problem. When <code>CSoundIn</code>
  and <code>CSoundOut</code> inherit from <code>CSoundBase</code> and <code>CWinThread</code>
  the order in which they are listed MUST be as shown below. If not the callback
  functions, which are started by the WinThread message handler, will not be
  able to access the member variables of the <code>CSoundIn</code> object.

<pre>
class CSoundIn : public CWinThread, public CSoundBase
</pre>

  <p>During these two days of fustration I also tried to implement the callback
  as regular callback functions called by the device driver. This is possible
  using ::waveInOpen(...). But since the this callback function is not allowed
  to call any of the ::waveInXXX(...) functions it is not of much use.</p>

  <p>&nbsp;</p>

<!-------------------------------    That's it!   --------------------------->

</ul>

</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions