Klang C++
Language Reference (draft)
Loading...
Searching...
No Matches
klang::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, float initial=0)
 
virtual ~buffer ()
 
void rewind (int offset=0)
 
void clear ()
 
void clear (int size)
 
int offset () const
 
void set (float value=0)
 
signaloperator[] (int offset)
 
signal operator[] (float offset)
 
signal operator[] (float offset) const
 
const signaloperator[] (int index) const
 
 operator signal & ()
 
 operator const signal & () const
 
 operator double () const
 
bool finished () const
 
signaloperator++ (int)
 
signaloperator= (const signal &in)
 
signaloperator+= (const signal &in)
 
signaloperator*= (const signal &in)
 
bufferoperator= (const buffer &in)
 
bufferoperator<< (const signal &in)
 
const float * data () const
 

Public Attributes

const int size
 

Protected Attributes

const unsigned int mask = 0xFFFFFFFF
 
const bool owned = false
 
float * samples
 
signalptr
 
signalend
 

Detailed Description

Definition at line 1188 of file klang.h.

Member Typedef Documentation

◆ signal

Definition at line 1208 of file klang.h.

Constructor & Destructor Documentation

◆ buffer() [1/3]

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

Definition at line 1211 of file klang.h.

1212 : owned(false), samples(buffer), size(size) {
1213 rewind();
1214 }
const bool owned
Definition klang.h:1203
buffer(float *buffer, int size)
Definition klang.h:1211
float * samples
Definition klang.h:1204
const int size
Definition klang.h:1209
void rewind(int offset=0)
Definition klang.h:1233

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

◆ buffer() [2/3]

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

Definition at line 1216 of file klang.h.

1217 : owned(false), samples(buffer), size(size) {
1218 rewind();
1219 set(initial);
1220 }
void set(float value=0)
Definition klang.h:1253

References owned, rewind(), samples, set(), and size.

◆ buffer() [3/3]

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

Definition at line 1222 of file klang.h.

1223 : mask(capacity(size) - 1), owned(true), samples(new float[capacity(size)]), size(size) {
1224 rewind();
1225 set(initial);
1226 }
constexpr unsigned int capacity(unsigned int n)
Definition klang.h:1190
const unsigned int mask
Definition klang.h:1202

References capacity(), mask, owned, rewind(), samples, set(), and size.

Referenced by klang::Debug::Buffer::Buffer(), klang::Wavetable::Wavetable(), and klang::Wavetable::Wavetable().

◆ ~buffer()

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

Definition at line 1228 of file klang.h.

1228 {
1229 if (owned)
1230 delete[] samples;
1231 }

References owned, and samples.

Member Function Documentation

◆ capacity()

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

Definition at line 1190 of file klang.h.

1190 {
1191 // Decrement n to handle the case where n is already a power of 2
1192 n--;
1193 n |= n >> 1;
1194 n |= n >> 2;
1195 n |= n >> 4;
1196 n |= n >> 8;
1197 n |= n >> 16;
1198 // Increment n to get the next power of 2
1199 n++;
1200 return n;
1201 }

Referenced by buffer().

◆ clear() [1/2]

void klang::buffer::clear ( )
inline

Definition at line 1241 of file klang.h.

1241 {
1242 memset(samples, 0, sizeof(float) * size);
1243 }

References samples, and size.

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

◆ clear() [2/2]

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

Definition at line 1245 of file klang.h.

1245 {
1246 memset(samples, 0, sizeof(float) * (size < buffer::size ? size : buffer::size));
1247 }

References samples, and size.

Referenced by klang::Stereo::buffer::clear().

◆ data()

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

Definition at line 1327 of file klang.h.

1327{ return samples; }

References samples.

Referenced by klang::Debug::Buffer::get().

◆ finished()

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

Definition at line 1294 of file klang.h.

1294 {
1295 return ptr == end;
1296 }
signal * ptr
Definition klang.h:1205
signal * end
Definition klang.h:1206

References end, and ptr.

Referenced by klang::Stereo::buffer::finished(), klang::Effect::process(), and klang::Note::process().

◆ offset()

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

Definition at line 1249 of file klang.h.

1249 {
1250 return int((signal*)&samples[0] - ptr);
1251 }
signal signal
Definition klang.h:1208

References ptr, and samples.

◆ operator const signal &()

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

Definition at line 1286 of file klang.h.

1286 {
1287 return *ptr;
1288 }

References ptr.

◆ operator double()

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

Definition at line 1290 of file klang.h.

1290 {
1291 return *ptr;
1292 }

References ptr.

◆ operator signal &()

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

Definition at line 1282 of file klang.h.

1282 {
1283 return *ptr;
1284 }

References ptr.

◆ operator*=()

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

Definition at line 1311 of file klang.h.

1311 {
1312 *ptr *= in;
1313 return *this;
1314 }

References klang::signal::operator*=(), and ptr.

Referenced by klang::Stereo::buffer::operator*=(), and klang::Stereo::buffer::operator*=().

◆ operator++()

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

Definition at line 1298 of file klang.h.

1298 {
1299 return *ptr++;
1300 }

References ptr.

Referenced by klang::Stereo::buffer::operator++(), klang::Effect::process(), klang::Note::process(), and klang::Stereo::Effect::process().

◆ operator+=()

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

Definition at line 1306 of file klang.h.

1306 {
1307 *ptr += in;
1308 return *this;
1309 }

References klang::signal::operator+=(), and ptr.

Referenced by klang::Debug::Buffer::operator+=(), klang::Stereo::buffer::operator+=(), klang::Stereo::buffer::operator+=(), and klang::Stereo::buffer::operator+=().

◆ operator<<()

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

Definition at line 1322 of file klang.h.

1322 {
1323 *ptr = in;
1324 return *this;
1325 }

References ptr.

◆ operator=() [1/2]

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

Definition at line 1316 of file klang.h.

1316 {
1317 //assert(size == in.size);
1318 memcpy(samples, in.samples, size * sizeof(float));
1319 return *this;
1320 }

References samples, and size.

Referenced by klang::Stereo::buffer::operator=().

◆ operator=() [2/2]

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

Definition at line 1302 of file klang.h.

1302 {
1303 return *ptr = in;
1304 }

References ptr.

Referenced by klang::Stereo::buffer::operator=(), klang::Stereo::buffer::operator=(), and klang::Stereo::buffer::operator=().

◆ operator[]() [1/4]

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

Definition at line 1264 of file klang.h.

1264 {
1265 return ((const buffer*)this)->operator[](offset);
1266 }
int offset() const
Definition klang.h:1249

References operator[]().

Referenced by klang::Wavetable::process().

◆ operator[]() [2/4]

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

Definition at line 1268 of file klang.h.

1268 {
1269 const float f = floorf(offset);
1270 const float frac = offset - f;
1271
1272 const int i = (int)offset;
1273 const int j = (i == (size - 1)) ? 0 : (i + 1);
1274
1275 return samples[i] * (1.f - frac) + samples[j] * frac;
1276 }

References samples, and size.

Referenced by operator[]().

◆ operator[]() [3/4]

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

Definition at line 1278 of file klang.h.

1278 {
1279 return *(const signal*)&samples[index];
1280 }

References samples.

Referenced by klang::Stereo::buffer::operator[]().

◆ operator[]() [4/4]

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

Definition at line 1260 of file klang.h.

1260 {
1261 return *(signal*)&samples[offset & mask]; // owned array can't write beyond allocation
1262 }

References mask, and samples.

Referenced by klang::Wavetable::operator=(), klang::Stereo::buffer::operator[](), klang::Stereo::buffer::operator[](), and klang::Wavetable::operator[]().

◆ rewind()

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

Definition at line 1233 of file klang.h.

1233 {
1234#ifdef _MSC_VER
1235 _controlfp_s(nullptr, _DN_FLUSH, _MCW_DN);
1236#endif
1237 ptr = (signal*)&samples[offset];
1238 end = (signal*)&samples[size];
1239 }

References end, ptr, samples, and size.

Referenced by buffer(), buffer(), buffer(), and klang::Stereo::buffer::rewind().

◆ set()

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

Definition at line 1253 of file klang.h.

1253 {
1254 if (value == 0)
1255 clear();
1256 else for (int s = 0; s < size; s++)
1257 samples[s] = value;
1258 }
void clear()
Definition klang.h:1241

References clear(), samples, and size.

Referenced by buffer(), and buffer().

Member Data Documentation

◆ end

signal* klang::buffer::end
protected

Definition at line 1206 of file klang.h.

Referenced by finished(), and rewind().

◆ mask

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

Definition at line 1202 of file klang.h.

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

◆ owned

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

Definition at line 1203 of file klang.h.

Referenced by buffer(), buffer(), buffer(), and ~buffer().

◆ ptr

◆ samples

float* klang::buffer::samples
protected

◆ size

const int klang::buffer::size

Definition at line 1209 of file klang.h.

Referenced by buffer(), buffer(), buffer(), clear(), clear(), operator=(), operator[](), rewind(), and set().