Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
layersound.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 #include <QtDebug>
18 #include <QMediaPlayer>
19 #include "object.h"
20 #include "layersound.h"
21 #include "soundclip.h"
22 
23 
24 LayerSound::LayerSound( Object* object ) : Layer( object, Layer::SOUND )
25 {
26  mName = QString( tr( "Sound Layer" ) );
27 }
28 
29 LayerSound::~LayerSound()
30 {
31 }
32 
33 Status LayerSound::loadSoundClipAtFrame( const QString& sSoundClipName,
34  const QString& strFilePath,
35  int frameNumber )
36 {
37  if ( !QFile::exists( strFilePath ) )
38  {
39  return Status::FILE_NOT_FOUND;
40  }
41 
42  QFileInfo info( strFilePath );
43  if ( !info.isFile() )
44  {
45  return Status::ERROR_LOAD_SOUND_FILE;
46  }
47 
48  SoundClip* clip = new SoundClip;
49  clip->setSoundClipName( sSoundClipName );
50  clip->init( strFilePath );
51  clip->setPos( frameNumber );
52  loadKey( clip );
53  return Status::OK;
54 }
55 
56 void LayerSound::updateFrameLengths(int fps)
57 {
58  foreachKeyFrame( [&fps] (KeyFrame* pKeyFrame)
59  {
60  auto soundClip = dynamic_cast<SoundClip *>(pKeyFrame);
61  soundClip->updateLength(fps);
62  } );
63 }
64 
65 QDomElement LayerSound::createDomElement( QDomDocument& doc )
66 {
67  QDomElement layerTag = doc.createElement( "layer" );
68 
69  layerTag.setAttribute( "id", id() );
70  layerTag.setAttribute( "name", name() );
71  layerTag.setAttribute( "visibility", visible() );
72  layerTag.setAttribute( "type", type() );
73 
74  foreachKeyFrame( [ &doc, &layerTag ]( KeyFrame* pKeyFrame )
75  {
76  SoundClip* clip = static_cast<SoundClip*>(pKeyFrame);
77 
78  QDomElement imageTag = doc.createElement( "sound" );
79  imageTag.setAttribute( "frame", clip->pos() );
80  imageTag.setAttribute( "name", clip->soundClipName() );
81 
82  QFileInfo info( clip->fileName() );
83  //qDebug() << "Save=" << info.fileName();
84  imageTag.setAttribute( "src", info.fileName() );
85  layerTag.appendChild( imageTag );
86  } );
87 
88  return layerTag;
89 }
90 
91 void LayerSound::loadDomElement( QDomElement element, QString dataDirPath )
92 {
93  if ( !element.attribute( "id" ).isNull() )
94  {
95  int myId = element.attribute( "id" ).toInt();
96  setId( myId );
97  }
98  mName = element.attribute( "name" );
99  mVisible = ( element.attribute( "visibility" ).toInt() == 1 );
100 
101  QDomNode soundTag = element.firstChild();
102  while ( !soundTag.isNull() )
103  {
104  QDomElement soundElement = soundTag.toElement();
105  if ( soundElement.isNull() )
106  {
107  continue;
108  }
109 
110  if ( soundElement.tagName() == "sound" )
111  {
112  const QString soundFile = soundElement.attribute( "src" );
113  const QString sSoundClipName = soundElement.attribute( "name", "My Sound Clip" );
114 
115  // the file is supposed to be in the data directory
116  const QString sFullPath = QDir( dataDirPath ).filePath( soundFile );
117 
118  int position = soundElement.attribute( "frame" ).toInt();
119  Status st = loadSoundClipAtFrame( sSoundClipName, sFullPath, position );
120  Q_ASSERT( st.ok() );
121  }
122 
123  soundTag = soundTag.nextSibling();
124  }
125 }
126 
127 Status LayerSound::saveKeyFrame( KeyFrame*, QString path )
128 {
129  Q_UNUSED(path)
130 
131  return Status::OK;
132 }
Definition: layer.h:32
Definition: object.h:71