精華區beta GameDesign 關於我們 聯絡資訊
DirectSound8 聲音特效 2005/12 by WalkingAlone,源碼可以任意拷貝、修改作為任何用途,轉載請注明出處。 聲明:翻譯因涉及專業知識未必準確,請讀者自行斟酌。 ------------------------------------------------------------- 運行介面 近日一直在看網友"智慧的魚"在csdn的博客(http://blog.csdn.net/aoosang/), 他對DirectSound和DirectShow的認識比較深,同時翻譯了部分相關的MSDN,受益匪淺。 DirectSound的特效(Effects)部分他還沒完善,遺憾之餘經過一番Google總算略有收穫 ,姑且記錄下來幫助後來者少走點彎路,同時亦希望抛磚引玉,高手指點。 下文引用MSDN部分: DirectX provides support for effects processing of sounds by DirectX Media Objects (DMOs). A standard set of effects is available to every DirectX application. Other DMOs can be registered on the system. DirectX使用DirectX Media Objects(DMOs)提供對聲音的特效處理,每個DirectX應 用程式都可以使用一組標準的特效,系統也可以註冊其他特效。 All the standard DMOs except Waves reverberation can process 8- or 16-bit PCM data with one or two channels at any sample rate supported by DirectSound. Waves reverberation does not support 8-bit samples. 除聲波迴響(Waves reverberation)外,DirectSound所有標準DMOs都支援8或16bit 的PCM(脈衝編碼調製,Pulse Code Modulation),單聲道或雙聲道,所有採樣率。聲波 迴響不支持8bit採樣。 標準特效有9種: Chorus(合唱),對應的介面 IDirectSoundFXChorus8; Compression(壓縮),對應的介面 IDirectSoundFXCompressor8; Distortion(扭曲),對應的介面 IDirectSoundFXDistortion8; Echo(回聲),對應的介面 IDirectSoundFXEcho8; Environmental Reverberation(環境回聲),對應的介面 IDirectSoundFXI3DL2Reverb8; Flange(凸緣),對應的介面 IDirectSoundFXFlanger8; Gargle(漱口?),對應的介面 IDirectSoundFXGargle8; Parametric Equalizer(參數平衡?),對應的介面 IDirectSoundFXParamEq8; Waves Reverberation(聲波迴響),對應的介面 IDirectSoundFXWavesReverb8。 ------------------------------------------------------------- 創建特效的過程如下: 1、創建IDirectSound8和主緩衝區,不再累贅,記得CoInitialize; 2、創建IDirectSoundBuffer8介面作為第二緩衝區(SecondaryBuffer),注意三點 :一是先用IDirectSound8的CreateSoundBuffer方法創建一個IDirectSoundBuffer,再 QueryInterface才能獲得IDirectSoundBuffer8介面;二是DSBUFFERDESC中必須有 DSBCAPS_CTRLFX標記,以下引用MSDN部分:You can set any number of effects on a secondary buffer that was created with the DSBCAPS_CTRLFX flag in the DSBUFFERDESC structure;三是該緩衝區不能正在播放(可以用GetStatus方法獲得當前 緩衝區狀態,或者簡單的用Stop方法不管當前狀態先停止)或被鎖定的(用unlock解鎖) ; 3、使用IDirectSoundBuffer8的SetFX方法設置特效; 4、使用IDirectSoundBuffer8的GetObjectInPath方法獲得特效的介面; 5、特效介面的SetAllParameters方法設置參數,GetAllParameters獲取參數, IDirectSoundFXI3DL2Reverb8比較特殊,多了GetQuality,SetQuality,GetPreset, SetPreset 4個方法。 完成上述工作後,調用IDirectSoundBuffer8的Play方法播放音效就可以測試效果了 。 ------------------------------------------------------------- 清除特效:同樣使用SetFX方法,把dwEffectsCount設成0,pDSFXDesc設成NULL(C++ )或Nil(Delphi),因為圖方便在例子裏我並沒有這麼做,而是把介面釋放再重新創建 。 下面連接是DirectSound8 聲音特效測試Demo,使用Delphi + Jedi DirectX8 Header ,並在Windows2000(2003) + Delphi7下編譯通過,僅供參考,對於 IDirectSoundFXI3DL2Reverb8介面的4個額外方法沒有作測試,也沒有嘗試一次生成多個 特效。壓縮包中還有vb(VBeffects.rtf)和c++(LunaSound_cpp.htm,日語)兩個源碼 。 ------------------------------------------------------------- soundeffects.rar 不知是我耳朵太背,還是樂理太差,抑或硬體太爛,回聲以外的特效都不是很明顯, 參數的作用也未能理解,希望試過的朋友交流一下。 附1:細心的朋友可能看見根源程式中有一句Set8087CW($133f);這是筆者花了一晚時間換 回來的血淚教訓。在使用SetFX方法調用Environmental Reverberation特效時,會返回一 個系統級的"invalid floating point operation"異常(其他特效不會),而不是 interface返回的出錯代碼,根本無法調試。查了幾遍google之後才找到問題癥結,這是 Delphi7幫助的說明,可以查找關鍵字Set8087CW獲得: The floating-point unit control word controls the precision of floating point calculations, the rounding mode, and whether certain floating-point operations trigger exceptions. See Intel's processor documentation for details. This routine allows the programmer to have direct access to the CW. Be aware that using this routine to change the value of the 8087CW will change the behavior of the program's FP calculations. It is the programmer's responsibility to reset it. It is recommended that you disable all floating-point exceptions when using OpenGL to render 3D graphics. To do this, call Set8087CW(0x133f) in your main form's OnCreate event before calling any OpenGL functions. 雖然幫助裏說的是OpenGL,想想Directx和OpenGL是殊途同歸的東西,死馬當活馬醫 終於解決問題。 附2:合唱特效的DSFXChorus紀錄參數翻譯 Syntax typedef struct _DSFXChorus { FLOAT fWetDryMix; FLOAT fDepth; FLOAT fFeedback; FLOAT fFrequency; LONG lWaveform; FLOAT fDelay; LONG lPhase; } DSFXChorus, *LPDSFXChorus; typedef const DSFXChorus *LPCDSFXChorus; ------------------------------------------------------------- WetDryMix(幹濕混合) Ratio of wet (processed) signal to dry (unprocessed) signal. Must be in the range from DSFXCHORUS_WETDRYMIX_MIN through DSFXCHORUS_WETDRYMIX_MAX (all wet). The default value is 50. 濕信號(wet signal,處理過的信號)對幹信號(dry signal,未處理的信號)的比率, 取值範圍在 DSFXCHORUS_WETDRYMIX_MIN(0)和 DSFXCHORUS_WETDRYMIX_MAX(100.0,全 濕)之間,缺省值50。 ------------------------------------------------------------- Depth(深度) Percentage by which the delay time is modulated by the low-frequency oscillator, in hundredths of a percentage point. Must be in the range from DSFXCHORUS_DEPTH_MIN through DSFXCHORUS_DEPTH_MAX. The default value is 10. 低頻振盪器(LFO,Low Frequency Oscillator)調整延遲的百分比,取值範圍在 DSFXCHORUS_DEPTH_MIN(0)和 DSFXCHORUS_DEPTH_MAX(100.0)之間,缺省值是10。 ------------------------------------------------------------- Feedback(回饋) Percentage of output signal to feed back into the effect's input, in the range from DSFXCHORUS_FEEDBACK_MIN to DSFXCHORUS_FEEDBACK_MAX. The default value is 25. 輸出信號回饋到輸入信號的百分比,取值範圍在 DSFXCHORUS_FEEDBACK_MIN(-99.0)和 DSFXCHORUS_FEEDBACK_MAX(99.0)之間,缺省值25。 ------------------------------------------------------------- Frequency(頻率) Frequency of the LFO, in the range from DSFXCHORUS_FREQUENCY_MIN to DSFXCHORUS_FREQUENCY_MAX. The default value is 1.1. 低頻振盪器(LFO,Low Frequency Oscillator)的頻率,取值範圍在 DSFXCHORUS_FREQUENCY_MIN(0)和 DSFXCHORUS_FREQUENCY_MAX(10.0)之間,缺省值 1.1。 ------------------------------------------------------------- Waveform(波形) Waveform shape of the LFO. Defined values are DSFXCHORUS_WAVE_TRIANGLE and DSFXCHORUS_WAVE_SIN. By default, the waveform is a sine. 低頻振盪器(LFO,Low Frequency Oscillator)的波形,取值範圍是 DSFXCHORUS_WAVE_TRIANGLE(0)和 DSFXCHORUS_WAVE_SIN(1),缺省值 DSFXCHORUS_WAVE_SIN。 ------------------------------------------------------------- Delay(延遲) Number of milliseconds the input is delayed before it is played back, in the range from DSFXCHORUS_DELAY_MIN to DSFXCHORUS_DELAY_MAX. The default value is 16 ms. 播放之前的延遲,以毫秒作單位,取值範圍在 DSFXCHORUS_DELAY_MIN(0)和 DSFXCHORUS_DELAY_MAX(20.0)之間,缺省值16毫秒。 ------------------------------------------------------------- Phase(相位) Phase differential between left and right LFOs, in the range from DSFXCHORUS_PHASE_MIN through DSFXCHORUS_PHASE_MAX. Possible values are defined as follows. 左右低頻振盪器(LFO,Low Frequency Oscillator)之間的微分相位,取值範圍在 DSFXCHORUS_PHASE_MIN(0)和 DSFXCHORUS_PHASE_MAX(4)之間,可選值如下: DSFXCHORUS_PHASE_NEG_180 (0) DSFXCHORUS_PHASE_NEG_90 (1) DSFXCHORUS_PHASE_ZERO (2) DSFXCHORUS_PHASE_90 (3) DSFXCHORUS_PHASE_180 (4) The default value is DSFXCHORUS_PHASE_90. 缺省值 DSFXCHORUS_PHASE_90。 -- 遊戲設計團隊等待你的加入與支持: /\●/\ ))  http://220.132.66.21/phpbb2/portal.php  (( / /▲\ \    PTT Game Design板 PTT2 DreamYeh板 歡迎你來圓夢 \\      巴哈姆特好像才是我大本營XD~讓我們一起Kuso吧!               想了解更多DreamYeh嗎?歡迎來無名P_DreamYeh板! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.105.58.54