Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • structure
soundclip.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU 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
25SoundClip::SoundClip()
26{
27}
28
29SoundClip::SoundClip(const SoundClip& s2) : KeyFrame(s2)
30{
31 mOriginalSoundClipName = s2.mOriginalSoundClipName;
32}
33
34SoundClip::~SoundClip()
35{
36 //QFile::remove( fileName() );
37}
38
39SoundClip& 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
51SoundClip* 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
58Status 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
68bool 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
83void SoundClip::attachPlayer(SoundPlayer* player)
84{
85 Q_ASSERT( player != nullptr );
86 mPlayer.reset(player);
87}
88
89void SoundClip::detachPlayer()
90{
91 mPlayer.reset();
92}
93
94void SoundClip::play()
95{
96 if (mPlayer)
97 {
98 mPlayer->play();
99 }
100}
101
102void 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
118void SoundClip::pause()
119{
120 if (mPlayer)
121 {
122 mPlayer->pause();
123 }
124}
125
126void SoundClip::stop()
127{
128 if (mPlayer)
129 {
130 mPlayer->stop();
131 }
132}
133
134int64_t SoundClip::duration() const
135{
136 return mDuration;
137}
138
139void SoundClip::setDuration(const int64_t& duration)
140{
141 mDuration = duration;
142}
143
144void SoundClip::updateLength(int fps)
145{
146 setLength(qCeil(mDuration * fps / 1000.0));
147}
KeyFrame
Definition: keyframe.h:30
SoundClip
Definition: soundclip.h:27
SoundPlayer
Definition: soundplayer.h:30
Status
Definition: pencilerror.h:40
QString
QString::isEmpty
bool isEmpty() const const
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39