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