summaryrefslogtreecommitdiffstats
path: root/arts/midi/midimsg.c
blob: 7032fba8638676c6ca6519ef253d727eb5000b4b (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

#include <stdio.h>
#include <stdlib.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#include "midimsg.h"

#define STATUS_MASK 0x80
#define MESSAGE_TYPE_MASK 0xf0
#define CHANNEL_MASK 0x0f

Byte midiReadByte(int fd)
{
   Byte current_byte;
   /*
   ** for now ignore all realtime-messages (0xF8 .. 0xFF), which may get
   ** embedded *inside* every other message
   */
   do {
      /* 
      ** read() is wrapped in a while() in order to handle interruption
      ** by a signal.  In the normal case, read() is only called once.
      */
      while (read(/* fd = */ fd, /* buf = */ (void *)(&current_byte), 
         /* count = */ 1) < 1) {}

   } while(current_byte >= 0xf8);

   return current_byte;
}

int midimsgGetMessageType(Byte *message)
{
   return (message[0] & MESSAGE_TYPE_MASK);
}

void midimsgSetMessageType(Byte *message_inout, int message_type)
{
   message_inout[0] = (message_inout[0] & ~MESSAGE_TYPE_MASK) | message_type;
}

int midimsgGetChannel(Byte *message)
{
   return (message[0] & CHANNEL_MASK);
}

void midimsgSetChannel(Byte *message_inout, int channel)
{
   message_inout[0] = (message_inout[0] & ~CHANNEL_MASK) | channel;
}

int midimsgGetPitch(Byte *message)
{
   return (message[1]);
}

void midimsgSetPitch(Byte *message_inout, int pitch)
{
   message_inout[1] = pitch;
}

int midimsgGetVelocity(Byte *message)
{
   return (message[2]);
}

void midimsgSetVelocity(Byte *message_inout, int velocity)
{
   message_inout[2] = velocity;
}

int midimsgGetParameterNumber(Byte *message)
{
   return (message[1]);
}

void midimsgSetParameterNumber(Byte *message_inout, int number)
{
   message_inout[1] = number;
}

int midimsgGetParameterValue(Byte *message)
{
   return (message[2]);
}

void midimsgSetParameterValue(Byte *message_inout, int value)
{
   message_inout[2] = value;
}

int midimsgGetPitchWheelValue(Byte *message)
{
   return (((int)(message[2]) << 7) + message[1]);
}

void midimsgRead(int fd, Byte *message_return)
{
   Byte current_byte;
   static Byte last_status_byte;

   current_byte = midiReadByte(fd);

   if ((current_byte & STATUS_MASK) == 0)
   {
      /* 
      ** must be a running status, unless we're picking up mid-message 
      ** (which would be an unhandled error)
      */

      message_return[0] = last_status_byte;
   }
   else
   {
      message_return[0] = current_byte;
      last_status_byte = current_byte;

      current_byte = midiReadByte(fd);
   }

   switch (midimsgGetMessageType(/* message = */ message_return))
   {
      case MIDIMSG_NOTE_ON:
      case MIDIMSG_NOTE_OFF:
      case MIDIMSG_KEY_PRESSURE:
      case MIDIMSG_PARAMETER:
      case MIDIMSG_PITCH_WHEEL:

         message_return[1] = current_byte;

         current_byte = midiReadByte(fd);
         message_return[2] = current_byte;

         if ((midimsgGetMessageType(/* message = */ message_return) == 
            MIDIMSG_NOTE_ON) && (midimsgGetVelocity(/* message = */ 
            message_return) == 0))
         {
            /* note-on with velocity of zero is equivalent to note-off */

            midimsgSetMessageType(/* message_inout = */ message_return, 
               /* message_type = */ MIDIMSG_NOTE_OFF);
         }

         return;

      case MIDIMSG_PROGRAM:
      case MIDIMSG_CHANNEL_PRESSURE:

         message_return[1] = current_byte;

         return;
   }
}

void midimsgWrite(int fd, Byte *message)
{
   switch (midimsgGetMessageType(/* message = */ message))
   {
      case MIDIMSG_NOTE_ON:
      case MIDIMSG_NOTE_OFF:
      case MIDIMSG_KEY_PRESSURE:
      case MIDIMSG_PARAMETER:
      case MIDIMSG_PITCH_WHEEL:
         write(fd, message, 3);
         break;
      case MIDIMSG_PROGRAM:
      case MIDIMSG_CHANNEL_PRESSURE:
         write(fd, message, 2);
         break;
   }
}