求DirectSound例程,能播放一个wav就好了。需要混音。
1个回答
展开全部
此接口 早已经被升级了 .. 不怎么好用的 .. 以下 是代码 ..
#include <windows.h>
#include <dsound.h>
#include <vector>
using namespace std ;
#define SOUND_NULL 0
#define SOUND_LOADED 1
#define SOUND_PLAYING_LOOP 2
#define SOUND_PLAYING_ALON 3
#define SOUND_STOPPED 4
#pragma comment(lib,"dsound.lib")
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM) ;
typedef struct MUSDAT_TAG
{
LPDIRECTSOUNDBUFFER Sound_Buffer ; //WAV
int state ;
UINT VEC_XS ;
} MUSDAT_STRUCT,*MUSDAT_STRUCT_PTR ;
template <typename T>
void _asm0(T& ddstruct)
{
T* _PTR_ = &ddstruct ;
register UINT num = sizeof(ddstruct)/4 ;
__asm PUSH ECX __asm PUSH EDI
__asm PUSH EAX __asm CLD
__asm MOV EAX,0 __asm MOV ECX,num
__asm MOV EDI,_PTR_ __asm REP STOSD
__asm POP EAX __asm POP EDI __asm POP ECX
ddstruct.dwSize = sizeof(ddstruct) ;
}
class m3x4 //DirectSound decode WAV Class
{
public:
LPDIRECTSOUND lpds ; //DirectSound音频指针..
DSBUFFERDESC dsbd ; //音频描述结构..
WAVEFORMATEX wfmtx ; //wav结构..
public:
m3x4::m3x4() ;
virtual m3x4::~m3x4() ;
void m3x4::SetupLevel(HWND) ;
void m3x4::LoadMusic(LPWSTR,MUSDAT_STRUCT&) ;
void m3x4::AltMusSt(int,MUSDAT_STRUCT&) ;
private:
static MMCKINFO parent,child ;
static HMMIO hwav ;
static DWORD audio_length_1,
audio_length_2;
static UCHAR *snd_buffer ,
*audio_ptr_1,
*audio_ptr_2;
vector <LPDIRECTSOUNDBUFFER> msVEC ;
void m3x4::Delete_MUSDAT() ;
} ;
void m3x4::AltMusSt(int LSQ,MUSDAT_STRUCT& mET)
{
switch(LSQ)
{
case 2:
mET.Sound_Buffer->Play(0,0,DSBPLAY_LOOPING) ;
mET.state = SOUND_PLAYING_LOOP ;
break ;
case 3:
mET.Sound_Buffer->Play(0,0,0) ;
mET.state = SOUND_PLAYING_ALON ;
break ;
case 4:
mET.Sound_Buffer->Stop() ;
mET.state = SOUND_STOPPED ;
break ;
}
}
m3x4::m3x4()
{
lpds = NULL ;
_asm0(this->dsbd) ;
if(DirectSoundCreate(NULL,&lpds,NULL))
MessageBox(NULL,L"DirectSoundCreate Failed",L"DirectSound",MB_ICONERROR) ;
dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE | DSBCAPS_GLOBALFOCUS ;
dsbd.lpwfxFormat = &wfmtx ;
}
void m3x4::Delete_MUSDAT()
{
vector<LPDIRECTSOUNDBUFFER>::reverse_iterator msVEC_iter = msVEC.rbegin() ;
while(msVEC_iter != msVEC.rend())
(*msVEC_iter++)->Release() ;
return ;
}
m3x4::~m3x4()
{
Delete_MUSDAT() ;
if(lpds)
lpds->Release() ;
}
void m3x4::LoadMusic(LPWSTR filename,MUSDAT_STRUCT& Magan)
{
Magan.state = 0 ;
child = parent ;
hwav = mmioOpen(filename,NULL,MMIO_READ | MMIO_ALLOCBUF) ;
parent.fccType = mmioFOURCC('W','A','V','E') ;
mmioDescend(hwav,&parent,NULL,MMIO_FINDRIFF) ;
child.ckid = mmioFOURCC('f','m','t',' ') ;
mmioDescend(hwav,&child,&parent,0) ;
mmioRead(hwav,(char*)&(wfmtx),sizeof(WAVEFORMATEX)) ;
mmioAscend(hwav,&child,0) ;
child.ckid = mmioFOURCC('d','a','t','a') ;
mmioDescend(hwav,&child,&parent,MMIO_FINDCHUNK) ;
snd_buffer = (UCHAR*)malloc(child.cksize) ;
mmioRead(hwav,(char*)snd_buffer,child.cksize) ;
mmioClose(hwav,0) ;
Magan.state = SOUND_LOADED ;
dsbd.dwBufferBytes = child.cksize ;
lpds->CreateSoundBuffer(&dsbd,&(Magan.Sound_Buffer),NULL) ;
Magan.Sound_Buffer->Lock(0,child.cksize,(void **)&audio_ptr_1,&audio_length_1,(void**)&audio_ptr_2,&audio_length_2,DSBLOCK_FROMWRITECURSOR) ;
msVEC.push_back(Magan.Sound_Buffer) ;
memcpy(audio_ptr_1,snd_buffer,audio_length_1) ;
memcpy(audio_ptr_2,(snd_buffer + audio_length_1),audio_length_2) ;
Magan.Sound_Buffer->Unlock(audio_ptr_1,audio_length_1,audio_ptr_2,audio_length_2) ;
}
void m3x4::SetupLevel(HWND hWnd)
{
if(lpds->SetCooperativeLevel(hWnd,DSSCL_NORMAL))
MessageBox(NULL,L"SetCooperativeLevel",L"DirectSound",MB_ICONERROR) ;
}
m3x4 Music ;
MUSDAT_STRUCT WAV ;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SampleWAV") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
Music.SetupLevel(hwnd) ;
Music.LoadMusic(L"create.wav",WAV) ;
Music.AltMusSt(SOUND_PLAYING_LOOP,WAV) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
HMMIO m3x4::hwav ;
MMCKINFO m3x4::parent ;
MMCKINFO m3x4::child ;
DWORD m3x4::audio_length_1 ;
DWORD m3x4::audio_length_2 ;
UCHAR* m3x4::snd_buffer = NULL ;
UCHAR* m3x4::audio_ptr_1 = NULL ;
UCHAR* m3x4::audio_ptr_2 = NULL ;
建立 多缓冲 只要 再 声明 一个 MUSDAT_STRUCT 调用 一次 LoadMusic 和 AltMusSt 即可 播放
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
宿兴电子
2024-10-18 广告
2024-10-18 广告
联系我们一、有无震荡源 无源这里的“源”不是指电源,而是指震荡源。也就是说,有源蜂鸣器内部带震荡源,所以只要一通电就会叫。而无源内部不带震荡源,所以如果用直流信号无法令其鸣叫。 二、价格不同 有源蜂鸣器往比无源蜂鸣器贵,就是因为里面多个震荡...
点击进入详情页
本回答由宿兴电子提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询