Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SoundD3D.h
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: SoundD3D.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  DirectSound3D Audio Output and Buffer classes
13 */
14 
15 #ifndef SoundD3D_h
16 #define SoundD3D_h
17 
18 //#define DIRECT_SOUND_3D
19 #include "SoundCard.h"
20 #include "Sound.h"
21 #include "Camera.h"
22 #include "ThreadSync.h"
23 #include <stdio.h>
24 #include <dsound.h>
25 #include "vorbis/vorbisfile.h"
26 
27 // +--------------------------------------------------------------------+
28 
29 class SoundD3D;
30 class SoundCardD3D;
31 
32 // +--------------------------------------------------------------------+
33 // Sound Implementation for DirectSound and DirectSound3D
34 
35 class SoundD3D : public Sound
36 {
37 public:
38  static const char* TYPENAME() { return "SoundD3D"; }
39 
40  SoundD3D(LPDIRECTSOUND card, DWORD flags, LPWAVEFORMATEX format);
41  SoundD3D(LPDIRECTSOUND card, DWORD flags, LPWAVEFORMATEX format, DWORD len, LPBYTE data);
42  virtual ~SoundD3D();
43 
44  virtual void Update();
45 
46  virtual HRESULT StreamFile(const char* name, DWORD offset);
47  virtual HRESULT Load(DWORD bytes, BYTE* data);
48  virtual HRESULT Play();
49  virtual HRESULT Rewind();
50  virtual HRESULT Pause();
51  virtual HRESULT Stop();
52 
53  virtual Sound* Duplicate();
54 
55  // (only for streamed sounds)
56  virtual double GetTotalTime() const { return total_time; }
57  virtual double GetTimeRemaining() const;
58  virtual double GetTimeElapsed() const;
59 
60  // (only used for localized sounds)
61  virtual void SetVolume(long v);
62  virtual long GetPan() const;
63  virtual void SetPan(long p);
64  virtual void SetLocation(const Vec3& l);
65  virtual void SetVelocity(const Vec3& v);
66 
67  virtual float GetMinDistance() const;
68  virtual void SetMinDistance(float f);
69  virtual float GetMaxDistance() const;
70  virtual void SetMaxDistance(float f);
71 
72 
73 protected:
74  void Localize();
75  HRESULT AllocateBuffer(DWORD bytes);
76  HRESULT StreamOggFile();
77 
78  void StreamBlock();
79  void StreamOggBlock();
80  void RewindStream();
81  void RewindOggStream();
82 
83  LPDIRECTSOUND soundcard;
84  WAVEFORMATEX wfex;
85  DSBUFFERDESC dsbd;
86  LPDIRECTSOUNDBUFFER buffer;
87 
88  DWORD data_len;
89  LPBYTE data;
90 
91 #ifdef DIRECT_SOUND_3D
92  LPDIRECTSOUND3DBUFFER sound3d;
93 #endif
94 
95  float min_dist;
96  float max_dist;
97 
98  // STREAMED SOUND SUPPORT:
99  FILE* stream;
100  DWORD stream_left;
101  double total_time;
102  DWORD min_safety;
103  DWORD read_size;
104  BYTE* transfer;
105  DWORD w, r;
108  BYTE eos_latch;
109  bool moved;
110 
112  OggVorbis_File* ov_file;
113 };
114 
115 // +--------------------------------------------------------------------+
116 // Sound Card Implementation for DS and DS3D
117 
118 class SoundCardD3D : public SoundCard
119 {
120  friend class SoundD3D;
121 
122 public:
123  static const char* TYPENAME() { return "SoundCardD3D"; }
124 
125  SoundCardD3D(HWND hwnd);
126  virtual ~SoundCardD3D();
127 
128  // Format of the sound card's primary buffer:
129  virtual bool GetFormat(LPWAVEFORMATEX format);
130  virtual bool SetFormat(LPWAVEFORMATEX format);
131  virtual bool SetFormat(int bits, int channels, int hertz);
132 
133  virtual void ShowFormat();
134 
135  // Get a blank, writable sound buffer:
136  virtual Sound* CreateSound(DWORD flags, LPWAVEFORMATEX format);
137 
138  // Create a sound resource:
139  virtual Sound* CreateSound(DWORD flags, LPWAVEFORMATEX format, DWORD len, LPBYTE data);
140 
141  virtual void SetListener(const Camera& cam, const Vec3& vel);
142  virtual bool Pause();
143  virtual bool Resume();
144  virtual bool StopSoundEffects();
145 
146 protected:
147  LPDIRECTSOUND soundcard;
148  LPDIRECTSOUNDBUFFER primary;
149 
150 #ifdef DIRECT_SOUND_3D
151  LPDIRECTSOUND3DLISTENER listener;
152 #else
155 #endif
156 
157  WAVEFORMATEX wfex;
158  DSBUFFERDESC dsbd;
159 };
160 
161 #endif SoundD3D_h
162