Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
soundclip.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 "soundclip.h"
19 
20 #include <QFile>
21 #include <QMediaPlayer>
22 #include "soundplayer.h"
23 
24 SoundClip::SoundClip()
25 {
26 
27 }
28 
29 SoundClip::~SoundClip()
30 {
31  //QFile::remove( fileName() );
32 }
33 
34 Status SoundClip::init( const QString& strSoundFile )
35 {
36  if ( !fileName().isEmpty() )
37  {
38  return Status::FAIL;
39  }
40  setFileName( strSoundFile );
41  return Status::OK;
42 }
43 
44 bool SoundClip::isValid()
45 {
46  if ( fileName().isEmpty() )
47  {
48  return false;
49  }
50 
51  if ( mPlayer == nullptr )
52  {
53  return false;
54  }
55 
56  return true;
57 }
58 
59 void SoundClip::attachPlayer( SoundPlayer* player )
60 {
61  Q_ASSERT( player != nullptr );
62  mPlayer.reset( player );
63 }
64 
65 void SoundClip::detachPlayer()
66 {
67  mPlayer.reset();
68 }
69 
70 void SoundClip::play()
71 {
72  if ( mPlayer )
73  {
74  mPlayer->play();
75  }
76 }
77 
78 void SoundClip::playFromPosition(int frameNumber, int fps)
79 {
80  int framesIntoSound = frameNumber - pos();
81  int msPerFrame = 1000 / fps;
82  int msIntoSound = framesIntoSound * msPerFrame;
83 
84  if ( mPlayer )
85  {
86  mPlayer->setMediaPlayerPosition(msIntoSound);
87  mPlayer->play();
88  }
89 }
90 
91 void SoundClip::stop()
92 {
93  if ( mPlayer )
94  {
95  mPlayer->stop();
96  }
97 }
98 
99 int64_t SoundClip::duration() const
100 {
101  return mDuration;
102 }
103 
104 void SoundClip::setDuration(const int64_t &duration)
105 {
106  mDuration = duration;
107 }
108 
109 void SoundClip::updateLength(int fps)
110 {
111  setLength(mDuration * fps / 1000.0);
112 }