Click here to Skip to main content
13,732,045 members
Click here to Skip to main content


140 bookmarked
Posted 2 Jan 2001

How to play and record sounds

, 12 Mar 2001
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 

<!--                        IGNORE THIS SECTION                            -->
<title>The Code Project</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
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; }
<body bgcolor="#FFFFFF" color=#000000>

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

Basename:    ?? please check the spelling for me!
Title:       HOWTO play and record sound
Author:      Thomas.Holme 
Environment: VC++ 6.0, win2k
Keywords:    mmsystem, sound, play, record
Level:       Beginner
Description: Howto record and play sound
Section      Multimedia
SubSection   Audio

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

<li class=download><a href="">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)   --> 

  <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><img border="0" src="UML.gif" width="427" height="327">

  <h3>HOWTO read the code fast

  <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

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

class CSoundIn : public CWinThread, public CSoundBase

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


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



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.


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


About the Author

Thomas Holme
Web Developer
Denmark Denmark
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 13 Mar 2001
Article Copyright 2001 by Thomas Holme
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid