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
  • managers
soundmanager.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
18#include "soundmanager.h"
19
20#include <QString>
21#include <QFileInfo>
22#include "editor.h"
23#include "object.h"
24#include "layersound.h"
25#include "soundclip.h"
26#include "soundplayer.h"
27#include "layermanager.h"
28
29SoundManager::SoundManager(Editor* editor) : BaseManager(editor, __FUNCTION__)
30{
31}
32
33SoundManager::~SoundManager()
34{
35}
36
37bool SoundManager::init()
38{
39 return true;
40}
41
42Status SoundManager::load(Object* obj)
43{
44 int count = obj->getLayerCount();
45 for (int i = 0; i < count; ++i)
46 {
47 Layer* layer = obj->getLayer(i);
48 if (layer->type() != Layer::SOUND)
49 {
50 continue;
51 }
52
53 LayerSound* soundLayer = static_cast<LayerSound*>(layer);
54
55 soundLayer->foreachKeyFrame([this](KeyFrame* key)
56 {
57 SoundClip* clip = dynamic_cast<SoundClip*>(key);
58 Q_ASSERT(clip);
59
60 createMediaPlayer(clip);
61 });
62 }
63 return Status::OK;
64}
65
66Status SoundManager::save(Object*)
67{
68 return Status::OK;
69}
70
71Status SoundManager::loadSound(SoundClip* soundClip, QString strSoundFile)
72{
73 Q_ASSERT(soundClip);
74
75 if (!QFile::exists(strSoundFile))
76 {
77 return Status::FILE_NOT_FOUND;
78 }
79
80 if (strSoundFile.isEmpty())
81 {
82 return Status::FAIL;
83 }
84
85 QString strCopyFile = editor()->object()->copyFileToDataFolder(strSoundFile);
86 Q_ASSERT(!strCopyFile.isEmpty());
87
88 soundClip->init(strCopyFile);
89 if (soundClip->soundClipName().isEmpty())
90 {
91 soundClip->setSoundClipName(QFileInfo(strSoundFile).fileName());
92 }
93
94 Status st = createMediaPlayer(soundClip);
95 if (!st.ok())
96 {
97 delete soundClip;
98 return st;
99 }
100
101 editor()->layers()->notifyAnimationLengthChanged();
102
103 return Status::OK;
104}
105
106Status SoundManager::processSound(SoundClip* soundClip)
107{
108 Q_ASSERT(soundClip);
109
110 if (!QFile::exists(soundClip->fileName()))
111 {
112 return Status::FILE_NOT_FOUND;
113 }
114 soundClip->init(soundClip->fileName());
115
116 Status st = createMediaPlayer(soundClip);
117 if (!st.ok())
118 {
119 return st;
120 }
121 return Status::OK;
122}
123
124int SoundManager::soundClipCount() const
125{
126 LayerManager *layerManager = editor()->layers();
127 int totalCount = 0;
128
129
130 for (int i = 0; i < layerManager->count(); ++i)
131 {
132 Layer* layer = layerManager->getLayer(i);
133 if (layer->type() != Layer::SOUND)
134 {
135 continue;
136 }
137
138 totalCount += layer->keyFrameCount();
139 }
140 return totalCount;
141}
142
143void SoundManager::onDurationChanged(SoundPlayer* player, int64_t duration)
144{
145 SoundClip* clip = player->clip();
146
147 double fps = static_cast<double>(editor()->fps());
148
149 double frameLength = duration * fps / 1000.0;
150 clip->setLength(static_cast<int>(frameLength));
151 clip->setDuration(duration);
152
153 editor()->layers()->notifyAnimationLengthChanged();
154
155 emit soundClipDurationChanged();
156}
157
158Status SoundManager::createMediaPlayer(SoundClip* clip)
159{
160 SoundPlayer* newPlayer = new SoundPlayer();
161 newPlayer->init(clip);
162
163 connect(newPlayer, &SoundPlayer::durationChanged, this, &SoundManager::onDurationChanged);
164
165 return Status::OK;
166}
BaseManager
Definition: basemanager.h:29
Editor
Definition: editor.h:71
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:33
LayerManager
Definition: layermanager.h:31
LayerManager::notifyAnimationLengthChanged
void notifyAnimationLengthChanged()
This should be emitted whenever the animation length frames, eg.
Definition: layermanager.cpp:403
LayerSound
Definition: layersound.h:26
Object
Definition: object.h:42
SoundClip
Definition: soundclip.h:27
SoundPlayer
Definition: soundplayer.h:30
Status
Definition: pencilerror.h:40
QFile::exists
bool exists() const const
QFileInfo
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString
QString::isEmpty
bool isEmpty() const const
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39