summaryrefslogtreecommitdiff
path: root/libaudiofile/loop.c
blob: c0964c047c1c64f1d168813a3e8aec1cccadfe9f (plain)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
	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.c

	All routines that operate on loops.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "audiofile.h"
#include "afinternal.h"
#include "util.h"
#include "setup.h"
#include "instrument.h"

void afInitLoopIDs (AFfilesetup setup, int instid, int *loopids, int nloops)
{
	int instno;

	if (!_af_filesetup_ok(setup))
		return;

	if (!_af_unique_ids(loopids, nloops, "loop", AF_BAD_LOOPID))
		return;

	if ((instno = _af_setup_instrument_index_from_id(setup, instid)) == -1)
		return;

	_af_setup_free_loops(setup, instno);

	setup->instruments[instno].loopCount = nloops;
	setup->instruments[instno].loopSet = AF_TRUE;

	if (nloops == 0)
		setup->instruments[instno].loops = NULL;
	else
	{
		int i;

		if ((setup->instruments[instno].loops = _af_calloc(nloops, sizeof (_LoopSetup))) == NULL)
			return;

		for (i=0; i < nloops; i++)
			setup->instruments[instno].loops[i].id = loopids[i];
	}
}

int afGetLoopIDs (AFfilehandle file, int instid, int *loopids)
{
	int instno;
	int i;

	if (!_af_filehandle_ok(file))
		return AF_FAIL;

	if ((instno = _af_handle_instrument_index_from_id(file, instid)) == -1)
		return AF_FAIL;

	if (loopids)
		for (i=0; i < file->instruments[instno].loopCount; i++)
			loopids[i] = file->instruments[instno].loops[i].id;

	return file->instruments[instno].loopCount;
}

int _af_handle_loop_index_from_id (AFfilehandle file, int instno, int loopid)
{
	int	i;
	for (i=0; i<file->instruments[instno].loopCount; i++)
		if (file->instruments[instno].loops[i].id == loopid)
			return i;

	_af_error(AF_BAD_LOOPID, "no loop with id %d for instrument %d",
		loopid, file->instruments[instno].id);

	return -1;
}

/*
	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)
{
	int	loopno, instno;

	if (!_af_filehandle_ok(handle))
		return NULL;

	if (mustWrite && !_af_filehandle_can_write(handle))
		return NULL;

	if ((instno = _af_handle_instrument_index_from_id(handle, instid)) == -1)
		return NULL;

	if ((loopno = _af_handle_loop_index_from_id(handle, instno, loopid)) == -1)
		return NULL;

	return &handle->instruments[instno].loops[loopno];
}

/*
	Set loop mode (as in AF_LOOP_MODE_...).
*/
void afSetLoopMode (AFfilehandle file, int instid, int loopid, int mode)
{
	_Loop	*loop = getLoop(file, instid, loopid, AF_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, AF_FALSE);

	if (loop == NULL)
		return -1;

	return loop->mode;
}

/*
	Set loop count.
*/
int afSetLoopCount (AFfilehandle file, int instid, int loopid, int count)
{
	_Loop	*loop = getLoop(file, instid, loopid, AF_TRUE);

	if (loop == NULL)
		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, AF_FALSE);

	if (loop == NULL)
		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, AF_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, AF_FALSE);

	if (loop == NULL)
		return -1;

	return loop->beginMarker;
}

/*
	Set loop start frame in the file structure.
*/
int afSetLoopStartFrame (AFfilehandle file, int instid, int loopid, AFframecount startFrame)
{
	int	trackid, beginMarker;
	_Loop	*loop = getLoop(file, instid, loopid, AF_TRUE);

	if (loop == NULL)
		return -1;

	if (startFrame < 0)
	{
		_af_error(AF_BAD_FRAME, "loop start frame must not be negative");
		return AF_FAIL;
	}

	trackid = loop->trackid;
	beginMarker = loop->beginMarker;

	afSetMarkPosition(file, trackid, beginMarker, startFrame);
	return AF_SUCCEED;
}

/*
	Get loop start frame.
*/
AFframecount afGetLoopStartFrame (AFfilehandle file, int instid, int loopid)
{
	int	trackid, beginMarker;
	_Loop	*loop = getLoop(file, instid, loopid, AF_FALSE);

	if (loop == NULL)
		return -1;

	trackid = loop->trackid;
	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, AF_TRUE);

	if (!loop) return;

	loop->trackid = track;
}

/*
	Get loop track.
*/
int afGetLoopTrack (AFfilehandle file, int instid, int loopid)
{
	_Loop	*loop = getLoop(file, instid, loopid, AF_FALSE);

	if (loop == NULL)
		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, AF_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, AF_FALSE);

	if (loop == NULL)
		return -1;

	return loop->endMarker;
}

/*
	Set loop end frame.
*/
int afSetLoopEndFrame (AFfilehandle file, int instid, int loopid, AFframecount endFrame)
{
	int	trackid, endMarker;
	_Loop	*loop = getLoop(file, instid, loopid, AF_TRUE);

	if (loop == NULL)
		return -1;

	if (endFrame < 0)
	{
		_af_error(AF_BAD_FRAME, "loop end frame must not be negative");
		return AF_FAIL;
	}

	trackid = loop->trackid;
	endMarker = loop->endMarker;

	afSetMarkPosition(file, trackid, endMarker, endFrame);
	return AF_SUCCEED;
}

/*
	Get loop end frame.
*/

AFframecount afGetLoopEndFrame (AFfilehandle file, int instid, int loopid)
{
	int	trackid, endMarker;
	_Loop	*loop = getLoop(file, instid, loopid, AF_FALSE);

	if (loop == NULL)
		return -1;

	trackid = loop->trackid;
	endMarker = loop->endMarker;

	return afGetMarkPosition(file, trackid, endMarker);
}