Version
menu_open
link
Wwise SDK 2023.1.4
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  static_assert(std::is_trivially_copyable<T>::value, "Unaligned operations require being trivially copiable");
59 
60  T result;
61  AKPLATFORM::AkMemCpy(&result, in_pVal, sizeof(T));
62  return result;
63  }
64 
65  template<
66  typename T,
67  typename std::enable_if<std::is_fundamental<T>::value || std::is_enum<T>::value, bool>::type = true
68  >
69  inline void WriteUnaligned(AkUInt8* out_pVal, const T in_val)
70  {
71 #if defined(__GNUC__)
72  #if defined(__has_warning)
73  #if __has_warning("-Walign-mismatch")
74  #define __IGNORE_RECENT_CLANG_WARNINGS
75  #pragma clang diagnostic push
76  #pragma clang diagnostic ignored "-Walign-mismatch"
77  #endif
78  #endif
79 
80  typedef T __attribute__((aligned(1))) UnalignedT;
81  *reinterpret_cast<UnalignedT *>(out_pVal) = in_val;
82 
83  #ifdef __IGNORE_RECENT_CLANG_WARNINGS
84  #undef __IGNORE_RECENT_CLANG_WARNINGS
85  #pragma clang diagnostic pop
86  #endif
87 #elif defined(_MSC_VER) && !defined(AK_CPU_X86)
88  *reinterpret_cast<T __unaligned *>(out_pVal) = in_val; // __unaligned not supported on 32-bit x86
89 #else
90  *reinterpret_cast<T *>(out_pVal) = in_val;
91 #endif
92  }
93 
94  template<
95  typename T,
96  typename std::enable_if<std::is_class<T>::value, bool>::type = true
97  >
98  inline void WriteUnaligned(AkUInt8* out_pVal, const T& in_val)
99  {
100  static_assert(std::is_trivially_copyable<T>::value, "Unaligned operations require being trivially copiable");
101  AKPLATFORM::AkMemCpy(out_pVal, &in_val, sizeof(T));
102  }
103 
104  /// Read data from bank and advance pointer.
105  template< typename T >
106  inline T ReadBankData(
107  AkUInt8*& in_rptr
108 #ifdef _DEBUG
109  , AkUInt32& in_rSize
110 #endif
111  )
112  {
113  T l_Value = ReadUnaligned<T>(in_rptr);
114 
115  in_rptr += sizeof(T);
116 #ifdef _DEBUG
117  in_rSize -= sizeof(T);
118 #endif
119  return l_Value;
120  }
121 
122  template< typename T >
124  AkUInt8*& in_rptr
125 #ifdef _DEBUG
126  , AkUInt32& in_rSize
127 #endif
128  )
129  {
130  AkUInt32 l_Value = 0;
131 
132  AkUInt8 currentByte = *in_rptr;
133  ++in_rptr;
134 #ifdef _DEBUG
135  --in_rSize;
136 #endif
137  l_Value = (currentByte & 0x7F);
138  while (0x80 & currentByte)
139  {
140  currentByte = *in_rptr;
141  ++in_rptr;
142 #ifdef _DEBUG
143  --in_rSize;
144 #endif
145  l_Value = l_Value << 7;
146  l_Value |= (currentByte & 0x7F);
147  }
148 
149  return (T)l_Value;
150  }
151 
152  inline char * ReadBankStringUtf8(
153  AkUInt8*& in_rptr
154 #ifdef _DEBUG
155  , AkUInt32& in_rSize
156 #endif
157  , AkUInt32& out_uStringSize)
158  {
159  char* pString = (char *)in_rptr;
160  out_uStringSize = 0;
161 
162  while (*in_rptr != 0)
163  {
164  ++in_rptr;
165 #ifdef _DEBUG
166  --in_rSize;
167 #endif
168  ++out_uStringSize;
169  }
170 
171  ++in_rptr;
172 #ifdef _DEBUG
173  --in_rSize;
174 #endif
175 
176  return pString;
177  }
178 }
179 
180 
181 #ifdef _DEBUG
182 
183 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
184 #define READBANKDATA( _Type, _Ptr, _Size ) \
185  AK::ReadBankData<_Type>( _Ptr, _Size )
186 
187 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
188  AK::ReadVariableSizeBankData<_Type>( _Ptr, _Size )
189 
190 /// Read and return a null-terminated UTF-8 string, written with the DataWriter of \ref AK::Wwise::Plugin::CustomData::GetPluginData or
191 /// \ref AK::Wwise::Plugin::AudioPlugin::GetBankParameters, and stored in bank, and its size.
192 /// \ref wwiseplugin_bank
193 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
194  AK::ReadBankStringUtf8( _Ptr, _Size, _out_StringSize )
195 
196 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
197 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
198  ( _Ptr ) += sizeof( _Type ); \
199  ( _Size ) -= sizeof( _Type )
200 
201 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
202 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
203  ( _Size ) -= _NumBytes; \
204  ( _Ptr ) += _NumBytes
205 
206 /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
207 #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
208  AKPLATFORM::SafeStrCpy( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
209  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
210 
211 /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
212 #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxOutPtrSize ) \
213  AK_UTF8_TO_OSCHAR( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
214  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
215 
216 /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
217 #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxOutPtrSize ) \
218  AK_CHAR_TO_UTF16( _OutPtr, ((const char*)_Ptr), (_MaxOutPtrSize) ); \
219  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr) + 1, _Ptr, _Size )
220 
221 
222 #else
223 
224 /// Read and return bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
225 #define READBANKDATA( _Type, _Ptr, _Size ) \
226  AK::ReadBankData<_Type>( _Ptr )
227 
228 #define READVARIABLESIZEBANKDATA( _Type, _Ptr, _Size ) \
229  AK::ReadVariableSizeBankData<_Type>( _Ptr )
230 
231 #define READBANKSTRING( _Ptr, _Size, _out_StringSize ) \
232  AK::ReadBankStringUtf8( _Ptr, _out_StringSize )
233 
234 /// Skip over some bank data of a given type, incrementing running pointer and decrementing block size for debug tracking purposes
235 #define SKIPBANKDATA( _Type, _Ptr, _Size ) \
236  ( _Ptr ) += sizeof( _Type )
237 
238 /// Skip over some bank data by a given size in bytes, incrementing running pointer and decrementing block size for debug tracking purposes
239 #define SKIPBANKBYTES( _NumBytes, _Ptr, _Size ) \
240  ( _Ptr ) += _NumBytes;
241 
242 /// Read and copy to a null-terminated UTF-8 string conversion from string stored in bank.
243 #define COPYBANKSTRING_CHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
244  AKPLATFORM::SafeStrCpy( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
245  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
246 
247 /// Read and copy to a null-terminated OSChar string conversion from string stored in bank.
248 #define COPYBANKSTRING_OSCHAR( _Ptr, _Size, _OutPtr, _MaxPtrSize ) \
249  AK_UTF8_TO_OSCHAR( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
250  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
251 
252 /// Read and copy to a null-terminated wchar_t string conversion from string stored in bank.
253 #define COPYBANKSTRING_WCHAR( _Ptr, _Size, OutPtr, _MaxPtrSize ) \
254  AK_CHAR_TO_UTF16( _OutPtr, (const char*)_Ptr, _MaxPtrSize ); \
255  SKIPBANKBYTES( (AkUInt32)strlen((const char*)_Ptr)+1, _Ptr, _Size )
256 
257 #endif
258 
259 #define GETBANKDATABIT( _Data, _Shift ) \
260  (((_Data) >> (_Shift)) & 0x1)
261 
262 /// Helper macro to determine whether the full content of a block of memory was properly parsed
263 #ifdef _DEBUG
264  #define CHECKBANKDATASIZE( _DATASIZE_, _ERESULT_ ) AKASSERT( _DATASIZE_ == 0 || _ERESULT_ != AK_Success );
265 #else
266  #define CHECKBANKDATASIZE(_DATASIZE_, _ERESULT_ )
267 #endif
268 
269 #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)
T ReadUnaligned(const AkUInt8 *in_pVal)
AkForceInline void AkMemCpy(void *pDest, const void *pSrc, AkUInt32 uSize)
Platform Independent Helper for memcpy/memmove/memset.
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