| 1 | /* |
| 2 | * |
| 3 | * File: elements.m - Operation on all elements |
| 4 | * |
| 5 | * Copyright (C) 1999 Steve Ratcliffe |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "glib.h" |
| 19 | #include "elements.h" |
| 20 | #include "except.h" |
| 21 | #include "intl.h" |
| 22 | |
| 23 | #include "md.h" |
| 24 | |
| 25 | static void md_container_init(struct containerElement *e); |
| 26 | |
| 27 | /* |
| 28 | * Create and initialise a element element. |
| 29 | */ |
| 30 | struct element * |
| 31 | md_element_new() |
| 32 | { |
| 33 | struct element * new; |
| 34 | |
| 35 | new = g_malloc0(sizeof(*new)); |
| 36 | MD_ELEMENT(new)->type = MD_TYPE_ELEMENT; |
| 37 | return new; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Create and initialise a container element. |
| 42 | */ |
| 43 | struct containerElement * |
| 44 | md_container_new() |
| 45 | { |
| 46 | struct containerElement * new; |
| 47 | |
| 48 | new = g_malloc0(sizeof(*new)); |
| 49 | MD_ELEMENT(new)->type = MD_TYPE_CONTAINER; |
| 50 | return new; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Initialize data structures within the element. This is |
| 55 | * only really required when pointers need allocating. |
| 56 | * Arguments: |
| 57 | * e - Element to init |
| 58 | */ |
| 59 | static void |
| 60 | md_container_init(struct containerElement *e) |
| 61 | { |
| 62 | |
| 63 | e->elements = g_ptr_array_new(); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Create and initialise a root element. |
| 68 | * Arguments: |
| 69 | * - |
| 70 | */ |
| 71 | struct rootElement * |
| 72 | md_root_new(void) |
| 73 | { |
| 74 | struct rootElement * new; |
| 75 | |
| 76 | new = g_malloc0(sizeof(*new)); |
| 77 | MD_ELEMENT(new)->type = MD_TYPE_ROOT; |
| 78 | md_container_init(MD_CONTAINER(new)); |
| 79 | return new; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Create and initialise a track element. |
| 84 | * Arguments: |
| 85 | * - |
| 86 | */ |
| 87 | struct trackElement * |
| 88 | md_track_new(void) |
| 89 | { |
| 90 | struct trackElement * new; |
| 91 | |
| 92 | new = g_malloc0(sizeof(*new)); |
| 93 | MD_ELEMENT(new)->type = MD_TYPE_TRACK; |
| 94 | md_container_init(MD_CONTAINER(new)); |
| 95 | return new; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Create and initialise a tempomap element. |
| 100 | */ |
| 101 | struct tempomapElement * |
| 102 | md_tempomap_new() |
| 103 | { |
| 104 | struct tempomapElement * new; |
| 105 | |
| 106 | new = g_malloc0(sizeof(*new)); |
| 107 | MD_ELEMENT(new)->type = MD_TYPE_TEMPOMAP; |
| 108 | md_container_init(MD_CONTAINER(new)); |
| 109 | return new; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Create and initialise a note element. |
| 114 | * Arguments: |
| 115 | * note - |
| 116 | * vel - |
| 117 | * length - |
| 118 | */ |
| 119 | struct noteElement * |
| 120 | md_note_new(short note, short vel, int length) |
| 121 | { |
| 122 | struct noteElement * new; |
| 123 | |
| 124 | new = g_malloc0(sizeof(*new)); |
| 125 | MD_ELEMENT(new)->type = MD_TYPE_NOTE; |
| 126 | new->note = note; |
| 127 | new->vel = vel; |
| 128 | new->length = length; |
| 129 | new->offvel = 0; |
| 130 | return new; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Create and initialise a part element. |
| 135 | * Arguments: |
| 136 | * - |
| 137 | */ |
| 138 | struct partElement * |
| 139 | md_part_new(void) |
| 140 | { |
| 141 | struct partElement * new; |
| 142 | |
| 143 | new = g_malloc0(sizeof(*new)); |
| 144 | MD_ELEMENT(new)->type = MD_TYPE_PART; |
| 145 | md_container_init(MD_CONTAINER(new)); |
| 146 | return new; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Create and initialise a control element. |
| 151 | * Arguments: |
| 152 | * control - |
| 153 | * value - |
| 154 | */ |
| 155 | struct controlElement * |
| 156 | md_control_new(short control, short value) |
| 157 | { |
| 158 | struct controlElement * new; |
| 159 | |
| 160 | new = g_malloc0(sizeof(*new)); |
| 161 | MD_ELEMENT(new)->type = MD_TYPE_CONTROL; |
| 162 | new->control = control; |
| 163 | new->value = value; |
| 164 | return new; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Create and initialise a program element. |
| 169 | * Arguments: |
| 170 | * program - |
| 171 | */ |
| 172 | struct programElement * |
| 173 | md_program_new(int program) |
| 174 | { |
| 175 | struct programElement * new; |
| 176 | |
| 177 | new = g_malloc0(sizeof(*new)); |
| 178 | MD_ELEMENT(new)->type = MD_TYPE_PROGRAM; |
| 179 | new->program = program; |
| 180 | return new; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Create and initialise a keytouch element. |
| 185 | * Arguments: |
| 186 | * note - |
| 187 | * vel - |
| 188 | */ |
| 189 | struct keytouchElement * |
| 190 | md_keytouch_new(int note, int vel) |
| 191 | { |
| 192 | struct keytouchElement * new; |
| 193 | |
| 194 | new = g_malloc0(sizeof(*new)); |
| 195 | MD_ELEMENT(new)->type = MD_TYPE_KEYTOUCH; |
| 196 | new->note = note; |
| 197 | new->velocity = vel; |
| 198 | return new; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Create and initialise a pressure element. |
| 203 | * Arguments: |
| 204 | * vel - |
| 205 | */ |
| 206 | struct pressureElement * |
| 207 | md_pressure_new(int vel) |
| 208 | { |
| 209 | struct pressureElement * new; |
| 210 | |
| 211 | new = g_malloc0(sizeof(*new)); |
| 212 | MD_ELEMENT(new)->type = MD_TYPE_PRESSURE; |
| 213 | new->velocity = vel; |
| 214 | return new; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Create and initialise a pitch element. |
| 219 | */ |
| 220 | struct pitchElement * |
| 221 | md_pitch_new(int val) |
| 222 | { |
| 223 | struct pitchElement * new; |
| 224 | |
| 225 | new = g_malloc0(sizeof(*new)); |
| 226 | MD_ELEMENT(new)->type = MD_TYPE_PITCH; |
| 227 | new->pitch = val; |
| 228 | return new; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Create and initialise a sysex element. |
| 233 | */ |
| 234 | struct sysexElement * |
| 235 | md_sysex_new(int status, unsigned char *data, int len) |
| 236 | { |
| 237 | struct sysexElement * new; |
| 238 | |
| 239 | new = g_malloc0(sizeof(*new)); |
| 240 | MD_ELEMENT(new)->type = MD_TYPE_SYSEX; |
| 241 | new->status = status; |
| 242 | new->data = data; |
| 243 | new->length = len; |
| 244 | return new; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Create and initialise a meta element. |
| 249 | */ |
| 250 | struct metaElement * |
| 251 | md_meta_new() |
| 252 | { |
| 253 | struct metaElement * new; |
| 254 | |
| 255 | new = g_malloc0(sizeof(*new)); |
| 256 | MD_ELEMENT(new)->type = MD_TYPE_META; |
| 257 | return new; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Create and initialise a map element. |
| 262 | */ |
| 263 | struct mapElement * |
| 264 | md_map_new() |
| 265 | { |
| 266 | struct mapElement * new; |
| 267 | |
| 268 | new = g_malloc0(sizeof(*new)); |
| 269 | MD_ELEMENT(new)->type = MD_TYPE_MAP; |
| 270 | return new; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Create and initialise a keysig element. |
| 275 | */ |
| 276 | struct keysigElement * |
| 277 | md_keysig_new(short key, short minor) |
| 278 | { |
| 279 | struct keysigElement * new; |
| 280 | |
| 281 | new = g_malloc0(sizeof(*new)); |
| 282 | MD_ELEMENT(new)->type = MD_TYPE_KEYSIG; |
| 283 | new->key = key; |
| 284 | new->minor = minor != 0? 1: 0; |
| 285 | return new; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Create and initialise a timesig element. |
| 290 | */ |
| 291 | struct timesigElement * |
| 292 | md_timesig_new(short top, short bottom, short clocks, short n32pq) |
| 293 | { |
| 294 | struct timesigElement * new; |
| 295 | |
| 296 | new = g_malloc0(sizeof(*new)); |
| 297 | MD_ELEMENT(new)->type = MD_TYPE_TIMESIG; |
| 298 | new->top = top; |
| 299 | new->bottom = bottom; |
| 300 | new->clocks = clocks; |
| 301 | new->n32pq = n32pq; |
| 302 | return new; |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Create and initialise a tempo element. |
| 307 | */ |
| 308 | struct tempoElement * |
| 309 | md_tempo_new(int m) |
| 310 | { |
| 311 | struct tempoElement * new; |
| 312 | |
| 313 | new = g_malloc0(sizeof(*new)); |
| 314 | MD_ELEMENT(new)->type = MD_TYPE_TEMPO; |
| 315 | new->micro_tempo = m; |
| 316 | return new; |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Create and initialise a text element. |
| 321 | */ |
| 322 | struct textElement * |
| 323 | md_text_new(int type, char *text) |
| 324 | { |
| 325 | struct textElement * new; |
| 326 | |
| 327 | new = g_malloc0(sizeof(*new)); |
| 328 | MD_ELEMENT(new)->type = MD_TYPE_TEXT; |
| 329 | { |
| 330 | static char *typenames[] = { |
| 331 | "", |
| 332 | "text", |
| 333 | "copyright", |
| 334 | "trackname", |
| 335 | "instrument", |
| 336 | "lyric", |
| 337 | "marker", |
| 338 | "cuepoint", |
| 339 | }; |
| 340 | new->type = type; |
| 341 | new->name = typenames[type]; |
| 342 | new->text = text; |
| 343 | if (text) |
| 344 | new->length = strlen(text); |
| 345 | else |
| 346 | new->length = 0; |
| 347 | } |
| 348 | return new; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Create and initialise a smpteoffset element. |
| 353 | */ |
| 354 | struct smpteoffsetElement * |
| 355 | md_smpteoffset_new(short hours, short minutes, short seconds, short frames, |
| 356 | short subframes) |
| 357 | { |
| 358 | struct smpteoffsetElement * new; |
| 359 | |
| 360 | new = g_malloc0(sizeof(*new)); |
| 361 | MD_ELEMENT(new)->type = MD_TYPE_SMPTEOFFSET; |
| 362 | new->hours = hours; |
| 363 | new->minutes = minutes; |
| 364 | new->seconds = seconds; |
| 365 | new->frames = frames; |
| 366 | new->subframes = subframes; |
| 367 | return new; |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Add an element to a container element. |
| 372 | */ |
| 373 | void |
| 374 | md_add(struct containerElement *c, struct element *e) |
| 375 | { |
| 376 | g_ptr_array_add(c->elements, e); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Free a complete element tree. |
| 381 | */ |
| 382 | void |
| 383 | md_free(struct element *el) |
| 384 | { |
| 385 | struct containerElement *c; |
| 386 | int i; |
| 387 | |
| 388 | if (el->type >= MD_CONTAINER_BEGIN) { |
| 389 | c = MD_CONTAINER(el); |
| 390 | for (i = 0; i < c->elements->len; i++) { |
| 391 | struct element *p = g_ptr_array_index(c->elements, i); |
| 392 | md_free(p); |
| 393 | } |
| 394 | g_ptr_array_free(c->elements, 1); |
| 395 | } |
| 396 | switch (el->type) { |
| 397 | case MD_TYPE_TEXT: |
| 398 | g_free(MD_TEXT(el)->text); |
| 399 | break; |
| 400 | case MD_TYPE_SYSEX: |
| 401 | g_free(MD_SYSEX(el)->data); |
| 402 | break; |
| 403 | } |
| 404 | g_free(el); |
| 405 | |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Check that the given element can be casted to the given type. |
| 410 | * This is mainly for debugging as mismatches will not happen |
| 411 | * in proper use. In particular do not use this routine to check |
| 412 | * types unless you want the program to exit if the check fails. |
| 413 | * Arguments: |
| 414 | * el - Element to be cast |
| 415 | * type - type to cast to |
| 416 | */ |
| 417 | struct element * |
| 418 | md_check_cast(struct element *el, int type) |
| 419 | { |
| 420 | |
| 421 | switch (type) { |
| 422 | case MD_TYPE_CONTAINER: |
| 423 | if (!iscontainer(el)) |
| 424 | except(debugError, "Cast to container from %d", el->type); |
| 425 | return el; |
| 426 | case MD_TYPE_ELEMENT: |
| 427 | /* Anything can be cast to an element */ |
| 428 | if (el->type > 100 || el->type < 0) |
| 429 | break; /* Sanity check */ |
| 430 | return el; |
| 431 | case MD_TYPE_META: |
| 432 | case MD_TYPE_MAP: |
| 433 | /* TEMP: this is a parent type */ |
| 434 | return el; |
| 435 | case MD_TYPE_TRACK: |
| 436 | if (el->type == MD_TYPE_TEMPOMAP) |
| 437 | return el; |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | if (type == el->type) |
| 442 | return el; |
| 443 | |
| 444 | except(debugError, "Cast from %d to %d not allowed", el->type, type); |
| 445 | return NULL;/* not reached */ |
| 446 | |
| 447 | } |
| 448 |