1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435:
| unit T_DSound;
interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ActiveX, //OpenGL OpenGL, //Direct X DirectSound, DirectMusic, DirectXGraphics; //für D3D-Vektor //Meine Units
const ConIs3DSound : Byte = 1;
type PIDirectSound3DBuffer8 = ^IDirectSound3DBuffer8; PIDirectMusicSegment8 = ^IDirectMusicSegment8; PIDirectMusicPerformance8 = ^IDirectMusicPerformance8; PIDirectSound3DBuffer = ^IDirectSound3DBuffer;
CAudio = object //Abgetipt und in Delphi übersetzt private dmusicSegment : PIDirectMusicSegment8; ds3DBuffer : PIDirectSound3DBuffer; //not used if is3DSound = false Flags : Boolean;//war vorher is3DSound public constructor create; destructor destroy;
procedure SetSegment(seg : PIDirectMusicSegment8); function getSegment: PIDirectMusicSegment8;
procedure Set3DBuffer(dsBuffer : PIDirectSound3DBuffer); function get3DBuffer : PIDirectSound3DBuffer;
function Is3DSound : Boolean; procedure SetFlags(b : Boolean);
procedure Set3DParams(minDistance,maxDistance : Single); procedure Set3DPos(x,y,z : Single)overload; end;
T_DS_Audio = object(CAudio) public procedure setOrientation(forwardX, forwardY, forwardZ: Single; InSideConeAngle,OutSideConeAngle :DWord; OutSideConeVolume : Longint)overload; procedure setVelocity(x,y,z : Single); procedure SetAll6F(posX,posY,posZ, VelocityX,VelocityY,VelocityZ : Single); procedure SetAll(posX,posY,posZ : single; VelocityX,VelocityY,VelocityZ : Single; forwardX, forwardY, forwardZ: Single; InSideConeAngle,OutSideConeAngle :DWord; OutSideConeVolume : Longint); procedure SetAll6F3I(posX,posY,posZ, forwardX, forwardY, forwardZ: Single; InSideConeAngle,OutSideConeAngle :DWord; OutSideConeVolume : Longint); end;
P_DS_Audio = ^T_DS_Audio;
T_DSoundSystem = object private dmusicLoader : ^IDirectMusicLoader8 ; dmusicPerformance : PIDirectMusicPerformance8 ; dmusicSegment : ^IDirectMusicSegment8 ; dmusic3DAudioPath : ^IDirectMusicAudioPath ; ds3DBuffer : ^IDirectSound3DBuffer ; ds3DListener : ^IDirectSound3DListener ;
dsBufferParams : TDS3DBuffer ; dsListenerParams : TDS3DListener ; public constructor Create; function InitDirectXAudio(HwND:HWND) : Boolean; function Create3DBuffer : PIDirectSound3DBuffer8; function CreateSound(Filename : PWideChar;is3DSound : Boolean): P_DS_Audio; function CreateSegment(Filename : PWideChar;is3DSound : Boolean):PIDirectMusicSegment8;
procedure Play(audio : P_DS_Audio;numRepeats : DWord); procedure Stop(audio : P_DS_Audio);
procedure PlaySegment(dmSeg : PIDirectmusicSegment8; is3DSound : Boolean;NumRepeats : DWord); procedure StopSegment(dmSeg : PIDirectmusicSegment8); procedure Shutdown;
procedure SetListenerPos(cameraX, CameraY,CameraZ : Single)overload; procedure SetListenerRolloff(rolloff : Single); procedure SetListenerOrientation(forwardX, forwardY, forwardZ, topX, topY, topZ : Single)overload;
function GetPerformance: PIDirectMusicPerformance8;
destructor destroy; end;
var DSoundSystem : T_DSoundSystem;
implementation
constructor CAudio.create; begin dmusicSegment := nil; ds3DBuffer := nil; Flags := false; end;
destructor CAudio.destroy; begin if dmusicSegment<>nil then begin dispose(dmusicSegment); dmusicSegment := nil; end; if ds3DBuffer<>nil then begin dispose(ds3DBuffer); ds3DBuffer:=nil; end; end;
procedure CAudio.SetSegment(seg : PIDirectMusicSegment8); begin dmusicSegment := seg; end;
function CAudio.getSegment : PIDirectMusicSegment8; begin result := dmusicSegment; end;
procedure CAudio.Set3DBuffer(dsBuffer : PIDirectSound3DBuffer); begin ds3DBuffer := dsBuffer; end;
function CAudio.get3DBuffer : PIDirectSound3DBuffer; begin result := ds3DBuffer; end;
function CAudio.Is3DSound : Boolean; begin result := flags; end;
procedure CAudio.SetFlags(b : Boolean); begin if Flags<>b then Flags := b; end;
procedure CAudio.Set3DParams(minDistance,maxDistance : Single); var t : TDS3DBUFFER; begin // set minimum and maximum distances t.dwSize := Sizeof(TDS3DBUFFER); ds3DBuffer^.GetAllParameters(t); t.flMinDistance := minDistance; t.flMaxDistance := maxDistance;
if (ds3DBuffer<>nil) then begin ds3DBuffer^.SetAllParameters(t,ds3d_immediate); end; end;
procedure CAudio.Set3DPos(x,y,z : single)overload; begin ds3DBuffer.SetPosition(x, y, -z, DS3D_IMMEDIATE); end;
procedure T_DS_Audio.setOrientation(forwardX, forwardY, forwardZ: Single; InSideConeAngle,OutSideConeAngle :DWord; OutSideConeVolume : Longint)overload; begin //**************************************************************** end;
procedure T_DS_Audio.setVelocity(x,y,z : single); begin //**************************************************************** end;
procedure T_DS_Audio.SetAll6F(posX,posY,posZ, VelocityX,VelocityY,VelocityZ : Single); begin //**************************************************************** end;
procedure T_DS_Audio.SetAll(posX,posY,posZ : single; VelocityX,VelocityY,VelocityZ : Single; forwardX, forwardY, forwardZ: Single; InSideConeAngle,OutSideConeAngle :DWord; OutSideConeVolume : Longint); begin //**************************************************************** end;
procedure T_DS_Audio.SetAll6F3I(posX,posY,posZ, forwardX, forwardY, forwardZ: Single; InSideConeAngle,OutSideConeAngle :DWord; OutSideConeVolume : Longint); begin //**************************************************************** end;
constructor T_DSoundSystem.Create; begin dmusic3DAudiopath := nil; dmusicLoader := nil; dmusicPerformance := nil; ds3DListener := nil;
end;
destructor T_DSoundSystem.destroy; begin if ds3DListener<>nil then begin Dispose(ds3DListener); ds3DListener := nil; end;
if dmusic3DAudioPath<>nil then begin Dispose(dmusic3DAudioPath); dmusic3DAudioPath := nil; end;
if dmusicPerformance<>nil then begin Dispose(dmusicPerformance); dmusicPerformance := nil; end;
if dmusicLoader<>nil then begin Dispose(dmusicLoader); dmusicLoader := nil; end;
//COM Bibliothek freigegeben CoUninitialize; end;
function T_DSoundSystem.InitDirectXAudio(hwnd : HWND): Boolean; begin CoInitialize(nil); result := false; dmusicLoader := nil; dmusicPerformance := nil;
if failed(CoCreateInstance(CLSID_DirectMusicLoader, nil, CLSCTX_INPROC, IID_IDirectMusicLoader8,dmusicloader)) then begin messagebox(hwnd,'Unable to create IDirectMusicLoader8 object'+#10#13+'Press OK to exit','Error',MB_OK); exit; end; if failed(CoCreateInstance(CLSID_DirectMusicPerformance, nil, CLSCTX_INPROC, IID_IDirectMusicPerformance8,dmusicPerformance)) then begin messagebox(hwnd,'Unable to create IDirectMusicPerformance8 object'+#10#13+'Press OK to exit','Error',MB_OK); exit; end; dmusicPerformance.InitAudio(nil,nil,0,DMUS_APATH_SHARED_STEREOPLUSREVERB, 64,DMUS_AUDIOF_ALL,nil);
if Failed(dmusicPerformance^.CreateStandardAudioPath(DMUS_APATH_DYNAMIC_3D, 64, true ,dmusic3DAudioPath^)) then //hier stürtzt das Programm mit einer Zugriffsverletzung ab. begin messagebox(hwnd,'Unable to create standart 3D audiopath !'+#10#13+'Press OK to exit','Error',MB_OK); exit; end; dsListenerParams.dwSize := sizeof(TDS3DLISTENER); ds3DListener^.GetAllParameters(dsListenerParams); // set position of listener dsListenerParams.vPosition.x := 0; dsListenerParams.vPosition.y := 0; dsListenerParams.vPosition.z := 0; ds3DListener^.SetAllParameters(dsListenerParams,DS3D_IMMEDIATE);
//Hier kommt nun die Überprüfung ob sich das alles gelohnt hat if (dmusicLoader=nil) or (dmusicPerformance=nil) then result:=false else result:=true; end;
function T_DSoundSystem.Create3DBuffer : PIDirectSound3DBuffer8; var buff : IDirectSound3DBuffer; mdsBufferParams : TDS3DBUFFER; begin result := nil; if Failed(dmusic3DAudioPath^.getObjectinPath(DMUS_PCHANNEL_ALL, DMUS_PATH_BUFFER, 0, GUID_NULL, 0, IID_IDirectSound3DBuffer, buff)) then begin Application.MessageBox('Unable to retrive 3D buffer from audiopeath! Press OK to Exit','ERROR', MB_OK); exit; end; // get the 3D buffer parameters dsBufferParams.dwSize := sizeof(DS3DBUFFER); buff.GetAllParameters(mdsBufferParams);
// set the new 3D buffer parameters dsBufferParams.dwMode := DS3DMODE_HEADRELATIVE; // relative to the listener buff.SetAllParameters(mdsBufferParams, DS3D_IMMEDIATE);
result := @buff; end;
function T_DSoundSystem.CreateSound(Filename : PWideChar;is3DSound : Boolean): P_DS_Audio; var audio : P_DS_Audio; dmSeg : PIDirectMusicSegment8; ds3D : PIDirectSound3DBuffer8; begin new(audio); dmSeg := CreateSegment(filename,is3DSound); audio^.SetSegment(dmSeg); if is3dSound then begin ds3D := Create3DBuffer; audio^.Set3DBuffer(@ds3D); audio^.SetFlags(true); end else begin audio^.Set3DBuffer(nil); audio^.SetFlags(false); end; result := audio; end;
function T_DSoundSystem.CreateSegment(filename :PWideChar;is3DSound : Boolean): PIDirectMusicSegment8; var seg : PIDirectMusicSegment8; begin if Failed(dmusicLoader^.loadObjectfromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8, filename, seg)) then begin Application.MessageBox('Audio file not found ! Press OK to exit','ERROR !',MB_OK); result := nil; exit; end; result := seg; end;
procedure T_DSoundSystem.Play(audio : P_DS_Audio;NumRepeats : DWord); var t : IDirectMusicSegmentState; begin audio^.getSegment^.SetRepeats(numRepeats); if audio^.Is3DSound then begin audio^.getsegment^.Download(dmusic3DAudioPath^); dmusicPerformance^.PlaySegmentEx(audio^.getsegment^,nil,nil,DMUS_SEGF_SECONDARY,0,t,nil,dmusic3DAudioPath^); end else begin audio^.getSegment^.Download(dmusicPerformance^); dMusicPerformance^.PlaySegmentEx(audio^.getsegment^,nil,nil,DMUS_SEGF_DEFAULT, 0,t,nil,nil); end; end;
procedure T_DSoundSystem.Stop(audio : P_DS_Audio); begin dmusicPerformance^.StopEx(audio^.getsegment^,0,0); end;
procedure T_DSoundSystem.PlaySegment(dmSeg : PIDirectMusicSegment8;is3DSound : Boolean; NumRepeats : DWord); var t : IDirectMusicSegmentState; begin dmseg^.SetRepeats(numRepeats); if not is3dSound then dmseg^.Download(dmusicPerformance^) else begin dmSeg^.Download(dmusic3DAudioPath^); dmusicPerformance^.PlaySegmentEx(dmSeg^,nil,nil ,DMUS_SEGF_DEFAULT,0,t, nil,dmusic3DAudioPath^); end; end;
procedure T_DSoundSystem.StopSegment(dmSeg : PIDirectMusicSegment8); begin dmusicPerformance^.StopEx(dmseg^,0,0); end;
procedure T_DSoundSystem.Shutdown; begin dmusicPerformance^.Stop(nil,nil,0,0); dmusicperformance^.CloseDown; end;
procedure T_DSoundSystem.SetListenerPos(cameraX,cameraY,cameraZ : Single)overload; begin ds3dListener^.SetPosition(camerax,cameray,-cameraz,DS3D_IMMEDIATE); end;
procedure T_DSoundSystem.SetListenerRolloff(rolloff : Single); begin if ds3DListener<>nil then ds3dListener^.SetRolloffFactor(rolloff,DS3D_IMMEDIATE); end;
procedure T_DSoundSystem.SetListenerOrientation(forwardX, forwardY, forwardZ, topX, topY, topZ : Single)overload; begin ds3DListener.SetOrientation(forwardX,forwardY,-forwardZ,Topx,topY,-topZ,DS3D_IMMEDIATE); end;
function T_DSoundSystem.GetPerformance : PIDirectMusicPerformance8; begin result := dMusicPerformance; end;
end. |