YSE sound engine  1.0
cross platform sound engine
 All Classes Namespaces Functions Pages
sample.hpp
1 /*
2  ==============================================================================
3 
4  sample.h
5  Created: 28 Jan 2014 2:25:46pm
6  Author: yvan
7 
8  ==============================================================================
9 */
10 
11 #ifndef SAMPLE_H_INCLUDED
12 #define SAMPLE_H_INCLUDED
13 
14 #include <vector>
15 #include "../headers/constants.hpp"
16 
17 namespace YSE {
18 
19 
20  namespace DSP {
21  /*
22  - This class serves as a sound buffer. It can be used for low level
23  audio operations where you need access to every frame in the buffer.
24  - Don't create samples as local variables because they have to
25  allocate dynamic memory. This is too costly to do during callback
26  functions.
27  - Don't use sample outside DSP routines if you can avoid it.
28  - Only the length functions are threadsafe.
29  */
30  class API sample {
31  public:
32  // Creates an audio buffer with standard size of 512
33  sample(UInt length = STANDARD_BUFFERSIZE);
34  // Creates a new audio buffer by copying an existing one
35  sample(const AUDIOBUFFER & cp);
36 
37  // gets the length of a sample in frames (also called 'samples' like in '44100 samples per second')
38  UInt getLength() const;
39  // gets the length of a sample in milliseconds
40  UInt getLengthMS() const;
41  // gets the length of a sample in seconds
42  Flt getLengthSec() const;
43 
44  // WARNING: try to avoid this function. It will give you write access
45  // to the internal buffer, but there might be unexpected consequenses
46  Flt * getBuffer();
47 
48  // Add the same value (f) to all samples in the buffer
49  AUDIOBUFFER & operator+=(Flt f);
50  // Distract the same value (f) from all samples in the buffer
51  AUDIOBUFFER & operator-=(Flt f);
52  // Multiply all samples by f
53  AUDIOBUFFER & operator*=(Flt f);
54  // Divide all samples by f
55  AUDIOBUFFER & operator/=(Flt f);
56 
57  AUDIOBUFFER & operator+=(const AUDIOBUFFER & s);
58  AUDIOBUFFER & operator-=(const AUDIOBUFFER & s);
59  AUDIOBUFFER & operator*=(const AUDIOBUFFER & s);
60  AUDIOBUFFER & operator/=(const AUDIOBUFFER & s);
61 
62  AUDIOBUFFER & operator=(const AUDIOBUFFER & s);
63  AUDIOBUFFER & operator=(Flt f);
64  AUDIOBUFFER & copyFrom(const AUDIOBUFFER & s, UInt SourcePos, UInt DestPos, UInt length);
65 
77  sample& drawLine(UInt start, UInt stop, Flt startValue, Flt stopValue);
78 
89  sample& drawLine(UInt start, UInt stop, Flt value); // horizontal line
90 
91  // get the last sample of the buffer
92  Flt getBack();
93 
94  // Each buffer holds an internal cursor. You can use this to remember a
95  // buffer position.
96  Flt * cursor;
97 
98  // resize a sample buffer, copy current contents if needed
99  // copy = true will retain the current values and fill remaining values with zeroes
100  // with copy = false, the buffer values are not initialized
101  AUDIOBUFFER & resize(UInt length, Bool copy = false);
102  private:
103  std::vector<Flt> buffer;
104  };
105  }
106 }
107 
108 
109 
110 #endif // SAMPLE_H_INCLUDED