Click here to Skip to main content
Click here to Skip to main content

How to play and record sounds

By , 12 Mar 2001
 

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.

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

About the Author

Thomas Holme
Web Developer
Denmark Denmark
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionaudioportmemberfaezeh sadat6 Apr '13 - 12:31 
hi,i want to a code record and play audio with mic in C language,and make a dll with the code ,then load it in matlab software,and call the micros there to improve the speed ,but i dont know which of the code samples i should selected to this,help me plz
GeneralMy vote of 5memberDr Bob12 Jan '12 - 10:24 
This is outstanding work!
QuestionThis sample is very portable, but has some wrong points.memberYohei Murakami29 Dec '11 - 14:50 
void CSoundBase::Update()
{
  m_Format.nAvgBytesPerSec    = m_Format.nSamplesPerSec*(m_Format.wBitsPerSample/8);
  m_Format.nBlockAlign        = m_Format.nChannels     *(m_Format.wBitsPerSample/8);
}
 
Above calculations of parameters in WAVEFORMATEX are, assumedly, wrong.
An implementation along the MSDN description is below.
 
void CSoundBase::Update()
{
  m_Format.nBlockAlign = (m_Format.nChannels * m_Format.wBitsPerSample) / 8;
  m_Format.nAvgBytesPerSec = m_Format.nSamplesPerSec * m_Format.nBlockAlign;
}

QuestionI can not record fix period time using winmm.lib!!!!! [modified]memberfaird1 Aug '11 - 18:26 
Hi dear,
I am using the winmm.lib to record audio device.
I want to record with fix period time(e.g 60ms) but I get the variable
period of recording(e.g. 47,62,78,47,62,78,. . .).
How can I solve this problem?
Thanks

modified on Wednesday, August 3, 2011 2:40 AM

QuestionHow do I save my recorded sound using this application?membergameBaaj19 Feb '11 - 2:08 
there is a file ma,e soundfile.cpp, here there is a function mmioWrite that is being used, how do I change the m_hFIle variable to something like an array where I can store the byte and save the recorded voice into .txt format. ANy sugestions, thanks in advance.
Generalproblem with stalls due to waveoutresetmemberleehartin27 Jan '10 - 5:12 
Thank you for this code, it has been very useful. I have had problems when playing one file after another in rapid succession with it hanging after a random number of calls. I presume that this is because the device is not ready. Is there a solution?
Questionwhat technique did u usememberAdnan Aman13 May '09 - 1:09 
Just wondering what technique did you use.. MCI or directX? internally
 
Muhammad Adnan
Software Engineer (.NET)

QuestionHelp On sondcard recording upon data arrivalmemberfunboyhere16 Jun '08 - 4:30 
Hello. I was working on a C# program for reading a data through a sound card in .wav form. I wanted for the sound card to automatically start recording the data upon arrival and these store /pass it for manipulation.
thank you for the help in advance. --fun is the way of life--L.Z
 
I have fun coding using c# + c++

GeneralNeed Help in Looping Soundmemberdemxine25 Nov '07 - 21:14 
Greetings.
 
Can anyone please help me on how to modify the source code so that when you play a wav file, the playback would loop continuously until I set it to stop.
 
Your help would be greatly appreciated, thank you.
 
->Demsen
GeneralRe: Need Help in Looping SoundmemberKarstenK12 Dec '07 - 21:48 
I do this via ::PlaySound( m_csAudio, 0, SND_ASYNC|SND_NODEFAULT|SND_LOOP/*Repeat Flag*/ )
 
Greetings from Germany

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 13 Mar 2001
Article Copyright 2001 by Thomas Holme
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid