| 1 | /* |
| 2 | * File: mdutil.m - Utility routines for manipulating MD |
| 3 | * |
| 4 | * Copyright (C) 1999 Steve Ratcliffe |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | #include "glib.h" |
| 18 | #include "elements.h" |
| 19 | #include "except.h" |
| 20 | #include "intl.h" |
| 21 | |
| 22 | #include "md.h" |
| 23 | |
| 24 | /* |
| 25 | * Given a container c then md_walk iterates through all |
| 26 | * elements and calls fn for each one with the following |
| 27 | * arguments: |
| 28 | * |
| 29 | * fn(element, arg, start-end-flags) |
| 30 | * |
| 31 | * The flag MD_WALK_START is set if this is an element start and |
| 32 | * MD_WALK_END is set if this is an element end. Both are set for |
| 33 | * empty elements (also can use MD_WALK_EMPTY) |
| 34 | * |
| 35 | * Arguments: |
| 36 | * c - Container to be walked |
| 37 | * fn - Callback function to call on each element |
| 38 | * arg - Argument passed to fn |
| 39 | * flags - Flags eg. include all elements or just current ones. |
| 40 | */ |
| 41 | void |
| 42 | md_walk(struct containerElement *c, walkFunc fn, void *arg, int flags) |
| 43 | { |
| 44 | struct element *el; |
| 45 | GPtrArray *ar; |
| 46 | int i; |
| 47 | |
| 48 | fn(MD_ELEMENT(c), arg, MD_WALK_START); |
| 49 | |
| 50 | ar = c->elements; |
| 51 | for (i = 0; i < ar->len; i++) { |
| 52 | el = g_ptr_array_index(ar, i); |
| 53 | if (iscontainer(el)) { |
| 54 | md_walk(MD_CONTAINER(el), fn, arg, flags); |
| 55 | } else { |
| 56 | fn(el, arg, MD_WALK_EMPTY); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn(MD_ELEMENT(c), arg, MD_WALK_END); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Return true if the element is some kind of container. |
| 65 | * Arguments: |
| 66 | * el - Element to be tested |
| 67 | */ |
| 68 | int |
| 69 | iscontainer(struct element *el) |
| 70 | { |
| 71 | |
| 72 | if (el->type >= MD_CONTAINER_BEGIN) |
| 73 | return 1; |
| 74 | else |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Return an object that can be passed to md_sequence_next to |
| 80 | * iterate over all the elements in a tree in time sequenced |
| 81 | * order. This is what you want if you actually want to play the |
| 82 | * song for example. The elements in the container must |
| 83 | * already be in sorted order within their own tracks or |
| 84 | * subcontainers. |
| 85 | * |
| 86 | * Arguments: |
| 87 | * root - Root to sequence over |
| 88 | */ |
| 89 | struct sequenceState * |
| 90 | md_sequence_init(struct rootElement *root) |
| 91 | { |
| 92 | struct sequenceState *state; |
| 93 | int ntracks; |
| 94 | int i; |
| 95 | |
| 96 | state = g_new(struct sequenceState, 1); |
| 97 | |
| 98 | ntracks = MD_CONTAINER(root)->elements->len; |
| 99 | state->nmerge = ntracks; |
| 100 | state->track_ptrs = g_new(struct trackPos, ntracks); |
| 101 | state->root = root; |
| 102 | state->endtime = 0; |
| 103 | |
| 104 | /* XXX time ignored for present */ |
| 105 | /* Initialise the pointers */ |
| 106 | for (i = 0; i < ntracks; i++) { |
| 107 | struct containerElement *c; |
| 108 | /* c is a track or a tempo element */ |
| 109 | c = MD_CONTAINER(MD_CONTAINER(root)->elements->pdata[i]); |
| 110 | state->track_ptrs[i].len = c->elements->len; |
| 111 | state->track_ptrs[i].count = 0; |
| 112 | state->track_ptrs[i].currel = (struct element **)c->elements->pdata; |
| 113 | if (MD_ELEMENT(c)->type == MD_TYPE_TRACK) |
| 114 | if (MD_TRACK(c)->final_time > state->endtime) |
| 115 | state->endtime = MD_TRACK(c)->final_time; |
| 116 | } |
| 117 | |
| 118 | return state; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Return the next element as sorted by time. |
| 123 | * Arguments: |
| 124 | * seq - Sequence state information |
| 125 | */ |
| 126 | struct element * |
| 127 | md_sequence_next(struct sequenceState *seq) |
| 128 | { |
| 129 | struct element *el; |
| 130 | unsigned long leasttime; |
| 131 | struct trackPos *besttp; |
| 132 | struct trackPos *tp; |
| 133 | int i; |
| 134 | |
| 135 | if (seq->root) { |
| 136 | /* The first time we return the root element */ |
| 137 | el = MD_ELEMENT(seq->root); |
| 138 | seq->root = NULL; |
| 139 | return el; |
| 140 | } |
| 141 | |
| 142 | leasttime = -1; |
| 143 | besttp = NULL; |
| 144 | |
| 145 | for (i = 0; i < seq->nmerge; i++) { |
| 146 | tp = &seq->track_ptrs[i]; |
| 147 | /* Still some left? */ |
| 148 | if (tp->count < tp->len) { |
| 149 | el = *tp->currel; |
| 150 | if (leasttime == -1 || el->element_time < leasttime) { |
| 151 | leasttime = el->element_time; |
| 152 | besttp = tp; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | if (besttp) { |
| 157 | el = *besttp->currel; |
| 158 | besttp->count++; |
| 159 | besttp->currel++; |
| 160 | return el; |
| 161 | } |
| 162 | |
| 163 | /* Nothing left */ |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Finish with a sequence object. Frees all resources. The |
| 169 | * object cannot be used any more. |
| 170 | * Arguments: |
| 171 | * seq - Sequence state information |
| 172 | */ |
| 173 | void |
| 174 | md_sequence_end(struct sequenceState *seq) |
| 175 | { |
| 176 | |
| 177 | if (!seq) |
| 178 | return; |
| 179 | if (seq->track_ptrs) |
| 180 | g_free(seq->track_ptrs); |
| 181 | g_free(seq); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Get the final time for the song. |
| 186 | * Arguments: |
| 187 | * seq - Sequence state information |
| 188 | */ |
| 189 | unsigned long |
| 190 | md_sequence_end_time(struct sequenceState *seq) |
| 191 | { |
| 192 | return seq->endtime; |
| 193 | } |
| 194 |