All Classes Namespaces Functions Variables Enumerations Properties Pages
soundclip.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 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 "soundclip.h"
19 
20 #include <QFile>
21 #include <QMediaPlayer>
22 #include <QtMath>
23 #include "soundplayer.h"
24 
25 SoundClip::SoundClip()
26 {
27 }
28 
29 SoundClip::SoundClip(const SoundClip& s2) : KeyFrame(s2)
30 {
31  mOriginalSoundClipName = s2.mOriginalSoundClipName;
32 }
33 
34 SoundClip::~SoundClip()
35 {
36  //QFile::remove( fileName() );
37 }
38 
39 SoundClip& SoundClip::operator=(const SoundClip& a)
40 {
41  if (this == &a)
42  {
43  return *this; // a self-assignment
44  }
45 
46  KeyFrame::operator=(a);
47  mOriginalSoundClipName = a.mOriginalSoundClipName;
48  return *this;
49 }
50 
51 SoundClip* SoundClip::clone() const
52 {
53  // Question: need to copy the file?
54  // The audio files are not allowed to be edited in Pencil2D, it should be file for now.
55  return new SoundClip(*this);
56 }
57 
58 Status SoundClip::init(const QString& strSoundFile)
59 {
60  if (strSoundFile.isEmpty())
61  {
62  return Status::FAIL;
63  }
64  setFileName(strSoundFile);
65  return Status::OK;
66 }
67 
68 bool SoundClip::isValid() const
69 {
70  if (fileName().isEmpty())
71  {
72  return false;
73  }
74 
75  if (!mPlayer)
76  {
77  return false;
78  }
79 
80  return true;
81 }
82 
83 void SoundClip::attachPlayer(SoundPlayer* player)
84 {
85  Q_ASSERT( player != nullptr );
86  mPlayer.reset(player);
87 }
88 
89 void SoundClip::detachPlayer()
90 {
91  mPlayer.reset();
92 }
93 
94 void SoundClip::play()
95 {
96  if (mPlayer)
97  {
98  mPlayer->play();
99  }
100 }
101 
102 void SoundClip::playFromPosition(int frameNumber, int fps)
103 {
104  int framesIntoSound = frameNumber;
105  if (pos() > 1)
106  {
107  framesIntoSound = frameNumber - pos();
108  }
109  qreal msPerFrame = 1000.0 / fps;
110  qint64 msIntoSound = qRound(framesIntoSound * msPerFrame);
111  if (mPlayer)
112  {
113  mPlayer->setMediaPlayerPosition(msIntoSound);
114  mPlayer->play();
115  }
116 }
117 
118 void SoundClip::pause()
119 {
120  if (mPlayer)
121  {
122  mPlayer->pause();
123  }
124 }
125 
126 void SoundClip::stop()
127 {
128  if (mPlayer)
129  {
130  mPlayer->stop();
131  }
132 }
133 
134 int64_t SoundClip::duration() const
135 {
136  return mDuration;
137 }
138 
139 void SoundClip::setDuration(const int64_t& duration)
140 {
141  mDuration = duration;
142 }
143 
144 void SoundClip::updateLength(int fps)
145 {
146  setLength(qCeil(mDuration * fps / 1000.0));
147 }
bool isEmpty() const const