18 #include "soundmanager.h"
22 #include "layersound.h"
23 #include "soundclip.h"
24 #include "soundplayer.h"
26 SoundManager::SoundManager( QObject* parnet ) :
BaseManager( parnet )
30 SoundManager::~SoundManager()
34 bool SoundManager::init()
41 int count = obj->getLayerCount();
42 for (
int i = 0; i < count; ++i )
44 Layer* layer = obj->getLayer( i );
45 if ( layer->type() != Layer::SOUND )
52 soundLayer->foreachKeyFrame( [
this ](
KeyFrame* key )
57 createMediaPlayer( clip );
68 Status SoundManager::loadSound(
Layer* soundLayer,
int frameNumber, QString strSoundFile )
70 Q_ASSERT( soundLayer );
71 if ( soundLayer->type() != Layer::SOUND )
73 return Status::ERROR_INVALID_LAYER_TYPE;
76 if ( frameNumber < 0 )
78 return Status::ERROR_INVALID_FRAME_NUMBER;
81 if ( !QFile::exists( strSoundFile ) )
83 return Status::FILE_NOT_FOUND;
86 KeyFrame* key = soundLayer->getKeyFrameAt( frameNumber );
90 soundLayer->addKeyFrame( frameNumber, key );
93 if ( !key->fileName().isEmpty() )
98 QString strCopyFile = soundLayer->object()->copyFileToDataFolder( strSoundFile );
99 Q_ASSERT( !strCopyFile.isEmpty() );
101 QString sOriginalName = QFileInfo( strSoundFile ).fileName();
104 soundClip->init( strCopyFile );
105 soundClip->setSoundClipName( sOriginalName );
107 Status st = createMediaPlayer( soundClip );
117 Status SoundManager::loadSound(
SoundClip* soundClip, QString strSoundFile )
119 Q_ASSERT( soundClip );
121 if ( !QFile::exists( strSoundFile ) )
123 return Status::FILE_NOT_FOUND;
126 if ( !soundClip->fileName().isEmpty() )
131 QString strCopyFile = editor()->object()->copyFileToDataFolder( strSoundFile );
132 Q_ASSERT( !strCopyFile.isEmpty() );
134 soundClip->init( strCopyFile );
135 soundClip->setSoundClipName( QFileInfo( strSoundFile ).fileName() );
137 Status st = createMediaPlayer( soundClip );
147 void SoundManager::onDurationChanged(
SoundPlayer* player, int64_t duration )
151 double fps =
static_cast< double >( editor()->fps() );
153 double frameLength = duration * fps / 1000.0;
154 clip->setLength( frameLength );
155 clip->setDuration( duration );
157 emit soundClipDurationChanged();
163 newPlayer->init( clip );
165 connect( newPlayer, &SoundPlayer::durationChanged,
this, &SoundManager::onDurationChanged );