Fix "no remove aqua speed" bug when player leaves the water
[ryzomcore.git] / nel / src / sound / driver / xaudio2 / buffer_xaudio2.cpp
blobd6e170f26ec5bb0c31607074c22f0dfe10b30911
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2008 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdxaudio2.h"
19 // Project includes
20 #include "buffer_xaudio2.h"
21 #include "sound_driver_xaudio2.h"
23 #ifdef DEBUG_NEW
24 #define new DEBUG_NEW
25 #endif
27 using namespace std;
28 using namespace NLMISC;
30 namespace NLSOUND {
32 CBufferXAudio2::CBufferXAudio2(CSoundDriverXAudio2 *soundDriver)
33 : _SoundDriver(soundDriver), _DataAligned(NULL), _DataPtr(NULL),
34 _Size(0), _Name(NULL), _Format((IBuffer::TBufferFormat)~0),
35 _Channels(0), _BitsPerSample(0), _Frequency(0), _Capacity(0)
40 CBufferXAudio2::~CBufferXAudio2()
42 release();
45 void CBufferXAudio2::release()
47 if (_SoundDriver)
49 // Remove the buffer from the driver.
50 _SoundDriver->removeBuffer(this);
51 // Update stats.
52 _SoundDriver->performanceUnregisterBuffer(_Format, _Size);
53 _SoundDriver = NULL;
55 // Release possible _Data
56 if (_DataPtr)
58 delete[] _DataPtr;
59 _DataAligned = NULL;
60 _DataPtr = NULL;
64 /// Get a writable pointer to the buffer of specified size. Returns NULL in case of failure. It is only guaranteed that the original data is still available when using StorageSoftware and the specified size is not larger than the available data. Call setStorageMode() and setFormat() first.
65 uint8 *CBufferXAudio2::lock(uint capacity)
67 if (_DataPtr)
69 _SoundDriver->performanceUnregisterBuffer(_Format, _Size);
70 if (capacity > _Capacity)
72 delete[] _DataPtr;
73 _DataAligned = NULL;
74 _DataPtr = NULL;
78 if (!_DataPtr)
80 _DataPtr = new uint8[capacity + 15];
81 _DataAligned = (uint8 *)((size_t)_DataPtr + ((16 - ((size_t)_DataPtr % 16)) % 16));
82 _Capacity = capacity;
83 if (_Size > capacity)
84 _Size = capacity;
86 _SoundDriver->performanceRegisterBuffer(_Format, _Size);
88 return _DataAligned;
91 /// Notify that you are done writing to this buffer, so it can be copied over to hardware if needed. Returns true if ok.
92 bool CBufferXAudio2::unlock(uint size)
94 _SoundDriver->performanceUnregisterBuffer(_Format, _Size);
95 if (size > _Capacity)
97 _Size = _Capacity;
98 _SoundDriver->performanceRegisterBuffer(_Format, _Size);
99 return false;
101 else
103 _Size = size;
104 _SoundDriver->performanceRegisterBuffer(_Format, _Size);
105 return true;
109 /// Copy the data with specified size into the buffer. A readable local copy is only guaranteed when OptionLocalBufferCopy is set. Returns true if ok.
110 bool CBufferXAudio2::fill(const uint8 *src, uint size)
112 uint8 *dest = lock(size);
113 if (dest == NULL) return false;
114 CFastMem::memcpy(dest, src, size);
115 return unlock(size);
118 /** Preset the name of the buffer. Used for async loading to give a name
119 * before the buffer is effectivly loaded.
120 * If the name after loading of the buffer doesn't match the preset name,
121 * the load will assert.
123 void CBufferXAudio2::setName(NLMISC::TStringId bufferName)
125 _Name = bufferName;
128 void CBufferXAudio2::setFormat(TBufferFormat format, uint8 channels, uint8 bitsPerSample, uint32 frequency)
130 _Format = format;
131 _Channels = channels;
132 _Frequency = frequency;
133 _BitsPerSample = bitsPerSample;
136 /// Return the sample format information.
137 void CBufferXAudio2::getFormat(TBufferFormat &format, uint8 &channels, uint8 &bitsPerSample, uint32 &frequency) const
139 format = _Format;
140 channels = _Channels;
141 bitsPerSample = _BitsPerSample;
142 frequency = _Frequency;
145 /// Return the size of the buffer, in bytes
146 uint CBufferXAudio2::getSize() const
148 return _Size;
151 /// Return the duration (in ms) of the sample in the buffer
152 float CBufferXAudio2::getDuration() const
154 switch (_Format)
156 case FormatDviAdpcm:
157 nlassert(_Channels == 1 && _BitsPerSample == 16);
158 return 1000.0f * ((float)_Size * 2.0f) / (float)_Frequency;
159 break;
160 case FormatPcm:
161 return 1000.0f * getDurationFromPCMSize(_Size, _Channels, _BitsPerSample, _Frequency);
162 break;
164 return 0.0f;
167 /// Return true if the buffer is stereo, false if mono
168 bool CBufferXAudio2::isStereo() const
170 return _Channels > 1;
173 /// Return the name of this buffer
174 NLMISC::TStringId CBufferXAudio2::getName() const
176 return _Name;
179 /// Return true if the buffer is loaded. Used for async load/unload.
180 bool CBufferXAudio2::isBufferLoaded() const
182 return _DataPtr != NULL;
185 /// Set the storage mode of this buffer, call before filling this buffer. Storage mode is always software if OptionSoftwareBuffer is enabled. Default is auto.
186 void CBufferXAudio2::setStorageMode(IBuffer::TStorageMode /* storageMode */)
188 // software buffering, no hardware storage mode available
191 /// Get the storage mode of this buffer.
192 IBuffer::TStorageMode CBufferXAudio2::getStorageMode()
194 // always uses software buffers
195 return IBuffer::StorageSoftware;
198 } /* namespace NLSOUND */
200 /* end of file */