]> git.sesse.net Git - kdenlive/blob - src/docclipbase.cpp
Only create audio thumbs when they are enabled, clear cache when disabled
[kdenlive] / src / docclipbase.cpp
1 /**************************1*************************************************
2                           DocClipBase.cpp  -  description
3                              -------------------
4     begin                : Fri Apr 12 2002
5     copyright            : (C) 2002 by Jason Wood
6     email                : jasonwood@blueyonder.co.uk
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <KDebug>
19
20 #include "kdenlivesettings.h"
21 #include "docclipbase.h"
22
23 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, uint id):
24         m_xml(xml), m_id(id), m_description(""), m_refcount(0), m_projectThumbFrame(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL) {
25     int type = xml.attribute("type").toInt();
26     m_clipType = (CLIPTYPE) type;
27     m_name = xml.attribute("name");
28     m_xml.setAttribute("id", QString::number(id));
29     KUrl url = KUrl(xml.attribute("resource"));
30     int out = xml.attribute("out").toInt();
31     if (out != 0) setDuration(GenTime(out, 25));
32     if (m_name.isEmpty()) m_name = url.fileName();
33     if (!url.isEmpty()) {
34         m_thumbProd = new KThumb(clipManager, url, KdenliveSettings::track_height() * KdenliveSettings::project_display_ratio(), KdenliveSettings::track_height());
35         connect(m_thumbProd, SIGNAL(audioThumbReady(QMap <int, QMap <int, QByteArray> >)), this , SLOT(updateAudioThumbnail(QMap <int, QMap <int, QByteArray> >)));
36         connect(this, SIGNAL(getAudioThumbs()), this , SLOT(slotGetAudioThumbs()));
37
38     }
39     kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
40
41     if (m_clipType == AV || m_clipType == AUDIO || m_clipType == UNKNOWN) {
42         m_audioTimer = new QTimer(this);
43         connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
44     }
45 }
46
47
48
49 DocClipBase::DocClipBase(const DocClipBase& clip) {
50     m_xml = clip.toXML();
51     m_id = clip.getId();
52     m_clipType = clip.clipType();
53     m_name = clip.name();
54     m_duration = clip.duration();
55     m_audioThumbCreated = clip.audioThumbCreated();
56 }
57
58 DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
59     DocClipBase::operator=(clip);
60     m_xml = clip.toXML();
61     m_id = clip.getId();
62     m_clipType = clip.clipType();
63     m_name = clip.name();
64     m_duration = clip.duration();
65     m_audioThumbCreated = clip.audioThumbCreated();
66     return *this;
67 }
68
69 DocClipBase::~DocClipBase() {
70     //if (m_thumbProd) delete m_thumbProd;
71 }
72
73 void DocClipBase::slotRequestAudioThumbs() {
74     emit getAudioThumbs();
75 }
76
77 void DocClipBase::slotClearAudioCache() {
78     audioFrameChache.clear();
79     m_audioThumbCreated = false;
80 }
81
82 KThumb *DocClipBase::thumbProducer() {
83     return m_thumbProd;
84 }
85
86 bool DocClipBase::audioThumbCreated() const {
87     return m_audioThumbCreated;
88 }
89
90 void DocClipBase::setName(const QString name) {
91     m_name = name;
92 }
93
94 const QString & DocClipBase::name() const {
95
96     return m_name;
97 }
98
99 uint DocClipBase::getId() const {
100     return m_id;
101 }
102
103 void DocClipBase::setId(const uint &newId) {
104     m_id = newId;
105 }
106
107 const CLIPTYPE & DocClipBase::clipType() const {
108     return m_clipType;
109 }
110
111 void DocClipBase::setClipType(CLIPTYPE type) {
112     m_clipType = type;
113 }
114
115 KUrl DocClipBase::fileURL() const {
116     QString res = m_xml.attribute("resource");
117     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
118     return KUrl();
119 }
120
121 void DocClipBase::setProjectThumbFrame(const uint &ix) {
122     m_projectThumbFrame = ix;
123 }
124
125 uint DocClipBase::getProjectThumbFrame() const {
126     return m_projectThumbFrame;
127 }
128
129 void DocClipBase::setDescription(const QString & description) {
130     m_description = description;
131 }
132
133 const QString & DocClipBase::description() const {
134     return m_description;
135 }
136
137 void DocClipBase::setDuration(GenTime dur) {
138     m_duration = dur;
139 }
140
141 const GenTime &DocClipBase::duration() const {
142     return m_duration;
143 }
144
145 bool DocClipBase::hasFileSize() const {
146     return true;
147 }
148
149
150 // virtual
151 QDomElement DocClipBase::toXML() const {
152     /*
153         QDomDocument doc;
154
155         QDomElement clip = doc.createElement("kdenliveclip");
156         QDomText text = doc.createTextNode(description());
157         clip.appendChild(text);
158         doc.appendChild(clip);
159     */
160     return m_xml;
161 }
162
163 DocClipBase *DocClipBase::
164 createClip(KdenliveDoc *doc, const QDomElement & element) {
165     DocClipBase *clip = 0;
166     QString description;
167     QDomNode node = element;
168     node.normalize();
169     if (element.tagName() != "kdenliveclip") {
170         kWarning() <<
171         "DocClipBase::createClip() element has unknown tagName : " <<
172         element.tagName() << endl;
173         return 0;
174     }
175
176     QDomNode n = element.firstChild();
177
178     while (!n.isNull()) {
179         QDomElement e = n.toElement();
180         if (!e.isNull()) {
181             QString tagName = e.tagName();
182             if (e.tagName() == "avfile") {
183                 // clip = DocClipAVFile::createClip(e);
184             } else if (e.tagName() == "DocTrackBaseList") {
185                 // clip = DocClipProject::createClip(doc, e);
186             }
187         } else {
188             QDomText text = n.toText();
189             if (!text.isNull()) {
190                 description = text.nodeValue();
191             }
192         }
193
194         n = n.nextSibling();
195     }
196     if (clip == 0) {
197         kWarning() << "DocClipBase::createClip() unable to create clip" <<
198         endl;
199     } else {
200         // setup DocClipBase specifics of the clip.
201         clip->setDescription(description);
202         clip->setAudioThumbCreated(false);
203     }
204     return clip;
205 }
206
207 void DocClipBase::setAudioThumbCreated(bool isDone) {
208     m_audioThumbCreated = isDone;
209 }
210
211
212 QDomDocument DocClipBase::generateSceneList(bool, bool) const {
213 }
214
215 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
216     m_thumbnail = pixmap;
217 }
218
219 const QPixmap & DocClipBase::thumbnail() const {
220     return m_thumbnail;
221 }
222
223 void DocClipBase::updateAudioThumbnail(QMap<int, QMap<int, QByteArray> > data) {
224     kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
225     audioFrameChache = data;
226     m_audioThumbCreated = true;
227     emit gotAudioData();
228 }
229
230 QList < GenTime > DocClipBase::snapMarkers() const {
231     QList < GenTime > markers;
232
233     for (uint count = 0; count < m_snapMarkers.count(); ++count) {
234         markers.append(m_snapMarkers[count].time());
235     }
236
237     return markers;
238 }
239
240 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const {
241     return m_snapMarkers;
242 }
243
244 void DocClipBase::setSnapMarkers(QList < CommentedTime > markers) {
245     m_snapMarkers = markers;
246 }
247
248 void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
249     QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
250     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
251         if ((*it).time() >= time)
252             break;
253     }
254
255     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
256         kError() <<
257         "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo"
258         << endl;
259     } else {
260         CommentedTime t(time, comment);
261         m_snapMarkers.insert(it, t);
262     }
263
264 }
265
266 void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
267     QList < CommentedTime >::Iterator it;
268     for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
269         if ((*it).time() == time)
270             break;
271     }
272     if (it != m_snapMarkers.end()) {
273         (*it).setComment(comment);
274     } else {
275         kError() <<
276         "trying to edit Snap Marker that does not already exists"  << endl;
277     }
278 }
279
280 QString DocClipBase::deleteSnapMarker(const GenTime & time) {
281     QString result = i18n("Marker");
282     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
283
284     while (itt != m_snapMarkers.end()) {
285         if ((*itt).time() == time)
286             break;
287         ++itt;
288     }
289
290     if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
291         result = (*itt).comment();
292         m_snapMarkers.erase(itt);
293     }
294     return result;
295 }
296
297
298 GenTime DocClipBase::hasSnapMarkers(const GenTime & time) {
299     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
300
301     while (itt != m_snapMarkers.end()) {
302         if ((*itt).time() == time)
303             return time;
304         ++itt;
305     }
306
307     return GenTime(0.0);
308 }
309
310 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime) {
311     int it;
312     for (it = 0; it < m_snapMarkers.count(); it++) {
313         if (m_snapMarkers[it].time() >= currTime)
314             break;
315     }
316     if (it == 0) return GenTime();
317     else if (it == m_snapMarkers.count() - 1 && m_snapMarkers[it].time() < currTime)
318         return m_snapMarkers[it].time();
319     else return m_snapMarkers[it-1].time();
320 }
321
322 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime) {
323     int it;
324     for (it = 0; it < m_snapMarkers.count(); it++) {
325         if (m_snapMarkers[it].time() > currTime)
326             break;
327     }
328     if (it < m_snapMarkers.count() && m_snapMarkers[it].time() > currTime) return m_snapMarkers[it].time();
329     return duration();
330 }
331
332 QString DocClipBase::markerComment(GenTime t) {
333     QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
334
335     while (itt != m_snapMarkers.end()) {
336         if ((*itt).time() == t)
337             return (*itt).comment();
338         ++itt;
339     }
340     return QString::null;
341 }
342
343 //static
344 QString DocClipBase::getTypeName(CLIPTYPE type) {
345     QString result;
346     switch (type) {
347     case AV:
348         result = i18n("Video Clip");
349         break;
350     case COLOR:
351         result = i18n("Color Clip");
352         break;
353     case PLAYLIST:
354         result = i18n("Playlist Clip");
355         break;
356     case IMAGE:
357         result = i18n("Image Clip");
358         break;
359     case SLIDESHOW:
360         result = i18n("Slideshow Clip");
361         break;
362     case VIRTUAL:
363         result = i18n("Virtual Clip");
364         break;
365     case AUDIO:
366         result = i18n("Audio Clip");
367         break;
368     case VIDEO:
369         result = i18n("Mute Video Clip");
370         break;
371     case TEXT:
372         result = i18n("Text Clip");
373         break;
374     default:
375         result = i18n("None");
376         break;
377     }
378     return result;
379 }
380
381 void DocClipBase::slotGetAudioThumbs() {
382
383     if (m_audioThumbCreated) {
384         if (m_audioTimer != NULL)
385             m_audioTimer->stop();
386     } else {
387         if (m_audioTimer != NULL)
388             m_audioTimer->start(2000);
389         double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
390         m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
391     }
392 }
393
394
395