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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
## 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/>.
## -*- texinfo -*-
## @deftypefn {} {} midifilewrite (@var{filename}, @var{msgs})
## @deftypefnx {} {} midifilewrite (@var{filename}, @var{msgs}, @var{optionname}, @var{optionvalue})
## Write a midifile
##
## @subsubheading Inputs
## @var{filename} - filename of file to open.@*
## @var{msg} - a midimsg struct or a cell array of midimsg containing data to write to file@*
## @var{optionname}, @var{optionvalue} - option value/name pairs@*
##
## Known options are:
## @table @asis
## @item format
## MIDI file format number. (0 (default), 1, 2)
## @end table
##
## Where format is 0, if a cell array is passed to midifilewrite, the midimsg values will
## be concatenated together before saving.
##
## Were format is not 0, the cell array is treated as tracks of misimsg.
##
## @subsubheading Outputs
## None
## @seealso{midifileread, midimsg}
## @end deftypefn
function midifilewrite(varargin)
if nargin < 2
error ("Expected filename and messages");
endif
filename = varargin{1};
msg = varargin{2};
if iscell(msg)
for idx=1:length(msg)
if !isa(msg{idx}, "midimsg")
error ("Expected midimsg type messages in cell array");
endif
endfor
elseif !isa(msg, "midimsg")
error ("Expected midimsg type messages");
endif
if mod (nargin, 2) != 0
error ("midifilewrite: expected property name, value pairs");
endif
if !iscellstr (varargin (3:2:nargin))
error ("midifilewrite: expected property names to be strings");
endif
format = 0;
for idx = 3:2:nargin
propname = tolower (varargin{idx});
propvalue = varargin{idx+1};
if strcmp(propname, "format")
if isnumeric(propvalue)
format = int16(propvalue);
else
format = -1;
endif
if format != 1 && format != 2 && format != 0
error ("format must be one of 0,1,2");
endif
else
warning("Unknown option '%s' ignored", propname)
endif
endfor
if format == 0
trackcnt = 1;
# if we had a cell string, make a single track list
if iscell(msg)
cellmsg = msg;
msg = midimsg(0);
for idx=1:length(cellmsg)
msg = [msg cellmsg{idx}];
endfor
endif
else
# multi track mode, so make us a cell array
if ! iscell(msg)
msg = {msg};
endif
trackcnt = length(msg);
endif
fd = fopen (filename, "wb");
unwind_protect
# block hdr
hdr = {};
hdr.blocksize = 6;
hdr.blocktype = "MThd";
writeheader (fd, hdr);
# info
ticks_per_qtr = 1e3;
frames = floor(ticks_per_qtr/256);
ticks = mod(ticks_per_qtr, 256);
fwrite (fd, format, "uint16", 0, "ieee-be");
fwrite (fd, trackcnt, "uint16", 0, "ieee-be");
fwrite (fd, frames, "uint8");
fwrite (fd, ticks, "uint8");
tempo = 6e7/120;
if format == 0
# the tracks
fixpos = ftell (fd);
hdr.blocksize = 0;
hdr.blocktype = "MTrk";
writeheader (fd, hdr);
lasttime = [];
la = [];
for idx=1:length(msg)
a = msg(idx);
# on first time, set lasttime
if isempty(lasttime)
lasttime = a.timestamp;
endif
if a.timestamp >= lasttime
ts = (a.timestamp - lasttime);
else
ts = a.timestamp;
endif
lasttime = lasttime + ts;
ts = (ts*1e6)/(tempo/ticks_per_qtr);
if !isempty(la) && la.msgbytes(1) == 0xF0
# sysex msg previously started
sz = length(a.msgbytes) + 1;
setvariable (fd, sz);
fwrite (fd, a.msgbytes);
fwrite (fd, uint8([0xF7]));
elseif !isempty(a) && a.msgbytes(1) != 0xF7
setvariable (fd, ts);
fwrite (fd, a.msgbytes);
# tempo meta event
if a.msgbytes(1) == 0xFF && a.msgbytes(2) == 0x51
# FF 51 3 TT TT TT
tempo = polyval(double(a.msgbytes(4:end)), 256);
endif
endif
la = a;
endfor
# write eot and fix the header
fwrite (fd, uint8([0x01 0xff 0x2f 0x00]));
# fix the size of the track
cpos = ftell (fd);
len = cpos - fixpos - 8;
fseek (fd, fixpos, SEEK_SET);
hdr.blocksize = len;
hdr.blocktype = "MTrk";
writeheader (fd, hdr);
fseek (fd, cpos, SEEK_SET);
else
# the tracks
for t=1:length(msg)
fixpos = ftell (fd);
hdr.blocksize = 0;
hdr.blocktype = "MTrk";
writeheader (fd, hdr);
lasttime = [];
m = msg{t};
la = [];
for idx=1:length(m)
a = m(idx);
# on first time, set lasttime
if isempty(lasttime)
lasttime = a.timestamp;
endif
if a.timestamp >= lasttime
ts = (a.timestamp - lasttime);
else
ts = a.timestamp;
endif
lasttime = lasttime + ts;
ts = (ts*1e6)/(tempo/ticks_per_qtr);
if !isempty(la) && la.msgbytes(1) == 0xF0
# sysex msg previously started
sz = length(a.msgbytes) + 1;
setvariable (fd, sz);
fwrite (fd, a.msgbytes);
fwrite (fd, uint8([0xF7]));
elseif !isempty(a) && a.msgbytes(1) != 0xF7
setvariable (fd, ts);
fwrite (fd, a.msgbytes);
endif
la = a;
endfor
# write eot and fix the header
fwrite (fd, uint8([0x01 0xff 0x2f 0x00]));
# fix the size of the track
cpos = ftell (fd);
len = cpos - fixpos - 8;
fseek (fd, fixpos, SEEK_SET);
hdr.blocksize = len;
hdr.blocktype = "MTrk";
writeheader (fd, hdr);
fseek (fd, cpos, SEEK_SET);
endfor
endif
unwind_protect_cleanup
fclose (fd);
end_unwind_protect
endfunction
%!shared testname
%! testname = tempname;
%!test
%! data = midimsg("note", 1, 60, 100, 2);
%! midifilewrite(testname, data);
%! info = midifileinfo(testname);
%! t = info.header;
%! assert(info.header.format, 0);
%! assert(info.header.tracks, 1);
%! msg = midifileread(testname);
%! assert(msg(1).msgbytes, uint8([0x90 0x3C 0x64]));
%! assert(msg(2).msgbytes, uint8([0x90 0x3C 0x00]));
%! assert(msg(2).timestamp, 2);
%!test
%! data = midimsg("note", 1, 60, 100, 2);
%!
%! midifilewrite(testname, data, 'format', 0);
%! info = midifileinfo(testname);
%! assert(info.header.format, 0);
%! assert(info.header.tracks, 1);
%! msg = midifileread(testname);
%! assert(length(msg), 2);
%!
%! midifilewrite(testname, data, 'format', 1);
%! info = midifileinfo(testname);
%! assert(info.header.format, 1);
%! assert(info.header.tracks, 1);
%! msg = midifileread(testname);
%! assert(length(msg), 2);
%!
%! midifilewrite(testname, {data, data}, 'format', 0);
%! info = midifileinfo(testname);
%! assert(info.header.format, 0);
%! assert(info.header.tracks, 1);
%! msg = midifileread(testname);
%! assert(length(msg), 4);
%!
%! midifilewrite(testname, {data, data}, 'format', 1);
%! info = midifileinfo(testname);
%! assert(info.header.format, 1);
%! assert(info.header.tracks, 2);
%! msg = midifileread(testname);
%! assert(length(msg), 4);
%!test
%! if exist (testname, 'file');
%! delete (testname);
%! end
|