Klang C++ 0.7.15
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 }
 
unsigned int count
 
Note * items [CAPACITY]
 

Detailed Description

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

Definition at line 5654 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 5656 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 5660 of file klang.h.

5660 : synth(synth) {
5661 for (int n = 0; n < 128; n++)
5662 items[n] = nullptr;
5663 }
Note * items[CAPACITY]
Definition klang.h:309
SYNTH * synth
Definition klang.h:5655

◆ ~Notes()

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

Definition at line 5826 of file klang.h.

5826 {
5827 for (unsigned int n = 0; n < count; n++) {
5828 NOTE* tmp = items[n];
5829 items[n] = nullptr;
5830 delete tmp;
5831 }
5832 }

Member Function Documentation

◆ add() [1/3]

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

Definition at line 324 of file klang.h.

324 {
325 if (count < CAPACITY)
326 return &items[count++];
327 return nullptr;
328 }

◆ add() [2/3]

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

Definition at line 318 of file klang.h.

318 {
319 if (count < CAPACITY)
320 items[count++] = item;
321 }

◆ add() [3/3]

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

Definition at line 5667 of file klang.h.

5667 {
5668 for (int n = 0; n < count; n++) {
5669 TYPE* note = new TYPE();
5670 note->attach(synth);
5671 Array::add(note);
5672 }
5673 }
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:324

◆ assign()

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

Definition at line 5679 of file klang.h.

5679 {
5680 // favour unused voices
5681 for (unsigned int i = 0; i < count; i++) {
5682 if (items[i]->stage == NOTE::Off) {
5683 noteStart[i] = noteOns++;
5684 return i;
5685 }
5686 }
5687
5688 // no free notes => steal oldest released note?
5689 int oldest = -1;
5690 unsigned int oldest_start = 0;
5691 for (unsigned int i = 0; i < count; i++) {
5692 if (items[i]->stage == NOTE::Release) {
5693 if (oldest == -1 || noteStart[i] < oldest_start) {
5694 oldest = i;
5695 oldest_start = noteStart[i];
5696 }
5697 }
5698 }
5699 if (oldest != -1) {
5700 noteStart[oldest] = noteOns++;
5701 return oldest;
5702 }
5703
5704 // no available released notes => steal oldest playing note
5705 oldest = -1;
5706 oldest_start = 0;
5707 for (unsigned int i = 0; i < count; i++) {
5708 if (oldest == -1 || noteStart[i] < oldest_start) {
5709 oldest = i;
5710 oldest_start = noteStart[i];
5711 }
5712 }
5713 noteStart[oldest] = noteOns++;
5714 return oldest;
5715 }
unsigned int noteOns
Definition klang.h:5676
unsigned int noteStart[128]
Definition klang.h:5677

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

◆ capacity()

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

Definition at line 312 of file klang.h.

312{ return CAPACITY; }

◆ clear()

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

Definition at line 331 of file klang.h.

331{ count = 0; }

◆ max()

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

Definition at line 334 of file klang.h.

334 {
335 float max = 0.f;
336 for (unsigned int i = 0; i < count; i++)
337 if (abs(items[i]) > max)
338 max = items[i];
339 return max;
340 }

◆ mean()

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

Definition at line 343 of file klang.h.

343 {
344 float sum = 0.f;
345 for (unsigned int i = 0; i < count; i++)
346 sum += abs(items[i]);
347 return sum / count;
348 }

◆ normalise()

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

Definition at line 359 of file klang.h.

359 {
360 if (!count) return;
361
362 const float current = (mode == Peak) ? max() : (mode == Mean) ? mean() : rms();
363 const float scale = current == 0.f ? 0.f : target / current;
364 for (int i = 0; i < count; i++)
365 items[i] *= scale;
366 }
@ Peak
Definition klang.h:149
@ Mean
Definition klang.h:149

◆ operator[]() [1/2]

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

Definition at line 369 of file klang.h.

369{ return items[index]; }

◆ operator[]() [2/2]

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

Definition at line 372 of file klang.h.

372{ return items[index]; }

◆ rms()

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

Definition at line 351 of file klang.h.

351 {
352 float sum = 0.f;
353 for (unsigned int i = 0; i < count; i++)
354 sum += items[i] * items[i];
355 return sqrt(sum / count);
356 }

◆ size()

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

Definition at line 315 of file klang.h.

315{ return count; }

Member Data Documentation

◆ count

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

Definition at line 308 of file klang.h.

◆ items

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

Definition at line 309 of file klang.h.

◆ noteOns

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

Definition at line 5676 of file klang.h.

◆ noteStart

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

Definition at line 5677 of file klang.h.

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

◆ synth

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

Definition at line 5655 of file klang.h.