⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sound.cpp

📁 QAM module to use in Java with an easy interface and powerful performance
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

void CSound::GetDoneBuffer(int& iCntPrepBuf, int& iIndexDoneBuf)
{
	/* Get number of "done"-buffers and position of one of them */
	iCntPrepBuf = 0;
	for (int i = 0; i < NUM_SOUND_BUFFERS_OUT; i++)
	{
		if (m_WaveOutHeader[i].dwFlags & WHDR_DONE)
		{
			iCntPrepBuf++;
			iIndexDoneBuf = i;
		}
	}
}

void CSound::AddOutBuffer(int iBufNum)
{
	/* Unprepare old wave-header */
	waveOutUnprepareHeader(
		m_WaveOut, &m_WaveOutHeader[iBufNum], sizeof(WAVEHDR));

	/* Prepare buffers for sending to sound interface */
	PrepareOutBuffer(iBufNum);

	/* Send buffer to driver for filling with new data */
	waveOutWrite(m_WaveOut, &m_WaveOutHeader[iBufNum], sizeof(WAVEHDR));
}

void CSound::PrepareOutBuffer(int iBufNum)
{
	/* Set Header data */
	m_WaveOutHeader[iBufNum].lpData = (LPSTR) &psPlaybackBuffer[iBufNum][0];
	m_WaveOutHeader[iBufNum].dwBufferLength = iBufferSizeOut * BYTES_PER_SAMPLE;
	m_WaveOutHeader[iBufNum].dwFlags = 0;

	/* Prepare wave-header */
	waveOutPrepareHeader(m_WaveOut, &m_WaveOutHeader[iBufNum], sizeof(WAVEHDR));
}

void CSound::InitPlayback(int iNewBufferSize, bool bNewBlocking)
{
	int	i, j;

	/* Check if device must be opened or reinitialized */
	if (bChangDevOut == TRUE)
	{
		OpenOutDevice();

		/* Reset flag */
		bChangDevOut = FALSE;
	}

	/* Set internal parameters */
	iBufferSizeOut = iNewBufferSize;
	bBlockingPlay = bNewBlocking;

	/* Reset interface */
	waveOutReset(m_WaveOut);

	for (j = 0; j < NUM_SOUND_BUFFERS_OUT; j++)
	{
		/* Unprepare old wave-header (in case header was not prepared before,
		   simply nothing happens with this function call */
		waveOutUnprepareHeader(m_WaveOut, &m_WaveOutHeader[j], sizeof(WAVEHDR));

		/* Create memory for playback buffer */
		if (psPlaybackBuffer[j] != NULL)
			delete[] psPlaybackBuffer[j];

		psPlaybackBuffer[j] = new short[iBufferSizeOut];

		/* Clear new buffer */
		for (i = 0; i < iBufferSizeOut; i++)
			psPlaybackBuffer[j][i] = 0;

		/* Prepare buffer for sending to the sound interface */
		PrepareOutBuffer(j);

		/* Initially, send all buffers to the interface */
		AddOutBuffer(j);
	}
}

void CSound::OpenOutDevice()
{
	if (m_WaveOut != NULL)
	{
		waveOutReset(m_WaveOut);
		waveOutClose(m_WaveOut);
	}

	MMRESULT result = waveOutOpen(&m_WaveOut, iCurOutDev, &sWaveFormatEx,
		(DWORD) m_WaveOutEvent, NULL, CALLBACK_EVENT);

	if (result != MMSYSERR_NOERROR)
		throw CGenErr("Sound Interface Start, waveOutOpen() failed.");
}

void CSound::SetOutDev(int iNewDev)
{
	/* Set device to wave mapper if iNewDev is greater that the number of
	   sound devices in the system */
	if (iNewDev >= (int)iNumDevs)
		iNewDev = WAVE_MAPPER;

	/* Change only in case new device id is not already active */
	if (iNewDev != iCurOutDev)
	{
		iCurOutDev = iNewDev;
		bChangDevOut = TRUE;
	}
}


/******************************************************************************\
* Common                                                                       *
\******************************************************************************/
void CSound::Close()
{
	int			i;
	MMRESULT	result;

	/* Reset audio driver */
	if (m_WaveOut != NULL)
	{
		result = waveOutReset(m_WaveOut);
		if (result != MMSYSERR_NOERROR)
			throw CGenErr("Sound Interface, waveOutReset() failed.");
	}

	if (m_WaveIn != NULL)
	{
		result = waveInReset(m_WaveIn);
		if (result != MMSYSERR_NOERROR)
			throw CGenErr("Sound Interface, waveInReset() failed.");
	}

	/* Set event to ensure that thread leaves the waiting function */
	if (m_WaveInEvent != NULL)
		SetEvent(m_WaveInEvent);

	/* Wait for the thread to terminate */
	Sleep(500);

	/* Unprepare wave-headers */
	if (m_WaveIn != NULL)
	{
		for (i = 0; i < NUM_SOUND_BUFFERS_IN; i++)
		{
			result = waveInUnprepareHeader(
				m_WaveIn, &m_WaveInHeader[i], sizeof(WAVEHDR));

			if (result != MMSYSERR_NOERROR)
				throw CGenErr("Sound Interface, waveInUnprepareHeader()"
					" failed.");
		}

		/* Close the sound in device */
		result = waveInClose(m_WaveIn);
		if (result != MMSYSERR_NOERROR)
			throw CGenErr("Sound Interface, waveInClose() failed.");
	}

	if (m_WaveOut != NULL)
	{
		for (i = 0; i < NUM_SOUND_BUFFERS_OUT; i++)
		{
			result = waveOutUnprepareHeader(
				m_WaveOut, &m_WaveOutHeader[i], sizeof(WAVEHDR));

			if (result != MMSYSERR_NOERROR)
				throw CGenErr("Sound Interface, waveOutUnprepareHeader()"
					" failed.");
		}

		/* Close the sound out device */
		result = waveOutClose(m_WaveOut);
		if (result != MMSYSERR_NOERROR)
			throw CGenErr("Sound Interface, waveOutClose() failed.");
	}

	/* Set flag to open devices the next time it is initialized */
	bChangDevIn = TRUE;
	bChangDevOut = TRUE;
}

CSound::CSound(int SampleRate)
{
	int i;

	/* Should be initialized because an error can occur during init */
	m_WaveInEvent = NULL;
	m_WaveOutEvent = NULL;
	m_WaveIn = NULL;
	m_WaveOut = NULL;

	/* Init buffer pointer to zero */
	for (i = 0; i < NUM_SOUND_BUFFERS_IN; i++)
	{
		memset(&m_WaveInHeader[i], 0, sizeof(WAVEHDR));
		psSoundcardBuffer[i] = NULL;
	}

	for (i = 0; i < NUM_SOUND_BUFFERS_OUT; i++)
	{
		memset(&m_WaveOutHeader[i], 0, sizeof(WAVEHDR));
		psPlaybackBuffer[i] = NULL;
	}

	/* Init wave-format structure */
	sWaveFormatEx.wFormatTag = WAVE_FORMAT_PCM;
	sWaveFormatEx.nChannels = NUM_IN_OUT_CHANNELS;
	sWaveFormatEx.wBitsPerSample = BITS_PER_SAMPLE;
	sWaveFormatEx.nSamplesPerSec = SampleRate;  //SOUNDCRD_SAMPLE_RATE;//*NUM_IN_OUT_CHANNELS;
	sWaveFormatEx.nBlockAlign = sWaveFormatEx.nChannels *
		sWaveFormatEx.wBitsPerSample / 8;
	sWaveFormatEx.nAvgBytesPerSec = sWaveFormatEx.nBlockAlign *
		sWaveFormatEx.nSamplesPerSec;
	sWaveFormatEx.cbSize = 0;

	/* Get the number of digital audio devices in this computer, check range */
	iNumDevs = waveInGetNumDevs();

	if (iNumDevs > MAX_NUMBER_SOUND_CARDS)
		iNumDevs = MAX_NUMBER_SOUND_CARDS;

	/* At least one device must exist in the system */
	if (iNumDevs == 0)
		throw CGenErr("No audio device found.");

	/* Get info about the devices and store the names */
	for (i = 0; i < (int)iNumDevs; i++)
		if (!waveInGetDevCaps(i, &m_WaveInDevCaps, sizeof(WAVEINCAPS)))
			pstrDevices[i] = m_WaveInDevCaps.szPname;

	/* We use an event controlled wave-in (wave-out) structure */
	/* Create events */
	m_WaveInEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	m_WaveOutEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	/* Set flag to open devices */
	bChangDevIn = TRUE;
	bChangDevOut = TRUE;

	/* Default device number (first device in system) */
	iCurInDev = 0;
	iCurOutDev = 0;

	/* Non-blocking wave out is default */
	bBlockingPlay = FALSE;

	/* Blocking wave in is default */
	bBlockingRec = TRUE;
}

CSound::~CSound()
{
	int i;

	/* Delete allocated memory */
	for (i = 0; i < NUM_SOUND_BUFFERS_IN; i++)
	{
		if (psSoundcardBuffer[i] != NULL)
			delete[] psSoundcardBuffer[i];
	}

	for (i = 0; i < NUM_SOUND_BUFFERS_OUT; i++)
	{
		if (psPlaybackBuffer[i] != NULL)
			delete[] psPlaybackBuffer[i];
	}

	/* Close the handle for the events */
	if (m_WaveInEvent != NULL)
		CloseHandle(m_WaveInEvent);

	if (m_WaveOutEvent != NULL)
		CloseHandle(m_WaveOutEvent);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -