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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
Audio File Library
Copyright (C) 1998-2000, Michael Pruett <michael@68k.org>
Copyright (C) 2000, Silicon Graphics, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307 USA.
*/
/*
Loop.cpp
All routines that operate on loops.
*/
#include "config.h"
#include "FileHandle.h"
#include "Instrument.h"
#include "Setup.h"
#include "afinternal.h"
#include "audiofile.h"
#include "util.h"
void afInitLoopIDs (AFfilesetup setup, int instid, const int *loopids, int nloops)
{
if (!_af_filesetup_ok(setup))
return;
if (!_af_unique_ids(loopids, nloops, "loop", AF_BAD_LOOPID))
return;
InstrumentSetup *instrument = setup->getInstrument(instid);
if (!instrument)
return;
instrument->freeLoops();
if (!instrument->allocateLoops(nloops))
return;
for (int i=0; i < nloops; i++)
instrument->loops[i].id = loopids[i];
}
int afGetLoopIDs (AFfilehandle file, int instid, int *loopids)
{
if (!_af_filehandle_ok(file))
return AF_FAIL;
Instrument *instrument = file->getInstrument(instid);
if (!instrument)
return AF_FAIL;
if (loopids)
for (int i=0; i < instrument->loopCount; i++)
loopids[i] = instrument->loops[i].id;
return instrument->loopCount;
}
/*
getLoop returns pointer to requested loop if it exists, and if
mustWrite is true, only if handle is writable.
*/
static Loop *getLoop (AFfilehandle handle, int instid, int loopid,
bool mustWrite)
{
if (!_af_filehandle_ok(handle))
return NULL;
if (mustWrite && !handle->checkCanWrite())
return NULL;
Instrument *instrument = handle->getInstrument(instid);
if (!instrument)
return NULL;
return instrument->getLoop(loopid);
}
/*
Set loop mode (as in AF_LOOP_MODE_...).
*/
void afSetLoopMode (AFfilehandle file, int instid, int loopid, int mode)
{
Loop *loop = getLoop(file, instid, loopid, true);
if (!loop)
return;
if (mode != AF_LOOP_MODE_NOLOOP &&
mode != AF_LOOP_MODE_FORW &&
mode != AF_LOOP_MODE_FORWBAKW)
{
_af_error(AF_BAD_LOOPMODE, "unrecognized loop mode %d", mode);
return;
}
loop->mode = mode;
}
/*
Get loop mode (as in AF_LOOP_MODE_...).
*/
int afGetLoopMode (AFfilehandle file, int instid, int loopid)
{
Loop *loop = getLoop(file, instid, loopid, false);
if (!loop)
return -1;
return loop->mode;
}
/*
Set loop count.
*/
int afSetLoopCount (AFfilehandle file, int instid, int loopid, int count)
{
Loop *loop = getLoop(file, instid, loopid, true);
if (!loop)
return AF_FAIL;
if (count < 1)
{
_af_error(AF_BAD_LOOPCOUNT, "invalid loop count: %d", count);
return AF_FAIL;
}
loop->count = count;
return AF_SUCCEED;
}
/*
Get loop count.
*/
int afGetLoopCount(AFfilehandle file, int instid, int loopid)
{
Loop *loop = getLoop(file, instid, loopid, false);
if (!loop)
return -1;
return loop->count;
}
/*
Set loop start marker id in the file structure
*/
void afSetLoopStart(AFfilehandle file, int instid, int loopid, int markid)
{
Loop *loop = getLoop(file, instid, loopid, true);
if (!loop)
return;
loop->beginMarker = markid;
}
/*
Get loop start marker id.
*/
int afGetLoopStart (AFfilehandle file, int instid, int loopid)
{
Loop *loop = getLoop(file, instid, loopid, false);
if (!loop)
return -1;
return loop->beginMarker;
}
/*
Set loop start frame in the file structure.
*/
int afSetLoopStartFrame (AFfilehandle file, int instid, int loopid, AFframecount startFrame)
{
Loop *loop = getLoop(file, instid, loopid, true);
if (!loop)
return -1;
if (startFrame < 0)
{
_af_error(AF_BAD_FRAME, "loop start frame must not be negative");
return AF_FAIL;
}
int trackid = loop->trackid;
int beginMarker = loop->beginMarker;
afSetMarkPosition(file, trackid, beginMarker, startFrame);
return AF_SUCCEED;
}
/*
Get loop start frame.
*/
AFframecount afGetLoopStartFrame (AFfilehandle file, int instid, int loopid)
{
Loop *loop = getLoop(file, instid, loopid, false);
if (!loop)
return -1;
int trackid = loop->trackid;
int beginMarker = loop->beginMarker;
return afGetMarkPosition(file, trackid, beginMarker);
}
/*
Set loop track id.
*/
void afSetLoopTrack (AFfilehandle file, int instid, int loopid, int track)
{
Loop *loop = getLoop(file, instid, loopid, true);
if (!loop) return;
loop->trackid = track;
}
/*
Get loop track.
*/
int afGetLoopTrack (AFfilehandle file, int instid, int loopid)
{
Loop *loop = getLoop(file, instid, loopid, false);
if (!loop)
return -1;
return loop->trackid;
}
/*
Set loop end frame marker id.
*/
void afSetLoopEnd (AFfilehandle file, int instid, int loopid, int markid)
{
Loop *loop = getLoop(file, instid, loopid, true);
if (!loop)
return;
loop->endMarker = markid;
}
/*
Get loop end frame marker id.
*/
int afGetLoopEnd (AFfilehandle file, int instid, int loopid)
{
Loop *loop = getLoop(file, instid, loopid, false);
if (!loop)
return -1;
return loop->endMarker;
}
/*
Set loop end frame.
*/
int afSetLoopEndFrame (AFfilehandle file, int instid, int loopid, AFframecount endFrame)
{
Loop *loop = getLoop(file, instid, loopid, true);
if (!loop)
return -1;
if (endFrame < 0)
{
_af_error(AF_BAD_FRAME, "loop end frame must not be negative");
return AF_FAIL;
}
int trackid = loop->trackid;
int endMarker = loop->endMarker;
afSetMarkPosition(file, trackid, endMarker, endFrame);
return AF_SUCCEED;
}
/*
Get loop end frame.
*/
AFframecount afGetLoopEndFrame (AFfilehandle file, int instid, int loopid)
{
Loop *loop = getLoop(file, instid, loopid, false);
if (!loop)
return -1;
int trackid = loop->trackid;
int endMarker = loop->endMarker;
return afGetMarkPosition(file, trackid, endMarker);
}
|