Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::buffer

Language Guide / Signal Flow

buffer

klang::buffer

Mono audio storage or a view of existing samples.

Construct buffer(n) to allocate storage for n mono samples, or pass a pointer and a sample count to view existing storage. A view borrows that storage for its lifetime. Copying a buffer creates a view of the same samples. Use the cursor for sequential access and call rewind() before another pass through the block.

Example ยท Apply gain to a whole buffer

while (!block.finished())
    block++ *= 0.5;

See also stereo::buffer.

Audio buffer (mono) More...

#include <klang.h>

+ Inheritance diagram for klang::buffer:

Public Types

typedef signal signal
 

Public Member Functions

 buffer (float *buffer, int size)
 
 buffer (float *buffer, int size, float initial)
 
 buffer (int size=1, float initial=0)
 
 buffer (const buffer &buf)
 
virtual ~buffer ()
 
void attach (const buffer &buffer, int size=0)
 
void rewind (int offset=0)
 
void clear ()
 
void clear (int size)
 
int offset () const
 
void set (float value=0)
 
signal & operator[] (int offset)
 
signal & operator[] (unsigned int offset)
 
signal operator[] (float offset)
 
signal operator[] (float offset) const
 
const signal & operator[] (int index) const
 
 operator signal & ()
 
 operator const signal & () const
 
 operator double () const
 
bool finished () const
 
signal & operator++ (int)
 
signal & operator= (const signal &in)
 
signal & operator+= (const signal &in)
 
signal & operator*= (const signal &in)
 
buffer & operator= (const buffer &in)
 
buffer & operator<< (const signal &in)
 
float * data ()
 
const float * data () const
 
float level () const
 
float rms () const
 

Public Attributes

const int size
 

Protected Attributes

const unsigned int mask = 0xFFFFFFFF
 
const bool owned = false
 
float * samples
 
signal * ptr
 
signal * end
 

Detailed Description

Definition at line 2610 of file klang.h.

Member Typedef Documentation

◆ signal

Definition at line 2630 of file klang.h.

Constructor & Destructor Documentation

◆ buffer() [1/4]

klang::buffer::buffer ( float * buffer,
int size )
inline

Definition at line 2633 of file klang.h.

2634 : owned(false), samples(buffer), size(size) {
2635 rewind();
2636 }
const bool owned
Definition klang.h:2625
buffer(float *buffer, int size)
Definition klang.h:2633
float * samples
Definition klang.h:2626
const int size
Definition klang.h:2631
void rewind(int offset=0)
Definition klang.h:2665

References rewind().

◆ buffer() [2/4]

klang::buffer::buffer ( float * buffer,
int size,
float initial )
inline

Definition at line 2638 of file klang.h.

2639 : owned(false), samples(buffer), size(size) {
2640 rewind();
2641 set(initial);
2642 }
void set(float value=0)
Definition klang.h:2685

References rewind(), and set().

◆ buffer() [3/4]

klang::buffer::buffer ( int size = 1,
float initial = 0 )
inline

Definition at line 2644 of file klang.h.

2645 : mask(capacity(size) - 1), owned(true), samples(new float[capacity(size)]), size(size) {
2646 rewind();
2647 set(initial);
2648 }
constexpr unsigned int capacity(unsigned int n)
Definition klang.h:2612
const unsigned int mask
Definition klang.h:2624

References rewind(), and set().

◆ buffer() [4/4]

klang::buffer::buffer ( const buffer & buf)
inline

Definition at line 2650 of file klang.h.

2650 : mask(buf.mask), owned(false), samples(buf.samples), size(buf.size) {
2651 rewind();
2652 }

References rewind().

◆ ~buffer()

virtual klang::buffer::~buffer ( )
inlinevirtual

Definition at line 2654 of file klang.h.

2654 {
2655 if (owned)
2656 delete[] samples;
2657 }

References owned, and samples.

Member Function Documentation

◆ attach()

void klang::buffer::attach ( const buffer & buffer,
int size = 0 )
inline

Definition at line 2659 of file klang.h.

2659 {
2660 samples = buffer.samples;
2661 rewind();
2662 end = (signal*)&samples[size];
2663 }
signal signal
Definition klang.h:2630
signal * end
Definition klang.h:2628

References end, rewind(), samples, and size.

◆ capacity()

unsigned int klang::buffer::capacity ( unsigned int n)
inlineconstexprprotected

Definition at line 2612 of file klang.h.

2612 {
2613 // Decrement n to handle the case where n is already a power of 2
2614 n--;
2615 n |= n >> 1;
2616 n |= n >> 2;
2617 n |= n >> 4;
2618 n |= n >> 8;
2619 n |= n >> 16;
2620 // Increment n to get the next power of 2
2621 n++;
2622 return n;
2623 }

◆ clear() [1/2]

void klang::buffer::clear ( )
inline

Definition at line 2673 of file klang.h.

2673 {
2674 memset(samples, 0, sizeof(float) * size);
2675 }

References samples, and size.

Referenced by klang::Stereo::buffer::clear(), klang::Stereo::buffer::clear(), and set().

◆ clear() [2/2]

void klang::buffer::clear ( int size)
inline

Definition at line 2677 of file klang.h.

2677 {
2678 memset(samples, 0, sizeof(float) * (size < buffer::size ? size : buffer::size));
2679 }

References samples, and size.

◆ data() [1/2]

float * klang::buffer::data ( )
inline

Definition at line 2762 of file klang.h.

2762{ return samples; }

References samples.

Referenced by klang::File::operator>>().

◆ data() [2/2]

const float * klang::buffer::data ( ) const
inline

Definition at line 2763 of file klang.h.

2763{ return samples; }

References samples.

◆ finished()

bool klang::buffer::finished ( ) const
inline

◆ level()

float klang::buffer::level ( ) const
inline

Definition at line 2765 of file klang.h.

2765 {
2766 float level = 0.f;
2767 for (int s = 0; s < size; s++)
2768 level += FABS(samples[s]);
2769 return level / size;
2770 }
float level() const
Definition klang.h:2765

References level(), samples, and size.

Referenced by level().

◆ offset()

int klang::buffer::offset ( ) const
inline

Definition at line 2681 of file klang.h.

2681 {
2682 return int((signal*)&samples[0] - ptr);
2683 }

References ptr, and samples.

Referenced by operator[](), operator[](), operator[](), operator[](), and rewind().

◆ operator const signal &()

klang::buffer::operator const signal & ( ) const
inline

Definition at line 2722 of file klang.h.

2722 {
2723 return *ptr;
2724 }

References ptr.

◆ operator double()

klang::buffer::operator double ( ) const
inlineexplicit

Definition at line 2726 of file klang.h.

2726 {
2727 return *ptr;
2728 }

References ptr.

◆ operator signal &()

klang::buffer::operator signal & ( )
inline

Definition at line 2718 of file klang.h.

2718 {
2719 return *ptr;
2720 }

References ptr.

◆ operator*=()

signal & klang::buffer::operator*= ( const signal & in)
inline

Definition at line 2747 of file klang.h.

2747 {
2748 *ptr *= in;
2749 return *this;
2750 }

References ptr.

◆ operator++()

signal & klang::buffer::operator++ ( int )
inline

Definition at line 2734 of file klang.h.

2734 {
2735 return *ptr++;
2736 }

References ptr.

◆ operator+=()

signal & klang::buffer::operator+= ( const signal & in)
inline

Definition at line 2742 of file klang.h.

2742 {
2743 *ptr += in;
2744 return *this;
2745 }

References ptr.

◆ operator<<()

buffer & klang::buffer::operator<< ( const signal & in)
inline

Definition at line 2757 of file klang.h.

2757 {
2758 *ptr = in;
2759 return *this;
2760 }

References ptr.

◆ operator=() [1/2]

buffer & klang::buffer::operator= ( const buffer & in)
inline

Definition at line 2752 of file klang.h.

2752 {
2753 memcpy(samples, in.samples, min(size, in.size) * sizeof(float));
2754 return *this;
2755 }

References samples, and size.

◆ operator=() [2/2]

signal & klang::buffer::operator= ( const signal & in)
inline

Definition at line 2738 of file klang.h.

2738 {
2739 return *ptr = in;
2740 }

References ptr.

◆ operator[]() [1/5]

signal klang::buffer::operator[] ( float offset)
inline

Definition at line 2700 of file klang.h.

2700 {
2701 return ((const buffer*)this)->operator[](offset);
2702 }
int offset() const
Definition klang.h:2681

References offset().

◆ operator[]() [2/5]

signal klang::buffer::operator[] ( float offset) const
inline

Definition at line 2704 of file klang.h.

2704 {
2705 const float f = floorf(offset);
2706 const float frac = offset - f;
2707
2708 const int i = (int)offset;
2709 const int j = (i == (size - 1)) ? 0 : (i + 1);
2710
2711 return samples[i] * (1.f - frac) + samples[j] * frac;
2712 }

References offset(), samples, and size.

◆ operator[]() [3/5]

const signal & klang::buffer::operator[] ( int index) const
inline

Definition at line 2714 of file klang.h.

2714 {
2715 return *(const signal*)&samples[index];
2716 }

References samples.

◆ operator[]() [4/5]

signal & klang::buffer::operator[] ( int offset)
inline

Definition at line 2692 of file klang.h.

2692 {
2693 return *(signal*)&samples[offset & mask]; // owned array can't write beyond allocation
2694 }

References mask, offset(), and samples.

◆ operator[]() [5/5]

signal & klang::buffer::operator[] ( unsigned int offset)
inline

Definition at line 2696 of file klang.h.

2696 {
2697 return *(signal*)&samples[offset & mask]; // owned array can't write beyond allocation
2698 }

References mask, offset(), and samples.

◆ rewind()

void klang::buffer::rewind ( int offset = 0)
inline

Definition at line 2665 of file klang.h.

2665 {
2666#ifdef _MSC_VER
2667 _controlfp_s(nullptr, _DN_FLUSH, _MCW_DN);
2668#endif
2669 ptr = (signal*)&samples[offset];
2670 end = (signal*)&samples[size];
2671 }

References end, offset(), ptr, samples, and size.

Referenced by buffer(), buffer(), buffer(), buffer(), attach(), klang::Envelope::Follower::Window< WINDOW >::mean(), klang::Stereo::buffer::rewind(), and klang::Envelope::Follower::Window< WINDOW >::rms().

◆ rms()

float klang::buffer::rms ( ) const
inline

Definition at line 2772 of file klang.h.

2772 {
2773 float rms = 0.f;
2774 for (int s = 0; s < size; s++)
2775 rms += samples[s] * samples[s];
2776 return sqrtf(rms / size);
2777 }
float rms() const
Definition klang.h:2772

References rms(), samples, and size.

Referenced by rms().

◆ set()

void klang::buffer::set ( float value = 0)
inline

Definition at line 2685 of file klang.h.

2685 {
2686 if (value == 0)
2687 clear();
2688 else for (int s = 0; s < size; s++)
2689 samples[s] = value;
2690 }
void clear()
Definition klang.h:2673

References clear(), samples, and size.

Referenced by buffer(), and buffer().

Member Data Documentation

◆ end

signal* klang::buffer::end
protected

Definition at line 2628 of file klang.h.

Referenced by attach(), finished(), and rewind().

◆ mask

const unsigned int klang::buffer::mask = 0xFFFFFFFF
protected

Definition at line 2624 of file klang.h.

Referenced by operator[](), and operator[]().

◆ owned

const bool klang::buffer::owned = false
protected

Definition at line 2625 of file klang.h.

Referenced by ~buffer().

◆ ptr

◆ samples

float* klang::buffer::samples
protected

◆ size

const int klang::buffer::size