Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
soundmanager.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2017 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #include "soundmanager.h"
19 
20 #include <QString>
21 #include "object.h"
22 #include "layersound.h"
23 #include "soundclip.h"
24 #include "soundplayer.h"
25 
26 SoundManager::SoundManager( QObject* parnet ) : BaseManager( parnet )
27 {
28 }
29 
30 SoundManager::~SoundManager()
31 {
32 }
33 
34 bool SoundManager::init()
35 {
36  return true;
37 }
38 
39 Status SoundManager::load( Object* obj )
40 {
41  int count = obj->getLayerCount();
42  for ( int i = 0; i < count; ++i )
43  {
44  Layer* layer = obj->getLayer( i );
45  if ( layer->type() != Layer::SOUND )
46  {
47  continue;
48  }
49 
50  LayerSound* soundLayer = static_cast< LayerSound* >( layer );
51 
52  soundLayer->foreachKeyFrame( [ this ]( KeyFrame* key )
53  {
54  SoundClip* clip = dynamic_cast< SoundClip* >( key );
55  Q_ASSERT( clip );
56 
57  createMediaPlayer( clip );
58  } );
59  }
60  return Status::OK;
61 }
62 
63 Status SoundManager::save( Object* )
64 {
65  return Status::OK;
66 }
67 
68 Status SoundManager::loadSound( Layer* soundLayer, int frameNumber, QString strSoundFile )
69 {
70  Q_ASSERT( soundLayer );
71  if ( soundLayer->type() != Layer::SOUND )
72  {
73  return Status::ERROR_INVALID_LAYER_TYPE;
74  }
75 
76  if ( frameNumber < 0 )
77  {
78  return Status::ERROR_INVALID_FRAME_NUMBER;
79  }
80 
81  if ( !QFile::exists( strSoundFile ) )
82  {
83  return Status::FILE_NOT_FOUND;
84  }
85 
86  KeyFrame* key = soundLayer->getKeyFrameAt( frameNumber );
87  if ( key == nullptr )
88  {
89  key = new SoundClip;
90  soundLayer->addKeyFrame( frameNumber, key );
91  }
92 
93  if ( !key->fileName().isEmpty() )
94  {
95  return Status::FAIL;
96  }
97 
98  QString strCopyFile = soundLayer->object()->copyFileToDataFolder( strSoundFile );
99  Q_ASSERT( !strCopyFile.isEmpty() );
100 
101  QString sOriginalName = QFileInfo( strSoundFile ).fileName();
102 
103  SoundClip* soundClip = dynamic_cast< SoundClip* >( key );
104  soundClip->init( strCopyFile );
105  soundClip->setSoundClipName( sOriginalName );
106 
107  Status st = createMediaPlayer( soundClip );
108  if ( !st.ok() )
109  {
110  delete soundClip;
111  return st;
112  }
113 
114  return Status::OK;
115 }
116 
117 Status SoundManager::loadSound( SoundClip* soundClip, QString strSoundFile )
118 {
119  Q_ASSERT( soundClip );
120 
121  if ( !QFile::exists( strSoundFile ) )
122  {
123  return Status::FILE_NOT_FOUND;
124  }
125 
126  if ( !soundClip->fileName().isEmpty() )
127  {
128  return Status::FAIL;
129  }
130 
131  QString strCopyFile = editor()->object()->copyFileToDataFolder( strSoundFile );
132  Q_ASSERT( !strCopyFile.isEmpty() );
133 
134  soundClip->init( strCopyFile );
135  soundClip->setSoundClipName( QFileInfo( strSoundFile ).fileName() );
136 
137  Status st = createMediaPlayer( soundClip );
138  if ( !st.ok() )
139  {
140  delete soundClip;
141  return st;
142  }
143 
144  return Status::OK;
145 }
146 
147 void SoundManager::onDurationChanged( SoundPlayer* player, int64_t duration )
148 {
149  SoundClip* clip = player->clip();
150 
151  double fps = static_cast< double >( editor()->fps() );
152 
153  double frameLength = duration * fps / 1000.0;
154  clip->setLength( frameLength );
155  clip->setDuration( duration );
156 
157  emit soundClipDurationChanged();
158 }
159 
160 Status SoundManager::createMediaPlayer( SoundClip* clip )
161 {
162  SoundPlayer* newPlayer = new SoundPlayer();
163  newPlayer->init( clip );
164 
165  connect( newPlayer, &SoundPlayer::durationChanged, this, &SoundManager::onDurationChanged );
166 
167  return Status::OK;
168 }
Definition: layer.h:32
Definition: object.h:71