tagscan now tries to guess track number
[k8muffin.git] / src / tagview.c
blob33edc8c11c406524e6d3448bc8e9e012f56b5f17
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef _BSD_SOURCE
18 # define _BSD_SOURCE
19 #endif
21 //#include <ctype.h>
22 #include <dirent.h>
23 #include <iconv.h>
24 #include <limits.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
34 #include <tag_c.h>
36 #include "common.h"
38 #include "translit.h"
41 ////////////////////////////////////////////////////////////////////////////////
42 static char *trimstr (char *s) {
43 char *res = s;
44 if (s != NULL && s[0]) {
45 //char *olds = strdup(s), *xs = s;
46 char *ns, *p;
47 // change all '/' to underlines
48 for (ns = s; *ns; ++ns) if (*ns == '/' || *ns == 127) *ns = '_';
49 // remove duplicate underlines
50 for (ns = p = s; *ns; ++ns) {
51 if (*ns == '_') {
52 if (p > s && p[-1] != '_') *p++ = *ns;
53 } else {
54 *p++ = *ns;
57 *p = 0;
58 // remove duplicate spaces
59 for (ns = p = s; *ns; ++ns) {
60 if (leIsSpace(*ns)) {
61 if (p > s && !leIsSpace(p[-1])) *p++ = *ns;
62 } else {
63 *p++ = *ns;
66 *p = 0;
67 // trim trailing spaces and underlines
68 ns = s+strlen(s)-1;
69 while (ns >= s && (leIsSpace(*ns) || *ns == '_')) --ns;
70 ns[1] = 0;
71 // trim leading spaces and underlines
72 for (ns = s; *ns && (leIsSpace(*ns) || *ns == '_'); ++ns) ;
73 if (ns != s) memmove(s, ns, strlen(ns)+1);
75 return res;
79 ////////////////////////////////////////////////////////////////////////////////
80 #define xstrdup(ss) ({ \
81 const char *s = ss; \
82 char *d = alloca(s != NULL ? strlen(s)+1 : 1); \
83 if (s != NULL) strcpy(d, s); \
84 d; \
87 static int show_tags (const char *filename, int donl) {
88 int res = 0;
89 TagLib_Tag *tag;
90 //const TagLib_AudioProperties *properties;
91 TagLib_File *file;
92 taglib_set_strings_unicode(1);
93 file = taglib_file_new(filename);
94 if (file == NULL) return 0;
95 tag = taglib_file_tag(file);
96 if (tag != NULL) {
97 char *artist = trimstr(xstrdup(taglib_tag_artist(tag)));
98 char *album = trimstr(xstrdup(taglib_tag_album(tag)));
99 char *title = trimstr(xstrdup(taglib_tag_title(tag)));
100 char *genre = trimstr(xstrdup(taglib_tag_genre(tag)));
101 unsigned int yy = taglib_tag_year(tag);
102 unsigned int tno = taglib_tag_track(tag);
104 if (yy > 0) {
105 if (yy < 30) yy += 2000;
106 else if (yy < 100) yy += 1900;
107 else if (yy < 1930) yy = 0;
109 if (tno == 0 || tno > 99) tno = 0;
110 if (artist[0] || album[0] || title[0] || genre[0] || yy || tno) {
111 if (donl) printf("\n");
112 if (artist[0]) printf("ARTIST=%s\n", artist);
113 if (album[0]) printf("ALBUM=%s\n", album);
114 if (title[0]) printf("TITLE=%s\n", title);
115 if (genre[0]) printf("GENRE=%s\n", genre);
116 if (yy) printf("YEAR=%u\n", yy);
117 if (tno) printf("TRACKNUMBER=%u\n", tno);
118 res = 1;
120 // fuck comments
124 properties = taglib_file_audioproperties(file);
125 if (properties != NULL) {
126 int seconds = taglib_audioproperties_length(properties)%60;
127 int minutes = (taglib_audioproperties_length(properties)-seconds)/60;
128 printf("-- AUDIO --\n");
129 printf("bitrate : %d\n", taglib_audioproperties_bitrate(properties));
130 printf("sample rate: %d\n", taglib_audioproperties_samplerate(properties));
131 printf("channels : %d\n", taglib_audioproperties_channels(properties));
132 printf("length : %d:%02i\n", minutes, seconds);
136 taglib_tag_free_strings();
137 taglib_file_free(file);
138 return res;
142 ////////////////////////////////////////////////////////////////////////////////
143 int main (int argc, char *argv[]) {
144 int donl = 0;
145 for (int f = 1; f < argc; ++f) {
146 if (show_tags(argv[f], donl) > 0) donl = 1;
149 return 0;