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  if (this != &a) {
41  mOriginalSoundClipName = a.mOriginalSoundClipName;
42  }
43  return *this;
44 }
45 
46 SoundClip* SoundClip::clone()
47 {
48  return new SoundClip(*this);
49 }
50 
51 Status SoundClip::init(const QString& strSoundFile)
52 {
53  if (strSoundFile.isEmpty())
54  {
55  return Status::FAIL;
56  }
57  setFileName(strSoundFile);
58  return Status::OK;
59 }
60 
61 bool SoundClip::isValid() const
62 {
63  if (fileName().isEmpty())
64  {
65  return false;
66  }
67 
68  if (mPlayer == nullptr)
69  {
70  return false;
71  }
72 
73  return true;
74 }
75 
76 void SoundClip::attachPlayer(SoundPlayer* player)
77 {
78  Q_ASSERT( player != nullptr );
79  mPlayer.reset(player);
80 }
81 
82 void SoundClip::detachPlayer()
83 {
84  mPlayer.reset();
85 }
86 
87 void SoundClip::play()
88 {
89  if (mPlayer)
90  {
91  mPlayer->play();
92  }
93 }
94 
95 void SoundClip::playFromPosition(int frameNumber, int fps)
96 {
97  int framesIntoSound = frameNumber;
98  if (pos() > 1)
99  {
100  framesIntoSound = frameNumber - pos();
101  }
102  qreal msPerFrame = 1000.0 / fps;
103  qint64 msIntoSound = qRound(framesIntoSound * msPerFrame);
104  if (mPlayer)
105  {
106  mPlayer->setMediaPlayerPosition(msIntoSound);
107  mPlayer->play();
108  }
109 }
110 
111 void SoundClip::pause()
112 {
113  if (mPlayer)
114  {
115  mPlayer->pause();
116  }
117 }
118 
119 void SoundClip::stop()
120 {
121  if (mPlayer)
122  {
123  mPlayer->stop();
124  }
125 }
126 
127 int64_t SoundClip::duration() const
128 {
129  return mDuration;
130 }
131 
132 void SoundClip::setDuration(const int64_t& duration)
133 {
134  mDuration = duration;
135 }
136 
137 void SoundClip::updateLength(int fps)
138 {
139  setLength(qCeil(mDuration * fps / 1000.0));
140 }
bool isEmpty() const const