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 262 263 264 265 266
|
## Copyright (C) 2019-2021 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/>.
classdef midicontrols < handle
## -*- texinfo -*-
## @deftypefn {} {@var{obj} =} midicontrols ()
## @deftypefnx {} {@var{obj} =} midicontrols (@var{ctrlid})
## @deftypefnx {} {@var{obj} =} midicontrols (@var{ctrlid}, @var{initialvalues})
## @deftypefnx {} {@var{obj} =} midicontrols (__, @var{propertyname}, @var{propertyvalue})
## Create a midi controls object
##
## @subsubheading Inputs
## @var{ctrlid} - single control id or array of control ids to monitor, or [] to use any controller.@*
## @var{initialvalues} - initial values to use for controls. It should be the same size as @var{ctrlid}@*
## @var{propertyname}, @var{propertyvalue} - properties to set on the controller. If a device is not specified
## the value from getpref("midi", "DefaultDevice", 0) will be used.@*
##
##
## Known properties are:
## @table @asis
## @item mididevice
## name of the mididevice to monitor.
## @item outputmode
## the scaling mode for values: 'rawmidi' will return values between 0 .. 127,
## 'normalized' will use values between 0 .. 1.
## @end table
##
## @subsubheading Outputs
## @var{obj} - returns a midicontrols object
##
## @subsubheading Examples
## Create a midicontrols object monitoring control id 2001 on the default midi device
## @example
## ctrl = midicontrols(2001)
## @end example
##
## Create a midicontrols object monitoring control id 2001 on a a non default device
## @example
## ctrl = midicontrols(2001, 'mididevice', 1)
## @end example
##
## @seealso{midiread, midisync}
## @end deftypefn
properties (Access = private)
controls = [];
initialvalue = 0;
currentvalue = [];
device = [];
outscale = 1;
midiscale = 127;
endproperties
methods
function this = midicontrols (varargin)
devicename = "";
if nargin > 0
controls = varargin{1};
if !isnumeric(controls)
error ("Expected numeric controls ids");
endif
if isscalar(controls)
this.controls = [controls];
else
this.controls = controls;
endif
endif
if nargin > 1
if ischar (varargin{2})
propstart = 2;
initvals = [0];
else
propstart = 3;
initvals = varargin{2};
if !isnumeric (initvals)
error ("Expected numeric initial values");
endif
if isscalar (initvals)
initvals = [initvals];
endif
endif
if mod (nargin-propstart + 1, 2) != 0
error ("midicontrols: expected property name, value pairs");
endif
if !iscellstr (varargin (propstart:2:nargin))
error ("midicontrols: expected property names to be strings");
endif
for i = propstart:2:nargin
propname = tolower (varargin{i});
propvalue = varargin{i+1};
if strcmp (propname, "outputmode")
if !ischar (propvalue)
error ("output mode should be 'normalized' or 'rawmidi'")
elseif strcmpi (propvalue, "normalized")
this.outscale = 1;
elseif strcmpi (propvalue, "rawmidi")
this.outscale = 127;
else
error ("output mode should be 'normalized' or 'rawmidi'")
endif
elseif strcmp (propname, "mididevice")
devicename = propvalue;
else
error ("unknown property '%s'", propname)
endif
endfor
this.initialvalue = initvals/this.outscale;
endif
if length (this.controls) > 0
this.currentvalue = zeros (length(this.controls), 1);
else
this.currentvalue = zeros (1, 1);
endif
for i = 1:length (this.currentvalue)
this.currentvalue(i) = this.get_value (i, this.initialvalue);
endfor
if isempty(devicename)
devicename = getpref ("midi", "DefaultDevice", 0);
endif
this.device = mididevice (devicename);
endfunction
function delete (this)
try
this.callback("");
catch
# do nothing
end_try_catch
endfunction
function send (this, values)
if nargin < 2
values = this.initialvalue;
else
if isscalar(values)
values = [values];
endif
values = values/this.outscale;
endif
if isempty(this.controls)
warning ('Can not send control values when no specific controller ids were provided.')
val = this.get_value(1, values);
this.currentvalue(1) = val;
else
for i =1:length(this.controls)
ctrl = this.controls(i);
ch = int32(ctrl/1000);
id = mod(ctrl, 1000);
val = this.get_value(i, values);
this.currentvalue(i) = val;
midisend(this.device, midimsg("controlchange", ch, id, val*this.midiscale));
endfor
endif
endfunction
function val = recv(this)
mx = midireceive(this.device);
while !isempty(mx)
for j = 1:length(mx)
m = mx(j);
if strcmp(m.type, "ControlChange")
if isempty(this.controls)
idx = 1;
else
ctrlid = m.channel*1000 + double(m.msgbytes(2));
idx = find(this.controls==ctrlid, 1);
endif
if !isempty(idx)
val = double(m.msgbytes(3))/this.midiscale;
this.currentvalue(idx) = val;
endif
endif
endfor
mx = midireceive(this.device);
endwhile
val = zeros(length(this.currentvalue));
for i = 1:length(this.currentvalue)
val(i) = this.get_value(i, this.currentvalue)*this.outscale;
endfor
endfunction
function val = callback(this, cb=[])
# set/unset callback
if nargin < 2
val = __midicallback__(this.device);
else
if isempty(cb)
val = __midicallback__(this.device, cb, []);
else
val = __midicallback__(this.device, cb, this);
endif
endif
if ischar(val) && isempty(val)
val = [];
endif
endfunction
function val = get_value(this, ch, values)
if isscalar(values)
val = values;
else
if i > length(values)
val = values(i);
else
val = 0;
endif
endif
val = double(val);
endfunction
function out = disp (this)
if nargout == 0
disp(sprintf (" midicontrols object: listening for events on %s", this.device.Input));
else
out = sprintf ("midicontrols object: listening for events on %s\n", this.device.Input);
endif
if ( isempty(this.controls))
if nargout == 0
disp (" any control"); % any control on {devname}
else
out = [out " any control\n"];
endif
else
if nargout == 0
disp ([" controls " sprintf("%d ", this.controls)]);
else
out = [out " controls " sprintf("%d ", this.controls) "\n"];
endif
endif
endfunction
endmethods
endclassdef
%!xtest
%! a = midicontrols();
%! assert(isa(a, "midicontrols"));
|