Debian Codegraph @

test/printmarkers.c

raw
1 /*
2 Audio File Library
3
4 Copyright (C) 2002, Silicon Graphics, Inc.
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 as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 /*
22 printmarkers
23
24 This program lists the markers in an audio file.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <math.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include <audiofile.h>
36
37 void printtime (AFframecount position, double rate)
38 {
39 double time_in_seconds;
40
41 time_in_seconds = (double) position / rate;
42
43 /* Handle hours. */
44 if (time_in_seconds > 3600)
45 {
46 printf("%d:", (int) (time_in_seconds / 3600));
47 time_in_seconds = fmod(time_in_seconds, 3600);
48 }
49
50 /* Handle minutes. */
51 if (time_in_seconds > 60)
52 {
53 printf("%02d:", (int) (time_in_seconds / 60));
54 time_in_seconds = fmod(time_in_seconds, 60);
55 }
56
57 /* Handle seconds and milliseconds. */
58 printf("%02.3f", time_in_seconds);
59 }
60
61 int main (int argc, char **argv)
62 {
63 AFfilehandle file;
64 double rate;
65 int markcount;
66 int *markids;
67 int i;
68
69 if (argc != 2)
70 {
71 fprintf(stderr, "usage: %s filename\n", argv[0]);
72 fprintf(stderr, "where filename is the name of an audio file containing markers\n");
73 exit(0);
74 }
75
76 file = afOpenFile(argv[1], "r", NULL);
77 if (file == AF_NULL_FILEHANDLE)
78 {
79 fprintf(stderr, "Could not open file '%s' for reading.", argv[1]);
80 exit(0);
81 }
82
83 markcount = afGetMarkIDs(file, AF_DEFAULT_TRACK, NULL);
84 if (markcount <= 0)
85 {
86 fprintf(stderr, "The file '%s' does not contain any markers.", argv[1]);
87 exit(0);
88 }
89
90 markids = calloc(markcount, sizeof (int));
91 if (markids == NULL)
92 {
93 fprintf(stderr, "Could not allocate enough memory for markers.");
94 exit(0);
95 }
96
97 afGetMarkIDs(file, AF_DEFAULT_TRACK, markids);
98
99 rate = afGetRate(file, AF_DEFAULT_TRACK);
100
101 for (i=0; i<markcount; i++)
102 {
103 AFframecount position;
104 const char *name, *comment;
105
106 position = afGetMarkPosition(file, AF_DEFAULT_TRACK, markids[i]);
107
108 name = afGetMarkName(file, AF_DEFAULT_TRACK, markids[i]);
109 comment = afGetMarkComment(file, AF_DEFAULT_TRACK, markids[i]);
110
111 printf("marker %d, position %lld, time ", markids[i], position);
112
113 printtime(position, rate);
114
115 printf("\n");
116
117 if (name != NULL)
118 printf("\tname: %s\n", name);
119 if (comment != NULL)
120 printf("\tcomment: %s\n", comment);
121 }
122
123 afCloseFile(file);
124
125 return EXIT_SUCCESS;
126 }