Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • 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#include "util/util.h"
25
26
27LayerSound::LayerSound(int id) : Layer(id, Layer::SOUND)
28{
29 setName(tr("Sound Layer"));
30}
31
32LayerSound::~LayerSound()
33{
34}
35
36Status LayerSound::loadSoundClipAtFrame(const QString& sSoundClipName,
37 const QString& strFilePath,
38 int frameNumber)
39{
40 if (!QFile::exists(strFilePath))
41 {
42 return Status::FILE_NOT_FOUND;
43 }
44
45 QFileInfo info(strFilePath);
46 if (!info.isFile())
47 {
48 return Status::ERROR_LOAD_SOUND_FILE;
49 }
50
51 SoundClip* clip = new SoundClip;
52 clip->setSoundClipName(sSoundClipName);
53 clip->init(strFilePath);
54 clip->setPos(frameNumber);
55 loadKey(clip);
56 return Status::OK;
57}
58
59void LayerSound::updateFrameLengths(int fps)
60{
61 foreachKeyFrame([&fps](KeyFrame* pKeyFrame)
62 {
63 auto soundClip = dynamic_cast<SoundClip *>(pKeyFrame);
64 soundClip->updateLength(fps);
65 });
66}
67
68QDomElement LayerSound::createDomElement(QDomDocument& doc) const
69{
70 QDomElement layerElem = createBaseDomElement(doc);
71
72 foreachKeyFrame([&doc, &layerElem](KeyFrame* pKeyFrame)
73 {
74 SoundClip* clip = static_cast<SoundClip*>(pKeyFrame);
75
76 QDomElement imageTag = doc.createElement("sound");
77 imageTag.setAttribute("frame", clip->pos());
78 imageTag.setAttribute("name", clip->soundClipName());
79
80 QFileInfo info(clip->fileName());
81 //qDebug() << "Save=" << info.fileName();
82 imageTag.setAttribute("src", info.fileName());
83 layerElem.appendChild(imageTag);
84 });
85
86 return layerElem;
87}
88
89void LayerSound::loadDomElement(const QDomElement& element, QString dataDirPath, ProgressCallback progressStep)
90{
91 this->loadBaseDomElement(element);
92
93 QDomNode soundTag = element.firstChild();
94 while (!soundTag.isNull())
95 {
96 QDomElement soundElement = soundTag.toElement();
97
98 if (!soundElement.isNull() && soundElement.tagName() == "sound")
99 {
100 const QString soundFile = soundElement.attribute("src");
101 const QString sSoundClipName = soundElement.attribute("name", "My Sound Clip");
102
103 if (!soundFile.isEmpty())
104 {
105 QString path = validateDataPath(soundFile, dataDirPath);
106 if (!path.isEmpty())
107 {
108 int position = soundElement.attribute("frame").toInt();
109 Status st = loadSoundClipAtFrame(sSoundClipName, path, position);
110 Q_ASSERT(st.ok());
111 }
112 }
113 progressStep();
114 }
115
116 soundTag = soundTag.nextSibling();
117 }
118}
119
120void LayerSound::replaceKeyFrame(const KeyFrame* soundClip)
121{
122 *getSoundClipWhichCovers(soundClip->pos()) = *static_cast<const SoundClip*>(soundClip);
123}
124
125Status LayerSound::saveKeyFrameFile(KeyFrame* key, QString path)
126{
127 Q_ASSERT(key);
128
129 if (key->fileName().isEmpty())
130 {
131 return Status::SAFE;
132 }
133
134 QFileInfo info(key->fileName());
135 QString sDestFileLocation = QDir(path).filePath(info.fileName());
136
137 if (sDestFileLocation != key->fileName())
138 {
139 if (QFile::exists(sDestFileLocation))
140 QFile::remove(sDestFileLocation);
141
142 bool ok = QFile::copy(key->fileName(), sDestFileLocation);
143 if (!ok)
144 {
145 key->setFileName("");
146
147 DebugDetails dd;
148 dd << "LayerSound::saveKeyFrameFile";
149 dd << QString(" KeyFrame.pos() = %1").arg(key->pos());
150 dd << QString(" Key->fileName() = %1").arg(key->fileName());
151 dd << QString(" FilePath = %1").arg(sDestFileLocation);
152 dd << QString("Error: Failed to save SoundClip");
153 return Status(Status::FAIL, dd);
154 }
155 key->setFileName(sDestFileLocation);
156 }
157 return Status::OK;
158}
159
160KeyFrame* LayerSound::createKeyFrame(int position)
161{
162 SoundClip* s = new SoundClip;
163 s->setPos(position);
164 return s;
165}
166
167SoundClip* LayerSound::getSoundClipWhichCovers(int frameNumber)
168{
169 KeyFrame* key = getKeyFrameWhichCovers(frameNumber);
170 return static_cast<SoundClip*>(key);
171}
DebugDetails
Definition: pencilerror.h:25
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:33
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 Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39