summaryrefslogtreecommitdiff
path: root/lib/libsalsa/seq_input.c
blob: b61d22a264bc7d537f6cef2c990fe72dde7f96d5 (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
/*
 *  Copyright (c) 2004 by Hannu Savolainen < hannu@opensound.com>
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License version 2.1 as
 *   published by the Free Software Foundation.
 *
 *   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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */
#include <stdio.h>
#include "local.h"

static snd_seq_event_t *
alloc_event (snd_seq_t * seq)
{
  snd_seq_event_t *ev;

  if (seq->nevents >= MAX_EVENTS)
    {
      fprintf (stderr, "libsalsa: Local buffer overflow - event dropped\n");
      return NULL;
    }

  ev = &seq->events[seq->nevents++];

  ev->type = SND_SEQ_EVENT_SENSING;	/* NOP message */

  return ev;
}

static void
voice_message (snd_seq_t * seq, unsigned char msg, unsigned char ch,
	       unsigned char *parms, int len)
{
  snd_seq_event_t *ev;
  if ((ev = alloc_event (seq)) == NULL)
    return;

  dbg_printf3 ("Voice message %02x, ch=%d, %3d, %3d\n", msg, ch, parms[0],
	       parms[1]);

  switch (msg)
    {
    case MIDI_NOTEON:
      ev->type = SND_SEQ_EVENT_NOTEON;
      ev->data.note.channel = ch;
      ev->data.note.note = parms[0];
      ev->data.note.velocity = parms[1];
      break;

    case MIDI_NOTEOFF:
      ev->type = SND_SEQ_EVENT_NOTEOFF;
      ev->data.note.channel = ch;
      ev->data.note.note = parms[0];
      ev->data.note.velocity = parms[1];
      break;
    }
}

static void
channel_message (snd_seq_t * seq, unsigned char msg, unsigned char ch,
		 unsigned char *parms, int len)
{
  snd_seq_event_t *ev;
  if ((ev = alloc_event (seq)) == NULL)
    return;

  dbg_printf3 ("Channel message %02x, ch=%d, %3d, %3d\n", msg, ch, parms[0],
	       parms[1]);
}

static void
realtime_message (snd_seq_t * seq, unsigned char msg, unsigned char *parms,
		  int len)
{
  snd_seq_event_t *ev;
  if ((ev = alloc_event (seq)) == NULL)
    return;

  dbg_printf3 ("Realtime message %02x, %2x\n", msg, parms[0]);
}

static void
sysex_message (snd_seq_t * seq, unsigned char *parms, int len)
{
  int i;
  snd_seq_event_t *ev;
  if ((ev = alloc_event (seq)) == NULL)
    return;


  if (alib_verbose > 2)
    {
      printf ("Sysex message: ");
      for (i = 0; i < len; i++)
	printf ("%02x ", parms[i]);
      printf ("\n");
    }
}

void
midiparser_callback (void *context, int category, unsigned char msg,
		     unsigned char ch, unsigned char *parms, int len)
{

  switch (category)
    {
    case CAT_VOICE:
      voice_message ((snd_seq_t *) context, msg, ch, parms, len);
      break;

    case CAT_CHN:
      channel_message ((snd_seq_t *) context, msg, ch, parms, len);
      break;

    case CAT_REALTIME:
      realtime_message ((snd_seq_t *) context, msg, parms, len);
      break;

    case CAT_SYSEX:
      sysex_message ((snd_seq_t *) context, parms, len);
      break;

    case CAT_MTC:
    default:
      dbg_printf ("Unknown MIDI message category %d\n", category);
    }
}