Click here to Skip to main content
15,892,809 members
Articles / Desktop Programming / MFC

Morse code. Some examples of how to produce sounds.

Rate me:
Please Sign up or sign in to vote.
4.64/5 (16 votes)
3 Apr 2001 132.5K   2.6K   66  
Several classes demonstrating how to fill audio buffer and how to produce sound using waveOut* API
// AlexfSnd.h: interface for the CAlexfSnd class.
// Part of "Morse" project. (C) Alexander Fedorov, 2000. lamer2000@hotmail.com
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ALEXFSND_H__950F8104_A353_11D2_9864_E7C799AF3D43__INCLUDED_)
#define AFX_ALEXFSND_H__950F8104_A353_11D2_9864_E7C799AF3D43__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <mmsystem.h>

#pragma comment(lib,"winmm.lib")

#define ALEXF_PLAY_BUF 10
#define ALEXF_PLAY_SPS 44100

#define PCM_STAGE_FREE			0
#define PCM_STAGE_MARK_TO_FILL	1
#define PCM_STAGE_PLAY_NOW		2

class CAlexfPCM
{
public:
	WAVEHDR whdr;
	int iStage;
	long lFreq;
	double dDelay;
	long lNext;
	char * WaveBuf;

public:
	CAlexfPCM();
	virtual ~CAlexfPCM();

	static void FillSinus08(
	char* pBuf,			// buffer address
	int iBufLen,		// buffer size
	int iSPS,			// frequency (Samples Per Second)
	double fFreq);		// frequency, Hz

	BOOL CreateSinus08Sample();
	BOOL ResizeBuf(long lNewSize);

};

class CAlexfAsyncPlay2
{
	BOOL bOK;
	int SPS;			// Samples Per Sec
	WAVEFORMATEX wfme;
	HWAVEOUT hwout;
	MMRESULT mmres;
	static void CALLBACK CallbackWaveOutProc(HWAVEOUT hwo,
	UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);

	CAlexfPCM * pcm;

	static CAlexfAsyncPlay2 * ziz;

	int GetFreeBuf();
	int GetBusyBufferNumber();
	void ReleaseBuf(WAVEHDR * lpwhdr);
	
public:
	CAlexfAsyncPlay2();
	virtual ~CAlexfAsyncPlay2();
	BOOL AddSingleSound(long Freq, double Delay);
	BOOL IsOK() {return bOK;}
};

class CAlexfAsyncPlay
{
	BOOL bOK;
	int SPS;			// Samples Per Sec
	WAVEFORMATEX wfme;
	HWAVEOUT hwout;
	MMRESULT mmres;
	static void CALLBACK CallbackWaveOutProc(HWAVEOUT hwo,
	UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);

	CAlexfPCM * pcm;

	long lLastPCM;
	
	int GetFreeBuf();
	void ReleaseBuf(WAVEHDR * lpwhdr);
	int GetBusyBufferNumber();

	static CAlexfAsyncPlay * ziz;

	static DWORD WINAPI ThreadFunc(LPVOID lpThreadParameter);
	HANDLE hThread;
	CRITICAL_SECTION csCriticalSection;
	BOOL bStopThreadNow;
	BOOL StopThread();

public:
	CAlexfAsyncPlay();
	virtual ~CAlexfAsyncPlay();
	BOOL AddSingleSound(long Freq, double Delay);
	BOOL IsOK() {return bOK;}
};

class CAlexfSyncPlay
{
public:
	WAVEFORMATEX wfme;
	MMRESULT mmres;
	HWAVEOUT hwout;
	WAVEHDR whdr;
	char * WaveBuf;
	double dDuration;
	double dFreq;
	void FillSinus08(
	unsigned char* pBuf,
	int iBufLen,
	int iSPS,
	double fFreq);
	static void CALLBACK WaveOut_waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);
	BOOL Beep();

public:
	int SPS;				// Samples Per Sec
	CAlexfSyncPlay(BOOL bSoundOn = FALSE);
	virtual ~CAlexfSyncPlay();

	BOOL Beep08(double Freq, double Delay);
};

#endif // !defined(AFX_ALEXFSND_H__950F8104_A353_11D2_9864_E7C799AF3D43__INCLUDED_)

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 SEO
Russian Federation Russian Federation
AlexF's Blog in Russian
Owner Spy competition analysis
Rating Burner Rating of blogs

Comments and Discussions