File: mididevice.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 (196 lines) | stat: -rw-r--r-- 5,591 bytes parent folder | download | duplicates (3)
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
## 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{dev} =} mididevice (@var{mididev})
## @deftypefnx {} {@var{dev} =} mididevice (@var{mididir}, @var{mididev})
## @deftypefnx {} {@var{dev} =} mididevice ("input", @var{midiindev}, "output", @var{midioutdev})
## Create a midi device using the input parameters.
##
## When a single device name or id is provided, attempt to create the midi device using the same name for both input and output.
##
## Otherwise, use the name or device id for the given input or output direction.
##
## @subsubheading Inputs
## @var{mididev} - name or id of device to load.@*
## @var{mididir} - midi direction of "input" or "output"@*
## @var{midiindev} - midi input name or id@*
## @var{midioutdev} - midi output name or id
##
## @subsubheading Outputs
## @var{dev} - octave_midi class for opened device
##
## @subsubheading Properties
## @var{Input} - Input device name (read only).@*
## @var{Output} - Output device name (read only).@*
## @var{InputID} - Input device id (read only).@*
## @var{OutputID} - Output device id (read only).@*
##
## @subsubheading Examples
## Open midi device with ID of 0.
## @example
## @command{>} dev = mididevice(0);
##
##  mididevice connected to
##    input: "SparkFun Pro Micro:SparkFun Pro Micro MIDI 1 20:0" (1)
##    output: "SparkFun Pro Micro:SparkFun Pro Micro MIDI 1 20:0" (0)
## @end example
##
## Open a named midi device:
## @example
## @command{>} dev = mididevice("SparkFun Pro Micro:SparkFun Pro Micro MIDI 1 20:0");
##
##  mididevice connected to
##    input: "SparkFun Pro Micro:SparkFun Pro Micro MIDI 1 20:0" (1)
##    output: "SparkFun Pro Micro:SparkFun Pro Micro MIDI 1 20:0" (0)
## @end example
##
## @seealso{mididevinfo}
## @end deftypefn

function dev = mididevice (varargin)

  inname = "";
  outname = "";

  if nargin != 1 && nargin != 2 && nargin != 4
    error ("Expected device name, or device type/value pair");
  endif

  # dev list
  devs = __mididevinfo__ ();

  if nargin == 1
    if isscalar (varargin{1})
      name = devs (varargin{1}+1).Name;
    elseif ischar (varargin{1})
      name = varargin{1};
    endif

    # get in and out info

    idx = find (arrayfun(@(x) match_name_and_type(x, name, "input"), devs), 1);
    if !isempty (idx)
      indev = devs (idx);
      inname = indev.Name;
    endif

    idx = find (arrayfun(@(x) match_name_and_type(x, name,"output"), devs), 1);
    if !isempty (idx)
      outdev = devs (idx);
      outname = outdev.Name;
    endif

  elseif nargin == 2
    # type device (input, output)
    # dev num or name

    if isscalar (varargin{2})
      name = devs (varargin{2}+1).Name;
    elseif ischar (varargin{2})
      name = varargin{2};
    endif

    inname = "";
    outname = "";
    if strcmpi (varargin{1}, "input")
      inname = name;
    elseif strcmpi (varargin{1}, "output")
      outname = name;
    else
      error ("expected input or output");
    endif

    if !isempty(inname)
      idx = find (arrayfun(@(x) match_name_and_type(x, name,"input"), devs), 1);
      if !isempty (idx)
        indev = devs (idx);
        inname = indev.Name;
      else
        error ("unknown name %s", inname);
      endif
    endif 

    if !isempty(outname)
      idx = find (arrayfun(@(x) match_name_and_type(x, name,"output"), devs), 1);
      if !isempty (idx)
        outdev = devs (idx);
        outname = outdev.Name;
      else
        error ("unknown name %s", outname);
      endif
    endif 

  else # nargin == 4 
    # an input and output device specified

    if isscalar (varargin{2})
      name = devs (varargin{2}+1).Name;
    elseif ischar (varargin{2})
      name = varargin{2};
    endif

    if strcmpi (varargin{1}, "input")
      inname = name;
    elseif strcmpi (varargin{1}, "output")
      outname = name;
    else
      error ("expected input or output");
    endif

    if isscalar (varargin{4})
      name = devs (varargin{4}+1).Name;
    elseif ischar (varargin{4})
      name = varargin{4};
    endif

    if strcmpi (varargin{3}, "input")
      inname = name;
    elseif strcmpi (varargin{3}, "output")
      outname = name;
    else
      error ("expected input or output");
    endif

    idx = find (arrayfun(@(x) match_name_and_type(x, outname,"output"), devs), 1);
    if !isempty (idx)
        outdev = devs (idx);
        outname = outdev.Name;
    else
      error ("unknown name %s", outname);
    endif

    idx = find (arrayfun(@(x) match_name_and_type(x, inname,"input"), devs), 1);
    if !isempty (idx)
        indev = devs(idx);
        inname = indev.Name;
    else
      error ("unknown name %s", inname);
    endif

  endif

  dev = __mididevice__ (inname, outname);
endfunction

function t = match_name_and_type(x, name, type)
  t = (strncmpi(x.Name, name, length(name)) && strcmpi(x.Direction,type));
endfunction

%!xtest
%! a = mididevice(0);
%! assert(isa(a, "octave_midi"));