hydrogen 1.2.6
Sampler.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23
24#ifndef SAMPLER_H
25#define SAMPLER_H
26
27#include <core/Object.h>
28#include <core/Globals.h>
30
31#include <inttypes.h>
32#include <vector>
33#include <memory>
34
35namespace H2Core
36{
37
38class Note;
39class Song;
40class Sample;
42class Instrument;
45
49
50class Sampler : public H2Core::Object<Sampler>
51{
53public:
54
122
126 static constexpr float K_NORM_DEFAULT = 1.33333333333333;
127
128 // pan law functions
129 static float ratioStraightPolygonalPanLaw( float fPan );
130 static float ratioConstPowerPanLaw( float fPan );
131 static float ratioConstSumPanLaw( float fPan );
132 static float linearStraightPolygonalPanLaw( float fPan );
133 static float linearConstPowerPanLaw( float fPan );
134 static float linearConstSumPanLaw( float fPan );
135 static float polarStraightPolygonalPanLaw( float fPan );
136 static float polarConstPowerPanLaw( float fPan );
137 static float polarConstSumPanLaw( float fPan );
138 static float quadraticStraightPolygonalPanLaw( float fPan );
139 static float quadraticConstPowerPanLaw( float fPan );
140 static float quadraticConstSumPanLaw( float fPan );
141 // customly compensated
142 static float linearConstKNormPanLaw( float fPan, float k );
143 static float polarConstKNormPanLaw( float fPan, float k );
144 static float ratioConstKNormPanLaw( float fPan, float k );
145 static float quadraticConstKNormPanLaw( float fPan, float k );
146
159 static float getRatioPan( float fPan_L, float fPan_R );
160
161
164
171 Sampler();
172 ~Sampler();
173
174 void process( uint32_t nFrames );
175
179 bool isRenderingNotes() const;
180
182 void noteOn( Note * pNote );
183
185 void noteOff( Note *pNote );
186 void midiKeyboardNoteOff( int key );
187
188 void stopPlayingNotes( std::shared_ptr<Instrument> pInstr = nullptr );
189
191 return m_playingNotesQueue.size();
192 }
193
194 void preview_sample( std::shared_ptr<Sample> pSample, int length );
195 void preview_instrument( std::shared_ptr<Instrument> pInstr );
196
197 bool isInstrumentPlaying( std::shared_ptr<Instrument> pInstr );
198
202
203 std::shared_ptr<Instrument> getPreviewInstrument() const {
205 }
206
207 std::shared_ptr<Instrument> getPlaybackTrackInstrument() const {
209 }
210
212
222
235
236 const std::vector<Note*> getPlayingNotesQueue() const;
237
238private:
239 std::vector<Note*> m_playingNotesQueue;
240 std::vector<Note*> m_queuedNoteOffs;
241
243 std::shared_ptr<Instrument> m_pPlaybackTrackInstrument;
244
246 std::shared_ptr<Instrument> m_pPreviewInstrument;
247
254
256
259 float panLaw( float fPan, std::shared_ptr<Song> pSong );
260
261
262
263 bool processPlaybackTrack(int nBufferSize);
264
270 bool renderNote( Note* pNote, unsigned nBufferSize );
271
273
275 std::shared_ptr<Sample> pSample,
276 Note *pNote,
277 std::shared_ptr<SelectedLayerInfo> pSelectedLayerInfo,
278 std::shared_ptr<InstrumentComponent> pCompo,
279 std::shared_ptr<DrumkitComponent> pDrumCompo,
280 int nBufferSize,
281 int nInitialBufferPos,
282 float cost_L,
283 float cost_R,
284 float cost_track_L,
285 float cost_track_R,
286 float fLayerPitch
287 );
288};
289
290inline const std::vector<Note*> Sampler::getPlayingNotesQueue() const {
291 return m_playingNotesQueue;
292}
293
294} // namespace
295
296#endif
297
#define H2_OBJECT(name)
Definition Object.h:227
Instrument class.
Definition Instrument.h:55
A note plays an associated instrument with a velocity left and right pan.
Definition Note.h:101
static float linearConstKNormPanLaw(float fPan, float k)
Definition Sampler.cpp:353
void process(uint32_t nFrames)
Definition Sampler.cpp:106
void noteOff(Note *pNote)
Stop playing a note.
Definition Sampler.cpp:236
static float linearStraightPolygonalPanLaw(float fPan)
Definition Sampler.cpp:293
static float quadraticConstKNormPanLaw(float fPan, float k)
Definition Sampler.cpp:358
std::shared_ptr< Instrument > getPlaybackTrackInstrument() const
Definition Sampler.h:207
void preview_instrument(std::shared_ptr< Instrument > pInstr)
Definition Sampler.cpp:1392
void handleTimelineOrTempoChange()
Recalculates all note starts to make them valid again after a TempoMarker was added to or deleted fro...
Definition Sampler.cpp:421
static float linearConstPowerPanLaw(float fPan)
Definition Sampler.cpp:302
float * m_pMainOut_L
sampler main out (left channel)
Definition Sampler.h:162
static float ratioConstPowerPanLaw(float fPan)
Definition Sampler.cpp:275
bool processPlaybackTrack(int nBufferSize)
Definition Sampler.cpp:797
bool isRenderingNotes() const
Definition Sampler.cpp:181
float * m_pMainOut_R
sampler main out (right channel)
Definition Sampler.h:163
int m_nPlayBackSamplePosition
Definition Sampler.h:255
void reinitializePlaybackTrack()
Loading of the playback track.
Definition Sampler.cpp:1430
std::shared_ptr< Instrument > getPreviewInstrument() const
Definition Sampler.h:203
static float ratioStraightPolygonalPanLaw(float fPan)
Definition Sampler.cpp:266
bool renderNoteResample(std::shared_ptr< Sample > pSample, Note *pNote, std::shared_ptr< SelectedLayerInfo > pSelectedLayerInfo, std::shared_ptr< InstrumentComponent > pCompo, std::shared_ptr< DrumkitComponent > pDrumCompo, int nBufferSize, int nInitialBufferPos, float cost_L, float cost_R, float cost_track_L, float cost_track_R, float fLayerPitch)
Definition Sampler.cpp:987
static float quadraticConstSumPanLaw(float fPan)
Definition Sampler.cpp:348
static float ratioConstSumPanLaw(float fPan)
Definition Sampler.cpp:284
std::vector< Note * > m_queuedNoteOffs
Definition Sampler.h:240
std::shared_ptr< Instrument > m_pPlaybackTrackInstrument
Instrument used for the playback track feature.
Definition Sampler.h:243
static float polarConstPowerPanLaw(float fPan)
Definition Sampler.cpp:322
static float quadraticConstPowerPanLaw(float fPan)
Definition Sampler.cpp:343
static constexpr float K_NORM_DEFAULT
default k for pan law with -4.5dB center compensation, given L^k + R^k = const it is the mean comprom...
Definition Sampler.h:126
float panLaw(float fPan, std::shared_ptr< Song > pSong)
function to direct the computation to the selected pan law function
Definition Sampler.cpp:380
int m_nMaxLayers
Maximum number of layers to be used in the Instrument editor.
Definition Sampler.h:253
static float ratioConstKNormPanLaw(float fPan, float k)
Definition Sampler.cpp:370
void noteOn(Note *pNote)
Start playing a note.
Definition Sampler.cpp:185
void midiKeyboardNoteOff(int key)
Definition Sampler.cpp:224
bool renderNote(Note *pNote, unsigned nBufferSize)
Render a note.
Definition Sampler.cpp:511
std::vector< Note * > m_playingNotesQueue
Definition Sampler.h:239
Interpolation::InterpolateMode getInterpolateMode()
Definition Sampler.h:211
void stopPlayingNotes(std::shared_ptr< Instrument > pInstr=nullptr)
Definition Sampler.cpp:1335
static float quadraticStraightPolygonalPanLaw(float fPan)
Definition Sampler.cpp:334
Interpolation::InterpolateMode m_interpolateMode
Definition Sampler.h:272
static float linearConstSumPanLaw(float fPan)
Definition Sampler.cpp:307
static float polarConstKNormPanLaw(float fPan, float k)
Definition Sampler.cpp:363
const std::vector< Note * > getPlayingNotesQueue() const
Definition Sampler.h:290
void handleSongSizeChange()
Recalculates all note starts and positions to make them valid again after the song size changed,...
Definition Sampler.cpp:481
PAN_LAW_TYPES
PAN LAWS The following pan law functions return pan_L (==L, which is the gain for Left channel).
Definition Sampler.h:104
@ QUADRATIC_CONST_K_NORM
Definition Sampler.h:120
@ LINEAR_STRAIGHT_POLYGONAL
Definition Sampler.h:108
@ RATIO_STRAIGHT_POLYGONAL
Definition Sampler.h:105
@ POLAR_STRAIGHT_POLYGONAL
Definition Sampler.h:111
@ QUADRATIC_CONST_POWER
Definition Sampler.h:115
@ QUADRATIC_STRAIGHT_POLYGONAL
Definition Sampler.h:114
bool isInstrumentPlaying(std::shared_ptr< Instrument > pInstr)
Definition Sampler.cpp:1418
void setInterpolateMode(Interpolation::InterpolateMode mode)
Definition Sampler.h:199
Sampler()
Constructor of the Sampler.
Definition Sampler.cpp:70
static float polarStraightPolygonalPanLaw(float fPan)
Definition Sampler.cpp:312
void preview_sample(std::shared_ptr< Sample > pSample, int length)
Preview, uses only the first layer.
Definition Sampler.cpp:1362
static float polarConstSumPanLaw(float fPan)
Definition Sampler.cpp:328
static float getRatioPan(float fPan_L, float fPan_R)
This function is used to load old version files (v<=1.1).
Definition Sampler.cpp:252
std::shared_ptr< Instrument > m_pPreviewInstrument
Instrument used for the preview feature.
Definition Sampler.h:246
int getPlayingNotesNumber()
Definition Sampler.h:190
Song class.
Definition Song.h:61
Auxiliary variables storing the rendering state of a H2Core::Note within the H2Core::Sampler.
Definition Note.h:62