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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
/*
* This file is part of tk707.
*
* Copyright (C) 2000, 2001, 2002, 2003, 2004 Chris Willing and Pierre Saramito
*
* tk707 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Foobar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include "util707.h"
int
seq707_context_init(seq707_context_t **ctxp)
{
if( (*ctxp=(seq707_context_t*)calloc(1, sizeof(seq707_context_t))) == NULL )
return -1;
(*ctxp)->ppq = PPQ;
(*ctxp)->currticktime = 0;
(*ctxp)->tempo = 500000;
(*ctxp)->midichannel = DEF_MIDICHANNEL;
return 0;
}
int
usage(void)
{
fprintf(stderr, "\nusage:\ttk707 [options]\nwhere option:\n");
fprintf(stderr, " -l provides list of ports\n");
fprintf(stderr, " -p x:y specifies x:y as port to use, e.g. 64:0\n\n");
#ifdef TODO
-h
-l List the available sound ports
-p port
-colormap
-display
-geometry
-name Name to use for application
-sync Use synchronous mode for display server
-visual
-use Id of window in which to embed application
-- Pass all remaining arguments through to script
#endif /* TODO */
#ifdef TO_CLEAN
printf("NOTE:\n");
printf(" Setting ALSA_OUT_PORT=x:y allows tk707 to run without -p option.\n");
printf(" Try \"export ALSA_OUT_PORT=x:y\" in .profile (or /etc/profile),\n");
printf("where x:y is a port generated with -l option.\n\n");
#endif /* TO_CLEAN */
return 0;
}
int
set_client_port(char *portdesc, int *client, int *port)
{
char *astr;
char *cp;
snd_seq_addr_t *addr;
snd_seq_addr_t *ap;
int a[ADDR_PARTS];
int count, naddr;
int i;
addr = (snd_seq_addr_t*)calloc((unsigned)(sizeof(snd_seq_addr_t)*strlen(portdesc)), 1);
if( !addr )
return -1;
naddr = 0;
ap = addr;
for(astr=strtok(portdesc,SEP);astr;astr=strtok(NULL,SEP)) {
for(cp=astr,count=0;cp && *cp;cp++) {
if( count < ADDR_PARTS )
a[count++] = atoi(cp);
cp = strchr(cp, ':');
if( cp == NULL )
break;
}
switch(count)
{
case 2:
ap->client = a[0];
ap->port = a[1];
break;
default:
printf("Addresses in %d parts not supported yet\n", count);
break;
}
ap++;
naddr++;
}
*client = a[0];
*port = a[1];
return 0;
}
#ifdef HAVE_ALSA9
/* ALSA 0.9.0 API */
int
get_clientsports(char ***cpinfo)
{
snd_seq_client_info_t *cinfo;
snd_seq_port_info_t *pinfo;
snd_seq_t *handle;
int client, port, cp, i;
char buf[128];
if (snd_seq_open(&handle, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0)
{
fprintf(stderr, "Couldn't open sequence. Error was:\n%s\n", snd_strerror(errno));
return -1;
}
snd_seq_client_info_alloca(&cinfo);
snd_seq_port_info_alloca(&pinfo);
cp = 0;
printf("Port %-30.30s %s\n", "Client name", "Port name");
snd_seq_client_info_set_client(cinfo, 0);
while (snd_seq_query_next_client(handle, cinfo) >= 0) {
client = snd_seq_client_info_get_client(cinfo);
snd_seq_port_info_set_client(pinfo, client);
snd_seq_port_info_set_port(pinfo, -1);
while (snd_seq_query_next_port(handle, pinfo) >= 0) {
unsigned int cap;
cap = (SND_SEQ_PORT_CAP_SUBS_WRITE|SND_SEQ_PORT_CAP_WRITE);
if ((snd_seq_port_info_get_capability(pinfo) & cap) == cap)
cp++;
}
}
if( (*cpinfo=(char**)calloc(cp + 1, sizeof(char*))) == NULL )
{
fprintf(stderr, "Couldn't get memory for client:port info\n");
return 0;
}
/* Now do it again and add each client:port */
cp = 0;
snd_seq_client_info_set_client(cinfo, 0);
while (snd_seq_query_next_client(handle, cinfo) >= 0) {
client = snd_seq_client_info_get_client(cinfo);
snd_seq_port_info_set_client(pinfo, client);
snd_seq_port_info_set_port(pinfo, -1);
while (snd_seq_query_next_port(handle, pinfo) >= 0) {
unsigned int cap;
port = snd_seq_port_info_get_port(pinfo);
cap = (SND_SEQ_PORT_CAP_SUBS_WRITE|SND_SEQ_PORT_CAP_WRITE);
if ((snd_seq_port_info_get_capability(pinfo) & cap) == cap) {
memset(buf, 0, sizeof(buf));
sprintf(buf, "%3d:%-3d %-30.30s %s\n",
client, port,
snd_seq_client_info_get_name(cinfo),
snd_seq_port_info_get_name(pinfo));
(*cpinfo)[cp] = strdup(buf);
cp++;
}
}
}
/*
sprintf(buf, "END END");
(*cpinfo)[cp] = strdup(buf);
return cp + 1;
*/
return cp;
}
#else /* !HAVE_ALSA9 */
int
get_clientsports(char ***cpinfo)
{
snd_seq_client_info_t cinfo;
snd_seq_port_info_t pinfo;
snd_seq_system_info_t sysinfo;
snd_seq_t *handle;
int client, port, cp, i;
char buf[128];
if( snd_seq_open(&handle, SND_SEQ_OPEN) < 0 )
{
fprintf(stderr, "Couldn't open sequence. Error was:\n%s\n", snd_strerror(errno));
return -1;
}
if( snd_seq_system_info(handle, &sysinfo) < 0 )
{
fprintf(stderr, "Couldn't get sequencer information. Error was:\n%s\n", snd_strerror(errno));
return -1;
}
cp = 0;
fprintf(stderr, "Port %-30.30s %s\n", "Client name", "Port name");
/* First just count how many string pointers we'll need */
for(client=0;client<sysinfo.clients;client++)
{
if( snd_seq_get_any_client_info(handle, client, &cinfo) < 0 )
continue;
for(port=0;port<sysinfo.ports;port++)
{
unsigned int cap;
if( snd_seq_get_any_port_info(handle, client, port, &pinfo) < 0 )
continue;
cap = (SND_SEQ_PORT_CAP_SUBS_WRITE|SND_SEQ_PORT_CAP_WRITE);
if( (pinfo.capability & cap) == cap )
{
/*
printf("%3d:%-3d %-30.30s %s\n", pinfo.client, pinfo.port, cinfo.name, pinfo.name);
*/
cp++;
}
}
}
if( (*cpinfo=(char**)calloc(cp + 1, sizeof(char*))) == NULL )
{
fprintf(stderr, "Couldn't get memory for client:port info\n");
return 0;
}
/* Now do it again and add each client:port */
cp = 0;
for(client=0;client<sysinfo.clients;client++)
{
if( snd_seq_get_any_client_info(handle, client, &cinfo) < 0 )
continue;
for(port=0;port<sysinfo.ports;port++)
{
unsigned int cap;
if( snd_seq_get_any_port_info(handle, client, port, &pinfo) < 0 )
continue;
cap = (SND_SEQ_PORT_CAP_SUBS_WRITE|SND_SEQ_PORT_CAP_WRITE);
if( (pinfo.capability & cap) == cap )
{
memset(buf, 0, sizeof(buf));
sprintf(buf, "%3d:%-3d %-30.30s %s\n", pinfo.client, pinfo.port, cinfo.name, pinfo.name);
(*cpinfo)[cp] = strdup(buf);
cp++;
}
}
}
/*
sprintf(buf, "END END");
(*cpinfo)[cp] = strdup(buf);
return cp + 1;
*/
return cp;
}
#endif /* HAVE_ALSA9 */
void
set_event_header(seq707_context_t *ctxp, snd_seq_event_t *ev)
{
snd_seq_ev_clear(ev);
snd_seq_ev_set_dest(ev, ctxp->dest_client, ctxp->dest_port);
snd_seq_ev_set_source(ev, ctxp->local_port);
set_event_time(ctxp, ev, 0);
}
void
set_event_time(seq707_context_t *ctxp, snd_seq_event_t *ev, unsigned int currtime)
{
if( currtime == 0 )
currtime = ctxp->currticktime;
snd_seq_ev_schedule_tick(ev, ctxp->dest_queue, 0, currtime);
/*
printf("Set event time to %u\n", currtime);
*/
}
void
alsa_timer_start(seq707_context_t *ctxp)
{
snd_seq_start_queue(ctxp->handle, ctxp->dest_queue, NULL);
}
void
alsa_timer_stop(seq707_context_t *ctxp)
{
snd_seq_event_t ev;
set_event_header(ctxp, &ev);
snd_seq_stop_queue(ctxp->handle, ctxp->dest_queue, &ev);
}
void
alsa_timer_cont(seq707_context_t *ctxp)
{
snd_seq_event_t ev;
set_event_header(ctxp, &ev);
snd_seq_continue_queue(ctxp->handle, ctxp->dest_queue, &ev);
}
void
do_noteon(seq707_context_t *ctxp, int chan, int pitch, int vol)
{
int written;
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
set_event_header(ctxp, &ev);
snd_seq_ev_set_noteon(&ev, chan, pitch, vol);
written = snd_seq_event_output(ctxp->handle, &ev);
if (written < 0) {
printf("written = %i (%s)\n", written, snd_strerror(written));
exit(1);
}
/* printf("wrote %d\n", written); */
}
/* Send a note on event to the 2nd queue */
void
do_noteon_rt(seq707_context_t *ctxp, int chan, int pitch, int vol)
{
int written;
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
snd_seq_ev_set_dest(&ev, ctxp->dest_client, ctxp->dest_port);
snd_seq_ev_set_source(&ev, ctxp->local_port);
snd_seq_ev_schedule_tick(&ev, ctxp->dest_queue, 0, 0);
snd_seq_ev_set_noteon(&ev, chan, pitch, vol);
written = snd_seq_event_output(ctxp->handle, &ev);
if (written < 0) {
printf("written = %i (%s)\n", written, snd_strerror(written));
exit(1);
}
/* printf("wrote %d to chan %d\n", written, chan); */
}
void
do_noteoff(seq707_context_t *ctxp, int chan, int pitch, int vol)
{
int written;
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
set_event_header(ctxp, &ev);
snd_seq_ev_set_noteoff(&ev, chan, pitch, vol);
written = snd_seq_event_output(ctxp->handle, &ev);
if (written < 0) {
printf("written = %i (%s)\n", written, snd_strerror(written));
exit(1);
}
}
/* Send a note off event to the 2nd queue */
void
do_noteoff_rt(seq707_context_t *ctxp, int chan, int pitch, int vol)
{
int written;
snd_seq_event_t ev;
snd_seq_ev_clear(&ev);
snd_seq_ev_set_dest(&ev, ctxp->dest_client, ctxp->dest_port);
snd_seq_ev_set_source(&ev, ctxp->local_port);
snd_seq_ev_schedule_tick(&ev, ctxp->dest_queue, 0, old_tick_per_note_on);
snd_seq_ev_set_noteoff(&ev, chan, pitch, vol);
written = snd_seq_event_output(ctxp->handle, &ev);
if (written < 0) {
printf("written = %i (%s)\n", written, snd_strerror(written));
exit(1);
}
}
snd_seq_event_t *
wait_for_event(seq707_context_t *ctxp)
{
int left;
snd_seq_event_t *input_event;
/* read event - blocked until any event is read */
left = snd_seq_event_input(ctxp->handle, &input_event);
if (left < 0) {
printf("alsa_sync error!:%s\n", snd_strerror(left));
return NULL;
}
return input_event;
}
int
do_tempo_set(seq707_context_t *ctxp, unsigned int val)
{
snd_seq_event_t ev;
memset(&ev, 0, sizeof(snd_seq_event_t));
ev.queue = SND_SEQ_QUEUE_DIRECT;
ev.type = SND_SEQ_EVENT_TEMPO;
ev.dest.client = SND_SEQ_CLIENT_SYSTEM;
ev.dest.port = SND_SEQ_PORT_SYSTEM_TIMER;
ev.data.queue.queue = ctxp->dest_queue;
ev.data.queue.param.value = val;
return snd_seq_event_output(ctxp->handle, &ev);
}
int
alsa_setup(TK707 *tk707, char *portdesc, char *in_portdesc)
{
int c, res, myport, ctrlport, iport, dest_queue, ctrl_queue, input_queue, clientno, portno, ppq;
int i, bank, pattern;
snd_seq_t *seq_handle = NULL;
snd_seq_t *ctrl_handle = NULL;
snd_seq_t *input_handle = NULL;
#ifdef HAVE_ALSA9
snd_seq_queue_tempo_t *tempo;
#else
snd_seq_queue_tempo_t tempo;
#endif
/* Initialise contexts for pattern & control outputs */
if( seq707_context_init(&(tk707->ctxp_p)) < 0 )
return -1;
if( seq707_context_init(&(tk707->ctxp_c)) < 0 )
return -1;
if( seq707_context_init(&(tk707->ctxp_i)) < 0 )
return -1;
/* Obtain handles to sequencers */
#ifdef HAVE_ALSA9
if( snd_seq_open(&(tk707->ctxp_p->handle), "hw", SND_SEQ_OPEN_DUPLEX,
SND_SEQ_NONBLOCK) < 0 )
return -1;
if( snd_seq_open(&(tk707->ctxp_c->handle), "hw", SND_SEQ_OPEN_DUPLEX,
SND_SEQ_NONBLOCK) < 0 )
return -1;
if( snd_seq_open(&(tk707->ctxp_i->handle), "hw", SND_SEQ_OPEN_DUPLEX,
SND_SEQ_NONBLOCK) < 0 )
return -1;
#else
if( snd_seq_open(&(tk707->ctxp_p->handle), SND_SEQ_OPEN) < 0 )
return -1;
if( snd_seq_open(&(tk707->ctxp_c->handle), SND_SEQ_OPEN) < 0 )
return -1;
if( snd_seq_open(&(tk707->ctxp_i->handle), SND_SEQ_OPEN) < 0 )
return -1;
#endif
seq_handle = tk707->ctxp_p->handle;
ctrl_handle = tk707->ctxp_c->handle;
input_handle = tk707->ctxp_i->handle;
/* Set non-blocking mode for the queues */
#ifndef HAVE_ALSA9
if( snd_seq_block_mode(seq_handle, 0) < 0 )
return -1;
if( snd_seq_block_mode(ctrl_handle, 0) < 0 )
return -1;
if( snd_seq_block_mode(input_handle, 0) < 0 )
return -1;
#endif
/* Textual names for clients */
snd_seq_set_client_name(seq_handle, "TK707");
snd_seq_set_client_name(ctrl_handle, "TK707control");
snd_seq_set_client_name(input_handle, "TK707input");
/* Create the ports */
if( (myport=snd_seq_create_simple_port(seq_handle, "Port 0",
SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_READ,
SND_SEQ_PORT_TYPE_MIDI_GENERIC)) < 0 )
{
return -1;
}
if( (ctrlport=snd_seq_create_simple_port(ctrl_handle, "Port 1",
SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_READ,
SND_SEQ_PORT_TYPE_MIDI_GENERIC)) < 0 )
{
return -1;
}
if( (iport=snd_seq_create_simple_port(input_handle, "Port 2",
SND_SEQ_PORT_CAP_READ,
SND_SEQ_PORT_TYPE_MIDI_GENERIC)) < 0 )
{
return -1;
}
tk707->ctxp_p->local_port = myport;
tk707->ctxp_c->local_port = ctrlport;
tk707->ctxp_i->local_port = iport;
/* Allocate destination queues */
if( (dest_queue=snd_seq_alloc_queue(seq_handle)) < 0 )
return -1;
if( (ctrl_queue=snd_seq_alloc_queue(ctrl_handle)) < 0 )
return -1;
if( (input_queue=snd_seq_alloc_queue(input_handle)) < 0 )
return -1;
tk707->ctxp_p->dest_queue = dest_queue;
tk707->ctxp_c->dest_queue = ctrl_queue;
tk707->ctxp_i->dest_queue = input_queue;
/* No more _i's */
/* Set client:port from port descriptor (pattern & control use same) */
if( set_client_port(portdesc, &clientno, &portno) < 0 )
return -1;
tk707->ctxp_p->dest_client = clientno;
tk707->ctxp_p->dest_port = portno;
tk707->ctxp_c->dest_client = clientno;
tk707->ctxp_c->dest_port = portno;
/* Connect the queues */
if( snd_seq_connect_to(seq_handle, myport, clientno, portno) < 0 )
return -1;
if( snd_seq_connect_to(ctrl_handle, ctrlport, clientno, portno) < 0 )
return -1;
/* Set io buffer characteristics */
if( snd_seq_set_client_pool_output(seq_handle, WRITE_POOL_SIZE) < 0 ||
snd_seq_set_client_pool_input(seq_handle, READ_POOL_SIZE) < 0 ||
snd_seq_set_client_pool_output_room(seq_handle, WRITE_POOL_SPACE) < 0 )
{
return -1;
}
if( snd_seq_set_client_pool_output(ctrl_handle, WRITE_POOL_SIZE) < 0 ||
snd_seq_set_client_pool_input(ctrl_handle, READ_POOL_SIZE) < 0 ||
snd_seq_set_client_pool_output_room(ctrl_handle, WRITE_POOL_SPACE) < 0 )
{
return -1;
}
/* Set pattern queue tempo. Control queue doesn't seem to need it (yet?) */
#ifdef HAVE_ALSA9
snd_seq_queue_tempo_alloca(&tempo);
if (snd_seq_get_queue_tempo(seq_handle, dest_queue, tempo) < 0 )
return -1;
ppq = tk707->ctxp_p->ppq;
if (snd_seq_queue_tempo_get_ppq(tempo) != ppq )
{
snd_seq_queue_tempo_set_ppq(tempo, ppq);
if( snd_seq_set_queue_tempo(seq_handle, dest_queue, tempo) < 0 )
{
return -1;
}
}
tk707->ctxp_p->tempo = snd_seq_queue_tempo_get_tempo(tempo);
#else
if( snd_seq_get_queue_tempo(seq_handle, dest_queue, &tempo) < 0 )
return -1;
ppq = tk707->ctxp_p->ppq;
if( tempo.ppq != ppq )
{
tempo.ppq = ppq;
if( snd_seq_set_queue_tempo(seq_handle, dest_queue, &tempo) < 0 )
{
return -1;
}
}
tk707->ctxp_p->tempo = tempo.tempo;
#endif
return 0;
}
|