File: yset.c

package info (click to toggle)
yiff 2.14.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,344 kB
  • ctags: 3,360
  • sloc: ansic: 40,505; cpp: 7,420; makefile: 425; sh: 242
file content (371 lines) | stat: -rw-r--r-- 9,679 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../include/Y2/Y.h"
#include "../include/Y2/Ylib.h"

#include "../include/string.h"


static void print_help(void);


/* Parameter codes to determine what we need to set. */
#define PARM_NONE		0	/* Do nothing. */
#define PARM_CYCLE		1	/* Change cycle. */
#define PARM_AUDIO_PRESET	2	/* Change Audio mode preset. */
#define PARM_AUDIO_VALUES	3	/* Change Audio mode values. */
#define PARM_AUDIO_SHELLOUT	4	/* Shell out recorder. */


#define MIN(a,b)        ((a) < (b) ? (a) : (b))
#define MAX(a,b)        ((a) > (b) ? (a) : (b))
#define CLIP(a,l,h)     (MIN(MAX((a),(l)),(h)))
#define ABSOLUTE(x)     (((x) < 0) ? ((x) * -1) : (x))


/*
 *      Prints help message.
 */
static void print_help(void)
{
	printf("\
Usage: yset <parameter [value...]> [options]\n\
\n\
    <parameter> can be any of the following:\n\
\n\
        audio <name>                 Change to a preset audio mode.\n\
\n\
        audiovalues <sample_rate> <channels> <sample_size> <fragment_size>\n\
                                     Change Audio mode values:\n\
                                      <sample_rate>   Sample rate in Hz.\n\
                                      <channels>      Number of channels.\n\
                                      <sample_size>   Sample size in bits.\n\
                                      <fragment_size> Fragment size in bytes.\n\
\n\
        cycle <microseconds>         Adjust the cycle.\n\
\n\
        shellout                     Shells out the recorder.\n\
\n\
    [options] can be any of the following:\n\
\n\
        --recorder <address:port>    Specify which Y server to connect to.\n\
\n\
    Return values:\n\
\n\
        0       Success.\n\
        1       General error.\n\
        2       Unable to connect to the Y server.\n\
        3       Systems error.\n\
\n"
	);
}


int main(int argc, char *argv[])
{
	int i, status;

	const char *con_arg = NULL;

	int parm = PARM_NONE;
	char val[1024];

	YConnection *con = NULL;

	int	sample_rate		= 11025,
		channels		= 1,
		sample_size		= 8,
		fragment_size_bytes	= 512;
	long	cycle_us		= 30000;


	/* Parse arguments. */
	for(i = 1; i < argc; i++)
	{
	    if(argv[i] == NULL)
		continue;

	    /* Help. */
	    if(strcasepfx(argv[i], "--h") ||
               strcasepfx(argv[i], "-h") ||
               !strcmp(argv[i], "?")
	    )
	    {
		print_help();
		return(0);
	    }
	    /* Cycle. */
	    else if(strcasepfx(argv[i], "--cycle") ||
                    strcasepfx(argv[i], "-cycle") ||
                    strcasepfx(argv[i], "cycle")
	    )
	    {
                i++;
                if(i < argc)
                {
                    parm = PARM_CYCLE;
		    strncpy(val, argv[i], 1024);
		    val[1024 - 1] = '\0';
                }   
                else
                {
                    fprintf(stderr,
                        "%s: Requires argument.\n",
                        argv[i - 1]
                    );
                }
	    }
            /* Audio mode values. */
            else if(strcasepfx(argv[i], "--audiovalue") ||
                    strcasepfx(argv[i], "-audiovalue") ||
                    strcasepfx(argv[i], "audiovalue")
            )
            {
                parm = PARM_AUDIO_VALUES;

                i++;
                if(i < argc)
                    sample_rate = atoi(argv[i]);
                else
                    fprintf(stderr,
                        "Insufficient arguments.\n"
                    );

		i++;
                if(i < argc)
                    channels = atoi(argv[i]);
                else
                    fprintf(stderr,
                        "Insufficient arguments.\n" 
                    );

                i++;
                if(i < argc)
                    sample_size = atoi(argv[i]);
                else
                    fprintf(stderr,
                        "Insufficient arguments.\n" 
                    );

                i++;
                if(i < argc)
                    fragment_size_bytes = atoi(argv[i]);
                else
                    fprintf(stderr,
                        "Insufficient arguments.\n" 
                    );
            }
            /* Audio mode preset. */
            else if(strcasepfx(argv[i], "--audio") ||
                    strcasepfx(argv[i], "-audio") ||
                    strcasepfx(argv[i], "audio")

            )
            {
                i++;
                if(i < argc)
                {
                    parm = PARM_AUDIO_PRESET;
                    strncpy(val, argv[i], 1024);
                    val[1024 - 1] = '\0';
                }
                else
                {
                    fprintf(stderr,
                        "%s: Requires argument.\n",
                        argv[i - 1]
                    );
                }
            }
            /* Shell out. */
            else if(strcasepfx(argv[i], "--shellout") ||
                    strcasepfx(argv[i], "-shellout") ||
                    strcasepfx(argv[i], "shellout")
            )
            {
		parm = PARM_AUDIO_SHELLOUT;
            }
	    /* Connect address. */
	    else if(strcasepfx(argv[i], "--rec") ||
                    strcasepfx(argv[i], "-rec")
	    )
	    {
		i++;
		if(i < argc)
		{
		    con_arg = argv[i];
		}
		else
		{
		    fprintf(stderr,
			"%s: Requires argument.\n",
			argv[i - 1]
		    );
		}
	    }
	}

	/* If no operation specified, then just print help. */
	if(parm == PARM_NONE)
	{
            print_help();   
            return(1);
	}


        /* Connect to Y server. */
        con = YOpenConnection(
            NULL,               /* No start argument. */
            con_arg
        );
        if(con == NULL)
        {
            fprintf(stderr, "Unable to connect to the Y server");
            if(con_arg == NULL)
                con_arg = getenv("RECORDER");
            if(con_arg == NULL)
                fprintf(stderr, ".\n");
            else
                fprintf(stderr, ": %s\n", con_arg);
            return(2);
        }

	/* Begin setting new value by the given parameter code. */
	status = -1;
	switch(parm)
	{
	  /* Set cycle. */
	  case PARM_CYCLE:
            status = YSetCycle(
		con,
		atol(val)
	    );
            if(status)
            {
                fprintf(stderr,
                    "Could not set cycle to value `%s'.\n",
                    val
                );
            }
            break;

	  /* Set audio mode preset. */
	  case PARM_AUDIO_PRESET:
            status = YChangeAudioModePreset(con, val);
            if(status)
            {
                fprintf(stderr,
                    "Could not set audio mode to value `%s'.\n",
                    val
                );
            }
	    break;

	  /* Set audio mode values. */
	  case PARM_AUDIO_VALUES:
	    /* Sanitize values. */
	    if((channels != 1) &&
               (channels != 2)
	    )
	    {
		fprintf(stderr,
                    "Invalid number of channels %i.\n",
                    channels
                );
		break;
	    }
            if((sample_size != 8) &&
               (sample_size != 16)
            )
            {
                fprintf(stderr,
                    "Invalid sample size %i bits.\n",
                    sample_size
                );
                break;
            }
            if((fragment_size_bytes != 256) &&
               (fragment_size_bytes != 512) &&
               (fragment_size_bytes != 1024) &&
               (fragment_size_bytes != 2048) &&
               (fragment_size_bytes != 4096) &&
               (fragment_size_bytes != 8192) &&
               (fragment_size_bytes != 16384)
            )
            {
                fprintf(stderr,
 "Invalid fragment size %i bytes (needs to be a 2^n value).\n",
                    fragment_size_bytes
                );
                break;
            }

	    /* Calculate cycle. */
	    cycle_us = YCalculateCycle(
                con,
                sample_rate, channels,
                sample_size, fragment_size_bytes
	    );
	    if(cycle_us < 100)
		cycle_us = 100;

	    status = YSetAudioModeValues(
                con,
                sample_size,
                channels,
                sample_rate,
                0,		/* Direction: 0 = play. */
                1,		/* Allow fragmenting. */
		2, 		/* Number of fragments. */
                fragment_size_bytes
	    );
            if(status)
            {
                fprintf(stderr,
                    "Could not set audio mode values.\n"
                );
            }
	    else
	    {
		YSetCycle(con, cycle_us);
	        YSyncAll(con, True);	/* Ensures full affect of cycle. */
	    }
	    break;

	  /* Shell out. */
	  case PARM_AUDIO_SHELLOUT:
            status = YSetAudioModeValues(
                con,  
                8,	/* Sample size. */
                1,	/* Channels. */
                0,	/* Sample rate (set to 0 to mean shell out). */
                0,      /* Direction: 0 = play. */
                1,      /* Allow fragmenting. */
                2,      /* Number of fragments. */
                512	/* Fragment size in bytes. */
	    );
	    if(status)
            {
                fprintf(stderr,
                    "Could not shell out recorder.\n"
                );
            }
	    else
	    {
                printf(
"Recorder has been shelled out.\n\
To reininitialize the recorder, use `yset' and specify any valid Audio\n\
mode or any valid Audio values.\n"
);
	    }
	    break;
	}

        /* Disconnect from Y server. */
        YCloseConnection(con, False);
        con = NULL;

	return(0);
}