Click here to Skip to main content
12,291,271 members (59,111 online)
Click here to Skip to main content

Stats

494.1K views
16.5K downloads
281 bookmarked
Posted

Sound recording and encoding in MP3 format.

, 16 Nov 2006 CPOL
An article describing the technique of recording sound from waveform-audio input devices and encoding it in MP3 format.
mp3_stream_demo
lame_enc.dll
mp3_stream.exe
mp3_stream_src
INCLUDE
mp3_stream.dsp
mp3_stream.dsw
#ifndef ___SYNC_SIMPLE_H_INCLUDED___
#define ___SYNC_SIMPLE_H_INCLUDED___

//---------------------------- CLASS -------------------------------------------------------------

// Implementation of the critical section
class QMutex {
private:
	CRITICAL_SECTION	m_Cs;

public:
	QMutex() { ::InitializeCriticalSection(&this->m_Cs); }
	~QMutex() { ::DeleteCriticalSection(&this->m_Cs); }
	void Lock() { ::EnterCriticalSection(&this->m_Cs); }
	BOOL TryLock() { return (BOOL) ::TryEnterCriticalSection(&this->m_Cs); }
	void Unlock() { ::LeaveCriticalSection(&this->m_Cs); }
};

// Implementation of the semaphore
class QSemaphore {
private:
	HANDLE	m_hSemaphore;
	long m_lMaximumCount;

public:
	QSemaphore(long lMaximumCount) {
		this->m_hSemaphore = ::CreateSemaphore(NULL, lMaximumCount, lMaximumCount, NULL);

		if (this->m_hSemaphore == NULL) throw;
		this->m_lMaximumCount = lMaximumCount;
	}

	~QSemaphore() { ::CloseHandle(this->m_hSemaphore); }

	long GetMaximumCount() const { return this->m_lMaximumCount; }
	void Inc() { ::WaitForSingleObject(this->m_hSemaphore, INFINITE); }
	void Dec() { ::ReleaseSemaphore(this->m_hSemaphore, 1, NULL); }
	void Dec(long lCount) { ::ReleaseSemaphore(this->m_hSemaphore, lCount, NULL); }
};


// Implementation of the read-write mutex.
// Multiple threads can have read access at the same time.
// Write access is exclusive for only one thread.
class ReadWriteMutex {
private:
	QMutex		m_qMutex;
	QSemaphore	m_qSemaphore;

public:
	ReadWriteMutex(long lMaximumReaders): m_qSemaphore(lMaximumReaders) {}

	void lockRead() { m_qSemaphore.Inc(); }
	void unlockRead() { m_qSemaphore.Dec(); }

	void lockWrite() {
		m_qMutex.Lock();
		for (int i = 0; i < maxReaders(); ++i) m_qSemaphore.Inc();
		m_qMutex.Unlock();
	}
	
	void unlockWrite() {  m_qSemaphore.Dec(m_qSemaphore.GetMaximumCount()); }
	int maxReaders() const { return m_qSemaphore.GetMaximumCount(); }
};

//---------------------------- IMPLEMENTATION ----------------------------------------------------

#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)

Share

About the Author

rtybase
Software Developer (Senior) Snappli Ltd.
United Kingdom United Kingdom
My name is Ruslan Ciurca. Currently I am a Software Developer at snappli.com.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 16 Nov 2006
Article Copyright 2006 by rtybase
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid