comparison src/crossfade/timing.c @ 3059:2e241e90494a

Import work in progress xmms-crossfade rewrite.
author William Pitcock <nenolod@atheme.org>
date Fri, 24 Apr 2009 05:57:35 -0500
parents
children
comparison
equal deleted inserted replaced
3058:2e649bf16ebc 3059:2e241e90494a
1 /* timing.c
2 *
3 * get timing info based on file name
4 *
5 * file name should have a comment like this:
6 * R(RMS)-T(in):(out):(end)
7 * where:
8 * - RMS is an indication of the RMS value in between in and out
9 * (to be compared against maximum sample value, 32767 for 16 bit audio)
10 * - in, out and end are floats, giving the mixing start and end points
11 * and the length of the file in seconds
12 */
13
14 #include "timing.h"
15 #include "crossfade.h"
16 #include "debug.h"
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <locale.h>
21
22 #if defined(HAVE_ID3LIB)
23 # include <id3.h>
24 #endif
25
26 #undef VERBOSE
27
28 int
29 get_timing_comment(char *filename, quantaudio_t *qa)
30 /*
31 * check if the file given has a timing comment
32 * return 1 if it does and 0 if it doesn't
33 * store the relevant data in quantaudio
34 */
35 {
36 id3_t id3;
37 int nscanned;
38
39 setlocale(LC_NUMERIC, "C");
40
41 get_id3(filename, &id3);
42 if ((nscanned = sscanf(id3.comment, "R:%d-T:%f:%f:%f", &qa->RMS, &qa->mix_in, &qa->mix_out, &qa->length)) < 4)
43 {
44 /* tag not right */
45 #ifdef VERBOSE
46 DEBUG(("[crossfade] get_timing_comment: no quantaudio comment\n"));
47 DEBUG(("[crossfade] get_timing_comment: nscanned=%d (\"%s\")\n", nscanned, id3.comment));
48 DEBUG(("[crossfade] get_timing_comment: in %.2f, out %.2f, length %.2f, RMS %d\n",
49 qa->mix_in, qa->mix_out, qa->length, qa->RMS));
50 #endif
51 return 0;
52 }
53 else
54 {
55 #ifdef VERBOSE
56 DEBUG(("[crossfade] get_timing_comment: in %.2f, out %.2f, length %.2f, RMS %d\n",
57 qa->mix_in, qa->mix_out, qa->length, qa->RMS));
58 #endif
59 return 1;
60 }
61 }
62
63 int
64 get_id3(char *filename, id3_t *id3)
65 #if defined(HAVE_ID3LIB)
66 /* get the id3 tag of this file. Return 0 when failed or 1 when ok */
67 {
68 int status = 0;
69 ID3Tag *tag;
70 memset(id3, 0, sizeof(*id3));
71
72 if ((tag = ID3Tag_New()) != NULL)
73 {
74 ID3Frame *frame;
75 (void) ID3Tag_Link(tag, filename);
76
77 if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_COMMENT)) != NULL)
78 {
79 ID3Field *field;
80 if ((field = ID3Frame_GetField(frame, ID3FN_TEXT)) != NULL)
81 {
82 (void) ID3Field_GetASCII(field, id3->comment, sizeof(id3->comment));
83 DEBUG(("[crossfade] get_id3: comment: %s\n", id3->comment));
84 status = 1;
85 }
86 }
87
88 if ((frame = ID3Tag_FindFrameWithID(tag, ID3FID_TRACKNUM)) != NULL)
89 {
90 ID3Field *field;
91 if ((field = ID3Frame_GetField(frame, ID3FN_TEXT)) != NULL)
92 {
93 char buf[32];
94 buf[0] = 0;
95 (void) ID3Field_GetASCII(field, buf, sizeof(buf));
96 id3->track = atoi(buf);
97 DEBUG(("[crossfade] get_id3: track: %d\n", id3->track));
98 status = 1;
99 }
100 }
101
102 ID3Tag_Delete(tag);
103 }
104
105 return status;
106 }
107 #else
108 {
109 FILE *fp;
110 memset(id3, 0, sizeof(*id3));
111
112 fp = fopen(filename, "r"); /* read only */
113 if (fp == NULL)
114 {
115 /* file didn't open */
116 DEBUG(("[crossfade] get_id3: file %s didn't open !\n", filename));
117 return 0;
118 }
119 if (fseek(fp, -128, SEEK_END) < 0)
120 {
121 /* problem rewinding */
122 DEBUG(("[crossfade] get_id3: problem rewinding on %s !\n", filename));
123 return 0;
124 }
125 else
126 {
127 /* we rewound successfully */
128 if (fread(id3, 128, 1, fp) < 0)
129 {
130 /* read error */
131 DEBUG(("[crossfade] get_id3: read error on %s !\n", filename));
132 return 0;
133 }
134 }
135
136 return 1;
137 }
138 #endif