天行健 君子当自强而不息

用DirectX Audio和DirectShow播放声音和音乐(3)

 

本篇是用DirectX Audio和DirectShow播放声音和音乐(2)的续篇。



调整声道平衡

所谓声道平衡就是调节左右声道的大小,如下图所示:

我们一般使用的喇叭或耳机都有左右两个声道,把自己想象成在左右声道两边移动的点,一般情况下在中间,这样听到的来自左右声道的音量是一样的。你可以向左移动,移动过程中左声道音量逐渐变大,右声道音量逐渐变小。当移动到左声道最左边的时候,左声道音量最大(10000),右声道没有声音(-10000)。

DirectSound定义了两个宏帮助把声道平衡调节到最左边和最右边,使用DSBPAN_LEFT将声道调整到最左边,使用DSBPAN_RIGHT 将声道调整到最右边。

通过调用IDirectSoundBuffer8::SetPan函数可以调节声道平衡。

The SetPan method sets the relative volume of the left and right channels.

HRESULT SetPan(
LONG lPan
);

Parameters

lPan
Relative volume between the left and right channels.

Return Values

If the method succeeds, the return value is DS_OK. If the method fails, the return value may be one of the following error values:

Return code
DSERR_CONTROLUNAVAIL
DSERR_GENERIC
DSERR_INVALIDPARAM
DSERR_PRIOLEVELNEEDED

Remarks

The returned value is measured in hundredths of a decibel (dB), in the range of DSBPAN_LEFT to DSBPAN_RIGHT. These values are defined in Dsound.h as -10,000 and 10,000 respectively. The value DSBPAN_LEFT means the right channel is attenuated by 100 dB and is effectively silent. The value DSBPAN_RIGHT means the left channel is silent. The neutral value is DSBPAN_CENTER, defined as 0, which means that both channels are at full volume. When one channel is attenuated, the other remains at full volume.

The pan control acts cumulatively with the volume control.

 

改变播放速度

改变播放速度实际上改变的是声音的音调(pitch)。想象在游戏中通过改变播放速度将英雄的声音变成花栗鼠的声音。使用这种方法可以把一段男性的声音变成女性的声音,通过调用IDirectSoundBuffer8::SetFrequency来改变音调:

The SetFrequency method sets the frequency at which the audio samples are played.

HRESULT SetFrequency(
DWORD dwFrequency
);

Parameters

dwFrequency
Frequency, in hertz (Hz), at which to play the audio samples. A value of DSBFREQUENCY_ORIGINAL resets the frequency to the default value of the buffer format.

Return Values

If the method succeeds, the return value is DS_OK. If the method fails, the return value may be one of the following error values:

Return code
DSERR_CONTROLUNAVAIL
DSERR_GENERIC
DSERR_INVALIDPARAM
DSERR_PRIOLEVELNEEDED

Remarks

Increasing or decreasing the frequency changes the perceived pitch of the audio data. This method does not affect the format of the buffer.

Before setting the frequency, you should ascertain whether the frequency is supported by checking the dwMinSecondarySampleRate and dwMaxSecondarySampleRate members of the DSCAPS structure for the device. Some operating systems do not support frequencies greater than 100,000 Hz.

This method is not valid for the primary buffer.

如下图所示,它显示了音频缓冲以双倍的速度播放,也就是把播放频率调节为原来的两倍,这样音调就变高。


失去焦点

在很多情况下,其他程序会和你的程序抢占系统资源,然后把那些修改过配置的资源留给你的程序。这种情况多半发生在音频缓存上,所以需要调用IDirectSoundBuffer8::Restore来还原音频设置。如果缓冲区丢失,可以用这个函数找回。

The Restore method restores the memory allocation for a lost sound buffer.

HRESULT Restore();

Parameters

None.

Return Values

If the method succeeds, the return value is DS_OK. If the method fails, the return value may be one of the following error values:

Return code

  • DSERR_BUFFERLOST
  • DSERR_INVALIDCALL
  • DSERR_PRIOLEVELNEEDED

Remarks

If the application does not have the input focus, IDirectSoundBuffer8::Restore might not succeed. For example, if the application with the input focus has the DSSCL_WRITEPRIMARY cooperative level, no other application will be able to restore its buffers. Similarly, an application with the DSSCL_WRITEPRIMARY cooperative level must have the input focus to restore its primary buffer.

After DirectSound restores the buffer memory, the application must rewrite the buffer with valid sound data. DirectSound cannot restore the contents of the memory, only the memory itself.

The application can receive notification that a buffer is lost when it specifies that buffer in a call to the Lock or Play method. These methods return DSERR_BUFFERLOST to indicate a lost buffer. The GetStatus method can also be used to retrieve the status of the sound buffer and test for the DSBSTATUS_BUFFERLOST flag.


使用这个函数会导致缓存中的音频数据丢失,调用完此函数后需要重新加载。在创建音频缓存的时候使用 DSBCAPS_LOCSOFTWARE标志,这样DirectSound将在系统内存中分配缓冲区,因此数据基本上不可能丢失,也就不必担心丢失资源了。



加载声音到音频缓冲

最简单的方法就是通过Windows 最广泛使用的数字音频文件 ---- 波表文件,这种文件通常以.WAV作为它的扩展名。一个波表文件通常由两部分构成,一部分是文件开头的波表文件头,另外一部分是紧随其后的原始音频数据。这些原始音频数据可能是经过压缩的,也可能是未经压缩的。如果是压缩过的,操作起来会复杂很多,如果没有压缩过,操作起来就很容易。

下面的结构表示一个波表文件的文件头,通过观察能看出波表文件的文件头结构。

// .WAV file header
struct WAVE_HEADER
{
    
char    riff_sig[4];            // 'RIFF'
    long    waveform_chunk_size;    // 8
    char    wave_sig[4];            // 'WAVE'
    char    format_sig[4];          // 'fmt ' (notice space after)
    long    format_chunk_size;      // 16;
    short   format_tag;             // WAVE_FORMAT_PCM
    short   channels;               // # of channels
    long    sample_rate;            // sampling rate
    long    bytes_per_sec;          // bytes per second
    short   block_align;            // sample block alignment
    short   bits_per_sample;        // bits per second
    char    data_sig[4];            // 'data'
    long    data_size;              // size of waveform data
};

处理文件头非常简单,只需要打开文件,读取数据(读取数据的大小和WAVE_HEADER结构的大小一致)、填充 WAVE_HEADER结构就可以了。这个结构包含了我们所需要的所有关于音频文件的信息。你可以通过签名段来判断一个文件是否是波形文件,签名段在 WAVE_HEADER中是"*Sig"。请仔细查看 WAVE_HEADER中每个段的特征,如果不符合特征,说明所读取的不是一个波形文件。尤其是要检查签名段,如果签名段不是'WAVE'则说明加载了错误的音频文件。

有了必要的音频数据的结构信息后,就可以基于这些信息创建音频缓存,把音频数据放入其中,然后执行各种各样的操作。
可以编写两个函数来实现这样的功能,Create_Buffer_From_WAV读取并解析波表文件头,并且创建单独的音频缓冲区 ,Load_Sound_Data读取音频数据到缓冲区。
 
IDirectSound8*          g_ds;           // directsound component
IDirectSoundBuffer8*    g_ds_buffer;    // sound buffer object

//--------------------------------------------------------------------------------
// Create wave header information from wave file.
//--------------------------------------------------------------------------------
IDirectSoundBuffer8* Create_Buffer_From_WAV(FILE* fp, WAVE_HEADER* wave_header)
{
    IDirectSoundBuffer*     ds_buffer_main;
    IDirectSoundBuffer8*    ds_buffer_second;    
    DSBUFFERDESC            ds_buffer_desc;
    WAVEFORMATEX            wave_format;

    
// read in the header from beginning of file
    fseek(fp, 0, SEEK_SET);
    fread(wave_header, 1, 
sizeof(WAVE_HEADER), fp);

    
// check the sig fields. returning if an error.
    if(memcmp(wave_header->riff_sig, "RIFF", 4) || memcmp(wave_header->wave_sig, "WAVE", 4) ||
       memcmp(wave_header->format_sig, "fmt ", 4) || memcmp(wave_header->data_sig, "data", 4))
    {
        
return NULL;
    }

    
// setup the playback format
    ZeroMemory(&wave_format, sizeof(WAVEFORMATEX));

    wave_format.wFormatTag      = WAVE_FORMAT_PCM;
    wave_format.nChannels       = wave_header->channels;
    wave_format.nSamplesPerSec  = wave_header->sample_rate;
    wave_format.wBitsPerSample  = wave_header->bits_per_sample;
    wave_format.nBlockAlign     = wave_format.wBitsPerSample / 8 * wave_format.nChannels;
    wave_format.nAvgBytesPerSec = wave_format.nSamplesPerSec * wave_format.nBlockAlign;

    
// create the sound buffer using the header data
    ZeroMemory(&ds_buffer_desc, sizeof(DSBUFFERDESC));

    ds_buffer_desc.dwSize        = 
sizeof(DSBUFFERDESC);
    ds_buffer_desc.dwFlags       = DSBCAPS_CTRLVOLUME;
    ds_buffer_desc.dwBufferBytes = wave_header->data_size;
    ds_buffer_desc.lpwfxFormat   = &wave_format;

    
// create main sound buffer
    if(FAILED(g_ds->CreateSoundBuffer(&ds_buffer_desc, &ds_buffer_main, NULL)))
        
return NULL;

    
// get newer interface
    if(FAILED(ds_buffer_main->QueryInterface(IID_IDirectSoundBuffer8, (void**)&ds_buffer_second)))
    {
        ds_buffer_main->Release();
        
return NULL;
    }

    
// return the interface
    return ds_buffer_second;
}

//--------------------------------------------------------------------------------
// Load sound data from second directsound buffer.
//--------------------------------------------------------------------------------
BOOL Load_Sound_Data(IDirectSoundBuffer8* ds_buffer, long lock_pos, long lock_size, FILE* fp)
{
    BYTE* ptr1;
    BYTE* ptr2;
    DWORD size1, size2;

    
if(lock_size == 0)
        
return FALSE;

    
// lock the sound buffer at position specified
    if(FAILED(ds_buffer->Lock(lock_pos, lock_size, (void**)&ptr1, &size1, (void**)&ptr2, &size2, 0)))
        
return FALSE;

    
// read in the data
    fread(ptr1, 1, size1, fp);

    
if(ptr2 != NULL)
        fread(ptr2, 1, size2, fp);

    
// unlock it
    ds_buffer->Unlock(ptr1, size1, ptr2, size2);

    
return TRUE;
}

接着编写一个函数Load_WAV封装刚才那两个函数,从文件名加载波形文件信息。
 
//--------------------------------------------------------------------------------
// Load wave file.
//--------------------------------------------------------------------------------
IDirectSoundBuffer8* Load_WAV(char* filename)
{
    IDirectSoundBuffer8* ds_buffer;
    WAVE_HEADER wave_header = {0};
    FILE* fp;

    
// open the source file
    if((fp = fopen(filename, "rb")) == NULL)
        
return NULL;

    
// create the sound buffer
    if((ds_buffer = Create_Buffer_From_WAV(fp, &wave_header)) == NULL)
    {
        fclose(fp);
        
return NULL;
    }

    
// read in the data
    fseek(fp, sizeof(WAVE_HEADER), SEEK_SET);

    
// load sound data
    Load_Sound_Data(ds_buffer, 0, wave_header.data_size, fp);

    
// close the source file
    fclose(fp);

    
// return the new sound buffer fully loaded with sound
    return ds_buffer;
}


以下给出完整示例:

点击下载源码和工程

 
/***************************************************************************************
PURPOSE:
    Wave Playing Demo
 ***************************************************************************************/


#include <windows.h>
#include <stdio.h>
#include <dsound.h>
#include "resource.h"

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dsound.lib")

#pragma warning(disable : 4996)

#define Safe_Release(p) if((p)) (p)->Release();

// .WAV file header
struct WAVE_HEADER
{
    
char    riff_sig[4];            // 'RIFF'
    long    waveform_chunk_size;    // 8
    char    wave_sig[4];            // 'WAVE'
    char    format_sig[4];          // 'fmt ' (notice space after)
    long    format_chunk_size;      // 16;
    short   format_tag;             // WAVE_FORMAT_PCM
    short   channels;               // # of channels
    long    sample_rate;            // sampling rate
    long    bytes_per_sec;          // bytes per second
    short   block_align;            // sample block alignment
    short   bits_per_sample;        // bits per second
    char    data_sig[4];            // 'data'
    long    data_size;              // size of waveform data
};

// window handles, class and caption text.
HWND g_hwnd;
char g_class_name[] = "WavPlayClass";

IDirectSound8*          g_ds;           
// directsound component
IDirectSoundBuffer8*    g_ds_buffer;    // sound buffer object

//--------------------------------------------------------------------------------
// Create wave header information from wave file.
//--------------------------------------------------------------------------------
IDirectSoundBuffer8* Create_Buffer_From_WAV(FILE* fp, WAVE_HEADER* wave_header)
{
    IDirectSoundBuffer*     ds_buffer_main;
    IDirectSoundBuffer8*    ds_buffer_second;    
    DSBUFFERDESC            ds_buffer_desc;
    WAVEFORMATEX            wave_format;

    
// read in the header from beginning of file
    fseek(fp, 0, SEEK_SET);
    fread(wave_header, 1, 
sizeof(WAVE_HEADER), fp);

    
// check the sig fields. returning if an error.
    if(memcmp(wave_header->riff_sig, "RIFF", 4) || memcmp(wave_header->wave_sig, "WAVE", 4) ||
       memcmp(wave_header->format_sig, "fmt ", 4) || memcmp(wave_header->data_sig, "data", 4))
    {
        
return NULL;
    }

    
// setup the playback format
    ZeroMemory(&wave_format, sizeof(WAVEFORMATEX));

    wave_format.wFormatTag      = WAVE_FORMAT_PCM;
    wave_format.nChannels       = wave_header->channels;
    wave_format.nSamplesPerSec  = wave_header->sample_rate;
    wave_format.wBitsPerSample  = wave_header->bits_per_sample;
    wave_format.nBlockAlign     = wave_format.wBitsPerSample / 8 * wave_format.nChannels;
    wave_format.nAvgBytesPerSec = wave_format.nSamplesPerSec * wave_format.nBlockAlign;

    
// create the sound buffer using the header data
    ZeroMemory(&ds_buffer_desc, sizeof(DSBUFFERDESC));

    ds_buffer_desc.dwSize        = 
sizeof(DSBUFFERDESC);
    ds_buffer_desc.dwFlags       = DSBCAPS_CTRLVOLUME;
    ds_buffer_desc.dwBufferBytes = wave_header->data_size;
    ds_buffer_desc.lpwfxFormat   = &wave_format;

    
// create main sound buffer
    if(FAILED(g_ds->CreateSoundBuffer(&ds_buffer_desc, &ds_buffer_main, NULL)))
        
return NULL;

    
// get newer interface
    if(FAILED(ds_buffer_main->QueryInterface(IID_IDirectSoundBuffer8, (void**)&ds_buffer_second)))
    {
        ds_buffer_main->Release();
        
return NULL;
    }

    
// return the interface
    return ds_buffer_second;
}

//--------------------------------------------------------------------------------
// Load sound data from second directsound buffer.
//--------------------------------------------------------------------------------
BOOL Load_Sound_Data(IDirectSoundBuffer8* ds_buffer, long lock_pos, long lock_size, FILE* fp)
{
    BYTE* ptr1;
    BYTE* ptr2;
    DWORD size1, size2;

    
if(lock_size == 0)
        
return FALSE;

    
// lock the sound buffer at position specified
    if(FAILED(ds_buffer->Lock(lock_pos, lock_size, (void**)&ptr1, &size1, (void**)&ptr2, &size2, 0)))
        
return FALSE;

    
// read in the data
    fread(ptr1, 1, size1, fp);

    
if(ptr2 != NULL)
        fread(ptr2, 1, size2, fp);

    
// unlock it
    ds_buffer->Unlock(ptr1, size1, ptr2, size2);

    
return TRUE;
}

//--------------------------------------------------------------------------------
// Load wave file.
//--------------------------------------------------------------------------------
IDirectSoundBuffer8* Load_WAV(char* filename)
{
    IDirectSoundBuffer8* ds_buffer;
    WAVE_HEADER wave_header = {0};
    FILE* fp;

    
// open the source file
    if((fp = fopen(filename, "rb")) == NULL)
        
return NULL;

    
// create the sound buffer
    if((ds_buffer = Create_Buffer_From_WAV(fp, &wave_header)) == NULL)
    {
        fclose(fp);
        
return NULL;
    }

    
// read in the data
    fseek(fp, sizeof(WAVE_HEADER), SEEK_SET);

    
// load sound data
    Load_Sound_Data(ds_buffer, 0, wave_header.data_size, fp);

    
// close the source file
    fclose(fp);

    
// return the new sound buffer fully loaded with sound
    return ds_buffer;
}

//--------------------------------------------------------------------------------
// Window procedure.
//--------------------------------------------------------------------------------
long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    
switch(msg)
    {
    
case WM_DESTROY:
        PostQuitMessage(0);
        
return 0;
    }

    
return (long) DefWindowProc(hwnd, msg, wParam, lParam);
}

//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
    WNDCLASS            win_class;
    MSG                 msg;    

    
// create window class and register it
    win_class.style         = CS_HREDRAW | CS_VREDRAW;
    win_class.lpfnWndProc   = Window_Proc;
    win_class.cbClsExtra    = 0;
    win_class.cbWndExtra    = DLGWINDOWEXTRA;
    win_class.hInstance     = inst;
    win_class.hIcon         = LoadIcon(inst, IDI_APPLICATION);
    win_class.hCursor       = LoadCursor(NULL, IDC_ARROW);
    win_class.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    win_class.lpszMenuName  = NULL;
    win_class.lpszClassName = g_class_name;    

    
if(! RegisterClass(&win_class))
        
return FALSE;

    
// create the main window
    g_hwnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_WAVPLAY), 0, NULL);

    ShowWindow(g_hwnd, cmd_show);
    UpdateWindow(g_hwnd);

    
// initialize and configure directsound

    // creates and initializes an object that supports the IDirectSound8 interface
    if(FAILED(DirectSoundCreate8(NULL, &g_ds, NULL)))
    {
        MessageBox(NULL, "Unable to create DirectSound object", "Error", MB_OK);
        
return 0;
    }

    
// set the cooperative level of the application for this sound device
    g_ds->SetCooperativeLevel(g_hwnd, DSSCL_NORMAL);

    
// load a sound to play
    g_ds_buffer = Load_WAV("test.wav");

    
if(g_ds_buffer)
    {
        
// play sound looping
        g_ds_buffer->SetCurrentPosition(0);
        
// set volume
        g_ds_buffer->SetVolume(DSBVOLUME_MAX);
        
// play sound
        g_ds_buffer->Play(0, 0, DSBPLAY_LOOPING);
    }

    
// start message pump, waiting for signal to quit.
    ZeroMemory(&msg, sizeof(MSG));

    
while(msg.message != WM_QUIT)
    {
        
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }        
    }

    
// release directsound objects
    g_ds->Release();

    UnregisterClass(g_class_name, inst);
    
    
return (int) msg.wParam;
}
 

运行截图:


 

阅读下篇:用DirectX Audio和DirectShow播放声音和音乐(4)

posted on 2007-07-27 19:54 lovedday 阅读(3294) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论