summaryrefslogtreecommitdiff
path: root/media/native/midi/include/amidi/AMidi.h
blob: 0f930b5443e47044d7bb8cb97923843cca69c66b (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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * @addtogroup Midi
 * @{
 */

/**
 * @file AMidi.h
 */

#ifndef ANDROID_MEDIA_AMIDI_H_
#define ANDROID_MEDIA_AMIDI_H_

#include <stdarg.h>
#include <stdint.h>
#include <sys/types.h>

#include <jni.h>

#include <media/NdkMediaError.h>

#ifdef __cplusplus
extern "C" {
#endif

#define AMIDI_API __attribute__((visibility("default")))

typedef struct AMidiDevice AMidiDevice;
typedef struct AMidiInputPort AMidiInputPort;
typedef struct AMidiOutputPort AMidiOutputPort;

/*
 * Message Op Codes. Used to parse MIDI data packets
 */
enum {
    AMIDI_OPCODE_DATA = 1,      /* The MIDI packet contains normal MIDI data */
    AMIDI_OPCODE_FLUSH = 2,     /* The MIDI packet contains just a MIDI FLUSH command. */
                                /* Forces the send of any pending MIDI data. */
};

/*
 * Type IDs for various MIDI devices.
 */
enum {
    AMIDI_DEVICE_TYPE_USB = 1,      /* A MIDI device connected to the Android USB port */
    AMIDI_DEVICE_TYPE_VIRTUAL = 2,  /* A software object implementing MidiDeviceService */
    AMIDI_DEVICE_TYPE_BLUETOOTH = 3 /* A MIDI device connected via BlueTooth */
};

#if __ANDROID_API__ >= 29

/*
 * Device API
 */
/**
 * Connects a native Midi Device object to the associated Java MidiDevice object. Use this
 * AMidiDevice to access the rest of the native MIDI API. Use AMidiDevice_release() to
 * disconnect from the Java object when not being used any more.
 *
 * @param env   Points to the Java Environment.
 * @param midiDeviceObj   The Java MidiDevice Object.
 * @param outDevicePtrPtr  Points to the pointer to receive the AMidiDevice
 *
 * @return AMEDIA_OK on success, or a negative error value:
 *  @see AMEDIA_ERROR_INVALID_OBJECT - the midiDeviceObj
 *    is null or already connected to a native AMidiDevice
  *  @see AMEDIA_ERROR_UNKNOWN - an unknown error occurred.
 */
media_status_t AMIDI_API AMidiDevice_fromJava(
        JNIEnv *env, jobject midiDeviceObj, AMidiDevice **outDevicePtrPtr) __INTRODUCED_IN(29);

/**
 * Disconnects the native Midi Device Object from the associated Java MidiDevice object.
 *
 * @param midiDevice Points to the native AMIDI_MidiDevice.
 *
 * @return AMEDIA_OK on success,
 * or a negative error value:
 *  @see AMEDIA_ERROR_INVALID_PARAMETER - the device parameter is NULL.
 *  @see AMEDIA_ERROR_INVALID_OBJECT - the device is not consistent with the associated Java MidiDevice.
 *  @see AMEDIA_ERROR_INVALID_OBJECT - the JNI interface initialization to the associated java MidiDevice failed.
 *  @see AMEDIA_ERROR_UNKNOWN - couldn't retrieve the device info.
 */
media_status_t AMIDI_API AMidiDevice_release(const AMidiDevice *midiDevice) __INTRODUCED_IN(29);

/**
 * Gets the MIDI device type.
 *
 * @param device Specifies the MIDI device.
 *
 * @return The identifier of the MIDI device type:
 *  AMIDI_DEVICE_TYPE_USB
 *  AMIDI_DEVICE_TYPE_VIRTUAL
 *  AMIDI_DEVICE_TYPE_BLUETOOTH
 * or a negative error value:
 *  @see AMEDIA_ERROR_INVALID_PARAMETER - the device parameter is NULL.
 *  @see AMEDIA_ERROR_UNKNOWN - Unknown error.
 */
int32_t AMIDI_API AMidiDevice_getType(const AMidiDevice *device) __INTRODUCED_IN(29);

/**
 * Gets the number of input (sending) ports available on the specified MIDI device.
 *
 * @param device Specifies the MIDI device.
 *
 * @return If successful, returns the number of MIDI input (sending) ports available on the
 * device. If an error occurs, returns a negative value indicating the error:
 *  @see AMEDIA_ERROR_INVALID_PARAMETER - the device parameter is NULL.
 *  @see AMEDIA_ERROR_UNKNOWN - couldn't retrieve the device info.
 */
ssize_t AMIDI_API AMidiDevice_getNumInputPorts(const AMidiDevice *device) __INTRODUCED_IN(29);

/**
 * Gets the number of output (receiving) ports available on the specified MIDI device.
 *
 * @param device Specifies the MIDI device.
 *
 * @return If successful, returns the number of MIDI output (receiving) ports available on the
 * device. If an error occurs, returns a negative value indicating the error:
 *  @see AMEDIA_ERROR_INVALID_PARAMETER - the device parameter is NULL.
 *  @see AMEDIA_ERROR_UNKNOWN - couldn't retrieve the device info.
 */
ssize_t AMIDI_API AMidiDevice_getNumOutputPorts(const AMidiDevice *device) __INTRODUCED_IN(29);

/*
 * API for receiving data from the Output port of a device.
 */
/**
 * Opens the output port so that the client can receive data from it. The port remains open and
 * valid until AMidiOutputPort_close() is called for the returned AMidiOutputPort.
 *
 * @param device    Specifies the MIDI device.
 * @param portNumber Specifies the zero-based port index on the device to open. This value ranges
 *                  between 0 and one less than the number of output ports reported by the
 *                  AMidiDevice_getNumOutputPorts function.
 * @param outOutputPortPtr Receives the native API port identifier of the opened port.
 *
 * @return AMEDIA_OK, or a negative error code:
 *  @see AMEDIA_ERROR_UNKNOWN - Unknown Error.
 */
media_status_t AMIDI_API AMidiOutputPort_open(const AMidiDevice *device, int32_t portNumber,
                             AMidiOutputPort **outOutputPortPtr) __INTRODUCED_IN(29);

/**
 * Closes the output port.
 *
 * @param outputPort    The native API port identifier of the port.
 */
void AMIDI_API AMidiOutputPort_close(const AMidiOutputPort *outputPort) __INTRODUCED_IN(29);

/**
 * Receives the next pending MIDI message. To retrieve all pending messages, the client should
 * repeatedly call this method until it returns 0.
 *
 * Note that this is a non-blocking call. If there are no Midi messages are available, the function
 * returns 0 immediately (for 0 messages received).
 *
 * @param outputPort   Identifies the port to receive messages from.
 * @param opcodePtr  Receives the message Op Code.
 * @param buffer    Points to the buffer to receive the message data bytes.
 * @param maxBytes  Specifies the size of the buffer pointed to by the buffer parameter.
 * @param numBytesReceivedPtr  On exit, receives the actual number of bytes stored in buffer.
 * @param outTimestampPtr  If non-NULL, receives the timestamp associated with the message.
 *  (the current value of the running Java Virtual Machine's high-resolution time source,
 *  in nanoseconds)
 * @return the number of messages received (either 0 or 1), or a negative error code:
 *  @see AMEDIA_ERROR_UNKNOWN - Unknown Error.
 */
ssize_t AMIDI_API AMidiOutputPort_receive(const AMidiOutputPort *outputPort, int32_t *opcodePtr,
         uint8_t *buffer, size_t maxBytes, size_t* numBytesReceivedPtr, int64_t *outTimestampPtr) __INTRODUCED_IN(29);

/*
 * API for sending data to the Input port of a device.
 */
/**
 * Opens the input port so that the client can send data to it. The port remains open and
 * valid until AMidiInputPort_close() is called for the returned AMidiInputPort.
 *
 * @param device    Specifies the MIDI device.
 * @param portNumber Specifies the zero-based port index on the device to open. This value ranges
 *                  between 0 and one less than the number of input ports reported by the
 *                  AMidiDevice_getNumInputPorts() function..
 * @param outInputPortPtr Receives the native API port identifier of the opened port.
 *
 * @return AMEDIA_OK, or a negative error code:
 *  @see AMEDIA_ERROR_UNKNOWN - Unknown Error.
 */
media_status_t AMIDI_API AMidiInputPort_open(const AMidiDevice *device, int32_t portNumber,
                            AMidiInputPort **outInputPortPtr) __INTRODUCED_IN(29);

/**
 * Sends data to the specified input port.
 *
 * @param inputPort    The identifier of the port to send data to.
 * @param buffer       Points to the array of bytes containing the data to send.
 * @param numBytes     Specifies the number of bytes to write.
 *
 * @return The number of bytes sent, which could be less than specified or a negative error code:
 * @see AMEDIA_ERROR_INVALID_PARAMETER - The specified port was NULL, the specified buffer was NULL.
 */
ssize_t AMIDI_API AMidiInputPort_send(const AMidiInputPort *inputPort, const uint8_t *buffer,
                   size_t numBytes) __INTRODUCED_IN(29);

/**
 * Sends data to the specified input port with a timestamp.
 *
 * @param inputPort    The identifier of the port to send data to.
 * @param buffer       Points to the array of bytes containing the data to send.
 * @param numBytes     Specifies the number of bytes to write.
 * @param timestamp    The CLOCK_MONOTONIC time in nanoseconds to associate with the sent data.
 *
 * @return The number of bytes sent, which could be less than specified or a negative error code:
 * @see AMEDIA_ERROR_INVALID_PARAMETER - The specified port was NULL, the specified buffer was NULL.
 */
ssize_t AMIDI_API AMidiInputPort_sendWithTimestamp(const AMidiInputPort *inputPort,
        const uint8_t *buffer, size_t numBytes, int64_t timestamp) __INTRODUCED_IN(29);

/**
 * Sends a message with a 'MIDI flush command code' to the specified port. This should cause
 * a receiver to discard any pending MIDI data it may have accumulated and not processed.
 *
 * @param inputPort The identifier of the port to send the flush command to.
 *
 * @returns @see AMEDIA_OK if successful, otherwise a negative error code:
 * @see AMEDIA_ERROR_INVALID_PARAMETER - The specified port was NULL
 * @see AMEDIA_ERROR_UNSUPPORTED - The FLUSH command couldn't
 * be sent.
 */
media_status_t AMIDI_API AMidiInputPort_sendFlush(const AMidiInputPort *inputPort) __INTRODUCED_IN(29);

/**
 * Closes the input port.
 *
 * @param inputPort Identifies the input (sending) port to close.
 */
void AMIDI_API AMidiInputPort_close(const AMidiInputPort *inputPort) __INTRODUCED_IN(29);

#endif /* __ANDROID_API__ >= 29 */

#ifdef __cplusplus
}
#endif

#endif /* ANDROID_MEDIA_AMIDI_H_ */
/**
@}
*/