Click here to Skip to main content
15,881,938 members
Articles / Desktop Programming / MFC
Article

How to play and record sounds

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

Image 1

Introduction

This is a simple application that shows you how to play and record sound under windows.
It uses the old multimedia API. A better solution may be to use DirectSound.

Image 2

Quick Guide to the Code

Start with the two functions in CFisterDlg called OnPlay and OnRecord. Follow them down to the depth you need to use the classes.

Short description

CSoundIn is a wrapper class that will let you retreive sound from the soundcard. The main functions are Start() and Stop()

CSoundOut is a wrapper class that will let you play sound on the soundcard. The main functions are Start() and Stop()

CSoundFile is a wrapper class of a single wave file, it can either be a file reader or a file writer object. See the constructor.

CSoundBase is a very small class that encapsulates the wave format.

CBuffer is a very small class that encapsulates a simple one dimentional buffer.

The project has a number of different callback functions:

  • One callback function makes the Play button change it's label to stop when it has finished playing the file. CDialogDlg enherits
    CPipe 
    and overloads a function that CPipe can call when it has finished playing the wave file.
  • Another callback function make it possible for CSoundIn to callback to CPipe when it has filled the input buffer. Thus CPipe can give CSoundIn a new buffer to fill.
  • A clone of the above principle is also used in CSoundOut, which enables it to callback to the owner when it is finished playing the sound in a given buffer.

Problems that I encountered

I have spent almost 2 days debugging the following stupid problem. When CSoundIn and CSoundOut inherit from CSoundBase and CWinThread 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 CSoundIn object.

class CSoundIn : public CWinThread, public CSoundBase

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 this callback function is not allowed to call any of the ::waveInXXX(...) functions it is not of much use.

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

 
GeneralWhy i can't even open the zip file?Help.......... Pin
D'nise1-Sep-07 0:09
D'nise1-Sep-07 0:09 
AnswerRe: Why i can't even open the zip file?Help.......... Pin
rajneshmalik1-Sep-07 0:55
rajneshmalik1-Sep-07 0:55 
Questionrecording blank sound file Pin
rakeshkumarsonu1-Aug-07 21:19
rakeshkumarsonu1-Aug-07 21:19 
GeneralErrors!! Pin
kaunreeves6-May-07 1:29
kaunreeves6-May-07 1:29 
GeneralRe: Errors!! Pin
arler29-Jan-08 17:30
arler29-Jan-08 17:30 
GeneralHelp Pin
kaunreeves5-May-07 16:59
kaunreeves5-May-07 16:59 
Generalunable to record the data from microphone Pin
pinto2212-Feb-07 3:43
pinto2212-Feb-07 3:43 
AnswerRe: unable to record the data from microphone Pin
Pehuenco25-Feb-07 23:07
Pehuenco25-Feb-07 23:07 
GeneralRe: unable to record the data from microphone Pin
-=Seph=-25-Mar-07 21:04
-=Seph=-25-Mar-07 21:04 
GeneralRe: unable to record the data from microphone Pin
Figu.Lin10-Feb-09 16:30
Figu.Lin10-Feb-09 16:30 
GeneralHelp me with default recording device!!! Pin
Hung In26-Jul-06 16:01
Hung In26-Jul-06 16:01 
GeneralHi Pin
LuckyBoydtt13-Jul-06 8:05
LuckyBoydtt13-Jul-06 8:05 
GeneralRe: Hi Pin
Andy Byron22-Dec-06 3:47
Andy Byron22-Dec-06 3:47 
GeneralRe: Hi Pin
LuckyBoydtt22-Dec-06 4:05
LuckyBoydtt22-Dec-06 4:05 
Generalt Pin
angshu1201198415-Jun-06 23:54
angshu1201198415-Jun-06 23:54 
if we want to record sound in d fister program
without using play , record button but using indiactor soubnd
how can we proceed?
GeneralHelp me! Pin
trananh19-Jan-06 15:03
trananh19-Jan-06 15:03 
GeneralError Message: Still 3 buffers in waveIn queue Pin
srettig15-Nov-05 11:01
srettig15-Nov-05 11:01 
QuestionCan i do this in C# Pin
Kumar Muthu29-Oct-05 0:26
Kumar Muthu29-Oct-05 0:26 
GeneralMore help in rectifying errors Pin
armimex200028-Oct-05 13:46
armimex200028-Oct-05 13:46 
AnswerRe: More help in rectifying errors Pin
Gokcer Gokdal10-Nov-05 11:34
Gokcer Gokdal10-Nov-05 11:34 
GeneralAudio and Video Pin
Kisi00719-Oct-05 7:33
Kisi00719-Oct-05 7:33 
GeneralSave the Recorded Sound to a .Wav File, Pin
REU7-Oct-05 8:43
REU7-Oct-05 8:43 
GeneralRe: Save the Recorded Sound to a .Wav File, Pin
yasar7910-Jan-06 1:08
yasar7910-Jan-06 1:08 
Generalselecting two input sources Pin
rajeev_geom7-Oct-05 1:03
rajeev_geom7-Oct-05 1:03 
GeneralVery nice design Pin
Gokcer Gokdal24-Aug-05 13:13
Gokcer Gokdal24-Aug-05 13:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.