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.

cmSaProc.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "cmPrefix.h"
  4. #include "cmGlobal.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmFloatTypes.h"
  12. #include "cmComplexTypes.h"
  13. #include "cmFileSys.h"
  14. #include "cmJson.h"
  15. #include "cmSymTbl.h"
  16. #include "cmAudioFile.h"
  17. #include "cmText.h"
  18. #include "cmProcObj.h"
  19. #include "cmProcTemplate.h"
  20. #include "cmMath.h"
  21. #include "cmProc.h"
  22. #include "cmSaProc.h"
  23. #include "cmVectOps.h"
  24. #ifdef CM_SONICART
  25. #include "ss0/surroundstereo.h"
  26. #include "ss1/surroundstereo_1.h"
  27. #else
  28. void binauralEncoderProcessInit(long pNumInputs, long frames, int hrtfSet) {}
  29. void binauralEncoderProcessSetPosition(int clientNum, float cazimuth, float celevation) {}
  30. void binauralEncoderProcess(float **inputs, float **outputs, long numInputs, long sampleFrames){}
  31. typedef struct
  32. {
  33. void* h;
  34. } binauralEncoderH_t;
  35. binauralEncoderH_t binauralEncoderNullH = cmSTATIC_NULL_HANDLE;
  36. void binauralEncProcessInit(binauralEncoderH_t* hp, long pNumInputs, long frames, int hrtfSet)
  37. { if(hp!=NULL) hp->h=NULL; }
  38. void binauralEncProcessFree( binauralEncoderH_t* hp ){}
  39. void binauralEncProcessSetPosition(binauralEncoderH_t h, int clientNum, float cazimuth, float celevation){}
  40. void binauralEncProcess(binauralEncoderH_t h, float **inputs, float **outputs, long numInputs, long sampleFrames){}
  41. #endif
  42. cmBinEnc* cmBinEncAlloc( cmCtx* c, cmBinEnc* p, double srate, unsigned procSmpCnt )
  43. {
  44. cmBinEnc* op = cmObjAlloc(cmBinEnc,c,p);
  45. if( srate > 0 )
  46. if( cmBinEncInit(op,srate,procSmpCnt) != cmOkRC )
  47. cmBinEncFree(&op);
  48. return op;
  49. }
  50. cmRC_t cmBinEncFree( cmBinEnc** pp )
  51. {
  52. cmRC_t rc = cmOkRC;
  53. if( pp==NULL || *pp==NULL )
  54. return rc;
  55. cmBinEnc* p = *pp;
  56. if((rc = cmBinEncFinal(p)) != cmOkRC )
  57. return rc;
  58. cmObjFree(pp);
  59. return rc;
  60. }
  61. cmRC_t cmBinEncInit( cmBinEnc* p, double srate, unsigned procSmpCnt )
  62. {
  63. cmRC_t rc;
  64. binauralEncoderH_t h = binauralEncoderNullH;
  65. if((rc = cmBinEncFinal(p)) != cmOkRC )
  66. return rc;
  67. p->srate = srate;
  68. p->procSmpCnt = procSmpCnt;
  69. p->freeFl = false;
  70. long numInputs = 1;
  71. int hrtfSet = 1;
  72. switch(p->mode)
  73. {
  74. case 0:
  75. binauralEncoderProcessInit(numInputs, procSmpCnt, hrtfSet);
  76. break;
  77. case 1:
  78. binauralEncProcessInit(&h,numInputs, procSmpCnt, hrtfSet);
  79. p->freeFl = true;
  80. p->h = h.h;
  81. break;
  82. }
  83. return rc;
  84. }
  85. cmRC_t cmBinEncFinal(cmBinEnc* p )
  86. {
  87. if( p->h != NULL && p->freeFl )
  88. {
  89. binauralEncoderH_t h;
  90. h.h = p->h;
  91. binauralEncProcessFree(&h);
  92. p->freeFl = false;
  93. }
  94. return cmOkRC;
  95. }
  96. cmRC_t cmBinEncSetMode(cmBinEnc* p, unsigned mode )
  97. {
  98. if( mode != p->mode )
  99. {
  100. p->mode = mode;
  101. return cmBinEncInit(p,p->srate,p->procSmpCnt);
  102. }
  103. return cmOkRC;
  104. }
  105. cmRC_t cmBinEncSetLoc( cmBinEnc* p, float azimDegrees, float elevDegrees, float dist )
  106. {
  107. int clientNum = 0;
  108. if( elevDegrees > 90.0f )
  109. elevDegrees = 90.0f;
  110. if( elevDegrees < -40.0f )
  111. elevDegrees = -40.0f;
  112. float elev = (elevDegrees + 40.0f) / (90.0f + 40.0f);
  113. //printf("az:%f el:%f dist:%f\n",azimDegrees,elev,dist);
  114. switch( p->mode )
  115. {
  116. case 0:
  117. binauralEncoderProcessSetPosition(clientNum, azimDegrees/360.0f, elev);
  118. break;
  119. case 1:
  120. {
  121. binauralEncoderH_t h;
  122. h.h = p->h;
  123. binauralEncProcessSetPosition(h,clientNum, azimDegrees/360.0f, elev);
  124. }
  125. break;
  126. }
  127. return cmOkRC;
  128. }
  129. cmRC_t cmBinEncExec( cmBinEnc* p, const cmSample_t* x, cmSample_t* y0, cmSample_t* y1, unsigned xyN )
  130. {
  131. float *inputs[] = {(float*)x};
  132. float *outputs[] = {y0,y1};
  133. int numInputs = sizeof(inputs)/sizeof(inputs[0]);
  134. switch( p->mode )
  135. {
  136. case 0:
  137. binauralEncoderProcess(inputs,outputs,numInputs,xyN);
  138. break;
  139. case 1:
  140. {
  141. binauralEncoderH_t h;
  142. h.h = p->h;
  143. binauralEncProcess(h,inputs,outputs,numInputs,xyN);
  144. }
  145. break;
  146. }
  147. return cmOkRC;
  148. }