//-----------------------------------------------------------------------------
// File: DSUtil.cpp
//
// Desc: DirectSound framework classes for reading and writing wav files and
// playing them in DirectSound buffers. Feel free to use this class
// as a starting point for adding extra functionality.
//
// Copyright (c) Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <mmsystem.h>
#include <dxerr9.h>
#include <dsound.h>
#include "DSUtil.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name: CSoundManager::CSoundManager()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CSoundManager::CSoundManager()
{
m_pDS = NULL;
}
//-----------------------------------------------------------------------------
// Name: CSoundManager::~CSoundManager()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CSoundManager::~CSoundManager()
{
SAFE_RELEASE( m_pDS );
}
//-----------------------------------------------------------------------------
// Name: CSoundManager::Initialize()
// Desc: Initializes the IDirectSound object and also sets the primary buffer
// format. This function must be called before any others.
//-----------------------------------------------------------------------------
HRESULT CSoundManager::Initialize( HWND hWnd,
DWORD dwCoopLevel )
{
HRESULT hr;
SAFE_RELEASE( m_pDS );
// Create IDirectSound using the primary sound device
if( FAILED( hr = DirectSoundCreate8( NULL, &m_pDS, NULL ) ) )
return DXTRACE_ERR( TEXT("DirectSoundCreate8"), hr );
// Set DirectSound coop level
if( FAILED( hr = m_pDS->SetCooperativeLevel( hWnd, dwCoopLevel ) ) )
return DXTRACE_ERR( TEXT("SetCooperativeLevel"), hr );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CSoundManager::SetPrimaryBufferFormat()
// Desc: Set primary buffer to a specified format
// !WARNING! - Setting the primary buffer format and then using this
// same dsound object for DirectMusic messes up DirectMusic!
// For example, to set the primary buffer format to 22kHz stereo, 16-bit
// then: dwPrimaryChannels = 2
// dwPrimaryFreq = 22050,
// dwPrimaryBitRate = 16
//-----------------------------------------------------------------------------
HRESULT CSoundManager::SetPrimaryBufferFormat( DWORD dwPrimaryChannels,
DWORD dwPrimaryFreq,
DWORD dwPrimaryBitRate )
{
HRESULT hr;
LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;
if( m_pDS == NULL )
return CO_E_NOTINITIALIZED;
// Get the primary buffer
DSBUFFERDESC dsbd;
ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwBufferBytes = 0;
dsbd.lpwfxFormat = NULL;
if( FAILED( hr = m_pDS->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL ) ) )
return DXTRACE_ERR( TEXT("CreateSoundBuffer"), hr );
WAVEFORMATEX wfx;
ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM;
wfx.nChannels = (WORD) dwPrimaryChannels;
wfx.nSamplesPerSec = (DWORD) dwPrimaryFreq;
wfx.wBitsPerSample = (WORD) dwPrimaryBitRate;
wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
wfx.nAvgBytesPerSec = (DWORD) (wfx.nSamplesPerSec * wfx.nBlockAlign);
if( FAILED( hr = pDSBPrimary->SetFormat(&wfx) ) )
return DXTRACE_ERR( TEXT("SetFormat"), hr );
SAFE_RELEASE( pDSBPrimary );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CSoundManager::Get3DListenerInterface()
// Desc: Returns the 3D listener interface associated with primary buffer.
//-----------------------------------------------------------------------------
HRESULT CSoundManager::Get3DListenerInterface( LPDIRECTSOUND3DLISTENER* ppDSListener )
{
HRESULT hr;
DSBUFFERDESC dsbdesc;
LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;
if( ppDSListener == NULL )
return E_INVALIDARG;
if( m_pDS == NULL )
return CO_E_NOTINITIALIZED;
*ppDSListener = NULL;
// Obtain primary buffer, asking it for 3D control
ZeroMemory( &dsbdesc, sizeof(DSBUFFERDESC) );
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER;
if( FAILED( hr = m_pDS->CreateSoundBuffer( &dsbdesc, &pDSBPrimary, NULL ) ) )
return DXTRACE_ERR( TEXT("CreateSoundBuffer"), hr );
if( FAILED( hr = pDSBPrimary->QueryInterface( IID_IDirectSound3DListener,
(VOID**)ppDSListener ) ) )
{
SAFE_RELEASE( pDSBPrimary );
return DXTRACE_ERR( TEXT("QueryInterface"), hr );
}
// Release the primary buffer, since it is not need anymore
SAFE_RELEASE( pDSBPrimary );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CSoundManager::Create()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CSoundManager::Create( CSound** ppSound,
LPTSTR strWaveFileName,
DWORD dwCreationFlags,
GUID guid3DAlgorithm,
DWORD dwNumBuffers )
{
HRESULT hr;
HRESULT hrRet = S_OK;
DWORD i;
LPDIRECTSOUNDBUFFER* apDSBuffer = NULL;
DWORD dwDSBufferSize = NULL;
CWaveFile* pWaveFile = NULL;
if( m_pDS == NULL )
return CO_E_NOTINITIALIZED;
if( strWaveFileName == NULL || ppSound == NULL || dwNumBuffers < 1 )
return E_INVALIDARG;
apDSBuffer = new LPDIRECTSOUNDBUFFER[dwNumBuffers];
if( apDSBuffer == NULL )
{
hr = E_OUTOFMEMORY;
goto LFail;
}
pWaveFile = new CWaveFile();
if( pWaveFile == NULL )
{
hr = E_OUTOFMEMORY;
goto LFail;
}
pWaveFile->Open( strWaveFileName, NULL, WAVEFILE_READ );
if( pWaveFile->GetSize() == 0 )
{
// Wave is blank, so don't create it.
hr = E_FAIL;
goto LFail;
}
// Make the DirectSound buffer the same size as the wav file
dwDSBufferSize = pWaveFile->GetSize();
// Create the direct sound buffer, and only request the flags needed
// since each requires some overhead and limits if the buffer can
// be hardware accelerated
DSBUFFERDESC dsbd;
ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = dwCreationFlags;
dsbd.dwBufferBytes = dwDSBufferSize;
dsbd.guid3DAlgorithm = guid3DAlgorithm;
dsbd.lpwfxFormat = pWaveFile->m_pwfx;
// DirectSound is only guarenteed to play PCM data. Other
// formats may or may not work depending the sound card driver.
hr = m_pDS->CreateSoundBuffer( &dsbd, &apDSBuffer[0], NULL );
// Be sure to return this error code if it occurs so the
// callers knows this happened.
if( hr == DS_NO_VIRTUALIZATION )
hrRet = DS_NO_VIRTUALIZATION;
if( FAILED(hr) )
{
// DSERR_BUFFERTOOSMALL will be returned if the buffer is
//
没有合适的资源?快使用搜索试试~ 我知道了~
DirectMidi_src2_3b.zip_The Class_directmusic_directmusic midi
共146个文件
cpp:80个
h:34个
ico:8个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 96 浏览量
2022-09-24
13:18:09
上传
评论
收藏 936KB ZIP 举报
温馨提示
MIDI applications with DirectMusic,use the DirectMIDI class library to develop applications based on MIDI.
资源推荐
资源详情
资源评论
收起资源包目录
DirectMidi_src2_3b.zip_The Class_directmusic_directmusic midi (146个子文件)
MidiStation.clw.bak 5KB
LOGO.BMP 30KB
midistation.bmp 10KB
MidiStation.clw 6KB
DSUTIL.CPP 55KB
DSUTIL.CPP 55KB
DSUTIL.CPP 55KB
Midistationdlg.cpp 38KB
DXUTIL.CPP 37KB
DXUTIL.CPP 37KB
DXUTIL.CPP 37KB
COutputPort.cpp 17KB
COutputPort.cpp 17KB
COutputPort.cpp 17KB
CPerformance.cpp 17KB
CPerformance.cpp 17KB
CPerformance.cpp 17KB
CSampleInstrument.cpp 14KB
CSampleInstrument.cpp 14KB
CSampleInstrument.cpp 14KB
CInputPort.cpp 13KB
CInputPort.cpp 13KB
CInputPort.cpp 13KB
CDLSLoader.cpp 12KB
CDLSLoader.cpp 12KB
CDLSLoader.cpp 12KB
Example_aud.cpp 11KB
CSegment.cpp 11KB
CSegment.cpp 11KB
CSegment.cpp 11KB
CMidiPort.cpp 11KB
CMidiPort.cpp 11KB
CMidiPort.cpp 11KB
Example_mid.cpp 11KB
C3DBuffer.cpp 10KB
C3DBuffer.cpp 10KB
C3DBuffer.cpp 10KB
C3DListener.cpp 9KB
C3DListener.cpp 9KB
C3DListener.cpp 9KB
Piano.cpp 9KB
MessageList.cpp 9KB
CAPathPerformance.cpp 8KB
CAPathPerformance.cpp 8KB
CAPathPerformance.cpp 8KB
CPortPerformance.cpp 7KB
CPortPerformance.cpp 7KB
CPortPerformance.cpp 7KB
GMMusicData.cpp 7KB
C3DSegment.cpp 7KB
C3DSegment.cpp 7KB
C3DSegment.cpp 7KB
CMasterClock.cpp 6KB
CMasterClock.cpp 6KB
CMasterClock.cpp 6KB
CCollection.cpp 6KB
CCollection.cpp 6KB
CCollection.cpp 6KB
CAudioPath.cpp 6KB
CAudioPath.cpp 6KB
CAudioPath.cpp 6KB
Key.cpp 6KB
Keyboard.cpp 6KB
Octave.cpp 6KB
Record.cpp 5KB
MidiStation.cpp 5KB
CInstrument.cpp 5KB
CInstrument.cpp 5KB
CInstrument.cpp 5KB
CDMusicException.cpp 5KB
CDMusicException.cpp 5KB
CDMusicException.cpp 5KB
FileHandler.cpp 5KB
Dmhelp.cpp 5KB
Dmhelp.cpp 5KB
Dmhelp.cpp 5KB
InputDialog.cpp 4KB
AboutDlg.cpp 4KB
CDirectMusic.cpp 4KB
CDirectMusic.cpp 4KB
CDirectMusic.cpp 4KB
Link.cpp 3KB
Object.cpp 2KB
StdAfx.cpp 206B
CURSOR.CUR 326B
SAMPLE.DLS 539KB
MidiStation.dsp 9KB
Example_aud.dsp 6KB
Example_mid.dsp 6KB
Example_aud.dsw 547B
Example_mid.dsw 547B
MidiStation.dsw 545B
CMidipart.h 16KB
CMidipart.h 16KB
CMidipart.h 16KB
CAudioPart.h 14KB
CAudioPart.h 14KB
CAudioPart.h 14KB
Piano.h 10KB
CDirectBase.h 9KB
共 146 条
- 1
- 2
资源评论
御道御小黑
- 粉丝: 74
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功