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

📄 sound.cpp

📁 瑞星微公司RK27XX系列芯片的SDK开发包
💻 CPP
字号:
/* Copyright (C) 2006 ROCK-CHIPS FUZHOU . All Rights Reserved. */
/*
File		:	ide\rk27dll\src\lib\sound\sound.cpp
Desc	 	:	PC音频接口.


Author	   	:   huangsl
Date	 	:	080626
Notes	  	:   用于 PC仿真模拟 音频播放.
				为了支持 C++,该文件直接通过 PCCORE.CPP包含编译.
$Log	 :
sound.cpp,v $
Revision 1.2  2007/10/08 02:06:22  Lingzhaojun
添加CVS版本自动注释

*	080626	huangsl 	增加 PC仿真的音频支持.
*/
/***************************************************************************/
/***************************************************************************/

#include "mmsystem.h"

#include "..\application\audio\include\audio_Globals.h"
/***************************************************************************/
// Our supported sample rates
// Note that sndRate5K is really 5512.5khz
// Use defines instead of an enum since these must be 32 bits on all platforms
#define sndRate5K	5512L
#define sndRate11K	11025L
#define sndRate22K	22050L
#define sndRate44K	44100L

const int sndMono			= 0x0;
const int sndStereo 		= 0x1;

const int snd8Bit			= 0x0;
const int snd16Bit			= 0x2;

const int snd5K 			= 0<<2;
const int snd11K			= 1<<2;
const int snd22K			= 2<<2;
const int snd44K			= 3<<2;

const int sndCompressNone	= 0x00; // we could add 14 more compression types here...
const int sndCompressADPCM	= 0x10;
const int sndCompressMP3	= 0x20;
const int sndCompressNoneI	= 0x30; // save out in intel byte order
const int sndRateMask		= 0x3 << 2;
const int sndCompressMask	= 0xF0;

typedef int (*DMACallBack)(void);	   /*DMA 回调函数*/
#define MIN(a,b) ((a)>(b)?(b):(a))

/***************************************************************************/
HWAVEOUT 			hWaveOut;
signed long			gPCWaveDevFormat;
WAVEHDR 			gPCWaveHDR[AUDIO_OUT_BUF_NUM];
int 				gPCWaveHDRIndex;

DMACallBack			gPCWaveOutCallBack;

void CALLBACK PCSoundCallback(HWAVEOUT hwo, UINT uMsg, 
	unsigned long dwInstance, unsigned long dwParam1, unsigned long dwParam2 )
{
	if ( uMsg == WOM_DONE )
		{
	//	WAVEHDR* hdr = (WAVEHDR*)dwParam1;
	//	int res = waveOutUnprepareHeader(hWaveOut, hdr, sizeof(WAVEHDR));
	//	ASSERT(res == MMSYSERR_NOERROR);
		
		gPCWaveOutCallBack();
		}
}


/***************************************************************************/

void PCSoundGetCap( signed long* format )
{
	// Check to capabilities of the waveform device
	WAVEOUTCAPS caps;
	waveOutGetDevCaps(WAVE_MAPPER, &caps, sizeof(caps));

	// the desired format
	*format = sndStereo | snd16Bit | snd22K;

	// Check for sample rate support
	if ( (caps.dwFormats & WAVE_FORMAT_4M16) == 0 ) {
		*format = (*format & ~sndRateMask) | MIN(*format & sndRateMask, snd22K);
	}

	if ( (caps.dwFormats & WAVE_FORMAT_2M16) == 0 ) {
		*format = (*format & ~sndRateMask) | MIN(*format & sndRateMask, snd11K);
	}

	// Check to turn off stereo support
	if ( (caps.dwFormats & WAVE_FORMAT_2S16) == 0 )
		*format &= ~sndStereo;

	// Check to turn off 16 bit sound
	if ( (caps.dwFormats & WAVE_FORMAT_2M16) == 0 )
		*format &= ~snd16Bit;
}

BOOLEAN PCSoundOpenDevice( DWORD SamplesPerSec,
	WORD Channels, WORD BitsPerSample , DMACallBack	callBack )
{
	
	// Open a waveform device for output
	WAVEFORMATEX pcmWaveFormat;
	
	pcmWaveFormat.wFormatTag		= WAVE_FORMAT_PCM;
	pcmWaveFormat.nSamplesPerSec	= SamplesPerSec;
	pcmWaveFormat.nChannels			= Channels;
	pcmWaveFormat.wBitsPerSample	= BitsPerSample;
	pcmWaveFormat.nBlockAlign		= (WORD)( ( SamplesPerSec * Channels ) / 8 );
	pcmWaveFormat.nAvgBytesPerSec	= pcmWaveFormat.nBlockAlign * pcmWaveFormat.nSamplesPerSec;
	
	if ( waveOutOpen(	&hWaveOut, 
						WAVE_MAPPER, 
						&pcmWaveFormat, 	
						(unsigned long)PCSoundCallback, 
						(unsigned long)0, 
						CALLBACK_FUNCTION) == MMSYSERR_NOERROR )
	{
		gPCWaveOutCallBack = callBack;
		gPCWaveHDRIndex = 0;
		return true;
	}
	return false;
}

void PCSoundCloseDevice()
{
	
	HWAVEOUT hW = hWaveOut;
	hWaveOut = 0;	// flag that we are shutting down so PollSound will bail
	
	int res = waveOutReset(hW);
	ASSERT(res == MMSYSERR_NOERROR);

	for( int i = 0 ; i < AUDIO_OUT_BUF_NUM ; i++ )
	{
		//等待播放完成.
		int limit = 50;
		while ( limit-- ) 
		{
			int res = waveOutUnprepareHeader(hW, gPCWaveHDR , sizeof(WAVEHDR));
			if ( res != WAVERR_STILLPLAYING ) 
			{
				ASSERT(res == MMSYSERR_NOERROR);
				break;
			}
		}
	}
	
	res = waveOutClose(hW);
	ASSERT(res == MMSYSERR_NOERROR);
	
}


void PCSoundPlayWave( void *waveOutBuffer , long bufferLen ,
	DMACallBack	callBack )
{
	WAVEHDR* hdr = &gPCWaveHDR[gPCWaveHDRIndex];

	gPCWaveOutCallBack = callBack;

	gPCWaveHDRIndex++;
	if( gPCWaveHDRIndex == AUDIO_OUT_BUF_NUM )
		gPCWaveHDRIndex = 0;
	
	// Clean up the header
	int res = waveOutUnprepareHeader(hWaveOut, hdr, sizeof(WAVEHDR));
	ASSERT(res == MMSYSERR_NOERROR);
	
	hdr->lpData = (char*)waveOutBuffer;
	hdr->dwBufferLength = bufferLen;
	
	res = waveOutPrepareHeader(hWaveOut, hdr, sizeof(WAVEHDR));
	ASSERT(res == MMSYSERR_NOERROR);
	res = waveOutWrite(hWaveOut, hdr, sizeof(WAVEHDR)); 
	ASSERT(res == MMSYSERR_NOERROR);

	
}

void PCSoundSetVolume( int volume )
{
	DWORD 	v;

	//change from 0 -- MAX_VOLUME to 0x00 -- 0xff
	volume = volume*0xff/MAX_VOLUME;
	v = ( ((DWORD)volume)<<16)|volume;
	int res = waveOutSetVolume(hWaveOut,v);
	ASSERT(res == MMSYSERR_NOERROR);
	
}


⌨️ 快捷键说明

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