-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathSampleClip.cpp
More file actions
354 lines (266 loc) · 8.31 KB
/
Copy pathSampleClip.cpp
File metadata and controls
354 lines (266 loc) · 8.31 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* SampleClip.cpp
*
* Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of LMMS - https://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 "SampleClip.h"
#include <QDomElement>
#include <QFileInfo>
#include "PathUtil.h"
#include "SampleClipView.h"
#include "SampleTrack.h"
#include "Song.h"
namespace lmms
{
SampleClip::SampleClip(Track* _track, Sample sample, bool isPlaying)
: Clip(_track)
, m_sample(std::move(sample))
, m_isPlaying(false)
{
saveJournallingState( false );
setSampleFile( "" );
restoreJournallingState();
// we need to receive bpm-change-events, because then we have to
// change length of this Clip
connect( Engine::getSong(), SIGNAL(tempoChanged(lmms::bpm_t)),
this, SLOT(updateLength()), Qt::DirectConnection );
connect( Engine::getSong(), SIGNAL(timeSignatureChanged(int,int)),
this, SLOT(updateLength()));
//playbutton clicked or space key / on Export Song set isPlaying to false
connect( Engine::getSong(), SIGNAL(playbackStateChanged()),
this, SLOT(playbackPositionChanged()), Qt::DirectConnection );
//care about loops and jumps
connect(Engine::getSong(), &Song::playbackPositionJumped,
this, &SampleClip::playbackPositionChanged, Qt::DirectConnection);
//care about mute Clips
connect( this, SIGNAL(dataChanged()), this, SLOT(playbackPositionChanged()));
//care about mute track
connect( getTrack()->getMutedModel(), SIGNAL(dataChanged()),
this, SLOT(playbackPositionChanged()), Qt::DirectConnection );
//care about Clip position
connect( this, SIGNAL(positionChanged()), this, SLOT(updateTrackClips()));
updateTrackClips();
}
SampleClip::SampleClip(Track* track)
: SampleClip(track, Sample(), false)
{
}
SampleClip::SampleClip(const SampleClip& orig) :
Clip(orig),
m_sample(std::move(orig.m_sample)),
m_isPlaying(orig.m_isPlaying)
{
saveJournallingState( false );
setSampleFile( "" );
restoreJournallingState();
// we need to receive bpm-change-events, because then we have to
// change length of this Clip
connect( Engine::getSong(), SIGNAL(tempoChanged(lmms::bpm_t)),
this, SLOT(updateLength()), Qt::DirectConnection );
connect( Engine::getSong(), SIGNAL(timeSignatureChanged(int,int)),
this, SLOT(updateLength()));
//playbutton clicked or space key / on Export Song set isPlaying to false
connect( Engine::getSong(), SIGNAL(playbackStateChanged()),
this, SLOT(playbackPositionChanged()), Qt::DirectConnection );
//care about loops and jumps
connect(Engine::getSong(), &Song::playbackPositionJumped,
this, &SampleClip::playbackPositionChanged, Qt::DirectConnection);
//care about mute Clips
connect( this, SIGNAL(dataChanged()), this, SLOT(playbackPositionChanged()));
//care about mute track
connect( getTrack()->getMutedModel(), SIGNAL(dataChanged()),
this, SLOT(playbackPositionChanged()), Qt::DirectConnection );
//care about Clip position
connect( this, SIGNAL(positionChanged()), this, SLOT(updateTrackClips()));
updateTrackClips();
}
SampleClip::~SampleClip()
{
auto sampletrack = dynamic_cast<SampleTrack*>(getTrack());
if ( sampletrack )
{
sampletrack->updateClips();
}
}
void SampleClip::changeLength( const TimePos & _length )
{
Clip::changeLength(std::max(static_cast<int>(_length), 1));
}
const QString& SampleClip::sampleFile() const
{
return m_sample.sampleFile();
}
bool SampleClip::hasSampleFileLoaded(const QString & filename) const
{
return m_sample.sampleFile() == filename;
}
void SampleClip::setSampleBuffer(std::shared_ptr<const SampleBuffer> sb)
{
{
const auto guard = Engine::audioEngine()->requestChangesGuard();
m_sample = Sample(std::move(sb));
}
updateLength();
emit sampleChanged();
Engine::getSong()->setModified();
}
void SampleClip::setSampleFile(const QString& sf)
{
// Remove any prior offset in the clip
setStartTimeOffset(0);
if (!sf.isEmpty())
{
m_sample = Sample(SampleBuffer::fromFile(sf));
updateLength();
}
else
{
// If there is no sample, make the clip a bar long
float nom = Engine::getSong()->getTimeSigModel().getNumerator();
float den = Engine::getSong()->getTimeSigModel().getDenominator();
changeLength(DefaultTicksPerBar * (nom / den));
}
emit sampleChanged();
emit playbackPositionChanged();
}
void SampleClip::toggleRecord()
{
m_recordModel.setValue( !m_recordModel.value() );
emit dataChanged();
}
void SampleClip::playbackPositionChanged()
{
Engine::audioEngine()->removePlayHandlesOfTypes( getTrack(), PlayHandle::Type::SamplePlayHandle );
auto st = dynamic_cast<SampleTrack*>(getTrack());
st->setPlayingClips( false );
}
void SampleClip::updateTrackClips()
{
auto sampletrack = dynamic_cast<SampleTrack*>(getTrack());
if( sampletrack)
{
sampletrack->updateClips();
}
}
bool SampleClip::isPlaying() const
{
return m_isPlaying;
}
void SampleClip::setIsPlaying(bool isPlaying)
{
m_isPlaying = isPlaying;
}
void SampleClip::updateLength()
{
// If the clip has already been manually resized, don't automatically resize it.
if (getAutoResize())
{
changeLength(sampleLength());
setStartTimeOffset(0);
}
emit sampleChanged();
Engine::getSong()->setModified();
}
TimePos SampleClip::sampleLength() const
{
return static_cast<int>(m_sample.sampleSize() / Engine::framesPerTick(m_sample.sampleRate()));
}
void SampleClip::setSampleStartFrame(f_cnt_t startFrame)
{
m_sample.setStartFrame(startFrame);
}
void SampleClip::setSamplePlayLength(f_cnt_t length)
{
m_sample.setEndFrame(length);
}
void SampleClip::saveSettings( QDomDocument & _doc, QDomElement & _this )
{
if( _this.parentNode().nodeName() == "clipboard" )
{
_this.setAttribute( "pos", -1 );
}
else
{
_this.setAttribute( "pos", startPosition() );
}
_this.setAttribute( "len", length() );
_this.setAttribute( "hidden", isHidden() );
_this.setAttribute( "muted", isMuted() );
_this.setAttribute( "src", sampleFile() );
_this.setAttribute( "off", startTimeOffset() );
_this.setAttribute("autoresize", QString::number(getAutoResize()));
if( sampleFile() == "" )
{
QString s;
_this.setAttribute("data", m_sample.toBase64());
}
_this.setAttribute( "sample_rate", m_sample.sampleRate());
if (const auto& c = color())
{
_this.setAttribute("color", c->name());
}
if (m_sample.reversed())
{
_this.setAttribute("reversed", "true");
}
// TODO: start- and end-frame
}
void SampleClip::loadSettings( const QDomElement & _this )
{
if( _this.attribute( "pos" ).toInt() >= 0 )
{
movePosition( _this.attribute( "pos" ).toInt() );
}
if (const auto srcFile = _this.attribute("src"); !srcFile.isEmpty())
{
if (QFileInfo(PathUtil::toAbsolute(srcFile)).exists())
{
setSampleFile(srcFile);
}
else { Engine::getSong()->collectError(QString("%1: %2").arg(tr("Sample not found"), srcFile)); }
}
if( sampleFile().isEmpty() && _this.hasAttribute( "data" ) )
{
auto sampleRate = _this.hasAttribute("sample_rate") ? _this.attribute("sample_rate").toInt() :
Engine::audioEngine()->outputSampleRate();
auto buffer = SampleBuffer::fromBase64(_this.attribute("data"), sampleRate);
m_sample = Sample(std::move(buffer));
}
changeLength( _this.attribute( "len" ).toInt() );
setHidden( _this.attribute( "hidden" ).toInt() );
setMuted( _this.attribute( "muted" ).toInt() );
setStartTimeOffset( _this.attribute( "off" ).toInt() );
setAutoResize(_this.attribute("autoresize", "1").toInt());
if (_this.hasAttribute("color"))
{
setColor(QColor{_this.attribute("color")});
}
if(_this.hasAttribute("reversed"))
{
m_sample.setReversed(true);
emit wasReversed(); // tell SampleClipView to update the view
}
}
gui::ClipView * SampleClip::createView( gui::TrackView * _tv )
{
return new gui::SampleClipView( this, _tv );
}
} // namespace lmms