1 /***************************************************************************
2 copyright : (C) 2012 by Lukáš Lalinský
3 email : lalinsky@gmail.com
4
5 copyright : (C) 2002 - 2008 by Scott Wheeler
6 email : wheeler@kde.org
7 (original Vorbis implementation)
8 ***************************************************************************/
9
10 /***************************************************************************
11 * This library is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License version *
13 * 2.1 as published by the Free Software Foundation. *
14 * *
15 * This library is distributed in the hope that it will be useful, but *
16 * WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18 * Lesser General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU Lesser General Public *
21 * License along with this library; if not, write to the Free Software *
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
23 * 02110-1301 USA *
24 * *
25 * Alternatively, this file is available under the Mozilla Public *
26 * License Version 1.1. You may obtain a copy of the License at *
27 * http://www.mozilla.org/MPL/ *
28 ***************************************************************************/
29
30 #include <tstring.h>
31 #include <tdebug.h>
32 #include <tpropertymap.h>
33 #include <tagutils.h>
34
35 #include "opusfile.h"
36
37 using namespace TagLib;
38 using namespace TagLib::Ogg;
39
40 class Opus::File::FilePrivate
41 {
42 public:
FilePrivate()43 FilePrivate() :
44 comment(0),
45 properties(0) {}
46
~FilePrivate()47 ~FilePrivate()
48 {
49 delete comment;
50 delete properties;
51 }
52
53 Ogg::XiphComment *comment;
54 Properties *properties;
55 };
56
57 ////////////////////////////////////////////////////////////////////////////////
58 // static members
59 ////////////////////////////////////////////////////////////////////////////////
60
isSupported(IOStream * stream)61 bool Ogg::Opus::File::isSupported(IOStream *stream)
62 {
63 // An Opus file has IDs "OggS" and "OpusHead" somewhere.
64
65 const ByteVector buffer = Utils::readHeader(stream, bufferSize(), false);
66 return (buffer.find("OggS") >= 0 && buffer.find("OpusHead") >= 0);
67 }
68
69 ////////////////////////////////////////////////////////////////////////////////
70 // public members
71 ////////////////////////////////////////////////////////////////////////////////
72
File(FileName file,bool readProperties,Properties::ReadStyle)73 Opus::File::File(FileName file, bool readProperties, Properties::ReadStyle) :
74 Ogg::File(file),
75 d(new FilePrivate())
76 {
77 if(isOpen())
78 read(readProperties);
79 }
80
File(IOStream * stream,bool readProperties,Properties::ReadStyle)81 Opus::File::File(IOStream *stream, bool readProperties, Properties::ReadStyle) :
82 Ogg::File(stream),
83 d(new FilePrivate())
84 {
85 if(isOpen())
86 read(readProperties);
87 }
88
~File()89 Opus::File::~File()
90 {
91 delete d;
92 }
93
tag() const94 Ogg::XiphComment *Opus::File::tag() const
95 {
96 return d->comment;
97 }
98
properties() const99 PropertyMap Opus::File::properties() const
100 {
101 return d->comment->properties();
102 }
103
setProperties(const PropertyMap & properties)104 PropertyMap Opus::File::setProperties(const PropertyMap &properties)
105 {
106 return d->comment->setProperties(properties);
107 }
108
audioProperties() const109 Opus::Properties *Opus::File::audioProperties() const
110 {
111 return d->properties;
112 }
113
save()114 bool Opus::File::save()
115 {
116 if(!d->comment)
117 d->comment = new Ogg::XiphComment();
118
119 setPacket(1, ByteVector("OpusTags", 8) + d->comment->render(false));
120
121 return Ogg::File::save();
122 }
123
124 ////////////////////////////////////////////////////////////////////////////////
125 // private members
126 ////////////////////////////////////////////////////////////////////////////////
127
read(bool readProperties)128 void Opus::File::read(bool readProperties)
129 {
130 ByteVector opusHeaderData = packet(0);
131
132 if(!opusHeaderData.startsWith("OpusHead")) {
133 setValid(false);
134 debug("Opus::File::read() -- invalid Opus identification header");
135 return;
136 }
137
138 ByteVector commentHeaderData = packet(1);
139
140 if(!commentHeaderData.startsWith("OpusTags")) {
141 setValid(false);
142 debug("Opus::File::read() -- invalid Opus tags header");
143 return;
144 }
145
146 d->comment = new Ogg::XiphComment(commentHeaderData.mid(8));
147
148 if(readProperties)
149 d->properties = new Properties(this);
150 }
151