-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderManager.cpp
214 lines (187 loc) · 5.51 KB
/
RenderManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* RenderManager - exporting logic common between the CLI and GUI.
*
* Copyright (c) 2015 Ryan Roden-Corrent <ryan/at/rcorre.net>
*
* This file is part of LMMS - http://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include <QFileInfo>
#include <QDebug>
#include <QDir>
#include "RenderManager.h"
#include "Song.h"
#include "BBTrackContainer.h"
#include "BBTrack.h"
#include "debug.h"
RenderManager::RenderManager(
const Mixer::qualitySettings & qualitySettings,
const ProjectRenderer::OutputSettings & outputSettings,
ProjectRenderer::ExportFileFormats fmt,
QString outputPath) :
m_qualitySettings(qualitySettings),
m_outputSettings(outputSettings),
m_format(fmt),
m_outputPath(outputPath),
m_activeRenderer(NULL)
{
}
RenderManager::~RenderManager()
{
delete m_activeRenderer;
}
void RenderManager::abortProcessing()
{
if ( m_activeRenderer ) {
m_activeRenderer->abortProcessing();
}
restoreMutedState();
}
// Called to render each new track when rendering tracks individually.
void RenderManager::renderNextTrack()
{
delete m_activeRenderer;
m_activeRenderer = NULL;
if( m_tracksToRender.isEmpty() )
{
// nothing left to render
restoreMutedState();
emit finished();
}
else
{
// pop the next track from our rendering queue
Track* renderTrack = m_tracksToRender.back();
m_tracksToRender.pop_back();
// mute everything but the track we are about to render
for( auto it = m_unmuted.begin(); it != m_unmuted.end(); ++it )
{
(*it)->setMuted( (*it) != renderTrack );
}
// for multi-render, prefix each output file with a different number
int trackNum = m_tracksToRender.size() + 1;
// create a renderer for this track
m_activeRenderer = new ProjectRenderer(
m_qualitySettings,
m_outputSettings,
m_format,
pathForTrack(renderTrack, trackNum));
if ( m_activeRenderer->isReady() )
{
// pass progress signals through
connect( m_activeRenderer, SIGNAL( progressChanged( int ) ),
this, SIGNAL( progressChanged( int ) ) );
// when it is finished, render the next track
connect( m_activeRenderer, SIGNAL( finished() ),
this, SLOT( renderNextTrack() ) );
m_activeRenderer->startProcessing();
}
else
{
qDebug( "Renderer failed to acquire a file device!" );
renderNextTrack();
}
}
}
// Render the song into individual tracks
void RenderManager::renderTracks()
{
const TrackContainer::TrackList & tl = Engine::getSong()->tracks();
// find all currently unnmuted tracks -- we want to render these.
for( auto it = tl.begin(); it != tl.end(); ++it )
{
Track* tk = (*it);
Track::TrackTypes type = tk->type();
// Don't mute automation tracks
if ( tk->isMuted() == false &&
( type == Track::InstrumentTrack || type == Track::SampleTrack ) )
{
m_unmuted.push_back(tk);
}
}
const TrackContainer::TrackList t2 = Engine::getBBTrackContainer()->tracks();
for( auto it = t2.begin(); it != t2.end(); ++it )
{
Track* tk = (*it);
if ( tk->isMuted() == false )
{
m_unmuted.push_back(tk);
}
}
// copy the list of unmuted tracks into our rendering queue.
// we need to remember which tracks were unmuted to restore state at the end.
m_tracksToRender = m_unmuted;
renderNextTrack();
}
// Render the song into a single track
void RenderManager::renderProject()
{
m_activeRenderer = new ProjectRenderer(
m_qualitySettings,
m_outputSettings,
m_format,
m_outputPath);
if( m_activeRenderer->isReady() )
{
// pass progress signals through
connect( m_activeRenderer, SIGNAL( progressChanged( int ) ),
this, SIGNAL( progressChanged( int ) ) );
// as we have not queued any tracks, renderNextTrack will just clean up
connect( m_activeRenderer, SIGNAL( finished() ),
this, SLOT( renderNextTrack() ) );
m_activeRenderer->startProcessing();
}
else
{
qDebug( "Renderer failed to acquire a file device!" );
emit finished();
}
}
// Unmute all tracks that were muted while rendering tracks
void RenderManager::restoreMutedState()
{
while( !m_unmuted.isEmpty() )
{
Track* restoreTrack = m_unmuted.back();
m_unmuted.pop_back();
restoreTrack->setMuted( false );
}
}
// Determine the output path for a track when rendering tracks individually
QString RenderManager::pathForTrack(const Track *track, int num)
{
QString extension = ProjectRenderer::getFileExtensionFromFormat( m_format );
QString name = track->name();
name = name.remove(QRegExp("[^a-zA-Z]"));
name = QString( "%1_%2%3" ).arg( num ).arg( name ).arg( extension );
return QDir(m_outputPath).filePath(name);
}
void RenderManager::updateConsoleProgress()
{
if ( m_activeRenderer )
{
m_activeRenderer->updateConsoleProgress();
int totalNum = m_unmuted.size();
if ( totalNum > 0 )
{
// we are rendering multiple tracks, append a track counter to the output
int trackNum = totalNum - m_tracksToRender.size();
fprintf( stderr, "(%d/%d)", trackNum, totalNum );
}
}
}