summaryrefslogtreecommitdiffstats
path: root/libtdemid/alsaout.cc
blob: 517852b25f08b0c8c7f6d359090d7356bde78591 (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
/**************************************************************************

    alsaout.cc   - class AlsaOut which represents an alsa client/port pair
    This file is part of LibKMid 0.9.5
    Copyright (C) 2000  Antonio Larrosa Jimenez
    LibKMid's homepage : http://www.arrakis.es/~rlarrosa/libtdemid.html

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    Send comments and bug fixes to Antonio Larrosa <larrosa@kde.org>

***************************************************************************/
#include "alsaout.h"
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "sndcard.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/param.h>
#include "midispec.h"


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_ALSA_ASOUNDLIB_H
#	include <alsa/asoundlib.h>
#elif defined(HAVE_SYS_ASOUNDLIB_H)
#	include <sys/asoundlib.h>
#endif

#ifdef HAVE_LIBASOUND2
#	define HAVE_ALSA_SEQ 1
#	define snd_seq_flush_output(x)	snd_seq_drain_output(x)
#elif defined(HAVE_LIBASOUND)
#	define HAVE_ALSA_SEQ 1
#	include <linux/asequencer.h>
#endif


SEQ_USE_EXTBUF();

class AlsaOut::AlsaOutPrivate
{
public:
#ifdef HAVE_ALSA_SEQ
  AlsaOutPrivate(int _client, int _port, const char *cname,const char *pname)
    {
      handle=0L;
      src=tgt=0L;
      queue=0;
      tPCN=1;
      tgtclient=_client;
      tgtport=_port;
      tgtname=new char[strlen(cname)+strlen(pname)+3];
      strcpy(tgtname, cname);
      strcat(tgtname, "  ");
      strcat(tgtname, pname);
      ev=new snd_seq_event_t;
      timerStarted=false;
    }
#else
  AlsaOutPrivate(int, int, const char *,const char *)
    {
    }
#endif

  ~AlsaOutPrivate()
    {
#ifdef HAVE_ALSA_SEQ
      delete ev;
      delete tgtname;
#endif
    }

#ifdef HAVE_ALSA_SEQ
  snd_seq_t *handle;
  int  client;
  int  queue;
  snd_seq_addr_t *src;
  snd_seq_addr_t *tgt;

  snd_seq_event_t *ev;
  int tPCN;

  int tgtclient;
  int tgtport;
  char *tgtname;

  bool timerStarted;

#endif
};

AlsaOut::AlsaOut(int d,int _client, int _port, const char *cname,const char *pname) : MidiOut (d)
{
  di = new AlsaOutPrivate( _client, _port, cname, pname);
  seqfd = 0;
  devicetype=KMID_ALSA;
  device= d;

  volumepercentage=100;
#ifdef HAVE_ALSA_SEQ
//  printf("%d %d %d (%s)\n",device, di->tgtclient, di->tgtport, di->tgtname);
#endif

  _ok=1;
}

AlsaOut::~AlsaOut()
{
  closeDev();
  delete di;
}

void AlsaOut::openDev (int)
{
#ifndef HAVE_ALSA_SEQ
  return;
#else
  _ok=1;
#ifdef HAVE_LIBASOUND2
  if (snd_seq_open(&di->handle, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0)
                fprintf(stderr, "Couldn't open sequencer: %s", snd_strerror(errno));
#else
  if (snd_seq_open(&di->handle, SND_SEQ_OPEN) < 0)
                fprintf(stderr, "Couldn't open sequencer: %s", snd_strerror(errno));
#endif

  di->queue = snd_seq_alloc_queue(di->handle);
  if (di->queue < 0) {fprintf(stderr, "Couldn't allocate queue"); return; };
  di->client = snd_seq_client_id(di->handle);
  if (di->client < 0) {fprintf(stderr, "Couldn't get client id"); return; };
  di->tgt = new snd_seq_addr_t;
  di->tgt->client=di->tgtclient;
  di->tgt->port=di->tgtport;

  di->src = new snd_seq_addr_t;
  di->src->client = di->client;
  int port = snd_seq_create_simple_port(di->handle, NULL,
	SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE
	| SND_SEQ_PORT_CAP_READ, SND_SEQ_PORT_TYPE_MIDI_GENERIC);
  if ( port < 0 )
  {
    delete di->src;
    delete di->tgt;
    di->src=0;
    di->tgt=0;
    _ok=0;
    time=0;
    snd_seq_free_queue(di->handle, di->queue);
    snd_seq_close(di->handle);
    fprintf(stderr, "Cannot connect to %d:%d\n",di->tgtclient,di->tgtport);
    return;
  }
  di->src->port = port;
  

  int r=snd_seq_connect_to(di->handle, di->src->port, di->tgt->client, di->tgt->port);
  if (r < 0) { _ok=0; fprintf(stderr, "Cannot connect to %d:%d\n",di->tgtclient,di->tgtport); }
  time=0;
#endif
}

void AlsaOut::closeDev (void)
{
  if (!ok()) return;
#ifdef HAVE_ALSA_SEQ
  if (di->handle)
  {
    if (di->src) 
    {
       snd_seq_delete_simple_port(di->handle,di->src->port);
       delete di->src;
       di->src=0;
    }
    if (di->tgt) 
    {
       delete di->tgt;
       di->tgt=0;
    }
    if (di->queue) 
    {
      snd_seq_free_queue(di->handle, di->queue);
      snd_seq_close(di->handle);
    }
    di->handle=0;
  }

#endif
}

void AlsaOut::initDev (void)
{
#ifdef HAVE_ALSA_SEQ
  int chn;
  if (!ok()) return;
  uchar gm_reset[5]={0x7e, 0x7f, 0x09, 0x01, 0xf7};
  sysex(gm_reset, sizeof(gm_reset));
  for (chn=0;chn<16;chn++)
  {
    chnmute[chn]=0;
    if (chn!=9) chnPatchChange(chn,0);
    chnPressure(chn,64);
    chnPitchBender(chn, 0x00, 0x40);
    chnController(chn, CTL_MAIN_VOLUME,110*volumepercentage);
    chnController(chn, CTL_EXT_EFF_DEPTH, 0);
    chnController(chn, CTL_CHORUS_DEPTH, 0);
    chnController(chn, 0x4a, 127);
  }
#endif
}

#ifdef HAVE_ALSA_SEQ
void AlsaOut::eventInit(snd_seq_event_t *ev)
{
  snd_seq_ev_clear(ev);
  snd_seq_real_time_t tmp;
  tmp.tv_sec=(time)/1000;
  tmp.tv_nsec=(time%1000)*1000000;
//  printf("time : %d %d %d\n",(int)time,(int)tmp.tv_sec, (int)tmp.tv_nsec);
  if (!di->src) { fprintf(stderr,"AlsaOut::eventInit : no source\n"); return; }
  ev->source = *di->src;
  if (!di->tgt) { fprintf(stderr,"AlsaOut::eventInit : no target\n"); return; }
  ev->dest = *di->tgt;

  snd_seq_ev_schedule_real(ev, di->queue, 0, &tmp);
}

void AlsaOut::eventSend(snd_seq_event_t *ev)
{
    /*int err = */ snd_seq_event_output(di->handle, ev);
/*        if (err < 0)
                return;
*/
//#ifndef SND_SEQ_IOCTL_GET_CLIENT_POOL
        /*
         * If this is not defined then block mode writes will not be
         * working correctly.  Therefore loop until all events are flushed
         * out.
         */
/*        err = 0;
        do {
                err = snd_seq_flush_output(di->handle);
                if (err > 0)
                        usleep(2000);
        } while (err > 0);

#endif

        return ;
*/
}

void AlsaOut::timerEventSend(int type)
{
  snd_seq_event_t ev;

  ev.queue = di->queue;
  ev.dest.client = SND_SEQ_CLIENT_SYSTEM;
  ev.dest.port = SND_SEQ_PORT_SYSTEM_TIMER;

  ev.data.queue.queue = di->queue;

  ev.flags = SND_SEQ_TIME_STAMP_REAL | SND_SEQ_TIME_MODE_REL;
  ev.time.time.tv_sec = 0;
  ev.time.time.tv_nsec = 0;

  ev.type = type;

  snd_seq_event_output(di->handle, &ev);
  snd_seq_flush_output(di->handle);
}

#endif // HAVE_ALSA_SEQ

#ifndef HAVE_ALSA_SEQ
void AlsaOut::noteOn  (uchar , uchar , uchar )
{
#else
void AlsaOut::noteOn  (uchar chn, uchar note, uchar vel)
{
  if (vel==0)
  {
    noteOff(chn,note,vel);
  }
  else
  {
    eventInit(di->ev);
    snd_seq_ev_set_noteon(di->ev,map->channel(chn), map->key(chn,chnpatch[chn],note), vel);
    eventSend(di->ev);
  }
#endif
#ifdef MIDIOUTDEBUG
  printfdebug("Note ON >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::noteOff (uchar , uchar , uchar )
{
#else
void AlsaOut::noteOff (uchar chn, uchar note, uchar vel)
{
  eventInit(di->ev);
  snd_seq_ev_set_noteoff(di->ev,map->channel(chn), map->key(chn,chnpatch[chn],note), vel);
  eventSend(di->ev);
#endif
#ifdef MIDIOUTDEBUG
  printfdebug("Note OFF >\t chn : %d\tnote : %d\tvel: %d\n",chn,note,vel);
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::keyPressure (uchar , uchar , uchar )
{
#else
void AlsaOut::keyPressure (uchar chn, uchar note, uchar vel)
{
  eventInit(di->ev);
  snd_seq_ev_set_keypress(di->ev,map->channel(chn), map->key(chn,chnpatch[chn],note), vel);
  eventSend(di->ev);
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::chnPatchChange (uchar , uchar )
{
#else
void AlsaOut::chnPatchChange (uchar chn, uchar patch)
{
#ifdef MIDIOUTDEBUG
  printfdebug("PATCHCHANGE [%d->%d] %d -> %d\n",
      chn,map->channel(chn),patch,map->patch(chn,patch));
#endif
  eventInit(di->ev);
  snd_seq_ev_set_pgmchange(di->ev,map->channel(chn), map->patch(chn,patch));
  eventSend(di->ev);
  chnpatch[chn]=patch;
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::chnPressure (uchar , uchar )
{
#else
void AlsaOut::chnPressure (uchar chn, uchar vel)
{
  eventInit(di->ev);
  snd_seq_ev_set_chanpress(di->ev,map->channel(chn), vel);
  eventSend(di->ev);

  chnpressure[chn]=vel;
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::chnPitchBender(uchar ,uchar , uchar )
{
#else
void AlsaOut::chnPitchBender(uchar chn,uchar lsb, uchar msb)
{
  map->pitchBender(chn,lsb,msb);
  chnbender[chn]=((short)msb<<7) | (lsb & 0x7F);
  chnbender[chn]=chnbender[chn]-0x2000;

  eventInit(di->ev);
  snd_seq_ev_set_pitchbend(di->ev,map->channel(chn), chnbender[chn]);
  eventSend(di->ev);
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::chnController (uchar , uchar , uchar )
{
#else
void AlsaOut::chnController (uchar chn, uchar ctl, uchar v)
{
  map->controller(chn,ctl,v);
  if ((ctl==11)||(ctl==7))
  {
    v=(v*volumepercentage)/100;
    if (v>127) v=127;
  }

  eventInit(di->ev);
  snd_seq_ev_set_controller(di->ev,map->channel(chn), ctl, v);
  eventSend(di->ev);

  chncontroller[chn][ctl]=v;
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::sysex(uchar *, ulong )
{
#else
void AlsaOut::sysex(uchar *data, ulong size)
{
  eventInit(di->ev);
  snd_seq_ev_set_sysex(di->ev, size, data);
  eventSend(di->ev);
#endif

#ifdef MIDIOUTDEBUG
  printfdebug("sysex\n");
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::channelSilence (uchar )
{
#else
void AlsaOut::channelSilence (uchar chn)
{
  uchar i;
  for ( i=0; i<127; i++)
  {
    noteOff(chn,i,0);
  }
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::channelMute(uchar , int )
{
#else
void AlsaOut::channelMute(uchar chn, int a)
{
  if (a==1)
  {
    chnmute[chn]=a;
    channelSilence(chn);
  }
  else if (a==0)
  {
    chnmute[chn]=a;
  }
  /*  else ignore the call to this procedure */
#endif
}

void AlsaOut::seqbuf_dump (void)
{
  printf("You shouldn't be here.\n");
}

void AlsaOut::seqbuf_clean(void)
{
  printf("You shouldn't be here neither.\n");
}

void AlsaOut::wait(double ticks)
{
//  SEQ_WAIT_TIME(((int)(ticks/convertrate)));
  time=static_cast<long int>(ticks);

#ifdef MIDIOUTDEBUG
  printfdebug("Wait  >\t ticks: %g\n",ticks);
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::tmrSetTempo(int )
{
#else
void AlsaOut::tmrSetTempo(int v)
{
  eventInit(di->ev);
  di->ev->type = SND_SEQ_EVENT_TEMPO;
  snd_seq_ev_set_direct(di->ev);
  di->ev->data.queue.queue = di->queue;
  di->ev->data.queue.param.value = v;
  di->ev->dest.client = SND_SEQ_CLIENT_SYSTEM;
  di->ev->dest.port = SND_SEQ_PORT_SYSTEM_TIMER;
  snd_seq_event_output_direct(di->handle, di->ev);
#ifdef MIDIOUTDEBUG
  printfdebug("SETTEMPO  >\t tempo: %d\n",v);
#endif
#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::sync(int )
{
#else
void AlsaOut::sync(int i)
{
  if (i==1)
  {
    snd_seq_flush_output(di->handle);
  }

  if (di->timerStarted && di->src)
  {
    eventInit(di->ev);
    di->ev->dest = *di->src;
    eventSend(di->ev);
    snd_seq_flush_output(di->handle);
    snd_seq_event_input(di->handle,&di->ev);
  }

#endif
}

#ifndef HAVE_ALSA_SEQ
void AlsaOut::tmrStart(int )
{
#else
void AlsaOut::tmrStart(int tpcn)
{
  int  ret;
  di->timerStarted=true;
  di->tPCN=tpcn;

#ifdef HAVE_LIBASOUND2
  snd_seq_queue_tempo_t *queuetempo;
  snd_seq_queue_tempo_alloca(&queuetempo);
  snd_seq_queue_tempo_set_ppq(queuetempo, tpcn);
  snd_seq_queue_tempo_set_tempo(queuetempo, 60*1000000/120);
  ret = snd_seq_set_queue_tempo(di->handle, di->queue, queuetempo);
#else
  snd_seq_queue_tempo_t queuetempo;
  memset(&queuetempo, 0, sizeof(queuetempo));
  queuetempo.queue = di->queue;
  queuetempo.ppq = tpcn;
  queuetempo.tempo = 60*1000000/120;
  ret = snd_seq_set_queue_tempo(di->handle, di->queue, &queuetempo);
#endif

  timerEventSend(SND_SEQ_EVENT_START);
  snd_seq_start_queue(di->handle,di->queue,NULL);
#endif
}

void AlsaOut::tmrStop(void)
{
#ifdef HAVE_ALSA_SEQ
  di->timerStarted=false;
  timerEventSend(SND_SEQ_EVENT_STOP);
#endif
}

void AlsaOut::tmrContinue(void)
{
}

const char * AlsaOut::deviceName(void) const
{
#ifdef HAVE_ALSA_SEQ
  return di->tgtname;
#else
  return 0L;
#endif
}