Klang C++
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Notes< SYNTH, NOTE >

Synthesiser note array. More...

#include <klang.h>

+ Inheritance diagram for klang::Notes< SYNTH, NOTE >:

Public Types

typedef Array< NOTE *, 128 > Array
 

Public Member Functions

 Notes (SYNTH *synth)
 
virtual ~Notes ()
 
template<class TYPE >
void add (int count)
 
int assign ()
 
unsigned int size () const
 The current number of items in the array.
 
void add (const Note *&item)
 Add the specified item to the end of the array.
 
Note ** add ()
 Add a blank item to the end of the array, returning a pointer that allows the item to be modified.
 
void clear ()
 Clear the array contents. Only resets the item count, without wiping memory.
 
float max () const
 Find the maximum value in the array.
 
float mean () const
 Find the mean average of values in the array.
 
float rms () const
 Find the root mean squared (RMS) average of values in the array.
 
void normalise (float target=1.f, int mode=Peak)
 Normalises values in the array to the specified target, based on peak, mean, or RMS value;.
 
Note *& operator[] (int index)
 Returns a reference to the array item at the given index.
 
const Note *& operator[] (int index) const
 Returns a read-only reference to the array item at the given index.
 

Static Public Member Functions

static int capacity ()
 The maximum capacity of the array.
 

Public Attributes

SYNTH * synth
 
unsigned int noteOns = 0
 
unsigned int noteStart [128] = { 0 }
 
Noteitems [CAPACITY]
 
unsigned int count
 

Detailed Description

template<class SYNTH, class NOTE = Note>
struct klang::Notes< SYNTH, NOTE >

Definition at line 3191 of file klang.h.

Member Typedef Documentation

◆ Array

template<class SYNTH , class NOTE = Note>
Array<NOTE*, 128> klang::Notes< SYNTH, NOTE >::Array

Definition at line 3193 of file klang.h.

Constructor & Destructor Documentation

◆ Notes()

template<class SYNTH , class NOTE = Note>
klang::Notes< SYNTH, NOTE >::Notes ( SYNTH * synth)
inline

Definition at line 3197 of file klang.h.

3197 : synth(synth) {
3198 for (int n = 0; n < 128; n++)
3199 items[n] = nullptr;
3200 }
Note * items[CAPACITY]
Definition klang.h:239
SYNTH * synth
Definition klang.h:3192

References klang::Notes< SYNTH, NOTE >::synth.

Referenced by klang::Synth::Synth().

◆ ~Notes()

template<class SYNTH , class NOTE >
klang::Notes< SYNTH, NOTE >::~Notes ( )
inlinevirtual

Definition at line 3294 of file klang.h.

3294 {
3295 for (unsigned int n = 0; n < count; n++) {
3296 NOTE* tmp = items[n];
3297 items[n] = nullptr;
3298 delete tmp;
3299 }
3300 }

Member Function Documentation

◆ add() [1/3]

Note * * klang::Array< Note *, CAPACITY >::add ( )
inlineinherited

Definition at line 255 of file klang.h.

255 {
256 if (count < CAPACITY)
257 return &items[count++];
258 return nullptr;
259 }

◆ add() [2/3]

void klang::Array< Note *, CAPACITY >::add ( const Note * & item)
inlineinherited

Definition at line 249 of file klang.h.

249 {
250 if(count < CAPACITY)
251 items[count++] = item;
252 }

◆ add() [3/3]

template<class SYNTH , class NOTE = Note>
template<class TYPE >
void klang::Notes< SYNTH, NOTE >::add ( int count)
inline

Definition at line 3204 of file klang.h.

3204 {
3205 for (int n = 0; n < count; n++) {
3206 TYPE* note = new TYPE();
3207 note->attach(synth);
3208 Array::add(note);
3209 }
3210 }
TYPE * add()
Add a blank item to the end of the array, returning a pointer that allows the item to be modified.
Definition klang.h:255

◆ assign()

template<class SYNTH , class NOTE = Note>
int klang::Notes< SYNTH, NOTE >::assign ( )
inline

Definition at line 3216 of file klang.h.

3216 {
3217 // favour unused voices
3218 for (int i = 0; i < count; i++) {
3219 if (items[i]->stage == NOTE::Off){
3220 noteStart[i] = noteOns++;
3221 return i;
3222 }
3223 }
3224
3225 // no free notes => steal oldest released note?
3226 int oldest = -1;
3227 unsigned int oldest_start = 0;
3228 for (int i = 0; i < count; i++) {
3229 if (items[i]->stage == NOTE::Release) {
3230 if(oldest == -1 || noteStart[i] < oldest_start){
3231 oldest = i;
3232 oldest_start = noteStart[i];
3233 }
3234 }
3235 }
3236 if(oldest != -1){
3237 noteStart[oldest] = noteOns++;
3238 return oldest;
3239 }
3240
3241 // no available released notes => steal oldest playing note
3242 oldest = -1;
3243 oldest_start = 0;
3244 for (int i = 0; i < count; i++) {
3245 if(oldest == -1 || noteStart[i] < oldest_start){
3246 oldest = i;
3247 oldest_start = noteStart[i];
3248 }
3249 }
3250 noteStart[oldest] = noteOns++;
3251 return oldest;
3252 }
unsigned int noteOns
Definition klang.h:3213
unsigned int noteStart[128]
Definition klang.h:3214

◆ capacity()

static int klang::Array< Note *, CAPACITY >::capacity ( )
inlinestaticinherited

Definition at line 243 of file klang.h.

243{ return CAPACITY; }

◆ clear()

void klang::Array< Note *, CAPACITY >::clear ( )
inlineinherited

Definition at line 262 of file klang.h.

262{ count = 0; }

◆ max()

float klang::Array< Note *, CAPACITY >::max ( ) const
inlineinherited

Definition at line 265 of file klang.h.

265 {
266 float max = 0.f;
267 for (unsigned int i = 0; i < count; i++)
268 if (abs(items[i]) > max)
269 max = items[i];
270 return max;
271 }

◆ mean()

float klang::Array< Note *, CAPACITY >::mean ( ) const
inlineinherited

Definition at line 274 of file klang.h.

274 {
275 float sum = 0.f;
276 for (unsigned int i = 0; i < count; i++)
277 sum += abs(items[i]);
278 return sum / count;
279 }

◆ normalise()

void klang::Array< Note *, CAPACITY >::normalise ( float target = 1.f,
int mode = Peak )
inlineinherited

Definition at line 290 of file klang.h.

290 {
291 if (!count) return;
292
293 const float current = (mode == Peak) ? max() : (mode == Mean) ? mean() : rms();
294 const float scale = current == 0.f ? 0.f : target / current;
295 for (int i = 0; i < count; i++)
296 items[i] *= scale;
297 }
@ Peak
Definition klang.h:83
@ Mean
Definition klang.h:83

◆ operator[]() [1/2]

Note * & klang::Array< Note *, CAPACITY >::operator[] ( int index)
inlineinherited

Definition at line 300 of file klang.h.

300{ return items[index]; }

◆ operator[]() [2/2]

const Note * & klang::Array< Note *, CAPACITY >::operator[] ( int index) const
inlineinherited

Definition at line 303 of file klang.h.

303{ return items[index]; }

◆ rms()

float klang::Array< Note *, CAPACITY >::rms ( ) const
inlineinherited

Definition at line 282 of file klang.h.

282 {
283 float sum = 0.f;
284 for (unsigned int i = 0; i < count; i++)
285 sum += items[i] * items[i];
286 return sqrt(sum / count);
287 }

◆ size()

unsigned int klang::Array< Note *, CAPACITY >::size ( ) const
inlineinherited

Definition at line 246 of file klang.h.

246{ return count; }

Member Data Documentation

◆ count

unsigned int klang::Array< Note *, CAPACITY >::count
inherited

Definition at line 240 of file klang.h.

◆ items

Note * klang::Array< Note *, CAPACITY >::items[CAPACITY]
inherited

Definition at line 239 of file klang.h.

◆ noteOns

template<class SYNTH , class NOTE = Note>
unsigned int klang::Notes< SYNTH, NOTE >::noteOns = 0

Definition at line 3213 of file klang.h.

◆ noteStart

template<class SYNTH , class NOTE = Note>
unsigned int klang::Notes< SYNTH, NOTE >::noteStart[128] = { 0 }

Definition at line 3214 of file klang.h.

3214{ 0 }; // track age of notes (for note stealing)

◆ synth

template<class SYNTH , class NOTE = Note>
SYNTH* klang::Notes< SYNTH, NOTE >::synth

Definition at line 3192 of file klang.h.

Referenced by klang::Notes< SYNTH, NOTE >::Notes().