Add demo source code
[processor-sdk/performance-audio-sr.git] / procsdk_audio_x_xx_xx_xx / common / dbgDib.c
1 #include <string.h> // for memcpy
2 #include <xdc/std.h>
3 #include <xdc/runtime/Log.h>
6 #include "common.h"
8 #ifndef _TMS320C6X
9 #include "c67x_cintrins.h"
10 #endif
12 #include "inpbuf.h"
13 #include "dbgDib.h"
15 // sinusoid generator parameters
16 /* Performs floating-point to 24-bit fixed point conversion.
17 Resulting fixed-point value is left-justified in 32-bit word. */
18 #define F2INT_SCALE     (float)0x7FFFFF
19 #define F2INT_ROUND(x)  _spint(x)
20 #define F2INT(x)        (((Int32)F2INT_ROUND(F2INT_SCALE * x) << 0x8) & 0xFFFFFF00)
22 #define TWO_PI          (6.283185307179586476925286766559L)
23 #define FS_48KHZ        (48000.0)
24 #define TWOPIOVERSRATE  ( TWO_PI / FS_48KHZ )
26 #define SINP_MAX_CHS    ( 8 )       // sin probe maximum number of channels
27 Int8 gSinPNumChs = SINP_MAX_CHS;    // sin probe number of channels 
28 Int8 gSinPChIdx = 0;                // sin probe channel index
29 // sinusoid data generated on these DIB channels
30 Int8 gSinPCh[SINP_MAX_CHS] = {0,1,2,3,4,5,6,7};
32 #define SINP_MAX_GEN ( 2 )      // sin probe maximum number of generators
33 // Configurable from CCS
34 Int8 gSinPNumGen = SINP_MAX_GEN;
35 float gSineProbeAmp[SINP_MAX_GEN]   = {0.0625, 0.125};  // sinusoid amplitudes
36 float gSineProbeFreq[SINP_MAX_GEN]  = {440.0, 1004.0};  // sinusoid frequencies (Hz)
38 static double gSineProbeArg[SINP_MAX_GEN] = {0.0, 0.0};  // sinusoid function arguments
40 #ifdef CAP_IB_PCM
41 // IB capture (PCM) buffer
42 #ifdef _TMS320C6X
43 #pragma DATA_SECTION(gCapIbPcmBuf, ".gCapIbPcmBuf");
44 Int32 gCapIbPcmBuf[CAP_IB_PCM_MAX_NUM_CH][CAP_IB_PCM_MAX_NUM_SAMP];
45 #else
46 Int32 gCapIbPcmBuf[CAP_IB_PCM_MAX_NUM_CH][CAP_IB_PCM_MAX_NUM_SAMP] __attribute__ ((section(".gCapIbPcmBuf")));
47 #endif
48 Int32 gCapIbPcmBufIdx=0;
49 Int32 gCapIbPcmBufWrapCnt=0;
50 static UInt32 capIbPcmStopCnt=5000;
51 #endif // CAP_IB_PCM
53 #ifdef CAP_IP
54 // IB capture buffer
55 #ifdef _TMS320C6X
56 #pragma DATA_SECTION(gCapIbBuf, ".gCapIbBuf");
57 Int8 gCapIbBuf[2][CAP_IB_BUF_SZ];
58 #else
59 Int8 gCapIbBuf[2][CAP_IB_BUF_SZ] __attribute__ ((section(".gCapIbBuf")));
60 //Int32 gCapIbBuf[CAP_IB_BUF_SZ] __attribute__ ((section(".noinit")));
61 #endif
62 Int32 gCapIbBufIdx[2]={0,0};
63 Int32 gCapIbBufWrapCnt[2]={0,0};
64 Int8 gCapIbBufPingPongSel=1;
65 Int32 gNumDiffFrame[2]={0,0};
67 #endif // CAP_IP
69 // Generate sinusoids in IB buffer
70 Void genSinIb(
71     PAF_InpBufConfig *pInpBufConfig
72 )
73 {
74     Int8 numCh;
75     Int16 numSamp;
76     Int8 genIdx;
77     double phaseInc, arg, amp;
78     Int32 *pCh;
79     Int16 i;
80     
81     numCh = pInpBufConfig->stride; // get number of channels
82     numSamp = pInpBufConfig->frameLength / numCh; // get number of samples to generate
84     for (genIdx=0; genIdx<gSinPNumGen; genIdx++)
85     {
86         // compute generator phase increment
87         phaseInc = (double)gSineProbeFreq[genIdx] * TWOPIOVERSRATE;
88         
89         arg = gSineProbeArg[genIdx]; // get generator arg
90         amp = gSineProbeAmp[genIdx]; // get generator amplitude
91         
92         // generate sinusoid on selected channel
93         pCh = &pInpBufConfig->pntr.pLgInt[gSinPCh[gSinPChIdx]];
94         for (i=0; i<numSamp; i++)
95         {
96             *pCh = F2INT(amp * sin(arg));
97             arg += phaseInc;
98             pCh += numCh; // skipped interleaved channels
99         }
100         
101         gSineProbeArg[genIdx] = arg; // save generator arg
102         
103         // update sin probe channel index
104         gSinPChIdx++;
105         if (gSinPChIdx >= gSinPNumChs)
106         {
107             gSinPChIdx = 0;
108         }
109     }
112 #ifdef CAP_IB_PCM
113 // Capture data in IB buffer to memory
114 Void capIbPcm(
115     PAF_InpBufConfig *pInpBufConfig
118     Int8 numCh;
119     Int16 numSamp;
120     Int8 sampSz;
121     Int32 samp;
122     Int8 *pCh;
123     Int16 i, j, k;
124     Int32 *pCapBuf;
126     if (--capIbPcmStopCnt == 0)
127     {
128         SW_BREAKPOINT;
129     }
131     numCh = pInpBufConfig->stride; // get number of channels
132     numSamp = pInpBufConfig->frameLength / numCh; // get number of samples to capture
133     sampSz = pInpBufConfig->sizeofElement; // get sample size (bytes)
134     
135     if ((CAP_IB_PCM_MAX_NUM_SAMP - gCapIbPcmBufIdx) < numSamp)
136     {
137         //return;
138         gCapIbPcmBufIdx = 0;
139         gCapIbPcmBufWrapCnt++;
140     }
141         
142     for (i=0; i<numCh; i++)
143     {
144         pCapBuf = &gCapIbPcmBuf[i][gCapIbPcmBufIdx];
145         pCh = &pInpBufConfig->pntr.pSmInt[i*sampSz];
146         for (j=0; j<numSamp; j++)
147         {
148             samp = (Int32)(*(pCh+sampSz-1));
149             for (k=sampSz-2; k>=0; k--)
150             {
151                 samp <<= 8;
152                 samp |= (UInt8)(*(pCh+k));
153             }
154             samp <<= 32-8*sampSz;
155             
156             *pCapBuf = samp;
157             pCapBuf++;
158             pCh += numCh * sampSz;
159         }
160     }
161     gCapIbPcmBufIdx += numSamp;
163 #endif // CAP_IB_PCM
165 #ifdef CAP_IP
166 // Reset IB capture buffer
167 Int capIbReset(Void)
169     gCapIbBufPingPongSel ^= 0x1;
170     gCapIbBufIdx[gCapIbBufPingPongSel] = 0;
171     gCapIbBufWrapCnt[gCapIbBufPingPongSel] = 0;
172     gNumDiffFrame[gCapIbBufPingPongSel] = 0;
174     return 0;
177 // Capture data in IB buffer to memory
178 Void capIb(
179     PAF_InpBufConfig *pInpBufConfig
182     UInt32 nBytes;
183     
184     nBytes = pInpBufConfig->frameLength * pInpBufConfig->sizeofElement;
185     
186     // FL: DDP debug
187     if (nBytes != 24576)
188     {
189         Log_info1("capIb(): nBytes=%d", nBytes);
190         gNumDiffFrame[gCapIbBufPingPongSel]++;
191     }
192     
193     if ((CAP_IB_BUF_SZ - gCapIbBufIdx[gCapIbBufPingPongSel]) < nBytes)
194     {
195         //return; // fixed buffer
196         gCapIbBufIdx[gCapIbBufPingPongSel] = 0;
197         gCapIbBufWrapCnt[gCapIbBufPingPongSel]++;
198     }
200     memcpy(&gCapIbBuf[gCapIbBufPingPongSel][gCapIbBufIdx[gCapIbBufPingPongSel]], pInpBufConfig->pntr.pSmInt, nBytes);
201     gCapIbBufIdx[gCapIbBufPingPongSel] += nBytes;
204 #endif // CAP_IP