1 /* RMS Filter
2 
3  * Copyright (C) 1998 J.A. Bezemer
4  *
5  * Licensed under the terms of the GNU General Public License.
6  * ABSOLUTELY NO WARRANTY.
7  * See the file `COPYING' in this directory.
8  */
9 
10 #include "signpr_rms.h"
11 #include "signpr_general.h"
12 #ifndef SWIG
13 #include "errorwindow.h"
14 #include "stringinput.h"
15 #include "buttons.h"
16 #include "clrscr.h"
17 #include "boxes.h"
18 #include "helpline.h"
19 #endif
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <math.h>
24 #ifndef SWIG
25 #ifndef OLD_CURSES
26 #include <ncurses.h>
27 #else
28 #include <curses.h>
29 #endif
30 #endif
31 
32 
33 void
rms_param_defaults(parampointer_t parampointer)34 rms_param_defaults (parampointer_t parampointer)
35 {
36   parampointer->postlength1 = 1;
37   parampointer->prelength1 = 1;
38 }
39 
40 #ifndef SWIG
41 void
rms_param_screen(parampointer_t parampointer)42 rms_param_screen (parampointer_t parampointer)
43 {
44   stringinput_t rmslengthstr;
45   button_t ok_button, cancel_button;
46   int dont_stop = TRUE;
47   int returnval = 0;
48   int focus = 0;
49   int in_ch;
50   int i;
51   long helplong;
52 
53   char *helplines[3] =
54   {
55     "                                                                               ",
56     " Discard changes.                                                              ",
57     " Accept changes.                                                               "};
58 
59   rmslengthstr.maxlen = 500;
60   rmslengthstr.string = (char *) malloc (rmslengthstr.maxlen *
61 					 sizeof (char));
62   sprintf (rmslengthstr.string, "%ld", parampointer->prelength1 +
63 	   parampointer->postlength1 + 1);
64   rmslengthstr.y = 6;
65   rmslengthstr.x = 40;
66   rmslengthstr.w = 15;
67   rmslengthstr.cursorpos = strlen (rmslengthstr.string);
68   rmslengthstr.firstcharonscreen = 0;
69 
70   ok_button.text = " OK ";
71   ok_button.y = 20;
72   ok_button.x = 71;
73   ok_button.selected = FALSE;
74 
75   cancel_button.text = " Cancel ";
76   cancel_button.y = 20;
77   cancel_button.x = 5;
78   cancel_button.selected = FALSE;
79 
80   clearscreen (SIGNPR_RMS_PARAMSCR_HEADERTEXT);
81 
82   do
83     {
84       header (SIGNPR_RMS_PARAMSCR_HEADERTEXT);
85 
86       if (focus == 1)
87 	cancel_button.selected = TRUE;
88       else
89 	cancel_button.selected = FALSE;
90 
91       if (focus == 2)
92 	ok_button.selected = TRUE;
93       else
94 	ok_button.selected = FALSE;
95 
96 /*      mvprintw (3, 2,
97    "See the Signproc.txt file for the meaning of the parameters.");
98  */
99       stringinput_display (&rmslengthstr);
100       mvprintw (rmslengthstr.y, 2,
101 		"Number of samples to compute RMS of:");
102 
103       button_display (&cancel_button);
104       mybox (cancel_button.y - 1, cancel_button.x - 1,
105 	     3, strlen (cancel_button.text) + 2);
106       button_display (&ok_button);
107       mybox (ok_button.y - 1, ok_button.x - 1,
108 	     3, strlen (ok_button.text) + 2);
109 
110       helpline (helplines[focus]);
111 
112       if (focus == 0)
113 	stringinput_display (&rmslengthstr);
114       else
115 	move (0, 79);
116 
117       refresh ();
118 
119       in_ch = getch ();
120 
121       switch (focus)
122 	{
123 	case 0:		/* rmslengthstr */
124 	  stringinput_stdkeys (in_ch, &rmslengthstr);
125 	  if (in_ch == KEY_ENTER || in_ch == 13)
126 	    {
127 	      i = sscanf (rmslengthstr.string, "%li", &helplong);
128 	      if (i < 1 || helplong < 1 || helplong % 2 == 0)
129 		error_window ("A whole, odd number, greater than 0, must \
130 be specified.");
131 	      else
132 		focus = 2;
133 	    }
134 	  else
135 	    switch (in_ch)
136 	      {
137 	      case KEY_UP:
138 		focus--;
139 		break;
140 	      case KEY_DOWN:
141 		focus++;
142 		break;
143 	      }
144 	  break;
145 
146 	case 1:		/* Cancel */
147 	  if (in_ch == KEY_ENTER || in_ch == 13)
148 	    {
149 	      returnval = 0;
150 	      dont_stop = FALSE;
151 	    }
152 	  else
153 	    switch (in_ch)
154 	      {
155 	      case KEY_LEFT:
156 	      case KEY_UP:
157 		focus--;
158 		break;
159 	      case KEY_RIGHT:
160 	      case KEY_DOWN:
161 		focus++;
162 		break;
163 	      }
164 	  break;
165 
166 	case 2:		/* OK */
167 	  if (in_ch == KEY_ENTER || in_ch == 13)
168 	    {
169 	      i = sscanf (rmslengthstr.string, "%li", &helplong);
170 	      if (i < 1 || helplong < 1 || helplong % 2 == 0)
171 		{
172 		  error_window ("A whole, odd number, greater than 0, must \
173 be specified as RMS length.");
174 		  rmslengthstr.cursorpos =
175 		    strlen (rmslengthstr.string);
176 		  focus = 0;
177 		}
178 	      else
179 		{
180 		  parampointer->prelength1 = (helplong - 1) / 2;
181 		  parampointer->postlength1 = (helplong - 1) / 2;
182 		  dont_stop = FALSE;
183 		}
184 	    }
185 	  else
186 	    switch (in_ch)
187 	      {
188 	      case KEY_LEFT:
189 	      case KEY_UP:
190 		focus--;
191 		break;
192 	      case KEY_RIGHT:
193 	      case KEY_DOWN:
194 		focus++;
195 		break;
196 	      }
197 	  break;
198 	}
199 
200       if (in_ch == 9)		/* TAB */
201 	focus++;
202 
203       if (in_ch == 27)
204 	dont_stop = FALSE;
205 
206       if (focus > 2)
207 	focus -= 3;
208       if (focus < 0)
209 	focus += 3;
210     }
211   while (dont_stop);
212 
213   free (rmslengthstr.string);
214 }
215 #endif
216 
217 void
init_rms_filter(int filterno,parampointer_t parampointer)218 init_rms_filter (int filterno, parampointer_t parampointer)
219 {
220   parampointer->buffer = init_buffer (parampointer->postlength1,
221 				      parampointer->prelength1);
222 
223   parampointer->filterno = filterno;
224 }
225 
226 void
delete_rms_filter(parampointer_t parampointer)227 delete_rms_filter (parampointer_t parampointer)
228 {
229   delete_buffer (&parampointer->buffer);
230 }
231 
232 
233 sample_t
234 #ifndef SWIG
rms_filter(parampointer_t parampointer)235 rms_filter (parampointer_t parampointer)
236 #else
237 rms_filter (parampointer_t parampointer, int *filter_type)
238 #endif
239 {
240   doublesample_t sum;
241   sample_t sample;
242   long i;
243 
244 #ifndef SWIG
245   advance_current_pos (&parampointer->buffer, parampointer->filterno);
246 #else
247   advance_current_pos (&parampointer->buffer, parampointer->filterno, filter_type);
248 #endif
249 
250   sum.left = 0;
251   sum.right = 0;
252 
253   for (i = 0; i <= parampointer->postlength1 + parampointer->prelength1;
254        i++)
255     {
256       sample = get_from_buffer (&parampointer->buffer,
257 				i - parampointer->postlength1);
258       sum.left += sample.left * sample.left;
259       sum.right += sample.right * sample.right;
260     }
261 
262   sample.left = sqrt (sum.left / (parampointer->postlength1 +
263 				  parampointer->prelength1 + 1));
264   sample.right = sqrt (sum.right / (parampointer->postlength1 +
265 				    parampointer->prelength1 + 1));
266 
267   return sample;
268 }
269