Version
menu_open
link
Wwise SDK 2022.1.11
AkBankReadHelpers.h
Go to the documentation of this file.
1 /*******************************************************************************
2 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
3 released in source code form as part of the SDK installer package.
4 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Copyright (c) 2024 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 #ifndef _AK_BANKREADHELPERS_H_
28 #define _AK_BANKREADHELPERS_H_
29 
31 
32 #include <type_traits>
33 
34 namespace AK
35 {
36  template<
37  typename T,
38  typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
39  >
40  inline T ReadUnaligned(const AkUInt8* in_pVal)
41  {
42 #if defined(__GNUC__)
43  typedef T __attribute__((aligned(1))) UnalignedT;
44  return *reinterpret_cast<const UnalignedT *>(in_pVal);
45 #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
46  return *reinterpret_cast<const T __unaligned *>(in_pVal); // __unaligned not supported on 32-bit x86
47 #else
48  return *reinterpret_cast<const T *>(in_pVal);
49 #endif
50  }
51 
52  template<
53  typename T,
54  typename std::enable_if<std::is_class<T>::value, bool>::type = true
55  >
56  inline T ReadUnaligned(const AkUInt8* in_pVal)
57  {
58  T result;
59  AKPLATFORM::AkMemCpy(&result, in_pVal, sizeof(T));
60  return result;
61  }
62 
63  template<
64  typename T,
65  typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
66  >
67  inline void WriteUnaligned(AkUInt8* out_pVal, const T in_val)
68  {
69 #if defined(__GNUC__)
70  typedef T __attribute__((aligned(1))) UnalignedT;
71  *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
72 #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
73  *reinterpret_cast<T __unaligned *>(out_pVal) = in_val; // __unaligned not supported on 32-bit x86
74 #else
75  *reinterpret_cast<T *>(out_pVal) = in_val;
76 #endif
77  }
78 
79  template<
80  typename T,
81  typename std::enable_if<std::is_class<T>::value, bool>::type = true
82  >
83  inline void WriteUnaligned(AkUInt8* out_pVal, const T& in_val)
84  {
85  AKPLATFORM::AkMemCpy(out_pVal, &in_val, sizeof(T));
86  }
87 
88  /// Read data from bank and advance pointer.
89  template< typename T >
90  inline T ReadBankData(
91  AkUInt8*& in_rptr
92 #ifdef _DEBUG
93  , AkUInt32& in_rSize
94 #endif
95  )
96  {
97  T l_Value = ReadUnaligned<T>(in_rptr);
98 
99  in_rptr += sizeof(T);
100 #ifdef _DEBUG
101  in_rSize -= sizeof(T);
102 #endif
103  return l_Value;
104  }
105 
106  template< typename T >
108  AkUInt8*& in_rptr
109 #ifdef _DEBUG
110  , AkUInt32& in_rSize
111 #endif
112  )
113  {
114  AkUInt32 l_Value = 0;
115 
116  AkUInt8 currentByte = *in_rptr;
117  ++in_rptr;
118 #ifdef _DEBUG
119  --in_rSize;
120 #endif
121  l_Value = (currentByte & 0x7F);
122  while (0x80 & currentByte)
123  {
124  currentByte = *in_rptr;
125  ++in_rptr;
126 #ifdef _DEBUG
127  --in_rSize;
128 #endif
129  l_Value = l_Value << 7;
130  l_Value |= (currentByte & 0x7F);
131  }
132 
133  return (T)l_Value;
134  }
135 
136  inline char * ReadBankStringUtf8(
137  AkUInt8*& in_rptr
138 #ifdef _DEBUG
139  , AkUInt32& in_rSize
140 #endif
141  , AkUInt32& out_uStringSize)
142  {
143  char* pString = (char *)in_rptr;
144  out_uStringSize = 0;
145 
146  while (*in_rptr != 0)
147  {
148  ++in_rptr;
149 #ifdef _DEBUG
150  --in_rSize;
151 #endif
152  ++out_uStringSize;
153  }
154 
155  ++in_rptr;
156 #ifdef _DEBUG
157  --in_rSize;
158 #endif
159 
160  return pString;
161  }
162 }
163 
164 
165 #ifdef _DEBUG
166 
167 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
168 #define READBANKDATA( _Type, _Ptr, _Size ) \
169  AK::ReadBankData<_Type>( _Ptr, _Size )
170 
171 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
172  AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
173 
174 /// Read and return a null-terminated UTF-8 string, written with the DataWriter of \ref AK::Wwise::Plugin::CustomData::GetPluginData or
175 /// \ref AK::Wwise::Plugin::AudioPlugin::GetBankParameters, and stored in bank, and its size.
176 /// \ref wwiseplugin_bank
177 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
178  AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
179 
180 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
181 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
182  ( _Ptr ) += sizeof( _Type ); \
183  ( _Size ) -= sizeof( _Type )
184 
185 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
186 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
187  ( _Size ) -= _NumBytes; \
188  ( _Ptr ) += _NumBytes
189 
190 /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
191 #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
192  AKPLATFORM::SafeStrCpy( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
193  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
194 
195 /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
196 #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
197  AK_UTF8_TO_OSCHAR( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
198  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
199 
200 /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
201 #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxOutPtrSize ) \
202  AK_CHAR_TO_UTF16( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
203  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
204 
205 
206 #else
207 
208 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
209 #define READBANKDATA( _Type, _Ptr, _Size ) \
210  AK::ReadBankData<_Type>( _Ptr )
211 
212 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
213  AK::ReadVariableSizeBankData<_Type>( _Ptr )
214 
215 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
216  AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
217 
218 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
219 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
220  ( _Ptr ) += sizeof( _Type )
221 
222 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
223 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
224  ( _Ptr ) += _NumBytes;
225 
226 /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
227 #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
228  AKPLATFORM::SafeStrCpy( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
229  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
230 
231 /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
232 #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
233  AK_UTF8_TO_OSCHAR( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
234  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
235 
236 /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
237 #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxPtrSize ) \
238  AK_CHAR_TO_UTF16( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
239  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
240 
241 #endif
242 
243 #define GETBANKDATABIT( _Data, _Shift ) \
244  (((_Data) >> (_Shift)) & 0x1)
245 
246 /// Helper macro to determine whether the full content of a block of memory was properly parsed
247 #ifdef _DEBUG
248  #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
249 #else
250  #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
251 #endif
252 
253 #endif //_AK_BANKREADHELPERS_H_
void WriteUnaligned(AkUInt8 *out_pVal, const T in_val)
Audiokinetic namespace.
uint8_t AkUInt8
Unsigned 8-bit integer.
T ReadBankData(AkUInt8 *&in_rptr)
Read data from bank and advance pointer.
T ReadVariableSizeBankData(AkUInt8 *&in_rptr)
AkForceInline void AkMemCpy(void *pDest, const void *pSrc, AkUInt32 uSize)
Platform Independent Helper.
T ReadUnaligned(const AkUInt8 *in_pVal)
uint32_t AkUInt32
Unsigned 32-bit integer.
char * ReadBankStringUtf8(AkUInt8 *&in_rptr, AkUInt32 &out_uStringSize)

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise