Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Morse code. Some examples of how to produce sounds.

, 3 Apr 2001
Several classes demonstrating how to fill audio buffer and how to produce sound using waveOut* API
// MorseParser.cpp: implementation of the CMorseParser class.
// Part of "Morse" project. (C) Alexander Fedorov, 2000. lamer2000@hotmail.com
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Morse.h"
#include "MorseParser.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

const UINT CMorseParser::WM_WORK_FINISHED = ::RegisterWindowMessage("CMorseParser_WM_WORK_FINISHED");

CMorseParser::CMorseParser()
{
	pSafeBuffer = NULL;
	m_bThreadRunned = FALSE;
	m_bStopThreadNow = FALSE;
}

CMorseParser::~CMorseParser()
{
	m_bStopThreadNow = TRUE;
	while (m_bThreadRunned)
		Sleep(1);
	if (pSafeBuffer)
		delete pSafeBuffer;
}

BOOL CMorseParser::AddTextToStream(CString str)
{
	return FALSE;
}

CString CMorseParser::Parse(CString str)
{
	str.MakeLower();
	CString sss;
	int l = str.GetLength();
	for (int i = 0; i < l; i++)
	{
		switch(str.GetAt(i))
		{
		case ' ': sss += "/";		break;
		case 'a': sss += ".-";		break;
		case 'b': sss += "-...";	break;
		case 'c': sss += "-.-.";	break;
		case 'd': sss += "-..";		break;
		case 'e': sss += ".";		break;
		case 'f': sss += "..-.";	break;
		case 'g': sss += "--.";		break;
		case 'h': sss += "....";	break;
		case 'i': sss += "..";		break;
		case 'j': sss += ".---";	break;
		case 'k': sss += "-.-";		break;
		case 'l': sss += ".-..";	break;
		case 'm': sss += "--";		break;
		case 'n': sss += "-.";		break;
		case 'o': sss += "---";		break;
		case 'p': sss += ".--.";	break;
		case 'q': sss += "--.-";	break;
		case 'r': sss += ".-.";		break;
		case 's': sss += "...";		break;
		case 't': sss += "-";		break;
		case 'u': sss += "..-";		break;
		case 'v': sss += "...-";	break;
		case 'w': sss += ".--"; 	break;
		case 'x': sss += "-..-";	break;
		case 'y': sss += "-.--";	break;
		case 'z': sss += "--..";	break;
		case '0': sss += "-----";	break;
		case '1': sss += ".----";	break;
		case '2': sss += "..---";	break;
		case '3': sss += "...--";	break;
		case '4': sss += "....-";	break;
		case '5': sss += ".....";	break;
		case '6': sss += "-....";	break;
		case '7': sss += "--...";	break;
		case '8': sss += "---..";	break;
		case '9': sss += "----.";	break;
		}
		sss += " ";
	}
	return sss;
}

BOOL CMorseParser::AddParsedBufferToStreamAsync(CString str)
{
	if (m_bThreadRunned)
		return FALSE;
	m_bStopThreadNow = FALSE;
	if (pSafeBuffer)
		delete pSafeBuffer;
	int l = str.GetLength() + 1;
	pSafeBuffer = new char[l];
	ZeroMemory(pSafeBuffer, l);
	strcpy(pSafeBuffer, str);
	if (!AfxBeginThread(ThreadProc, (LPVOID) this))
		return FALSE;
	m_bThreadRunned = TRUE;
	return TRUE;
}

UINT CMorseParser::ThreadProc(LPVOID lpThreadParameter)
{
    CMorseParser* ziz = (CMorseParser*) lpThreadParameter;
	if (!ziz)
	{
		ziz->m_bThreadRunned = FALSE;
		return 0;
	}
	CString str = ziz->pSafeBuffer;
	int l = str.GetLength();
	for (int i = 0; i < l; i++)
	{
		if (ziz->m_bStopThreadNow)
			break;
		switch(str.GetAt(i))
		{
		case '.':
				ziz->SafeAddSingleSound(ziz->m_lFreq, ziz->m_dShort);
				break;
		case '-':
				ziz->SafeAddSingleSound(ziz->m_lFreq, ziz->m_dLong);
				break;
		case ' ':
				ziz->SafeAddSingleSound(0, ziz->m_dDelay);
				break;
		}
		ziz->SafeAddSingleSound(0, ziz->m_dDelay);
	}
	ziz->m_bThreadRunned = FALSE;
	::PostMessage(ziz->m_hWnd, WM_WORK_FINISHED, (WPARAM)ziz, 0);
	return 0;
}

BOOL CMorseParser::SafeAddSingleSound(long Freq, double Delay)
{
	BOOL bRC = FALSE;
	while (!bRC)
	{
		bRC = m_play.AddSingleSound(Freq, Delay);
		// bRC = m_play.Beep08(Freq, Delay);
		if (!bRC) Sleep(1);
		if (m_bStopThreadNow)
			break;
	}
	return bRC;
}

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

Share

About the Author

Alexander Fedorov
Web Developer SEO
Ukraine Ukraine
AlexF's Blog in Russian
Owner Spy competition analysis
Rating Burner Rating of blogs

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 4 Apr 2001
Article Copyright 2001 by Alexander Fedorov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid