File: midireceive.m

package info (click to toggle)
octave-audio 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,632 kB
  • sloc: sh: 2,869; cpp: 886; python: 438; makefile: 228; xml: 21
file content (69 lines) | stat: -rw-r--r-- 2,129 bytes parent folder | download | duplicates (2)
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
## Copyright (C) 2019 John Donoghue <john.donoghue@ieee.org>
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## 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, see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {} {@var{midimsg} =} midireceive (@var{dev})
## @deftypefnx {} {@var{midimsg} =} midireceive (@var{dev}, @var{maxmsg})
## Attempt to receive midi messages from a midi device.
##
## @subsubheading Inputs
## @var{dev} - a octave midi device opened using mididevice.@*
## @var{maxmsg} - Maximum number of messages to retrieve. If not specified, the function will attempt to get all pending.@*
##
## @subsubheading Outputs
## @var{midimsg} - a midimsg containing the messages retrieved from the device.@*
## If no messages are available, @var{midimsg} will be empty.
##
## @subsubheading Examples
## Open device 0, and poll and display read messages
## @example
## dev = mididevice(0);
## while true
##    mx = midireceive(dev);
##    if !isempty(mx)
##      % display message
##      mx
##    endif
## endwhile
## @end example
##
## @seealso{mididevice, midisend}
## @end deftypefn

function msg = midireceive (dev, maxmsg=0)
  if nargin < 1 || !isobject(dev) || !strcmp(typeinfo(dev), "octave_midi")
    error ("Expected midi device");
  endif

  if nargin < 2
    maxmsg = 0;
  endif

  msg = midimsg (0);
  [ts, data] = __midirecv__(dev);
  while !isempty(data)
    msg = [ msg midimsg.createMessage(data, ts) ];

    if maxmsg > 0
      maxmsg = maxmsg - 1;
      if maxmsg == 0
        break;
      endif
    endif
    [ts, data] = __midirecv__(dev);
  endwhile

endfunction