(Metux) autogen.sh: not running ./configure anymore (breaks certain distro builders)
[mirror-ossqm-audiofile.git] / libaudiofile / marker.c
blobd921298dd0f95b19e31095914887a63ec08db6f1
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 marker.c
25 This file contains routines for dealing with loop markers.
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #include <string.h>
33 #include <stdlib.h>
34 #include <assert.h>
36 #include "audiofile.h"
37 #include "afinternal.h"
38 #include "util.h"
40 _Marker *_af_marker_find_by_id (_Track *track, int markerid)
42 int i;
44 assert(track);
46 for (i=0; i<track->markerCount; i++)
47 if (track->markers[i].id == markerid)
48 return &track->markers[i];
50 _af_error(AF_BAD_MARKID, "no mark with id %d found in track %d",
51 markerid, track->id);
53 return NULL;
56 void afInitMarkIDs(AFfilesetup setup, int trackid, int markids[], int nmarks)
58 int i;
59 _TrackSetup *track;
61 if (!_af_filesetup_ok(setup))
62 return;
64 if ((track = _af_filesetup_get_tracksetup(setup, trackid)) == NULL)
65 return;
67 if (track->markers != NULL)
69 for (i=0; i<track->markerCount; i++)
71 if (track->markers[i].name != NULL)
72 free(track->markers[i].name);
73 if (track->markers[i].comment != NULL)
74 free(track->markers[i].comment);
76 free(track->markers);
79 track->markers = _af_calloc(nmarks, sizeof (struct _MarkerSetup));
80 track->markerCount = nmarks;
82 for (i=0; i<nmarks; i++)
84 track->markers[i].id = markids[i];
85 track->markers[i].name = _af_strdup("");
86 track->markers[i].comment = _af_strdup("");
89 track->markersSet = true;
92 void afInitMarkName(AFfilesetup setup, int trackid, int markid,
93 const char *namestr)
95 int markno;
96 int length;
98 _TrackSetup *track = NULL;
100 assert(setup);
101 assert(markid > 0);
103 track = _af_filesetup_get_tracksetup(setup, trackid);
104 assert(track);
106 if (track == NULL)
108 _af_error(AF_BAD_TRACKID, "bad track id");
109 return;
112 for (markno=0; markno<track->markerCount; markno++)
114 if (track->markers[markno].id == markid)
115 break;
118 if (markno == track->markerCount)
120 _af_error(AF_BAD_MARKID, "no marker id %d for file setup", markid);
121 return;
124 length = strlen(namestr);
125 if (length > 255)
127 _af_error(AF_BAD_STRLEN,
128 "warning: marker name truncated to 255 characters");
129 length = 255;
132 if (track->markers[markno].name)
133 free(track->markers[markno].name);
134 if ((track->markers[markno].name = _af_malloc(length+1)) == NULL)
135 return;
136 strncpy(track->markers[markno].name, namestr, length);
138 The null terminator is not set by strncpy if
139 strlen(namestr) > length. Set it here.
141 track->markers[markno].name[length] = '\0';
144 void afInitMarkComment(AFfilesetup setup, int trackid, int markid,
145 const char *commstr)
147 int markno;
148 int length;
149 _TrackSetup *track = NULL;
151 assert(setup);
152 assert(markid > 0);
154 track = _af_filesetup_get_tracksetup(setup, trackid);
155 assert(track);
157 if (track == NULL)
159 _af_error(AF_BAD_TRACKID, "bad track id");
160 return;
163 for (markno=0; markno<track->markerCount; markno++)
165 if (track->markers[markno].id == markid)
166 break;
169 if (markno == track->markerCount)
171 _af_error(AF_BAD_MARKID, "no marker id %d for file setup", markid);
172 return;
175 length = strlen(commstr);
177 if (track->markers[markno].comment)
178 free(track->markers[markno].comment);
179 if ((track->markers[markno].comment = _af_malloc(length+1)) == NULL)
180 return;
181 strcpy(track->markers[markno].comment, commstr);
184 char *afGetMarkName (AFfilehandle file, int trackid, int markid)
186 _Track *track;
187 _Marker *marker;
189 assert(file != NULL);
190 assert(markid > 0);
192 if (!_af_filehandle_ok(file))
193 return NULL;
195 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
196 return NULL;
198 if ((marker = _af_marker_find_by_id(track, markid)) == NULL)
199 return NULL;
201 return marker->name;
204 char *afGetMarkComment (AFfilehandle file, int trackid, int markid)
206 _Track *track;
207 _Marker *marker;
209 assert(file != NULL);
210 assert(markid > 0);
212 if (!_af_filehandle_ok(file))
213 return NULL;
215 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
216 return NULL;
218 if ((marker = _af_marker_find_by_id(track, markid)) == NULL)
219 return NULL;
221 return marker->comment;
224 void afSetMarkPosition (AFfilehandle file, int trackid, int markid,
225 AFframecount pos)
227 _Track *track;
228 _Marker *marker;
230 assert(file != NULL);
231 assert(markid > 0);
233 if (!_af_filehandle_ok(file))
234 return;
236 if (!_af_filehandle_can_write(file))
237 return;
239 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
240 return;
242 if ((marker = _af_marker_find_by_id(track, markid)) == NULL)
243 return;
245 if (pos < 0)
247 _af_error(AF_BAD_MARKPOS, "invalid marker position %d", pos);
248 pos = 0;
251 marker->position = pos;
254 int afGetMarkIDs (AFfilehandle file, int trackid, int markids[])
256 _Track *track;
258 assert(file);
260 if (!_af_filehandle_ok(file))
261 return -1;
263 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
264 return -1;
266 if (markids != NULL)
268 int i;
270 for (i=0; i<track->markerCount; i++)
272 markids[i] = track->markers[i].id;
276 return track->markerCount;
279 AFframecount afGetMarkPosition (AFfilehandle file, int trackid, int markid)
281 _Track *track;
282 _Marker *marker;
284 assert(file);
285 assert(markid > 0);
287 if (!_af_filehandle_ok(file))
288 return 0L;
290 if ((track = _af_filehandle_get_track(file, trackid)) == NULL)
291 return 0L;
293 if ((marker = _af_marker_find_by_id(track, markid)) == NULL)
294 return 0L;
296 return marker->position;
299 _Marker *_af_marker_new (int count)
301 _Marker *markers = _af_calloc(count, sizeof (_Marker));
302 if (markers == NULL)
303 return NULL;
305 return markers;