1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2008 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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.
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"
20 #include "buffer_xaudio2.h"
21 #include "sound_driver_xaudio2.h"
28 using namespace NLMISC
;
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()
45 void CBufferXAudio2::release()
49 // Remove the buffer from the driver.
50 _SoundDriver
->removeBuffer(this);
52 _SoundDriver
->performanceUnregisterBuffer(_Format
, _Size
);
55 // Release possible _Data
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
)
69 _SoundDriver
->performanceUnregisterBuffer(_Format
, _Size
);
70 if (capacity
> _Capacity
)
80 _DataPtr
= new uint8
[capacity
+ 15];
81 _DataAligned
= (uint8
*)((size_t)_DataPtr
+ ((16 - ((size_t)_DataPtr
% 16)) % 16));
86 _SoundDriver
->performanceRegisterBuffer(_Format
, _Size
);
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
);
98 _SoundDriver
->performanceRegisterBuffer(_Format
, _Size
);
104 _SoundDriver
->performanceRegisterBuffer(_Format
, _Size
);
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
);
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
)
128 void CBufferXAudio2::setFormat(TBufferFormat format
, uint8 channels
, uint8 bitsPerSample
, uint32 frequency
)
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
140 channels
= _Channels
;
141 bitsPerSample
= _BitsPerSample
;
142 frequency
= _Frequency
;
145 /// Return the size of the buffer, in bytes
146 uint
CBufferXAudio2::getSize() const
151 /// Return the duration (in ms) of the sample in the buffer
152 float CBufferXAudio2::getDuration() const
157 nlassert(_Channels
== 1 && _BitsPerSample
== 16);
158 return 1000.0f
* ((float)_Size
* 2.0f
) / (float)_Frequency
;
161 return 1000.0f
* getDurationFromPCMSize(_Size
, _Channels
, _BitsPerSample
, _Frequency
);
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
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 */