From 21425e3477e2ed18445b44cc1aa933d9f0301906 Mon Sep 17 00:00:00 2001 From: Alexandre Almeida Date: Wed, 7 Jan 2015 21:11:42 -0200 Subject: [PATCH] Update Song.cpp --- src/core/Song.cpp | 231 +++++++++++++++++++++++++--------------------- 1 file changed, 127 insertions(+), 104 deletions(-) diff --git a/src/core/Song.cpp b/src/core/Song.cpp index 3dfa1fbd0..3bd0da7f7 100644 --- a/src/core/Song.cpp +++ b/src/core/Song.cpp @@ -144,7 +144,7 @@ void Song::masterVolumeChanged() void Song::setTempo() { Engine::mixer()->lockPlayHandleRemoval(); - const bpm_t tempo = (bpm_t) m_tempoModel.value(); + const bpm_t tempo = ( bpm_t ) m_tempoModel.value(); PlayHandleList & playHandles = Engine::mixer()->playHandles(); for( PlayHandleList::Iterator it = playHandles.begin(); it != playHandles.end(); ++it ) @@ -176,7 +176,8 @@ void Song::setTimeSignature() emit dataChanged(); m_oldTicksPerTact = ticksPerTact(); - m_vstSyncController.setTimeSignature( getTimeSigModel().getNumerator(), getTimeSigModel().getDenominator() ); + m_vstSyncController.setTimeSignature( + getTimeSigModel().getNumerator(), getTimeSigModel().getDenominator() ); } @@ -202,13 +203,13 @@ void Song::processNextBuffer() return; } - TrackList track_list; - int tco_num = -1; + TrackList trackList; + int tcoNum = -1; switch( m_playMode ) { case Mode_PlaySong: - track_list = tracks(); + trackList = tracks(); // at song-start we have to reset the LFOs if( m_playPos[Mode_PlaySong] == 0 ) { @@ -217,25 +218,25 @@ void Song::processNextBuffer() break; case Mode_PlayTrack: - track_list.push_back( m_trackToPlay ); + trackList.push_back( m_trackToPlay ); break; case Mode_PlayBB: if( Engine::getBBTrackContainer()->numOfBBs() > 0 ) { - tco_num = Engine::getBBTrackContainer()-> + tcoNum = Engine::getBBTrackContainer()-> currentBB(); - track_list.push_back( BBTrack::findBBTrack( - tco_num ) ); + trackList.push_back( BBTrack::findBBTrack( + tcoNum ) ); } break; case Mode_PlayPattern: if( m_patternToPlay != NULL ) { - tco_num = m_patternToPlay->getTrack()-> + tcoNum = m_patternToPlay->getTrack()-> getTCONum( m_patternToPlay ); - track_list.push_back( + trackList.push_back( m_patternToPlay->getTrack() ); } break; @@ -245,41 +246,44 @@ void Song::processNextBuffer() } - if( track_list.empty() == true ) + if( trackList.empty() == true ) { return; } // check for looping-mode and act if necessary TimeLineWidget * tl = m_playPos[m_playMode].m_timeLine; - bool check_loop = tl != NULL && m_exporting == false && + bool checkLoop = tl != NULL && m_exporting == false && tl->loopPointsEnabled(); - if( check_loop ) + + if( checkLoop ) { if( m_playPos[m_playMode] < tl->loopBegin() || m_playPos[m_playMode] >= tl->loopEnd() ) { - m_elapsedMilliSeconds = (tl->loopBegin().getTicks()*60*1000/48)/getTempo(); + m_elapsedMilliSeconds = + ( tl->loopBegin().getTicks() * 60 * 1000 / 48 ) / getTempo(); m_playPos[m_playMode].setTicks( tl->loopBegin().getTicks() ); } } - f_cnt_t total_frames_played = 0; - const float frames_per_tick = Engine::framesPerTick(); + f_cnt_t totalFramesPlayed = 0; + const float framesPerTick = Engine::framesPerTick(); - while( total_frames_played - < Engine::mixer()->framesPerPeriod() ) + while( totalFramesPlayed < Engine::mixer()->framesPerPeriod() ) { m_vstSyncController.update(); - f_cnt_t played_frames = Engine::mixer()->framesPerPeriod() - total_frames_played; + f_cnt_t playedFrames = Engine::mixer()->framesPerPeriod() - + totalFramesPlayed; - float current_frame = m_playPos[m_playMode].currentFrame(); + float currentFrame = m_playPos[m_playMode].currentFrame(); // did we play a tick? - if( current_frame >= frames_per_tick ) + if( currentFrame >= framesPerTick ) { - int ticks = m_playPos[m_playMode].getTicks() + (int)( current_frame / frames_per_tick ); + int ticks = m_playPos[m_playMode].getTicks() + + ( int )( currentFrame / framesPerTick ); m_vstSyncController.setAbsolutePosition( ticks ); @@ -289,14 +293,14 @@ void Song::processNextBuffer() // per default we just continue playing even if // there's no more stuff to play // (song-play-mode) - int max_tact = m_playPos[m_playMode].getTact() + int maxTact = m_playPos[m_playMode].getTact() + 2; // then decide whether to go over to next tact // or to loop back to first tact if( m_playMode == Mode_PlayBB ) { - max_tact = Engine::getBBTrackContainer() + maxTact = Engine::getBBTrackContainer() ->lengthOfCurrentBB(); } else if( m_playMode == Mode_PlayPattern && @@ -304,34 +308,39 @@ void Song::processNextBuffer() tl != NULL && tl->loopPointsEnabled() == false ) { - max_tact = m_patternToPlay->length() + maxTact = m_patternToPlay->length() .getTact(); } // end of played object reached? if( m_playPos[m_playMode].getTact() + 1 - >= max_tact ) + >= maxTact ) { // then start from beginning and keep // offset - ticks = ticks % ( max_tact * MidiTime::ticksPerTact() ); + ticks %= ( maxTact * MidiTime::ticksPerTact() ); // wrap milli second counter - m_elapsedMilliSeconds = ( ticks * 60 * 1000 / 48 ) / getTempo(); + m_elapsedMilliSeconds = + ( ticks * 60 * 1000 / 48 ) / getTempo(); m_vstSyncController.setAbsolutePosition( ticks ); } } m_playPos[m_playMode].setTicks( ticks ); - if( check_loop ) + if( checkLoop ) { - m_vstSyncController.startCycle( tl->loopBegin().getTicks(), tl->loopEnd().getTicks() ); + m_vstSyncController.startCycle( + tl->loopBegin().getTicks(), tl->loopEnd().getTicks() ); if( m_playPos[m_playMode] >= tl->loopEnd() ) { m_playPos[m_playMode].setTicks( tl->loopBegin().getTicks() ); - m_elapsedMilliSeconds = ((tl->loopBegin().getTicks())*60*1000/48)/getTempo(); + + m_elapsedMilliSeconds = + ( ( tl->loopBegin().getTicks() ) * 60 * 1000 / 48 ) / + getTempo(); } } else @@ -339,55 +348,57 @@ void Song::processNextBuffer() m_vstSyncController.stopCycle(); } - current_frame = fmodf( current_frame, frames_per_tick ); - m_playPos[m_playMode].setCurrentFrame( current_frame ); + currentFrame = fmodf( currentFrame, framesPerTick ); + m_playPos[m_playMode].setCurrentFrame( currentFrame ); } - f_cnt_t last_frames = (f_cnt_t)frames_per_tick - - (f_cnt_t) current_frame; + f_cnt_t lastFrames = ( f_cnt_t )framesPerTick - + ( f_cnt_t )currentFrame; // skip last frame fraction - if( last_frames == 0 ) + if( lastFrames == 0 ) { - ++total_frames_played; - m_playPos[m_playMode].setCurrentFrame( current_frame + ++totalFramesPlayed; + m_playPos[m_playMode].setCurrentFrame( currentFrame + 1.0f ); continue; } // do we have some samples left in this tick but these are // less then samples we have to play? - if( last_frames < played_frames ) + if( lastFrames < playedFrames ) { // then set played_samples to remaining samples, the // rest will be played in next loop - played_frames = last_frames; + playedFrames = lastFrames; } - if( (f_cnt_t) current_frame == 0 ) + if( ( f_cnt_t ) currentFrame == 0 ) { if( m_playMode == Mode_PlaySong ) { m_globalAutomationTrack->play( m_playPos[m_playMode], - played_frames, - total_frames_played, tco_num ); + playedFrames, + totalFramesPlayed, tcoNum ); } // loop through all tracks and play them - for( int i = 0; i < track_list.size(); ++i ) + for( int i = 0; i < trackList.size(); ++i ) { - track_list[i]->play( m_playPos[m_playMode], - played_frames, - total_frames_played, tco_num ); + trackList[i]->play( m_playPos[m_playMode], + playedFrames, + totalFramesPlayed, tcoNum ); } } // update frame-counters - total_frames_played += played_frames; - m_playPos[m_playMode].setCurrentFrame( played_frames + - current_frame ); - m_elapsedMilliSeconds += (((played_frames/frames_per_tick)*60*1000/48)/getTempo()); + totalFramesPlayed += playedFrames; + m_playPos[m_playMode].setCurrentFrame( playedFrames + + currentFrame ); + m_elapsedMilliSeconds += + ( ( playedFrames / framesPerTick ) * 60 * 1000 / 48 ) + / getTempo(); m_elapsedTacts = m_playPos[Mode_PlaySong].getTact(); - m_elapsedTicks = (m_playPos[Mode_PlaySong].getTicks()%ticksPerTact())/48; + m_elapsedTicks = ( m_playPos[Mode_PlaySong].getTicks() % ticksPerTact() ) / 48; } } @@ -396,17 +407,21 @@ bool Song::isExportDone() const if ( m_renderBetweenMarkers ) { return m_exporting == true && - m_playPos[Mode_PlaySong].getTicks() >= m_playPos[Mode_PlaySong].m_timeLine->loopEnd().getTicks(); + m_playPos[Mode_PlaySong].getTicks() >= + m_playPos[Mode_PlaySong].m_timeLine->loopEnd().getTicks(); } + if( m_exportLoop) { return m_exporting == true && - m_playPos[Mode_PlaySong].getTicks() >= length() * ticksPerTact(); + m_playPos[Mode_PlaySong].getTicks() >= + length() * ticksPerTact(); } else { return m_exporting == true && - m_playPos[Mode_PlaySong].getTicks() >= ( length() + 1 ) * ticksPerTact(); + m_playPos[Mode_PlaySong].getTicks() >= + ( length() + 1 ) * ticksPerTact(); } } @@ -454,13 +469,13 @@ void Song::playAndRecord() -void Song::playTrack( Track * _trackToPlay ) +void Song::playTrack( Track * trackToPlay ) { if( isStopped() == false ) { stop(); } - m_trackToPlay = _trackToPlay; + m_trackToPlay = trackToPlay; m_playMode = Mode_PlayTrack; m_playing = true; @@ -497,7 +512,7 @@ void Song::playBB() -void Song::playPattern( const Pattern* patternToPlay, bool _loop ) +void Song::playPattern( const Pattern* patternToPlay, bool loop ) { if( isStopped() == false ) { @@ -505,7 +520,7 @@ void Song::playPattern( const Pattern* patternToPlay, bool _loop ) } m_patternToPlay = patternToPlay; - m_loopPattern = _loop; + m_loopPattern = loop; if( m_patternToPlay != NULL ) { @@ -543,12 +558,14 @@ void Song::updateLength() -void Song::setPlayPos( tick_t _ticks, PlayModes _play_mode ) +void Song::setPlayPos( tick_t ticks, PlayModes playMode ) { - m_elapsedTicks += m_playPos[_play_mode].getTicks() - _ticks; - m_elapsedMilliSeconds += (((( _ticks - m_playPos[_play_mode].getTicks()))*60*1000/48)/getTempo()); - m_playPos[_play_mode].setTicks( _ticks ); - m_playPos[_play_mode].setCurrentFrame( 0.0f ); + m_elapsedTicks += m_playPos[playMode].getTicks() - ticks; + m_elapsedMilliSeconds += + ( ( ( ( ticks - m_playPos[playMode].getTicks() ) ) * 60 * 1000 / 48) / + getTempo() ); + m_playPos[playMode].setTicks( ticks ); + m_playPos[playMode].setCurrentFrame( 0.0f ); // send a signal if playposition changes during playback if( isPlaying() ) @@ -608,7 +625,9 @@ void Song::stop() if( tl->savedPos() >= 0 ) { m_playPos[m_playMode].setTicks( tl->savedPos().getTicks() ); - m_elapsedMilliSeconds = (((tl->savedPos().getTicks())*60*1000/48)/getTempo()); + m_elapsedMilliSeconds = + ( ( ( tl->savedPos().getTicks() ) * 60 * 1000 / 48 ) / + getTempo() ); tl->savePos( -1 ); } break; @@ -713,7 +732,7 @@ void Song::addBBTrack() void Song::addSampleTrack() { - (void) Track::create( Track::SampleTrack, this ); + ( void )Track::create( Track::SampleTrack, this ); } @@ -721,7 +740,7 @@ void Song::addSampleTrack() void Song::addAutomationTrack() { - (void) Track::create( Track::AutomationTrack, this ); + ( void )Track::create( Track::AutomationTrack, this ); } @@ -729,7 +748,7 @@ void Song::addAutomationTrack() bpm_t Song::getTempo() { - return (bpm_t) m_tempoModel.value(); + return ( bpm_t )m_tempoModel.value(); } @@ -824,24 +843,23 @@ void Song::clearProject() - // create new file void Song::createNewProject() { - QString default_template = ConfigManager::inst()->userProjectsDir() + QString defaultTemplate = ConfigManager::inst()->userProjectsDir() + "templates/default.mpt"; - if( QFile::exists( default_template ) ) + if( QFile::exists( defaultTemplate ) ) { - createNewProjectFromTemplate( default_template ); + createNewProjectFromTemplate( defaultTemplate ); return; } - default_template = ConfigManager::inst()->factoryProjectsDir() + defaultTemplate = ConfigManager::inst()->factoryProjectsDir() + "templates/default.mpt"; - if( QFile::exists( default_template ) ) + if( QFile::exists( defaultTemplate ) ) { - createNewProjectFromTemplate( default_template ); + createNewProjectFromTemplate( defaultTemplate ); return; } @@ -891,9 +909,9 @@ void Song::createNewProject() -void Song::createNewProjectFromTemplate( const QString & _template ) +void Song::createNewProjectFromTemplate( const QString & templ ) { - loadProject( _template ); + loadProject( templ ); // clear file-name so that user doesn't overwrite template when // saving... m_fileName = m_oldFileName = ""; @@ -909,7 +927,7 @@ void Song::createNewProjectFromTemplate( const QString & _template ) // load given song -void Song::loadProject( const QString & _file_name ) +void Song::loadProject( const QString & fileName ) { QDomNode node; @@ -917,8 +935,8 @@ void Song::loadProject( const QString & _file_name ) Engine::projectJournal()->setJournalling( false ); - m_fileName = _file_name; - m_oldFileName = _file_name; + m_fileName = fileName; + m_oldFileName = fileName; DataFile dataFile( m_fileName ); // if file could not be opened, head-node is null and we create @@ -1023,7 +1041,7 @@ void Song::loadProject( const QString & _file_name ) Engine::mixer()->unlock(); - ConfigManager::inst()->addRecentlyOpenedProject( _file_name ); + ConfigManager::inst()->addRecentlyOpenedProject( fileName ); Engine::projectJournal()->setJournalling( true ); @@ -1053,7 +1071,7 @@ void Song::loadProject( const QString & _file_name ) // only save current song as _filename and do nothing else -bool Song::saveProjectFile( const QString & _filename ) +bool Song::saveProjectFile( const QString & filename ) { DataFile::LocaleHelper localeHelper( DataFile::LocaleHelper::ModeSave ); @@ -1079,7 +1097,7 @@ bool Song::saveProjectFile( const QString & _filename ) saveControllerStates( dataFile, dataFile.content() ); - return dataFile.writeFile( _filename ); + return dataFile.writeFile( filename ); } @@ -1157,23 +1175,23 @@ void Song::importProject() -void Song::saveControllerStates( QDomDocument & _doc, QDomElement & _this ) +void Song::saveControllerStates( QDomDocument & doc, QDomElement & element ) { // save settings of controllers - QDomElement controllersNode =_doc.createElement( "controllers" ); - _this.appendChild( controllersNode ); + QDomElement controllersNode = doc.createElement( "controllers" ); + element.appendChild( controllersNode ); for( int i = 0; i < m_controllers.size(); ++i ) { - m_controllers[i]->saveState( _doc, controllersNode ); + m_controllers[i]->saveState( doc, controllersNode ); } } -void Song::restoreControllerStates( const QDomElement & _this ) +void Song::restoreControllerStates( const QDomElement & element ) { - QDomNode node = _this.firstChild(); + QDomNode node = element.firstChild(); while( !node.isNull() ) { Controller * c = Controller::create( node.toElement(), this ); @@ -1194,10 +1212,10 @@ void Song::restoreControllerStates( const QDomElement & _this ) void Song::exportProjectTracks() { - exportProject(true); + exportProject( true ); } -void Song::exportProject(bool multiExport) +void Song::exportProject( bool multiExport ) { if( isEmpty() ) { @@ -1210,7 +1228,7 @@ void Song::exportProject(bool multiExport) } FileDialog efd( gui->mainWindow() ); - if (multiExport) + if ( multiExport ) { efd.setFileMode( FileDialog::Directory); efd.setWindowTitle( tr( "Select directory for writing exported tracks..." ) ); @@ -1230,18 +1248,18 @@ void Song::exportProject(bool multiExport) ++idx; } efd.setNameFilters( types ); - QString base_filename; + QString baseFilename; if( !m_fileName.isEmpty() ) { efd.setDirectory( QFileInfo( m_fileName ).absolutePath() ); - base_filename = QFileInfo( m_fileName ).completeBaseName(); + baseFilename = QFileInfo( m_fileName ).completeBaseName(); } else { efd.setDirectory( ConfigManager::inst()->userProjectsDir() ); - base_filename = tr( "untitled" ); + baseFilename = tr( "untitled" ); } - efd.selectFile( base_filename + __fileEncodeDevices[0].m_extension ); + efd.selectFile( baseFilename + __fileEncodeDevices[0].m_extension ); efd.setWindowTitle( tr( "Select file for project-export..." ) ); } @@ -1268,8 +1286,8 @@ void Song::exportProject(bool multiExport) } } - const QString export_file_name = efd.selectedFiles()[0] + suffix; - ExportProjectDialog epd( export_file_name, gui->mainWindow(), multiExport ); + const QString exportFileName = efd.selectedFiles()[0] + suffix; + ExportProjectDialog epd( exportFileName, gui->mainWindow(), multiExport ); epd.exec(); } } @@ -1301,11 +1319,11 @@ void Song::setModified() -void Song::addController( Controller * _c ) +void Song::addController( Controller * c ) { - if( _c != NULL && !m_controllers.contains( _c ) ) + if( c != NULL && m_controllers.contains( c ) == false ) { - m_controllers.append( _c ); + m_controllers.append( c ); emit dataChanged(); } } @@ -1313,9 +1331,9 @@ void Song::addController( Controller * _c ) -void Song::removeController( Controller * _controller ) +void Song::removeController( Controller * controller ) { - int index = m_controllers.indexOf( _controller ); + int index = m_controllers.indexOf( controller ); if( index != -1 ) { m_controllers.remove( index ); @@ -1330,6 +1348,7 @@ void Song::removeController( Controller * _controller ) + void Song::clearErrors() { m_errors->clear(); @@ -1365,3 +1384,7 @@ QString* Song::errorSummary() return errors; } + + + +