(Metux) autogen.sh: not running ./configure anymore (breaks certain distro builders)
[mirror-ossqm-audiofile.git] / libaudiofile / loop.c
blobe0c13887b3909bc43c9488d9a67958325b476af7
1 /*
2 Audio File Library
3 Copyright (C) 1998-2000, Michael Pruett <michael@68k.org>
4 Copyright (C) 2000, Silicon Graphics, Inc.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library 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 GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307 USA.
23 loop.c
25 All routines that operate on loops.
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include "audiofile.h"
33 #include "afinternal.h"
34 #include "util.h"
35 #include "setup.h"
36 #include "instrument.h"
38 void afInitLoopIDs (AFfilesetup setup, int instid, int *loopids, int nloops)
40 int instno;
42 if (!_af_filesetup_ok(setup))
43 return;
45 if (!_af_unique_ids(loopids, nloops, "loop", AF_BAD_LOOPID))
46 return;
48 if ((instno = _af_setup_instrument_index_from_id(setup, instid)) == -1)
49 return;
51 _af_setup_free_loops(setup, instno);
53 setup->instruments[instno].loopCount = nloops;
54 setup->instruments[instno].loopSet = true;
56 if (nloops == 0)
57 setup->instruments[instno].loops = NULL;
58 else
60 int i;
62 if ((setup->instruments[instno].loops = _af_calloc(nloops, sizeof (_LoopSetup))) == NULL)
63 return;
65 for (i=0; i < nloops; i++)
66 setup->instruments[instno].loops[i].id = loopids[i];
70 int afGetLoopIDs (AFfilehandle file, int instid, int *loopids)
72 int instno;
73 int i;
75 if (!_af_filehandle_ok(file))
76 return AF_FAIL;
78 if ((instno = _af_handle_instrument_index_from_id(file, instid)) == -1)
79 return AF_FAIL;
81 if (loopids)
82 for (i=0; i < file->instruments[instno].loopCount; i++)
83 loopids[i] = file->instruments[instno].loops[i].id;
85 return file->instruments[instno].loopCount;
88 int _af_handle_loop_index_from_id (AFfilehandle file, int instno, int loopid)
90 int i;
91 for (i=0; i<file->instruments[instno].loopCount; i++)
92 if (file->instruments[instno].loops[i].id == loopid)
93 return i;
95 _af_error(AF_BAD_LOOPID, "no loop with id %d for instrument %d",
96 loopid, file->instruments[instno].id);
98 return -1;
102 getLoop returns pointer to requested loop if it exists, and if
103 mustWrite is true, only if handle is writable.
106 static _Loop *getLoop (AFfilehandle handle, int instid, int loopid,
107 bool mustWrite)
109 int loopno, instno;
111 if (!_af_filehandle_ok(handle))
112 return NULL;
114 if (mustWrite && !_af_filehandle_can_write(handle))
115 return NULL;
117 if ((instno = _af_handle_instrument_index_from_id(handle, instid)) == -1)
118 return NULL;
120 if ((loopno = _af_handle_loop_index_from_id(handle, instno, loopid)) == -1)
121 return NULL;
123 return &handle->instruments[instno].loops[loopno];
127 Set loop mode (as in AF_LOOP_MODE_...).
129 void afSetLoopMode (AFfilehandle file, int instid, int loopid, int mode)
131 _Loop *loop = getLoop(file, instid, loopid, true);
133 if (!loop)
134 return;
136 if (mode != AF_LOOP_MODE_NOLOOP &&
137 mode != AF_LOOP_MODE_FORW &&
138 mode != AF_LOOP_MODE_FORWBAKW)
140 _af_error(AF_BAD_LOOPMODE, "unrecognized loop mode %d", mode);
141 return;
144 loop->mode = mode;
148 Get loop mode (as in AF_LOOP_MODE_...).
150 int afGetLoopMode (AFfilehandle file, int instid, int loopid)
152 _Loop *loop = getLoop(file, instid, loopid, false);
154 if (loop == NULL)
155 return -1;
157 return loop->mode;
161 Set loop count.
163 int afSetLoopCount (AFfilehandle file, int instid, int loopid, int count)
165 _Loop *loop = getLoop(file, instid, loopid, true);
167 if (loop == NULL)
168 return AF_FAIL;
170 if (count < 1)
172 _af_error(AF_BAD_LOOPCOUNT, "invalid loop count: %d", count);
173 return AF_FAIL;
176 loop->count = count;
177 return AF_SUCCEED;
181 Get loop count.
183 int afGetLoopCount(AFfilehandle file, int instid, int loopid)
185 _Loop *loop = getLoop(file, instid, loopid, false);
187 if (loop == NULL)
188 return -1;
190 return loop->count;
194 Set loop start marker id in the file structure
196 void
197 afSetLoopStart(AFfilehandle file, int instid, int loopid, int markid)
199 _Loop *loop = getLoop(file, instid, loopid, true);
201 if (!loop)
202 return;
204 loop->beginMarker = markid;
208 Get loop start marker id.
210 int afGetLoopStart (AFfilehandle file, int instid, int loopid)
212 _Loop *loop = getLoop(file, instid, loopid, false);
214 if (loop == NULL)
215 return -1;
217 return loop->beginMarker;
221 Set loop start frame in the file structure.
223 int afSetLoopStartFrame (AFfilehandle file, int instid, int loopid, AFframecount startFrame)
225 int trackid, beginMarker;
226 _Loop *loop = getLoop(file, instid, loopid, true);
228 if (loop == NULL)
229 return -1;
231 if (startFrame < 0)
233 _af_error(AF_BAD_FRAME, "loop start frame must not be negative");
234 return AF_FAIL;
237 trackid = loop->trackid;
238 beginMarker = loop->beginMarker;
240 afSetMarkPosition(file, trackid, beginMarker, startFrame);
241 return AF_SUCCEED;
245 Get loop start frame.
247 AFframecount afGetLoopStartFrame (AFfilehandle file, int instid, int loopid)
249 int trackid, beginMarker;
250 _Loop *loop = getLoop(file, instid, loopid, false);
252 if (loop == NULL)
253 return -1;
255 trackid = loop->trackid;
256 beginMarker = loop->beginMarker;
258 return afGetMarkPosition(file, trackid, beginMarker);
262 Set loop track id.
264 void afSetLoopTrack (AFfilehandle file, int instid, int loopid, int track)
266 _Loop *loop = getLoop(file, instid, loopid, true);
268 if (!loop) return;
270 loop->trackid = track;
274 Get loop track.
276 int afGetLoopTrack (AFfilehandle file, int instid, int loopid)
278 _Loop *loop = getLoop(file, instid, loopid, false);
280 if (loop == NULL)
281 return -1;
283 return loop->trackid;
287 Set loop end frame marker id.
289 void afSetLoopEnd (AFfilehandle file, int instid, int loopid, int markid)
291 _Loop *loop = getLoop(file, instid, loopid, true);
293 if (!loop)
294 return;
296 loop->endMarker = markid;
300 Get loop end frame marker id.
302 int afGetLoopEnd (AFfilehandle file, int instid, int loopid)
304 _Loop *loop = getLoop(file, instid, loopid, false);
306 if (loop == NULL)
307 return -1;
309 return loop->endMarker;
313 Set loop end frame.
315 int afSetLoopEndFrame (AFfilehandle file, int instid, int loopid, AFframecount endFrame)
317 int trackid, endMarker;
318 _Loop *loop = getLoop(file, instid, loopid, true);
320 if (loop == NULL)
321 return -1;
323 if (endFrame < 0)
325 _af_error(AF_BAD_FRAME, "loop end frame must not be negative");
326 return AF_FAIL;
329 trackid = loop->trackid;
330 endMarker = loop->endMarker;
332 afSetMarkPosition(file, trackid, endMarker, endFrame);
333 return AF_SUCCEED;
337 Get loop end frame.
340 AFframecount afGetLoopEndFrame (AFfilehandle file, int instid, int loopid)
342 int trackid, endMarker;
343 _Loop *loop = getLoop(file, instid, loopid, false);
345 if (loop == NULL)
346 return -1;
348 trackid = loop->trackid;
349 endMarker = loop->endMarker;
351 return afGetMarkPosition(file, trackid, endMarker);