Pencil2D Animation
Download Community News Docs Contribute

core_lib/src/structure/layersound.cpp Source File

  • Main Page
  • Related Pages
  • Classes
  • Files
  •  
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • structure
layersound.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "layersound.h"
18
19#include <QDebug>
20#include <QMediaPlayer>
21#include <QFileInfo>
22#include <QDir>
23#include "soundclip.h"
24
25
26LayerSound::LayerSound(int id) : Layer(id, Layer::SOUND)
27{
28 setName(tr("Sound Layer"));
29}
30
31LayerSound::~LayerSound()
32{
33}
34
35Status LayerSound::loadSoundClipAtFrame(const QString& sSoundClipName,
36 const QString& strFilePath,
37 int frameNumber)
38{
39 if (!QFile::exists(strFilePath))
40 {
41 return Status::FILE_NOT_FOUND;
42 }
43
44 QFileInfo info(strFilePath);
45 if (!info.isFile())
46 {
47 return Status::ERROR_LOAD_SOUND_FILE;
48 }
49
50 SoundClip* clip = new SoundClip;
51 clip->setSoundClipName(sSoundClipName);
52 clip->init(strFilePath);
53 clip->setPos(frameNumber);
54 loadKey(clip);
55 return Status::OK;
56}
57
58void LayerSound::updateFrameLengths(int fps)
59{
60 foreachKeyFrame([&fps](KeyFrame* pKeyFrame)
61 {
62 auto soundClip = dynamic_cast<SoundClip *>(pKeyFrame);
63 soundClip->updateLength(fps);
64 });
65}
66
67QDomElement LayerSound::createDomElement(QDomDocument& doc) const
68{
69 QDomElement layerElem = createBaseDomElement(doc);
70
71 foreachKeyFrame([&doc, &layerElem](KeyFrame* pKeyFrame)
72 {
73 SoundClip* clip = static_cast<SoundClip*>(pKeyFrame);
74
75 QDomElement imageTag = doc.createElement("sound");
76 imageTag.setAttribute("frame", clip->pos());
77 imageTag.setAttribute("name", clip->soundClipName());
78
79 QFileInfo info(clip->fileName());
80 //qDebug() << "Save=" << info.fileName();
81 imageTag.setAttribute("src", info.fileName());
82 layerElem.appendChild(imageTag);
83 });
84
85 return layerElem;
86}
87
88void LayerSound::loadDomElement(const QDomElement& element, QString dataDirPath, ProgressCallback progressStep)
89{
90 this->loadBaseDomElement(element);
91
92 QDomNode soundTag = element.firstChild();
93 while (!soundTag.isNull())
94 {
95 QDomElement soundElement = soundTag.toElement();
96 if (soundElement.isNull())
97 {
98 continue;
99 }
100
101 if (soundElement.tagName() == "sound")
102 {
103 const QString soundFile = soundElement.attribute("src");
104 const QString sSoundClipName = soundElement.attribute("name", "My Sound Clip");
105
106 if (!soundFile.isEmpty())
107 {
108 // the file is supposed to be in the data directory
109 const QString sFullPath = QDir(dataDirPath).filePath(soundFile);
110
111 int position = soundElement.attribute("frame").toInt();
112 Status st = loadSoundClipAtFrame(sSoundClipName, sFullPath, position);
113 Q_ASSERT(st.ok());
114 }
115 progressStep();
116 }
117
118 soundTag = soundTag.nextSibling();
119 }
120}
121
122Status LayerSound::saveKeyFrameFile(KeyFrame* key, QString path)
123{
124 Q_ASSERT(key);
125
126 if (key->fileName().isEmpty())
127 {
128 return Status::SAFE;
129 }
130
131 QFileInfo info(key->fileName());
132 QString sDestFileLocation = QDir(path).filePath(info.fileName());
133
134 if (sDestFileLocation != key->fileName())
135 {
136 if (QFile::exists(sDestFileLocation))
137 QFile::remove(sDestFileLocation);
138
139 bool ok = QFile::copy(key->fileName(), sDestFileLocation);
140 if (!ok)
141 {
142 key->setFileName("");
143
144 DebugDetails dd;
145 dd << __FUNCTION__;
146 dd << QString(" KeyFrame.pos() = %1").arg(key->pos());
147 dd << QString(" Key->fileName() = %1").arg(key->fileName());
148 dd << QString(" FilePath = %1").arg(sDestFileLocation);
149 dd << QString("Couldn't save the sound clip");
150 return Status(Status::FAIL, dd);
151 }
152 key->setFileName(sDestFileLocation);
153 }
154 return Status::OK;
155}
156
157KeyFrame* LayerSound::createKeyFrame(int position)
158{
159 SoundClip* s = new SoundClip;
160 s->setPos(position);
161 return s;
162}
163
164SoundClip* LayerSound::getSoundClipWhichCovers(int frameNumber)
165{
166 KeyFrame* key = getKeyFrameWhichCovers(frameNumber);
167 return static_cast<SoundClip*>(key);
168}
DebugDetails
Definition: pencilerror.h:25
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:37
SoundClip
Definition: soundclip.h:27
Status
Definition: pencilerror.h:40
QDir
QDir::filePath
QString filePath(const QString &fileName) const const
QDomDocument
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
QDomElement
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const const
QDomElement::setAttribute
void setAttribute(const QString &name, const QString &value)
QDomElement::tagName
QString tagName() const const
QDomNode
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
QDomNode::firstChild
QDomNode firstChild() const const
QDomNode::isNull
bool isNull() const const
QDomNode::nextSibling
QDomNode nextSibling() const const
QDomNode::toElement
QDomElement toElement() const const
QFile::copy
bool copy(const QString &newName)
QFile::exists
bool exists() const const
QFile::remove
bool remove()
QFileInfo
QString
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString::isEmpty
bool isEmpty() const const
QString::toInt
int toInt(bool *ok, int base) const const
Generated on Sat Nov 25 2023 13:00:01 for Pencil2D by doxygen 1.9.6 based on revision 18c5494f61f228a0f7b8820420627d4ac76231a8