CSL  6.0
Window.cpp
Go to the documentation of this file.
1 //
2 // Window.cpp -- implementation of the various function window classes
3 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 //
5 
6 #include "Window.h"
7 #include <math.h>
8 
9 using namespace csl;
10 
11 Window::Window() : UnitGenerator(), mGain(1) {
13 }
14 
15 // Gain is optional and defaults to 1 when not specified.
16 
17 Window::Window(unsigned windowSize, float gain) : UnitGenerator(), mGain(gain) {
18  this->setSize(windowSize);
19 }
20 
23 }
24 
25 void Window::setGain(float gain) {
26  mGain = gain;
27  fillWindow(); // Re-caluclate window using new gain value.
28 }
29 
30 void Window::setSize(unsigned windowSize) {
31  mWindowBufferPos = 0;
32  mWindowSize = windowSize;
33  // If previously allocated, free me first.
35  mWindowBuffer.setSize(1, mWindowSize); // Allocate memory to store the window.
37  fillWindow(); // Fill the buffer with the window data.
38 }
39 
40 void Window::nextBuffer(Buffer &outputBuffer, unsigned outBufNum) throw (CException) {
41  /// get everything into registers
42  unsigned numFrames = outputBuffer.mNumFrames;
43  sample* outputBufferPtr = outputBuffer.buffer(outBufNum);
44  sample* windowBufferPtr = mWindowBuffer.buffer(0);
45  unsigned windowBufferPos = mWindowBufferPos;
46  unsigned windowBufferSize = mWindowSize;
47 #ifdef CSL_DEBUG
48  logMsg("Window::nextBuffer");
49 #endif
50  for (unsigned i = 0; i < numFrames; i++) {
51  if (windowBufferPos > windowBufferSize)
52  windowBufferPos = 0;
53  *outputBufferPtr++ = windowBufferPtr[windowBufferPos++];
54  }
55  mWindowBufferPos = windowBufferPos;
56 }
57 
59  sample* windowBufferPtr = mWindowBuffer.buffer(0);
60  // create the window -- I make a Hamming window, subclasses may override
61  for (unsigned i = 0; i < mWindowSize; i++ )
62  *windowBufferPtr++ = (0.54 - 0.46*cos(CSL_TWOPI*i/(mWindowSize - 1) ));
63 }
64 
66  sample* windowBufferPtr = mWindowBuffer.buffer(0);
67  for (unsigned i = 0; i < mWindowSize; i++ ) // create the window
68  *windowBufferPtr++ = mGain;
69 }
70 
72  sample* windowBufferPtr = mWindowBuffer.buffer(0);
73  float accum = 0.0f;
74  unsigned winHalf = mWindowSize / 2;
75  float step = 1.0f / ((float) winHalf);
76  for (unsigned i = 0; i < winHalf; i++ ) { // create the rising half
77  *windowBufferPtr++ = accum;
78  accum += step;
79  }
80  for (unsigned i = winHalf; i < mWindowSize; i++ ) { // create the falling half
81  *windowBufferPtr++ = accum;
82  accum -= step;
83  }
84 }
85 
87  sample* windowBufferPtr = mWindowBuffer.buffer(0);
88  sample increment = CSL_TWOPI/(mWindowSize - 1);
89  for (unsigned i = 0; i < mWindowSize; i++ )
90  *windowBufferPtr++ = (0.54 - 0.46*cos(i * increment));
91 }
92 
94  sample* windowBufferPtr = mWindowBuffer.buffer(0);
95  sample increment = CSL_TWOPI/(mWindowSize - 1);
96 
97  for (unsigned i = 0; i < mWindowSize; i++ )
98  *windowBufferPtr++ = 0.5 * (1 - cos(i * increment)) * mGain;
99 }
100 
102  sample* windowBufferPtr = mWindowBuffer.buffer(0);
103  sample increment = CSL_TWOPI/(mWindowSize - 1);
104 
105  for (unsigned i = 0; i < mWindowSize; i++ )
106  *windowBufferPtr++ = (0.42 - 0.5 * cos(i * increment) + 0.08 * cos(2 * i * increment)) * mGain;
107 }
108 
110  sample* windowBufferPtr = mWindowBuffer.buffer(0);
111  sample increment = CSL_TWOPI/(mWindowSize - 1);
112 
113  for (unsigned i = 0; i < mWindowSize; i++ )
114  *windowBufferPtr++ = (0.35875 - 0.48829 * cos(i * increment)
115  + 0.14128 * cos(2 * i * increment)
116  - 0.01168 * cos(3 * i * increment)) * mGain;
117 }
118 
120  sample* windowBufferPtr = mWindowBuffer.buffer(0);
121  sample increment = 2.f/(mWindowSize - 1);
122  sample phase = -1.f;
123 
124  for (unsigned i = 0; i < mWindowSize; i++ ) { // create the window
125  *windowBufferPtr++ = (1.f - phase * phase) * mGain;
126  phase += increment;
127  }
128 }
129 
130 void Window::dump() {
131  logMsg("Window of size: %d samples", mWindowSize);
132 }
void logMsg(const char *format,...)
These are the public logging messages.
Definition: CGestalt.cpp:292
void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:101
virtual void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:58
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
virtual SampleBuffer buffer(unsigned bufNum)
convenience accessors for sample buffers
Definition: CSL_Core.cpp:66
float mGain
gain for the window
Definition: Window.h:48
void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:86
void setGain(float gain)
Set the gain to which the window should be normalized.
Definition: Window.cpp:25
void freeBuffers()
fcn to free them
Definition: CSL_Core.cpp:141
void setSize(unsigned numChannels, unsigned numFrames)
Definition: CSL_Core.cpp:75
Buffer mWindowBuffer
used to store the window
Definition: Window.h:45
void dump()
Print some info about the window.
Definition: Window.cpp:130
unsigned mWindowBufferPos
where am I in the window buffer
Definition: Window.h:46
static unsigned blockSize()
the default block size
Definition: CGestalt.cpp:57
unsigned mWindowSize
length in samples of the window
Definition: Window.h:47
float sample
(could be changed to int, or double)
Definition: CSL_Types.h:191
void setSize(unsigned windowSize)
Set the number of samples the window spans.
Definition: Window.cpp:30
void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:119
#define CSL_TWOPI
Definition: CSL_Types.h:336
void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:65
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:93
void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:71
void allocateBuffers()
fcn to malloc storage buffers
Definition: CSL_Core.cpp:122
forward declaration
Definition: CSL_Core.h:241
~Window()
clean-up . . . free the allocated buffer that held the window data.
Definition: Window.cpp:21
void fillWindow()
subclasses override this to fill the buffer with corresponding function.
Definition: Window.cpp:109
void nextBuffer(Buffer &outputBuffer, unsigned outBufNum)
Returns a pointer to the window data.
Definition: Window.cpp:40
Base class of CSL exceptions (written upper-case). Has a string message.
Window()
Creates a window using the default Gestalt size and a gain of 1; < Creates a window (hann) with the s...
Definition: Window.cpp:11