首页 > 未分类 > 用DirectX Audio和DirectShow播放声音和音乐(3)
2019
11-29

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

调整声道平衡

所谓声道平衡就是调节左右声道的大小, 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

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

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中是"WaveSig"。请仔细查看 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, 
1sizeof(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, 
1sizeof(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(00, DSBPLAY_LOOPING);
    }

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

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

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

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

运行截图:

用DirectX Audio和DirectShow播放声音和音乐(3) - 第1张  | 逗分享开发经验


最后编辑:
作者:搬运工
这个作者貌似有点懒,什么都没有留下。