summaryrefslogtreecommitdiffstats
path: root/src/metadata/wavpack/wvfile.cpp
blob: 3890bc9ace03a621d5d925d7e90ae37d2875c55c (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/***************************************************************************
    copyright            : (C) 2006 by Lukáš Lalinský
    email                : lalinsky@gmail.com
    
    copyright            : (C) 2004 by Allan Sandfeld Jensen 
    email                : kde@carewolf.org 
                           (original MPC implementation)
 ***************************************************************************/

/***************************************************************************
 *   This library is free software; you can redistribute it and/or modify  *
 *   it  under the terms of the GNU Lesser General Public License version  *
 *   2.1 as published by the Free Software Foundation.                     *
 *                                                                         *
 *   This library 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     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,            *
 *   MA  02110-1301  USA                                                   *
 ***************************************************************************/

#include <tbytevector.h>
#include <tstring.h>
#if 0
#include <tdebug.h>
#endif

#include "wvfile.h"
#include "id3v1tag.h"
#include "id3v2header.h" 
#include "apetag.h"
#include "apefooter.h"
#include "combinedtag.h" 

using namespace TagLib;

class WavPack::File::FilePrivate
{
public:
  FilePrivate() :
    APETag(0),
    APELocation(-1),
    APESize(0),
    ID3v1Tag(0),
    ID3v1Location(-1),
    tag(0),     
    properties(0),
    scanned(false),
    hasAPE(false),
    hasID3v1(false) {}

  ~FilePrivate()
  {
    if (tag != ID3v1Tag && tag != APETag) delete tag;
    delete ID3v1Tag; 
    delete APETag;
    delete properties;
  }

  APE::Tag *APETag;
  long APELocation;
  uint APESize;

  ID3v1::Tag *ID3v1Tag;
  long ID3v1Location; 
  
  Tag *tag;
  
  Properties *properties;
  bool scanned;

  // These indicate whether the file *on disk* has these tags, not if
  // this data structure does.  This is used in computing offsets.

  bool hasAPE;
  bool hasID3v1; 
};

////////////////////////////////////////////////////////////////////////////////
// public members
////////////////////////////////////////////////////////////////////////////////

WavPack::File::File(const char *file, bool readProperties,
                Properties::ReadStyle propertiesStyle) : TagLib::File(file)
{
  d = new FilePrivate;
  read(readProperties, propertiesStyle);
}

WavPack::File::~File()
{
  delete d;
}

TagLib::Tag *WavPack::File::tag() const
{
  return d->tag;
}

WavPack::Properties *WavPack::File::audioProperties() const
{
  return d->properties;
}

bool WavPack::File::save()
{
  if(readOnly()) {
#if 0
    debug("WavPack::File::save() -- File is read only.");
#endif
    return false;
  }

  // Update ID3v1 tag

  if(d->ID3v1Tag) {
    if(d->hasID3v1) {
      seek(d->ID3v1Location);
      writeBlock(d->ID3v1Tag->render());
    }
    else {
      seek(0, End);
      d->ID3v1Location = tell();
      writeBlock(d->ID3v1Tag->render());
      d->hasID3v1 = true;
    }
  } else
    if(d->hasID3v1) {
      removeBlock(d->ID3v1Location, 128);
      d->hasID3v1 = false;
      if(d->hasAPE) {
        if(d->APELocation > d->ID3v1Location)
          d->APELocation -= 128;
      }
    }

  // Update APE tag

  if(d->APETag) {
    if(d->hasAPE)
      insert(d->APETag->render(), d->APELocation, d->APESize);
    else {
      if(d->hasID3v1)  {
        insert(d->APETag->render(), d->ID3v1Location, 0);
        d->APESize = d->APETag->footer()->completeTagSize();
        d->hasAPE = true;
        d->APELocation = d->ID3v1Location;
        d->ID3v1Location += d->APESize;
      }
      else {
        seek(0, End);
        d->APELocation = tell();
        writeBlock(d->APETag->render());
        d->APESize = d->APETag->footer()->completeTagSize();
        d->hasAPE = true;
      }
    }
  }
  else
    if(d->hasAPE) {
      removeBlock(d->APELocation, d->APESize);
      d->hasAPE = false;
      if(d->hasID3v1) {
        if (d->ID3v1Location > d->APELocation)
          d->ID3v1Location -= d->APESize;
      }
    }
    
   return true;
}

ID3v1::Tag *WavPack::File::ID3v1Tag(bool create)
{
  if(!create || d->ID3v1Tag)
    return d->ID3v1Tag;

  // no ID3v1 tag exists and we've been asked to create one

  d->ID3v1Tag = new ID3v1::Tag;

  if(d->APETag)
    d->tag = new CombinedTag(d->APETag, d->ID3v1Tag);
  else
    d->tag = d->ID3v1Tag;

  return d->ID3v1Tag;
}

APE::Tag *WavPack::File::APETag(bool create)
{
  if(!create || d->APETag)
    return d->APETag;

  // no APE tag exists and we've been asked to create one

  d->APETag = new APE::Tag;

  if(d->ID3v1Tag)
    d->tag = new CombinedTag(d->APETag, d->ID3v1Tag);
  else
    d->tag = d->APETag;

  return d->APETag;
}

void WavPack::File::remove(int tags)
{
  if(tags & ID3v1) {
    delete d->ID3v1Tag;
    d->ID3v1Tag = 0;

    if(d->APETag)
      d->tag = d->APETag;
    else
      d->tag = d->APETag = new APE::Tag;
  }

  if(tags & APE) {
    delete d->APETag;
    d->APETag = 0;

    if(d->ID3v1Tag)
      d->tag = d->ID3v1Tag;
    else
      d->tag = d->APETag = new APE::Tag;
  }
} 

////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////

void WavPack::File::read(bool readProperties, Properties::ReadStyle /* propertiesStyle */)
{
  // Look for an ID3v1 tag

  d->ID3v1Location = findID3v1();

  if(d->ID3v1Location >= 0) {
    d->ID3v1Tag = new ID3v1::Tag(this, d->ID3v1Location);
    d->hasID3v1 = true;
  }
  
  // Look for an APE tag

  d->APELocation = findAPE();

  if(d->APELocation >= 0) {
    d->APETag = new APE::Tag(this, d->APELocation);
    d->APESize = d->APETag->footer()->completeTagSize();
    d->APELocation = d->APELocation + d->APETag->footer()->size() - d->APESize;
    d->hasAPE = true;
  }

  if(d->hasID3v1 && d->hasAPE)
    d->tag = new CombinedTag(d->APETag, d->ID3v1Tag);
  else {
    if(d->hasID3v1)
      d->tag = d->ID3v1Tag;
    else {
      if(d->hasAPE)
        d->tag = d->APETag;
      else
        d->tag = d->APETag = new APE::Tag;
    }
  } 

  // Look for WavPack audio properties

  if(readProperties) {
    seek(0);
    d->properties = new Properties(readBlock(WavPack::HeaderSize),
                                   length() - d->APESize);
  }
}

long WavPack::File::findAPE()
{
  if(!isValid())
    return -1;

  if(d->hasID3v1)
    seek(-160, End);
  else
    seek(-32, End);

  long p = tell();

  if(readBlock(8) == APE::Tag::fileIdentifier())
    return p;

  return -1;
}

long WavPack::File::findID3v1()
{
  if(!isValid())
    return -1;

  seek(-128, End);
  long p = tell();

  if(readBlock(3) == ID3v1::Tag::fileIdentifier())
    return p;

  return -1;
}