forked from LMMS/lmms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGigPlayer.h
More file actions
372 lines (269 loc) · 8.24 KB
/
GigPlayer.h
File metadata and controls
372 lines (269 loc) · 8.24 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* GigPlayer.h - a GIG player using libgig (based on Sf2 player plugin)
*
* Copyright (c) 2008 Paul Giblock <drfaygo/at/gmail/dot/com>
* Copyright (c) 2009-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.
*
*/
#ifndef GIG_PLAYER_H
#define GIG_PLAYER_H
#include <QList>
#include <QMutex>
#include <QMutexLocker>
#include <samplerate.h>
#include "AudioResampler.h"
#include "Instrument.h"
#include "PixmapButton.h"
#include "InstrumentView.h"
#include "Knob.h"
#include "LcdSpinBox.h"
#include "SampleFrame.h"
#include "gig.h"
class QLabel;
namespace lmms
{
class NotePlayHandle;
namespace gui
{
class PatchesDialog;
class GigInstrumentView;
}
struct GIGPluginData
{
int midiNote;
} ;
// Load a GIG file using libgig
class GigInstance
{
public:
GigInstance( QString filename ) :
riff( filename.toUtf8().constData() ),
gig( &riff )
{}
private:
RIFF::File riff;
public:
gig::File gig;
} ;
// Stores options for the notes, e.g. velocity and release time
struct Dimension
{
Dimension() :
release( false )
{
for( int i = 0; i < 8; ++i )
{
DimValues[i] = 0;
}
}
uint DimValues[8];
bool release;
} ;
// Takes information from the GIG file for a certain note and provides the
// amplitude (0-1) to multiply the signal by (internally incrementing the
// position in the envelope when asking for the amplitude).
class ADSR
{
// From the file
float preattack; // initial amplitude (0-1)
float attack; // 0-60s
float decay1; // 0-60s
float decay2; // 0-60s
bool infiniteSustain; // i.e., no decay2
float sustain; // sustain amplitude (0-1)
float release; // 0-60s
// Used to calculate current amplitude
float amplitude;
bool isAttack;
bool isRelease;
bool isDone;
f_cnt_t attackPosition;
f_cnt_t attackLength;
f_cnt_t decayLength;
f_cnt_t releasePosition;
f_cnt_t releaseLength;
public:
ADSR();
ADSR( gig::DimensionRegion * region, int sampleRate );
void keyup(); // We will begin releasing starting now
bool done(); // Is this sample done playing?
float value(); // What's the current amplitude
void inc( f_cnt_t num ); // Increment internal positions by num
} ;
// The sample from the GIG file with our current position in both the sample
// and the envelope
class GigSample
{
public:
GigSample(gig::Sample* pSample, gig::DimensionRegion* pDimRegion, float attenuation,
AudioResampler::Mode interpolation, float desiredFreq);
~GigSample() = default;
// Needed when initially creating in QList
GigSample( const GigSample& g );
GigSample& operator=( const GigSample& g );
gig::Sample * sample;
gig::DimensionRegion * region;
float attenuation;
ADSR adsr;
// The position in sample
f_cnt_t pos;
// Whether to change the pitch of the samples, e.g. if there's only one
// sample per octave and you want that sample pitch shifted for the rest of
// the notes in the octave, this will be true
bool pitchtrack;
// Used to convert sample rates
AudioResampler m_resampler;
static constexpr auto BufferSize = f_cnt_t{16};
std::array<SampleFrame, BufferSize> m_sourceBuffer{};
std::array<SampleFrame, BufferSize> m_mixBuffer{};
std::span<SampleFrame> m_sourceWindow{};
std::span<SampleFrame> m_mixWindow{};
// Used changing the pitch of the note if desired
float sampleFreq;
float freqFactor;
} ;
// What portion of a note are we in?
enum class GigState
{
// We just pressed the key
KeyDown,
// The note is currently playing
PlayingKeyDown,
// We just released the key
KeyUp,
// The note is being released, e.g. a release sample is playing
PlayingKeyUp,
// The note is done playing, you can delete this note now
Completed
} ;
// Corresponds to a certain midi note pressed, but may contain multiple samples
class GigNote
{
public:
int midiNote;
int velocity;
bool release; // Whether to trigger a release sample on key up
bool isRelease; // Whether this is a release sample, changes when we delete it
GigState state;
float frequency;
std::vector<GigSample> samples;
// Used to determine which note should be released on key up
//
// Note: if accessing the data, be careful not to access it after the key
// has been released since that's when it is deleted
GIGPluginData * handle;
GigNote( int midiNote, int velocity, float frequency, GIGPluginData * handle )
: midiNote( midiNote ), velocity( velocity ),
release( false ), isRelease( false ), state( GigState::KeyDown ),
frequency( frequency ), handle( handle )
{
}
} ;
class GigInstrument : public Instrument
{
Q_OBJECT
mapPropertyFromModel( int, getBank, setBank, m_bankNum );
mapPropertyFromModel( int, getPatch, setPatch, m_patchNum );
public:
GigInstrument( InstrumentTrack * _instrument_track );
~GigInstrument() override;
void play( SampleFrame* _working_buffer ) override;
void playNote( NotePlayHandle * _n,
SampleFrame* _working_buffer ) override;
void deleteNotePluginData( NotePlayHandle * _n ) override;
void saveSettings( QDomDocument & _doc, QDomElement & _parent ) override;
void loadSettings( const QDomElement & _this ) override;
void loadFile( const QString & _file ) override;
AutomatableModel * childModel( const QString & _modelName ) override;
QString nodeName() const override;
gui::PluginView* instantiateView( QWidget * _parent ) override;
QString getCurrentPatchName();
void setParameter( const QString & _param, const QString & _value );
public slots:
void openFile( const QString & _gigFile, bool updateTrackName = true );
void updatePatch();
void updateSampleRate();
private:
// The GIG file and instrument we're using
GigInstance * m_instance;
gig::Instrument * m_instrument;
// Part of the UI
QString m_filename;
gui::LcdSpinBoxModel m_bankNum;
gui::LcdSpinBoxModel m_patchNum;
FloatModel m_gain;
// Locking for the data
QMutex m_synthMutex;
QMutex m_notesMutex;
// List of all the currently playing notes
QList<GigNote> m_notes;
// Used when determining which samples to use
uint32_t m_RandomSeed;
float m_currentKeyDimension;
private:
// Delete the current GIG instance if one is open
void freeInstance();
// Open the instrument in the currently-open GIG file
void getInstrument();
// Create "dimension" to select desired samples from GIG file based on
// parameters such as velocity
Dimension getDimensions( gig::Region * pRegion, int velocity, bool release );
// Load sample data from the Gig file, looping the sample where needed
void loadSample( GigSample& sample, SampleFrame* sampleData, f_cnt_t samples );
f_cnt_t getLoopedIndex( f_cnt_t index, f_cnt_t startf, f_cnt_t endf ) const;
f_cnt_t getPingPongIndex( f_cnt_t index, f_cnt_t startf, f_cnt_t endf ) const;
// Add the desired samples to the note, either normal samples or release
// samples
void addSamples( GigNote & gignote, bool wantReleaseSample );
friend class gui::GigInstrumentView;
signals:
void fileLoading();
void fileChanged();
void patchChanged();
} ;
namespace gui
{
class GigInstrumentView : public InstrumentViewFixedSize
{
Q_OBJECT
public:
GigInstrumentView( Instrument * _instrument,
QWidget * _parent );
~GigInstrumentView() override = default;
private:
void modelChanged() override;
PixmapButton * m_fileDialogButton;
PixmapButton * m_patchDialogButton;
LcdSpinBox * m_bankNumLcd;
LcdSpinBox * m_patchNumLcd;
QLabel * m_filenameLabel;
QLabel * m_patchLabel;
Knob * m_gainKnob;
static PatchesDialog * s_patchDialog;
protected slots:
void invalidateFile();
void showFileDialog();
void showPatchDialog();
void updateFilename();
void updatePatchName();
} ;
} // namespace gui
} // namespace lmms
#endif