/[pidim]/src/pidim_common.c
ViewVC logotype

Contents of /src/pidim_common.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (show annotations)
Fri Aug 27 00:11:30 2010 UTC (10 years, 9 months ago) by ben
File MIME type: text/plain
File size: 5422 byte(s)
factorised cli code
1 /* pidim_common, part of the Pidim project.
2 * Copyright (C) 2010 Benoit Rouits
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <math.h>
25 #include <alsa/asoundlib.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <libgen.h>
29 #include "pidim_common.h"
30
31 /* command-line stuff */
32
33 /* print version */
34 void cli_version (char* argv0)
35 {
36 printf("This is %s from %s version %s.\n", basename(argv0), PACKAGE_NAME, PACKAGE_VERSION);
37 }
38
39 /* print error */
40 void cli_error()
41 {
42 puts ("Could not parse parameters. Please use -h parameter for help");
43 }
44
45 /* print help */
46 void cli_manual (char* argv0, const char* brief, const char* text)
47 {
48 puts("Pidim © 2010 Benoît Rouits <brouits@free.fr>");
49 printf("%s: %s\n", basename(argv0), brief);
50 printf("Usage: %s [--help] [--version] [instance_number]\n", basename(argv0));
51 puts("Manual:");
52 puts(text);
53 }
54
55 /* parse instance name */
56 char* cli_parse (int argc, char** argv, const char* brief, const char* text)
57 {
58 int opt;
59 extern int optind, opterr, optopt;
60 #if defined USE_GETOPT
61 #else
62 int longopt_index = 0;
63 static struct option long_options[] = {
64 {"help", 0, NULL, 'h'},
65 {"version", 0, NULL, 'v'},
66 {NULL, 0, NULL, 0}
67 };
68 #endif
69 if (argc == 1) /* no instance name */
70 return NULL;
71 #if defined USE_GETOPT
72 while((opt = getopt (argc, argv, "hv")) != EOF) {
73 #else
74 while((opt = getopt_long (argc, argv, "hv", long_options, &longopt_index)) > 0) {
75 #endif
76 switch(opt) {
77 case 'h':
78 cli_manual(argv[0], brief, text);
79 exit (EXIT_SUCCESS);
80 break;
81 case 'v':
82 cli_version(argv[0]);
83 exit (EXIT_SUCCESS);
84 break;
85 default:
86 cli_error();
87 exit (EXIT_FAILURE);
88 break;
89 }
90 }
91 return argv[optind];
92 }
93
94 /* create a client handle */
95 snd_seq_t* alsa_open_client (const char* name)
96 {
97 snd_seq_t* handle;
98 int err;
99
100 err = snd_seq_open(&handle, "default", SND_SEQ_OPEN_INPUT|SND_SEQ_OPEN_OUTPUT, 0);
101 if (err < 0) {
102 return NULL;
103 }
104 snd_seq_set_client_name(handle, name);
105 return handle;
106 }
107
108 /* create an input port */
109 int alsa_open_input_port(snd_seq_t *handle, const char* name)
110 {
111 return snd_seq_create_simple_port(handle, name,
112 SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
113 SND_SEQ_PORT_TYPE_APPLICATION);
114 }
115
116 /* create an output port */
117 int alsa_open_output_port(snd_seq_t *handle, const char* name)
118 {
119 return snd_seq_create_simple_port(handle, name,
120 SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
121 SND_SEQ_PORT_TYPE_APPLICATION);
122 }
123
124 /* init and return a queue */
125 int alsa_init_queue(snd_seq_t *handle, int seq_len)
126 {
127 int queue_id;
128 queue_id = snd_seq_alloc_queue(handle);
129 snd_seq_set_client_pool_output(handle, (seq_len<<1) + 4);
130 return queue_id;
131 }
132
133 /* clear a queue */
134 void alsa_clear_queue(snd_seq_t *handle, int queue_id)
135 {
136 snd_seq_remove_events_t *remove_ev;
137
138 snd_seq_remove_events_malloc(&remove_ev);
139 snd_seq_remove_events_set_queue(remove_ev, queue_id);
140 snd_seq_remove_events_set_condition(remove_ev, SND_SEQ_REMOVE_OUTPUT | SND_SEQ_REMOVE_IGNORE_OFF);
141 snd_seq_remove_events(handle, remove_ev);
142 snd_seq_remove_events_free(remove_ev);
143 }
144
145 /* get the file descriptor of the first MIDI input port */
146 #if SND_LIB_MAJOR > 0 || SND_LIB_MINOR >= 6
147 /* FIXME: this is a dirty hack form XMBC code */
148 int alsa_get_fd(snd_seq_t *handle)
149 {
150 int pfds = snd_seq_poll_descriptors_count(handle, POLLIN);
151 if (pfds > 0) {
152 struct pollfd pfd;
153 if (snd_seq_poll_descriptors(handle, &pfd, 1, POLLIN) >= 0)
154 return pfd.fd;
155 }
156 return -1;
157 }
158 #endif
159
160 /* return an arbitrary lenghted array containing values 0 to max (but < 255) */
161 unsigned char* midi_ramp (int size, unsigned char max)
162 {
163 int i;
164 unsigned char* line;
165
166 line = (unsigned char*) malloc(size * sizeof(unsigned char));
167 if (!line)
168 return NULL;
169
170 for (i = 0; i < size; ++i) {
171 line[i] = (unsigned char)scale(i,size,max);
172 }
173 return line;
174 }
175
176 /* return a sinewave of arbitray length containing values < max < 255 */
177 unsigned char* midi_sine (int period, unsigned char max)
178 {
179 int i;
180 unsigned char* wave;
181
182 wave = (unsigned char*) malloc(period * sizeof(unsigned char));
183 if (!wave)
184 return NULL;
185
186 for (i = 0; i < period; ++i) {
187 wave[i] = (unsigned char)(sin(scale2PI(i,period)) * max);
188 }
189 return wave;
190 }
191
192 unsigned char* midi_rand (int length, unsigned char max)
193 {
194 int i;
195 unsigned char* noise;
196
197 noise = (unsigned char*) malloc(length * sizeof(unsigned char));
198 if (!noise)
199 return NULL;
200
201 /* srandom(max); */
202 for (i = 0; i < length; ++i) {
203 noise[i] = (unsigned char)random()%max;
204 }
205 return noise;
206 }
207

  ViewVC Help
Powered by ViewVC 1.1.26