libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmDspValue.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #ifndef cmDspValue_h
  4. #define cmDspValue_h
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. //( { file_desc:"'snap' variable value class." kw:[snap] }
  9. typedef unsigned cmDsvRC_t;
  10. enum
  11. {
  12. kOkDsvRC = cmOkRC,
  13. kNotMtxDsvRC,
  14. kRetTooSmallDsvRC,
  15. kUnknownTypeDsvRC,
  16. kJsonDeserialFailDsvRC,
  17. kConstViolationDsvRC
  18. };
  19. enum
  20. {
  21. kNullDsvFl = 0x00000000,
  22. kBoolDsvFl = 0x00000001,
  23. kCharDsvFl = 0x00000002,
  24. kUCharDsvFl = 0x00000004,
  25. kShortDsvFl = 0x00000008,
  26. kUShortDsvFl = 0x00000010,
  27. kLongDsvFl = 0x00000020,
  28. kULongDsvFl = 0x00000040,
  29. kIntDsvFl = 0x00000080,
  30. kUIntDsvFl = 0x00000100,
  31. kFloatDsvFl = 0x00000200,
  32. kDoubleDsvFl = 0x00000400,
  33. kSampleDsvFl = 0x00000800,
  34. kRealDsvFl = 0x00001000,
  35. kPtrDsvFl = 0x00002000,
  36. kStrzDsvFl = 0x00004000,
  37. kSymDsvFl = 0x00008000,
  38. kJsonDsvFl = 0x00010000,
  39. kMtxDsvFl = 0x00020000,
  40. kTypeDsvMask = 0x0003ffff,
  41. kProxyDsvFl = 0x00040000,
  42. // Auxilliary Flags
  43. kInDsvFl = 0x00100000, // parameter which is input to the instance (represented by an input port)
  44. kOutDsvFl = 0x00200000, // output value (represented by an output port)
  45. kReqArgDsvFl = 0x00400000, // marks DSP variables which are must be given initial values in cmDspInstAllocate() va_list argument.
  46. kOptArgDsvFl = 0x00800000, // marks DSP variables which may optionally be given initial values
  47. kNoArgDsvFl = 0x01000000, // marks DSP variable which may NOT be initialized from in cmDspInstAllocate() va_list argument.
  48. kAudioBufDsvFl = 0x02000000, // marks DSP variables which represent audio input or outputs
  49. kUiDsvFl = 0x04000000, // marks DSP variables whose values should be echoed to the UI
  50. kDynDsvFl = 0x08000000, // Set if the value was dynamically allocated
  51. kConstDsvFl = 0x10000000, // Set if this value is const.
  52. kDfltSetDsvFl = 0x20000000, // Set if the default variable in a DSP instance variable has a valid value.
  53. kSendDfltDsvFl = 0x40000000 // By default default values are not transmitted - setting this flag causes the default values to be sent.
  54. };
  55. typedef struct
  56. {
  57. unsigned rn;
  58. unsigned cn;
  59. union
  60. {
  61. bool* bp;
  62. char* cp;
  63. unsigned char* ucp;
  64. short* sp;
  65. unsigned short* usp;
  66. long* lp;
  67. unsigned long* ulp;
  68. int* ip;
  69. unsigned* up;
  70. float* fp;
  71. double* dp;
  72. cmSample_t* ap;
  73. cmReal_t* rp;
  74. cmJsonNode_t** jp;
  75. cmChar_t** zp;
  76. const cmChar_t** czp;
  77. void* vp;
  78. } u;
  79. } cmDspMtx_t;
  80. typedef struct cmDspValue_str
  81. {
  82. unsigned flags;
  83. union
  84. {
  85. bool b;
  86. char c;
  87. unsigned char uc;
  88. short s;
  89. unsigned short us;
  90. long l;
  91. unsigned long ul;
  92. int i;
  93. unsigned u;
  94. float f;
  95. double d;
  96. cmSample_t a;
  97. cmReal_t r;
  98. cmJsonNode_t* j;
  99. cmChar_t* z;
  100. const cmChar_t* cz;
  101. cmDspMtx_t m;
  102. struct cmDspValue_str* vp;
  103. } u;
  104. } cmDspValue_t;
  105. extern cmDspValue_t cmDspNullValue;
  106. bool cmDsvIsNull( const cmDspValue_t* vp );
  107. bool cmDsvIsBool( const cmDspValue_t* vp );
  108. bool cmDsvIsChar( const cmDspValue_t* vp );
  109. bool cmDsvIsUChar( const cmDspValue_t* vp );
  110. bool cmDsvIsShort( const cmDspValue_t* vp );
  111. bool cmDsvIsUShort( const cmDspValue_t* vp );
  112. bool cmDsvIsLong( const cmDspValue_t* vp );
  113. bool cmDsvIsULong( const cmDspValue_t* vp );
  114. bool cmDsvIsInt( const cmDspValue_t* vp );
  115. bool cmDsvIsUInt( const cmDspValue_t* vp );
  116. bool cmDsvIsFloat( const cmDspValue_t* vp );
  117. bool cmDsvIsDouble( const cmDspValue_t* vp );
  118. bool cmDsvIsSample( const cmDspValue_t* vp );
  119. bool cmDsvIsReal( const cmDspValue_t* vp );
  120. bool cmDsvIsStrz( const cmDspValue_t* vp );
  121. bool cmDsvIsSymbol( const cmDspValue_t* vp );
  122. bool cmDsvIsJson( const cmDspValue_t* vp );
  123. bool cmDsvIsBoolMtx( const cmDspValue_t* vp );
  124. bool cmDsvIsCharMtx( const cmDspValue_t* vp );
  125. bool cmDsvIsUCharMtx( const cmDspValue_t* vp );
  126. bool cmDsvIsShortMtx( const cmDspValue_t* vp );
  127. bool cmDsvIsUShortMtx( const cmDspValue_t* vp );
  128. bool cmDsvIsLongMtx( const cmDspValue_t* vp );
  129. bool cmDsvIsULongMtx( const cmDspValue_t* vp );
  130. bool cmDsvIsIntMtx( const cmDspValue_t* vp );
  131. bool cmDsvIsUIntMtx( const cmDspValue_t* vp );
  132. bool cmDsvIsFloatMtx( const cmDspValue_t* vp );
  133. bool cmDsvIsDoubleMtx( const cmDspValue_t* vp );
  134. bool cmDsvIsSampleMtx( const cmDspValue_t* vp );
  135. bool cmDsvIsRealMtx( const cmDspValue_t* vp );
  136. bool cmDsvIsJsonMtx( const cmDspValue_t* vp );
  137. bool cmDsvIsStrzMtx( const cmDspValue_t* vp );
  138. // Set scalar types from a va_list. Returns false if 'flags' does not
  139. // identify a scalar type. Note that this function cannot convert
  140. // scalar JSON nodes but can accept scalar Strz's.
  141. bool cmDsvSetFromValist( cmDspValue_t* vp, unsigned flags, va_list vl );
  142. // Return true if the source type can be converted to the dest type.
  143. bool cmDsvCanConvertFlags( unsigned destFlags, unsigned srcFlags );
  144. // Return true if svp can be assigned to dvp.
  145. bool cmDsvCanConvert( const cmDspValue_t* dvp, const cmDspValue_t* svp );
  146. // WARNING: All of the setter functions clear all existing flags
  147. // in the target cmDspValue_t object and then set the type flags
  148. // associated with the RHS value. This means that any auxilliary
  149. // flags will be cleared and therefore must be reset by the calling
  150. // function if they are to be preserved past the call to the setter.
  151. // Assign a scalar value to the cmDspValue_t and setup the type information.
  152. void cmDsvSetNull( cmDspValue_t* vp );
  153. void cmDsvSetBool( cmDspValue_t* vp, bool v );
  154. void cmDsvSetChar( cmDspValue_t* vp, char v );
  155. void cmDsvSetUChar( cmDspValue_t* vp, unsigned char v );
  156. void cmDsvSetShort( cmDspValue_t* vp, short v );
  157. void cmDsvSetUShort( cmDspValue_t* vp, unsigned short v );
  158. void cmDsvSetLong( cmDspValue_t* vp, long v );
  159. void cmDsvSetULong( cmDspValue_t* vp, unsigned long v );
  160. void cmDsvSetInt( cmDspValue_t* vp, int v );
  161. void cmDsvSetUInt( cmDspValue_t* vp, unsigned int v );
  162. void cmDsvSetFloat( cmDspValue_t* vp, float v );
  163. void cmDsvSetDouble( cmDspValue_t* vp, double v );
  164. void cmDsvSetSample( cmDspValue_t* vp, cmSample_t v );
  165. void cmDsvSetReal( cmDspValue_t* vp, cmReal_t v );
  166. void cmDsvSetPtr( cmDspValue_t* vp, void* v );
  167. void cmDsvSetSymbol( cmDspValue_t* vp, unsigned int v );
  168. void cmDsvSetStrz( cmDspValue_t* vp, cmChar_t* v );
  169. void cmDsvSetStrcz( cmDspValue_t* vp, const cmChar_t* v );
  170. void cmDsvSetJson( cmDspValue_t* vp, cmJsonNode_t* v);
  171. // Assign a matrix to the cmDspValue_t.
  172. void cmDsvSetBoolMtx( cmDspValue_t* vp, bool* v, unsigned rn, unsigned cn );
  173. void cmDsvSetCharMtx( cmDspValue_t* vp, char* v, unsigned rn, unsigned cn );
  174. void cmDsvSetUCharMtx( cmDspValue_t* vp, unsigned char* v, unsigned rn, unsigned cn );
  175. void cmDsvSetShortMtx( cmDspValue_t* vp, short* v, unsigned rn, unsigned cn );
  176. void cmDsvSetUShortMtx( cmDspValue_t* vp, unsigned short* v, unsigned rn, unsigned cn );
  177. void cmDsvSetLongMtx( cmDspValue_t* vp, long* v, unsigned rn, unsigned cn );
  178. void cmDsvSetULongMtx( cmDspValue_t* vp, unsigned long* v, unsigned rn, unsigned cn );
  179. void cmDsvSetIntMtx( cmDspValue_t* vp, int* v, unsigned rn, unsigned cn );
  180. void cmDsvSetUIntMtx( cmDspValue_t* vp, unsigned int* v, unsigned rn, unsigned cn );
  181. void cmDsvSetFloatMtx( cmDspValue_t* vp, float* v, unsigned rn, unsigned cn );
  182. void cmDsvSetDoubleMtx( cmDspValue_t* vp, double* v, unsigned rn, unsigned cn );
  183. void cmDsvSetSampleMtx( cmDspValue_t* vp, cmSample_t* v, unsigned rn, unsigned cn );
  184. void cmDsvSetRealMtx( cmDspValue_t* vp, cmReal_t* v, unsigned rn, unsigned cn );
  185. void cmDsvSetJsonMtx( cmDspValue_t* vp, cmJsonNode_t** v, unsigned rn, unsigned cn );
  186. void cmDsvSetStrzMtx( cmDspValue_t* vp, cmChar_t** v, unsigned rn, unsigned cn );
  187. void cmDsvSetStrczMtx( cmDspValue_t* vp, const cmChar_t** v, unsigned rn, unsigned cn );
  188. void cmDsvSetMtx( cmDspValue_t* vp, unsigned flags, void* v, unsigned rn, unsigned cn );
  189. void cmDsvSetProxy( cmDspValue_t* vp, cmDspValue_t* pp );
  190. // Get the value of a char cmDspValue_t.
  191. // Returns 0 if the cmDspValue_t does not exactly match the return type.
  192. bool cmDsvBool( const cmDspValue_t* vp );
  193. char cmDsvChar( const cmDspValue_t* vp );
  194. unsigned char cmDsvUChar( const cmDspValue_t* vp );
  195. short cmDsvShort( const cmDspValue_t* vp );
  196. unsigned short cmDsvUShort( const cmDspValue_t* vp );
  197. long cmDsvLong( const cmDspValue_t* vp );
  198. unsigned long cmDsvULong( const cmDspValue_t* vp );
  199. int cmDsvInt( const cmDspValue_t* vp );
  200. unsigned int cmDsvUInt( const cmDspValue_t* vp );
  201. float cmDsvFloat( const cmDspValue_t* vp );
  202. double cmDsvDouble( const cmDspValue_t* vp );
  203. cmSample_t cmDsvSample( const cmDspValue_t* vp );
  204. cmReal_t cmDsvReal( const cmDspValue_t* vp );
  205. void* cmDsvPtr( const cmDspValue_t* vp );
  206. unsigned int cmDsvSymbol( const cmDspValue_t* vp );
  207. cmChar_t* cmDsvStrz( const cmDspValue_t* vp );
  208. const cmChar_t*cmDsvStrcz( const cmDspValue_t* vp );
  209. cmJsonNode_t* cmDsvJson( const cmDspValue_t* vp );
  210. const cmDspValue_t* cmDsvValueCPtr( const cmDspValue_t* vp );
  211. cmDspValue_t* cmDsvValuePtr( cmDspValue_t* vp );
  212. // Return a pointer to the base of a matrix.
  213. // Returns NULL if the cmDspValue_t is does not exactly match the return type.
  214. const bool* cmDsvBoolCMtx( const cmDspValue_t* vp );
  215. const char* cmDsvCharCMtx( const cmDspValue_t* vp );
  216. const unsigned char* cmDsvUCharCMtx( const cmDspValue_t* vp );
  217. const short* cmDsvShortCMtx( const cmDspValue_t* vp );
  218. const unsigned short* cmDsvUShortCMtx( const cmDspValue_t* vp );
  219. const long* cmDsvLongCMtx( const cmDspValue_t* vp );
  220. const unsigned long* cmDsvULongCMtx( const cmDspValue_t* vp );
  221. const int* cmDsvIntCMtx( const cmDspValue_t* vp );
  222. const unsigned int* cmDsvUIntCMtx( const cmDspValue_t* vp );
  223. const float* cmDsvFloatCMtx( const cmDspValue_t* vp );
  224. const double* cmDsvDoubleCMtx( const cmDspValue_t* vp );
  225. const cmSample_t* cmDsvSampleCMtx( const cmDspValue_t* vp );
  226. const cmReal_t* cmDsvRealCMtx( const cmDspValue_t* vp );
  227. const cmChar_t** cmDsvStrzCMtx( const cmDspValue_t* vp );
  228. const cmChar_t** cmDsvStrczCMtx( const cmDspValue_t* vp );
  229. bool* cmDsvBoolMtx( cmDspValue_t* vp );
  230. char* cmDsvCharMtx( cmDspValue_t* vp );
  231. unsigned char* cmDsvUCharMtx( cmDspValue_t* vp );
  232. short* cmDsvShortMtx( cmDspValue_t* vp );
  233. unsigned short* cmDsvUShortMtx( cmDspValue_t* vp );
  234. long* cmDsvLongMtx( cmDspValue_t* vp );
  235. unsigned long* cmDsvULongMtx( cmDspValue_t* vp );
  236. int* cmDsvIntMtx( cmDspValue_t* vp );
  237. unsigned int* cmDsvUIntMtx( cmDspValue_t* vp );
  238. float* cmDsvFloatMtx( cmDspValue_t* vp );
  239. double* cmDsvDoubleMtx( cmDspValue_t* vp );
  240. cmSample_t* cmDsvSampleMtx( cmDspValue_t* vp );
  241. cmReal_t* cmDsvRealMtx( cmDspValue_t* vp );
  242. cmJsonNode_t** cmDsvJsonMtx( cmDspValue_t* vp );
  243. cmChar_t** cmDsvStrzMtx( cmDspValue_t* vp );
  244. const cmChar_t**cmDsvStrczMtx( cmDspValue_t* vp );
  245. // Get the value of a cmDspValue_t.
  246. // Conversion is performed if the return type does not exactly match the cmDspValue_t type.
  247. bool cmDsvGetBool( const cmDspValue_t* vp );
  248. char cmDsvGetChar( const cmDspValue_t* vp );
  249. unsigned char cmDsvGetUChar( const cmDspValue_t* vp );
  250. short cmDsvGetShort( const cmDspValue_t* vp );
  251. unsigned short cmDsvGetUShort( const cmDspValue_t* vp );
  252. long cmDsvGetLong( const cmDspValue_t* vp );
  253. unsigned long cmDsvGetULong( const cmDspValue_t* vp );
  254. int cmDsvGetInt( const cmDspValue_t* vp );
  255. unsigned int cmDsvGetUInt( const cmDspValue_t* vp );
  256. float cmDsvGetFloat( const cmDspValue_t* vp );
  257. double cmDsvGetDouble( const cmDspValue_t* vp );
  258. cmSample_t cmDsvGetSample( const cmDspValue_t* vp );
  259. cmReal_t cmDsvGetReal( const cmDspValue_t* vp );
  260. void* cmDsvGetPtr( const cmDspValue_t* vp );
  261. unsigned cmDsvGetSymbol( const cmDspValue_t* vp );
  262. cmChar_t* cmDsvGetStrz( const cmDspValue_t* vp );
  263. const cmChar_t*cmDsvGetStrcz( const cmDspValue_t* vp );
  264. cmJsonNode_t* cmDsvGetJson( const cmDspValue_t* vp );
  265. cmDsvRC_t cmDsvGetBoolMtx( cmDspValue_t* vp, bool* v, unsigned vn );
  266. cmDsvRC_t cmDsvGetCharMtx( cmDspValue_t* vp, char* v, unsigned vn );
  267. cmDsvRC_t cmDsvGetUCharMtx( cmDspValue_t* vp, unsigned char* v, unsigned vn );
  268. cmDsvRC_t cmDsvGetShortMtx( cmDspValue_t* vp, short* v, unsigned vn );
  269. cmDsvRC_t cmDsvGetUShortMtx( cmDspValue_t* vp, unsigned short* v, unsigned vn );
  270. cmDsvRC_t cmDsvGetLongMtx( cmDspValue_t* vp, long* v, unsigned vn );
  271. cmDsvRC_t cmDsvGetULongMtx( cmDspValue_t* vp, unsigned long* v, unsigned vn );
  272. cmDsvRC_t cmDsvGetIntMtx( cmDspValue_t* vp, int* v, unsigned vn );
  273. cmDsvRC_t cmDsvGetUIntMtx( cmDspValue_t* vp, unsigned int* v, unsigned vn );
  274. cmDsvRC_t cmDsvGetFloatMtx( cmDspValue_t* vp, float* v, unsigned vn );
  275. cmDsvRC_t cmDsvGetDoubleMtx( cmDspValue_t* vp, double* v, unsigned vn );
  276. cmDsvRC_t cmDsvGetSampleMtx( cmDspValue_t* vp, cmSample_t* v, unsigned vn );
  277. cmDsvRC_t cmDsvGetRealMtx( cmDspValue_t* vp, cmReal_t* v, unsigned vn );
  278. cmDsvRC_t cmDsvGetStrzMtx( cmDspValue_t* vp, cmChar_t* v, unsigned vn );
  279. bool cmDsvIsType( const cmDspValue_t* vp, unsigned flags );
  280. bool cmDsvIsMtx( const cmDspValue_t* vp );
  281. unsigned cmDsvEleCount( const cmDspValue_t* vp );
  282. unsigned cmDsvRows( const cmDspValue_t* vp );
  283. unsigned cmDsvCols( const cmDspValue_t* vp );
  284. // Basic type flag with kMtxDsvFl and any other flags cleared.
  285. unsigned cmDsvBasicType( const cmDspValue_t* vp );
  286. // Size of each data element in bytes.
  287. unsigned cmDsvEleByteCount( const cmDspValue_t* vp );
  288. // Count of data bytes given a matrix size. Note that 'rn' and 'cn' are
  289. // not used if the kMtxDsvFl is not set in flags.
  290. unsigned cmDsvByteCount( unsigned flags, unsigned rn, unsigned cn );
  291. // Return the number of bytes required to serialize the value.
  292. unsigned cmDsvSerialByteCount( const cmDspValue_t* vp );
  293. // Returns the number of bytes required to serialize the data alone not including
  294. // the cmDspValue_t record. This is just cmDsvSerialByteCount(vp) - sizeof(cmDspValue_t)
  295. unsigned cmDsvSerialDataByteCount( const cmDspValue_t* vp );
  296. cmDsvRC_t cmDsvSerialize( const cmDspValue_t* vp, void* buf, unsigned bufByteCnt );
  297. cmDsvRC_t cmDsvDeserializeInPlace( cmDspValue_t* vp, unsigned dataBufByteCnt );
  298. cmDsvRC_t cmDsvDeserializeJson( cmDspValue_t* vp, cmJsonH_t jsH );
  299. void cmDsvPrint( const cmDspValue_t* vp, const cmChar_t* label, cmRpt_t* rpt );
  300. void cmDsvToString( const cmDspValue_t* vp, const cmChar_t* label, cmChar_t* s, unsigned sN );
  301. //)
  302. #define cmDsvCopy( d, s ) (*(d)) = (*(s))
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306. #endif