File: metadata.cpp

package info (click to toggle)
libtunepimp 0.5.3-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,668 kB
  • ctags: 3,470
  • sloc: ansic: 15,652; cpp: 12,752; sh: 11,929; perl: 1,179; python: 720; makefile: 310; pascal: 33
file content (188 lines) | stat: -rw-r--r-- 5,616 bytes parent folder | download | duplicates (2)
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
/*----------------------------------------------------------------------------

   libtunepimp -- The MusicBrainz tagging library.  
                  Let a thousand taggers bloom!
   
   Copyright (C) Robert Kaye 2003
   
   This file is part of libtunepimp.

   libtunepimp is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   libtunepimp 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libtunepimp; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   $Id: metadata.cpp 8359 2006-08-07 20:34:50Z luks $

----------------------------------------------------------------------------*/
#include <math.h>
#include <stdio.h>
#include "metadata.h"
#include "../config.h"
using namespace std;

extern "C"
{
   #include "astrcmp.h"
}

const int numAlbumTypeStrings = 11;
const char *albumTypeStrings[] =
{
    "album", "single", "EP", "compilation", "soundtrack",
    "spokenword", "interview", "audiobook", "live", "remix", "other", "\0"
};

const int numAlbumStatusStrings = 3;
const char *albumStatusStrings[] =
{
    "official", "promotion", "bootleg", "\0"
};

TPAlbumType convertToAlbumType(const char *albumType)
{
    for(int i = 0;; i++)
    {
        if (albumTypeStrings[i][0] == 0)
            break;

        if (strlen(albumType) > 4 && strcasecmp(albumTypeStrings[i], albumType + 4) == 0)
            return (TPAlbumType)i;

        if (strcasecmp(albumTypeStrings[i], albumType) == 0)
            return (TPAlbumType)i;
    }
    return eAlbumType_Error;
}

void convertFromAlbumType(TPAlbumType type, string &albumType)
{
    if ((int)type >= 0 && (int)type < numAlbumTypeStrings)
        albumType = string(albumTypeStrings[(int)type]);
    else 
        albumType = "unknown";
}

TPAlbumStatus convertToAlbumStatus(const char *albumStatus)
{
    for(int i = 0;; i++)
    {
        if (albumStatusStrings[i][0] == 0)
            break;

        if (strlen(albumStatus) > 6 && strcasecmp(albumStatusStrings[i], albumStatus + 6) == 0)
            return (TPAlbumStatus)i;

        if (strcasecmp(albumStatusStrings[i], albumStatus) == 0)
            return (TPAlbumStatus)i;
    }
    return eAlbumStatus_Error;
}

void convertFromAlbumStatus(TPAlbumStatus status, string &albumStatus)
{
    if ((int)status >= 0 && (int)status < numAlbumStatusStrings)
        albumStatus = string(albumStatusStrings[(int)status]);
    else 
        albumStatus = "unknown";
}

const int numFlags = 6;
const int artistFlag =    0x01;
const int albumFlag =     0x02;
const int trackFlag =     0x04;
const int trackNumFlag =  0x08;
const int durationFlag =  0x10;
const int albumTypeFlag = 0x20;
const int lastFlag =      0x20; 

const int proportions[6] = 
{
    10, // artist     0x01
    9,  // album      0x02
    10, // track      0x04
    8,  // trackNum   0x08
    10,  // duration   0x10
    8,  // albumType  0x20
};

double MetadataCompare::durationSim(int trackA, int trackB) const
{
    int diff;

    diff = abs(trackA - trackB);
    if (diff > 30000)
       return 0;

    return 1.0 - ((double)diff / (double)30000);
}

int MetadataCompare::compare(const Metadata &a, const Metadata &b) const
{
    int index = 0, i;
    Metadata A = a, B = b;
    float weights[numFlags];

    // If one of the two is completely empty of meaningful info, just return 0
    if ((A.artist.empty() && A.album.empty() && A.track.empty()) ||
        (B.artist.empty() && B.album.empty() && B.track.empty()))
        return 0;

    if (!A.artist.empty() && !B.artist.empty())
        index |= artistFlag;

    // If one album is blank, and the other is an album, copy it over to favor it.
    //if (A.album.empty() && !B.album.empty() && B.albumType == eAlbumType_Album)
    //    A.album = B.album;

    // Now check the reverse case as well.
    //if (B.album.empty() && !A.album.empty() && A.albumType == eAlbumType_Album)
    //    B.album = A.album;

    if (!A.album.empty() && !B.album.empty())
        index |= albumFlag;

    if (!A.track.empty() && !B.track.empty())
        index |= trackFlag;

    if (A.trackNum != 0 && B.trackNum != 0)
        index |= trackNumFlag;

    if (A.duration != 0 && B.duration != 0)
        index |= durationFlag;

    if (A.albumType != eAlbumType_Error && B.albumType != eAlbumType_Error)
        index |= albumTypeFlag;

    if (index == 0)
        return 0;

    int total = 0;
    for(i = 0; i < numFlags; i++)
        if ((index & (1 << i)) != 0)
        {
            weights[i] = proportions[i];
            total += proportions[i];
        }
        else
            weights[i] = 0.0;

    for(i = 0; i < numFlags; i++)
        weights[i] /= (float)total;

    return (int)ceil(min(100.0, ((astrcmp(A.artist.c_str(), B.artist.c_str()) * weights[0]) +
                      (astrcmp(A.album.c_str(), B.album.c_str()) * weights[1]) +
                      (astrcmp(A.track.c_str(), B.track.c_str()) * weights[2]) +
                      (durationSim(A.duration, B.duration) * weights[3]) +
                      (((A.trackNum == B.trackNum) ? 1.0 : 0.0) * weights[4]) +  
                      (((A.albumType == B.albumType) ? 1.0 : 0.0) * weights[5])) * 100));
}