Click here to Skip to main content
15,886,565 members
Articles / Desktop Programming / Win32

Play Audio Files with DirectSound and Display its Spectrum in Real Time - Part 3

Rate me:
Please Sign up or sign in to vote.
4.93/5 (21 votes)
24 Dec 2008CPOL3 min read 273.7K   17.7K   86  
An article to show how to play audio file with DirectSound and display its spectrum in real time accurately
#pragma once

#include "stdafx.h"

#ifndef INCLUDE_THREAD
#define INCLUDE_THREAD

/************************************************************************/
/* CThread                                                              */
/************************************************************************/
class CThread
{
private:
	HANDLE m_hThread;
	DWORD m_dwThreadId;
	BOOL m_Suspended;

	static DWORD CALLBACK ThreadProc(LPVOID lpParameter)
	{
		CThread* pThread = (CThread*)lpParameter;
		if(pThread == NULL)
			return 0;

		pThread->Execute();
		return 0;
	}

protected:
	BOOL m_Stop;
	virtual void Execute() = 0; /* this pure virtual function should be implemented by child class 
								which inherited from this class
								*/
public:
	CThread(BOOL pCreateSuspened = TRUE) : m_hThread(NULL), m_dwThreadId(0), m_Stop(FALSE), m_Suspended(FALSE)
	{
		m_hThread = CreateThread(NULL, 0, ThreadProc, (void*)this,
			(pCreateSuspened == TRUE) ? CREATE_SUSPENDED : 0, &m_dwThreadId);
		m_Suspended = TRUE;
	}

	~CThread()
	{
		if(m_hThread != NULL)
		{
			CloseHandle(m_hThread);
		}
	}

	void Resume()
	{
		if(m_hThread != NULL && m_Suspended == TRUE && !m_Stop)
		{
			ResumeThread(m_hThread);
			m_Suspended = FALSE;
		}
	}

	void Suspend()
	{
		if(m_hThread != NULL && m_Suspended == FALSE && !m_Stop)
		{
			SuspendThread(m_hThread);
			m_Suspended = TRUE;
		}
	}

	BOOL Suspended() { return m_Suspended; }

	void Stop()
	{
		m_Stop = TRUE;
		if(!Suspended())
			Suspend();
	}
};

#endif

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer none
China China
To be, or not to be, this is question. That's are all depend on your decision. What do you think?

Comments and Discussions