summaryrefslogtreecommitdiff
path: root/muse/muse/driver/alsamidi.cpp
blob: f8133bd721e3d203a2845f5d44c6d7a676dd9017 (plain)
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program 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 this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#include "alsamidi.h"
#include "globals.h"
#include "midi.h"
#include "midiinport.h"
#include "midioutport.h"
#include "../midiseq.h"
#include "../song.h"
#include "al/pos.h"

static const unsigned int inCap  = SND_SEQ_PORT_CAP_SUBS_READ;
static const unsigned int outCap = SND_SEQ_PORT_CAP_SUBS_WRITE;

AlsaMidi alsaMidi;
AlsaMidi* midiDriver;

//---------------------------------------------------------
//   put
//    return true on fifo overflow
//---------------------------------------------------------

bool PortRouteFifo::put(const PortRoute& event)
      {
      if (size < PORT_ROUTE_FIFO_SIZE) {
            fifo[wIndex] = event;
            wIndex = (wIndex + 1) % PORT_ROUTE_FIFO_SIZE;
            q_atomic_increment(&size);
            return false;
            }
      return true;
      }

//---------------------------------------------------------
//   get
//---------------------------------------------------------

PortRoute PortRouteFifo::get()
      {
      PortRoute event(fifo[rIndex]);
      rIndex = (rIndex + 1) % PORT_ROUTE_FIFO_SIZE;
      q_atomic_decrement(&size);
      return event;
      }

//---------------------------------------------------------
//   AlsaMidi
//---------------------------------------------------------

AlsaMidi::AlsaMidi()
      {
      alsaSeq = 0;
      }

//---------------------------------------------------------
//   init
//    return true on error
//---------------------------------------------------------

bool AlsaMidi::init()
      {
      if (debugMsg)
            printf("init AlsaMidi\n");
      int error = snd_seq_open(&alsaSeq, "hw", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK);
      if (error < 0) {
            if (error == ENOENT)
            fprintf(stderr, "open ALSA sequencer failed: %s\n",
               snd_strerror(error));
            return true;
            }

      snd_seq_set_client_name(alsaSeq, "MusE Sequencer");

      //-----------------------------------------
      //    subscribe to "Announce"
      //    this enables callbacks for any
      //    alsa port changes
      //-----------------------------------------

      snd_seq_addr_t src, dst;
      int rv = snd_seq_create_simple_port(alsaSeq, "MusE Port 0",
         inCap | outCap | SND_SEQ_PORT_CAP_READ
         | SND_SEQ_PORT_CAP_WRITE
         | SND_SEQ_PORT_CAP_NO_EXPORT,
         SND_SEQ_PORT_TYPE_APPLICATION);
      if (rv < 0) {
            fprintf(stderr, "Alsa: create MusE port failed: %s\n", snd_strerror(error));
            exit(1);
            }
      dst.port   = rv;
      dst.client = snd_seq_client_id(alsaSeq);
      src.port   = SND_SEQ_PORT_SYSTEM_ANNOUNCE;
      src.client = SND_SEQ_CLIENT_SYSTEM;

      snd_seq_port_subscribe_t* subs;
      snd_seq_port_subscribe_alloca(&subs);
      snd_seq_port_subscribe_set_dest(subs, &dst);
      snd_seq_port_subscribe_set_sender(subs, &src);
      error = snd_seq_subscribe_port(alsaSeq, subs);
      if (error < 0) {
            fprintf(stderr, "Alsa: Subscribe System failed: %s\n", snd_strerror(error));
            return true;
            }

      return false;
      }

//---------------------------------------------------------
//   outputPorts
//---------------------------------------------------------

QList<PortName> AlsaMidi::outputPorts(bool)
      {
      QList<PortName> clientList;
      snd_seq_client_info_t* cinfo;
      snd_seq_client_info_alloca(&cinfo);
      snd_seq_client_info_set_client(cinfo, 0);

      while (snd_seq_query_next_client(alsaSeq, cinfo) >= 0) {
            snd_seq_port_info_t *pinfo;
            snd_seq_port_info_alloca(&pinfo);
            snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo));
            snd_seq_port_info_set_port(pinfo, -1);
            while (snd_seq_query_next_port(alsaSeq, pinfo) >= 0) {
                  unsigned int capability = snd_seq_port_info_get_capability(pinfo);
                  if (((capability & outCap) == outCap)
                     && !(capability & SND_SEQ_PORT_CAP_NO_EXPORT)) {
                        int client = snd_seq_port_info_get_client(pinfo);
                        if (client != snd_seq_client_id(alsaSeq)) {
                              PortName pn;
                              pn.name = QString(snd_seq_port_info_get_name(pinfo));
                              pn.port = Port(client, snd_seq_port_info_get_port(pinfo));
                              clientList.append(pn);
                              }
                        }
                  }
            }
      return clientList;
      }

//---------------------------------------------------------
//   inputPorts
//---------------------------------------------------------

QList<PortName> AlsaMidi::inputPorts(bool)
      {
      QList<PortName> clientList;

      snd_seq_client_info_t* cinfo;
      snd_seq_client_info_alloca(&cinfo);
      snd_seq_client_info_set_client(cinfo, 0);

      while (snd_seq_query_next_client(alsaSeq, cinfo) >= 0) {
            snd_seq_port_info_t *pinfo;
            snd_seq_port_info_alloca(&pinfo);
            snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo));
            snd_seq_port_info_set_port(pinfo, -1);
            while (snd_seq_query_next_port(alsaSeq, pinfo) >= 0) {
                  unsigned int capability = snd_seq_port_info_get_capability(pinfo);
                  if (((capability & inCap) == inCap)
                     && !(capability & SND_SEQ_PORT_CAP_NO_EXPORT)) {
                        int client = snd_seq_port_info_get_client(pinfo);
                        if (client != snd_seq_client_id(alsaSeq)) {
                              PortName pn;
                              pn.name = QString(snd_seq_port_info_get_name(pinfo));
                              pn.port = Port(client, snd_seq_port_info_get_port(pinfo));
                              clientList.append(pn);
                              }
                        }
                  }
            }
      return clientList;
      }

//---------------------------------------------------------
//   registerOutPort
//---------------------------------------------------------

Port AlsaMidi::registerOutPort(const QString& name, bool)
      {
      int alsaPort  = snd_seq_create_simple_port(alsaSeq, name.toLatin1().data(),
         outCap | SND_SEQ_PORT_CAP_WRITE, SND_SEQ_PORT_TYPE_APPLICATION);
      if (alsaPort < 0) {
            perror("cannot create alsa out port");
            return Port();
            }
      return Port(snd_seq_client_id(alsaSeq), alsaPort);
      }

//---------------------------------------------------------
//   registerInPort
//---------------------------------------------------------

Port AlsaMidi::registerInPort(const QString& name, bool)
      {
      int alsaPort  = snd_seq_create_simple_port(alsaSeq, name.toLatin1().data(),
         inCap | SND_SEQ_PORT_CAP_READ, SND_SEQ_PORT_TYPE_APPLICATION);
      if (alsaPort < 0) {
            perror("cannot create alsa in port");
            return Port();
            }
      return Port(snd_seq_client_id(alsaSeq), alsaPort);
      }

//---------------------------------------------------------
//   unregisterPort
//---------------------------------------------------------

void AlsaMidi::unregisterPort(Port port)
      {
      snd_seq_delete_simple_port(alsaSeq, port.alsaPort());
      }

//---------------------------------------------------------
//   setPortName
//---------------------------------------------------------

void AlsaMidi::setPortName(Port, const QString& name)
      {
      printf("AlsaMidi::setPortName(%s): not impl.\n", name.toLatin1().data());
      }

//---------------------------------------------------------
//   portName
//---------------------------------------------------------

QString AlsaMidi::portName(Port p)
      {
      snd_seq_port_info_t* pinfo;
      snd_seq_port_info_alloca(&pinfo);
      snd_seq_get_any_port_info(alsaSeq, p.alsaClient(), p.alsaPort(), pinfo);
      return QString(snd_seq_port_info_get_name(pinfo));
      }

//---------------------------------------------------------
//   findPort
//---------------------------------------------------------

Port AlsaMidi::findPort(const QString& name)
      {
      snd_seq_client_info_t* cinfo;
      snd_seq_client_info_alloca(&cinfo);
      snd_seq_client_info_set_client(cinfo, 0);

      while (snd_seq_query_next_client(alsaSeq, cinfo) >= 0) {
            snd_seq_port_info_t *pinfo;
            snd_seq_port_info_alloca(&pinfo);
            snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo));
            snd_seq_port_info_set_port(pinfo, -1);
            while (snd_seq_query_next_port(alsaSeq, pinfo) >= 0) {
                  const char* pn = snd_seq_port_info_get_name(pinfo);
                  if (name == pn) {
                        return Port(snd_seq_port_info_get_client(pinfo),
                           snd_seq_port_info_get_port(pinfo));
                        }
                  }
            }
      printf("AlsaMidi: port <%s> not found\n", name.toLatin1().data());
      return Port();
      }

//---------------------------------------------------------
//   connect
//    return false if connect fails
//---------------------------------------------------------

bool AlsaMidi::connect(Port src, Port dst)
      {
      snd_seq_port_subscribe_t* sub;
      snd_seq_port_subscribe_alloca(&sub);

      snd_seq_addr_t s, d;
      s.port   = src.alsaPort();
      s.client = src.alsaClient();
      d.port   = dst.alsaPort();
      d.client = dst.alsaClient();
      snd_seq_port_subscribe_set_sender(sub, &s);
      snd_seq_port_subscribe_set_dest(sub, &d);

      int rv = snd_seq_subscribe_port(alsaSeq, sub);
      if (rv < 0) {
            printf("AlsaMidi::connect(%d:%d, %d:%d) failed: %s\n",
               src.alsaClient(), src.alsaPort(),
               dst.alsaClient(), dst.alsaPort(),
               snd_strerror(rv));
            return false;
            }
      return true;
      }

//---------------------------------------------------------
//   disconnect
//    return false if disconnect fails
//---------------------------------------------------------

bool AlsaMidi::disconnect(Port src, Port dst)
      {
      snd_seq_port_subscribe_t* sub;
      snd_seq_port_subscribe_alloca(&sub);
      snd_seq_addr_t s, d;
      s.port   = src.alsaPort();
      s.client = src.alsaClient();
      d.port   = dst.alsaPort();
      d.client = dst.alsaClient();
      snd_seq_port_subscribe_set_sender(sub, &s);
      snd_seq_port_subscribe_set_dest(sub, &d);
      int rv = snd_seq_unsubscribe_port(alsaSeq, sub);
      if (rv < 0)
            printf("AlsaMidi::disconnect() failed: %s\n",
               snd_strerror(rv));
      return rv >= 0;
      }

//---------------------------------------------------------
//   getInputPollFd
//---------------------------------------------------------

void AlsaMidi::getInputPollFd(struct pollfd** p, int* n)
      {
      int npfdi = snd_seq_poll_descriptors_count(alsaSeq, POLLIN);
      struct pollfd* pfdi  = new struct pollfd[npfdi];
      snd_seq_poll_descriptors(alsaSeq, pfdi, npfdi, POLLIN);
      *p = pfdi;
      *n = npfdi;
      }

//---------------------------------------------------------
//   getOutputPollFd
//---------------------------------------------------------

void AlsaMidi::getOutputPollFd(struct pollfd** p, int* n)
      {
      int npfdo = snd_seq_poll_descriptors_count(alsaSeq, POLLOUT);
      struct pollfd* pfdo  = new struct pollfd[npfdo];
      snd_seq_poll_descriptors(alsaSeq, pfdo, npfdo, POLLOUT);
      *p = pfdo;
      *n = npfdo;
      }

//---------------------------------------------------------
//   addConnection
//    a new connection was added
//    called in MidiSeq context, FIFO synchronizes with
//    JACK callback
//---------------------------------------------------------

void AlsaMidi::addConnection(snd_seq_connect_t* ev)
      {
      Port rs(ev->sender.client, ev->sender.port);
      Port rd(ev->dest.client, ev->dest.port);
      PortRoute pr;
      pr.src = rs;
      pr.dst = rd;
      addCon.put(pr);
      }

//---------------------------------------------------------
//   removeConnection
//    a connection was removed
//    called in MidiSeq context, FIFO synchronizes with
//    JACK callback
//---------------------------------------------------------

void AlsaMidi::removeConnection(snd_seq_connect_t* ev)
      {
      Port rs(ev->sender.client, ev->sender.port);
      Port rd(ev->dest.client, ev->dest.port);
      PortRoute pr;
      pr.src = rs;
      pr.dst = rd;
      removeCon.put(pr);
      }

//---------------------------------------------------------
//   read
//    read ALSA midi events
//    This is called by the high priority RT MidiSeq
//    thread.
//---------------------------------------------------------

void AlsaMidi::read(MidiSeq* /*seq*/)
      {
      snd_seq_event_t* ev;
      for (int i = 0;; ++i) {
            int rv = snd_seq_event_input(alsaSeq, &ev);
            if (rv <= 0) {
                  if (rv < 0 && rv != -11)   // Resource temporarily unavailable
                        printf("AlsaMidi: read error 0x%x %s\n", rv, snd_strerror(rv));
                  break;
                  }
// printf("Alsa midi event %d %d\n", rv, ev->type);

            switch(ev->type) {
                  case SND_SEQ_EVENT_PORT_SUBSCRIBED:
                        // printf("subscribe\n");
                        addConnection((snd_seq_connect_t*)(&ev->data));
                        break;
                  case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
                        // printf("unsubscribe\n");
                        removeConnection((snd_seq_connect_t*)(&ev->data));
                        break;
                  case SND_SEQ_EVENT_CLIENT_START:
                        // printf("client start\n");
                        break;
                  case SND_SEQ_EVENT_CLIENT_EXIT:
                        // printf("client exit\n");
                        break;
                  case SND_SEQ_EVENT_PORT_START:
                        // printf("port start\n");
                        break;
                  case SND_SEQ_EVENT_PORT_EXIT:
                        // printf("port exit\n");
                        break;
                  case SND_SEQ_EVENT_SYSEX:
                  	{
                        //
                        // look for Midi Machine Control events (MMC)
                        //
                        unsigned char* data = ((unsigned char*)ev->data.ext.ptr) + 1;
                        int len = ev->data.ext.len - 2;
                        if ((len == 4) && (data[0] == 0x7f) && (data[2] == 0x06)) {
//TODO                              seq->mmcInput(data[1], data[3], 0);
                              break;
                              }
                        if ((len == 11) && (data[0] == 0x7f)
                           && (data[2] == 0x06)
                           && (data[3] == 0x44) && (data[4] == 0x06)
                           && (data[5] == 0x1)) {
                              int h = data[6];
                              int m = data[7];
                              int s = data[8];
                              int f = data[9];
                              int sf = data[10];
                              AL::Pos pos(h * 60 + m, s, f, sf);
//TODO                              seq->mmcInput(data[1], data[3], pos);
                              break;
                              }
                        }

                  case SND_SEQ_EVENT_KEYPRESS:
                  case SND_SEQ_EVENT_CHANPRESS:
                  case SND_SEQ_EVENT_NOTEON:
                  case SND_SEQ_EVENT_NOTEOFF:
                  case SND_SEQ_EVENT_PGMCHANGE:
                  case SND_SEQ_EVENT_PITCHBEND:
                  case SND_SEQ_EVENT_CONTROLLER:
                        {
                        Port port(ev->dest.client, ev->dest.port);

                        MidiInPortList* mpl = song->midiInPorts();
                        for (iMidiInPort i = mpl->begin(); i != mpl->end(); ++i) {
                              MidiInPort* inPort = *i;
                              if (port == inPort->alsaPort()) {
                                    inPort->eventReceived(ev);
                                    }
                              }
                        }
                        break;

                  case SND_SEQ_EVENT_CLOCK:
//TODO                        seq->realtimeSystemInput(0, 0xf8);
                        break;
                  case SND_SEQ_EVENT_START:
//TODO                        seq->realtimeSystemInput(0, 0xfa);
                        break;
                  case SND_SEQ_EVENT_CONTINUE:
//TODO                        seq->realtimeSystemInput(0, 0xfb);
                        break;
                  case SND_SEQ_EVENT_STOP:
//TODO                        seq->realtimeSystemInput(0, 0xfc);
                        break;
                  case SND_SEQ_EVENT_TICK:
//TODO                        seq->realtimeSystemInput(0, 0xf9);
                        break;
                  case SND_SEQ_EVENT_SONGPOS:
//TODO                        seq->setSongPosition(0, ev->data.control.value);
                        break;
                  case SND_SEQ_EVENT_SENSING:
                        break;
                  case SND_SEQ_EVENT_QFRAME:
//TODO                        seq->mtcInputQuarter(0, ev->data.control.value);
                        break;
                  // case SND_SEQ_EVENT_CLIENT_START:
                  // case SND_SEQ_EVENT_CLIENT_EXIT:
                  // case SND_SEQ_EVENT_CLIENT_CHANGE:
                  // case SND_SEQ_EVENT_PORT_CHANGE:
                  // case SND_SEQ_EVENT_SONGSEL:
                  // case SND_SEQ_EVENT_TIMESIGN:
                  // case SND_SEQ_EVENT_KEYSIGN:
                  // case SND_SEQ_EVENT_SETPOS_TICK:
                  // case SND_SEQ_EVENT_SETPOS_TIME:
                  // case SND_SEQ_EVENT_TEMPO:
                  // case SND_SEQ_EVENT_TUNE_REQUEST:
                  // case SND_SEQ_EVENT_RESET:
                  // case SND_SEQ_EVENT_NOTE:
                  // case SND_SEQ_EVENT_CONTROL14:
                  // case SND_SEQ_EVENT_NONREGPARAM:
                  // case SND_SEQ_EVENT_REGPARAM:
                  default:
                        printf("ALSA Midi input: type %d not handled\n", ev->type);
                        break;
                  }
            snd_seq_free_event(ev);
            }
      }

//---------------------------------------------------------
//   write
//---------------------------------------------------------

void AlsaMidi::write()
      {
      }

//---------------------------------------------------------
//   putEvent
//---------------------------------------------------------

void AlsaMidi::putEvent(Port p, const MidiEvent& e)
      {
      if (midiOutputTrace) {
            printf("MidiOut<%s>: midiAlsa: ", portName(p).toLatin1().data());
            e.dump();
            }
      int chn = e.channel();
      int a   = e.dataA();
      int b   = e.dataB();

      snd_seq_event_t event;
      memset(&event, 0, sizeof(event));
      snd_seq_ev_set_direct(&event);
      snd_seq_ev_set_source(&event, p.alsaPort());
      snd_seq_ev_set_dest(&event, SND_SEQ_ADDRESS_SUBSCRIBERS, 0);

      switch(e.type()) {
            case ME_NOTEON:
                  snd_seq_ev_set_noteon(&event, chn, a, b);
                  break;
            case ME_NOTEOFF:
                  snd_seq_ev_set_noteoff(&event, chn, a, 0);
                  break;
            case ME_PROGRAM:
                  snd_seq_ev_set_pgmchange(&event, chn, a);
                  break;
            case ME_CONTROLLER:
                  snd_seq_ev_set_controller(&event, chn, a, b);
                  break;
            case ME_PITCHBEND:
                  snd_seq_ev_set_pitchbend(&event, chn, a);
                  break;
            case ME_POLYAFTER:
                  // chnEvent2(chn, 0xa0, a, b);
                  break;
            case ME_AFTERTOUCH:
                  snd_seq_ev_set_chanpress(&event, chn, a);
                  break;
            case ME_SYSEX:
                  {
                  const unsigned char* p = e.data();
                  int n                  = e.len();
                  int len                = n + sizeof(event) + 2;
                  char buf[len];
                  event.type             = SND_SEQ_EVENT_SYSEX;
                  event.flags            = SND_SEQ_EVENT_LENGTH_VARIABLE;
                  event.data.ext.len     = n + 2;
                  event.data.ext.ptr  = (void*)(buf + sizeof(event));
                  memcpy(buf, &event, sizeof(event));
                  char* pp = buf + sizeof(event);
                  *pp++ = 0xf0;
                  memcpy(pp, p, n);
                  pp += n;
                  *pp = 0xf7;
                  putEvent(&event);
                  return;
                  }
            case ME_SONGPOS:
                  event.data.control.value = a;
                  event.type = SND_SEQ_EVENT_SONGPOS;
                  break;
            case ME_CLOCK:
                  event.type = SND_SEQ_EVENT_CLOCK;
                  break;
            case ME_START:
                  event.type = SND_SEQ_EVENT_START;
                  break;
            case ME_CONTINUE:
                  event.type = SND_SEQ_EVENT_CONTINUE;
                  break;
            case ME_STOP:
                  event.type = SND_SEQ_EVENT_STOP;
                  break;
            default:
                  printf("MidiAlsaDevice::putEvent(): event type %d not implemented\n",
                     e.type());
                  return;
            }
      putEvent(&event);
      }

//---------------------------------------------------------
//   putEvent
//    return false if event is delivered
//---------------------------------------------------------

bool AlsaMidi::putEvent(snd_seq_event_t* event)
      {
      int error;

      do {
            error   = snd_seq_event_output_direct(alsaSeq, event);
            int len = snd_seq_event_length(event);
            if (error == len) {
                  return false;
                  }
            if (error < 0) {
                  if (error == -12) {
                        return true;
                        }
                  else {
                        fprintf(stderr, "MidiAlsaDevice::%p putEvent(): midi write error: %s\n",
                           this, snd_strerror(error));
                        //exit(-1);
                        }
                  }
            else
                  fprintf(stderr, "MidiAlsaDevice::putEvent(): midi write returns %d, expected %d: %s\n",
                     error, len, snd_strerror(error));
            } while (error == -12);
      return true;
      }

//---------------------------------------------------------
//   updateConnections
//    this is called in JACK callback context
//---------------------------------------------------------

void AlsaMidi::updateConnections()
      {
      //
      //  add connections
      //
      while (!addCon.isEmpty()) {
            PortRoute pr = addCon.get();
            MidiOutPortList* opl = song->midiOutPorts();
            for (iMidiOutPort i = opl->begin(); i != opl->end(); ++i) {
                  MidiOutPort* oport = *i;
                  Port sPort = oport->alsaPort(0);

                  if (sPort == pr.src) {
                        RouteNode src(oport);
                        RouteNode dst(pr.dst, -1, RouteNode::MIDIPORT);
                        Route r = Route(src, dst);
                        if (oport->outRoutes()->indexOf(r) == -1)
                              oport->outRoutes()->push_back(r);
                        break;
                        }
                  }

            MidiInPortList* ipl = song->midiInPorts();
            for (iMidiInPort i = ipl->begin(); i != ipl->end(); ++i) {
                  MidiInPort* iport = *i;
                  Port dPort = iport->alsaPort(0);

                  if (dPort == pr.dst) {
                        RouteNode src(pr.src, -1, RouteNode::MIDIPORT);
                        RouteNode dst(iport);
                        Route r = Route(src, dst);
                        if (!iport->inRouteExists(r))
                              iport->addInRoute(r);
                        break;
                        }
                  }
            }
      //
      //  remove connections
      //
      while (!removeCon.isEmpty()) {
            PortRoute pr = removeCon.get();

            foreach(MidiInPort* iport, *(song->midiInPorts())) {
                  Port dst = iport->alsaPort();
                  if (dst == pr.dst) {
                        RouteList* irl = iport->inRoutes();
                        for (iRoute r = irl->begin(); r != irl->end(); ++r) {
                              if (r->src.port == pr.src) {
                                    iport->inRoutes()->erase(r);
                                    break;
                                    }
                              }
                        break;
                        }
                  }

            foreach(MidiOutPort* oport, *(song->midiOutPorts())) {
                  Port src = oport->alsaPort();
                  if (src == pr.src) {
                        RouteList* orl = oport->outRoutes();
                        for (iRoute r = orl->begin(); r != orl->end(); ++r) {
                              if (r->dst.port == pr.dst) {
                                    orl->erase(r);
                                    break;
                                    }
                              }
                        break;
                        }
                  }
            }
      }