00001
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <sent/stddefs.h>
00040 #include <sent/adin.h>
00041
00048 void
00049 init_count_zc_e(ZEROCROSS *zc, int length)
00050 {
00051
00052 zc->data = (SP16 *)mymalloc(length * sizeof(SP16));
00053
00054 zc->is_zc = (int *)mymalloc(length * sizeof(int));
00055
00056 zc->length = length;
00057 }
00058
00067 void
00068 reset_count_zc_e(ZEROCROSS *zc, int c_trigger, int c_length, int c_offset)
00069 {
00070 int i;
00071
00072 if (zc->length != c_length) {
00073 jlog("Warning: zerocross buffer length changed, re-allocate it\n");
00074 free_count_zc_e(zc);
00075 init_count_zc_e(zc, c_length);
00076 }
00077
00078 zc->trigger = c_trigger;
00079 zc->offset = c_offset;
00080
00081 zc->zero_cross = 0;
00082 zc->is_trig = FALSE;
00083 zc->sign = ZC_POSITIVE;
00084 zc->top = 0;
00085 zc->valid_len = 0;
00086
00087 for (i=0; i<c_length; i++){
00088 zc->is_zc[i] = ZC_UNDEF;
00089 }
00090 }
00091
00097 void
00098 free_count_zc_e(ZEROCROSS *zc)
00099 {
00100 free(zc->is_zc);
00101 free(zc->data);
00102 }
00103
00115 int
00116 count_zc_e(ZEROCROSS *zc, SP16 *buf, int step)
00117 {
00118 int i;
00119 SP16 tmp, level;
00120
00121 level = 0;
00122 for (i=0; i<step; i++) {
00123 if (zc->is_zc[zc->top] == TRUE) {
00124 zc->zero_cross--;
00125 }
00126 zc->is_zc[zc->top] = FALSE;
00127
00128 tmp = buf[i] + zc->offset;
00129 if (zc->is_trig) {
00130 if (zc->sign == ZC_POSITIVE && tmp < 0) {
00131 zc->zero_cross++;
00132 zc->is_zc[zc->top] = TRUE;
00133 zc->is_trig = FALSE;
00134 zc->sign = ZC_NEGATIVE;
00135 } else if (zc->sign == ZC_NEGATIVE && tmp > 0) {
00136 zc->zero_cross++;
00137 zc->is_zc[zc->top] = TRUE;
00138 zc->is_trig = FALSE;
00139 zc->sign = ZC_POSITIVE;
00140 }
00141 }
00142 if (abs(tmp) > zc->trigger) {
00143 zc->is_trig = TRUE;
00144 }
00145 if (abs(tmp) > level) level = abs(tmp);
00146 zc->data[zc->top] = buf[i];
00147 zc->top++;
00148 if (zc->valid_len < zc->top) zc->valid_len = zc->top;
00149 if (zc->top >= zc->length) {
00150 zc->top = 0;
00151 }
00152 }
00153 zc->level = (int)level;
00154 return (zc->zero_cross);
00155 }
00156
00164 void
00165 zc_copy_buffer(ZEROCROSS *zc, SP16 *newbuf, int *len)
00166 {
00167 int i, t;
00168 if (zc->valid_len < zc->length) {
00169 t = 0;
00170 } else {
00171 t = zc->top;
00172 }
00173 for(i=0;i<zc->valid_len;i++) {
00174 newbuf[i] = zc->data[t];
00175 if (++t == zc->length) t = 0;
00176 }
00177 *len = zc->valid_len;
00178 }