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.

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