beeps 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.doc/ext/beeps/beeps.cpp +46 -0
  3. data/.doc/ext/beeps/file_in.cpp +59 -0
  4. data/.doc/ext/beeps/native.cpp +41 -0
  5. data/.doc/ext/beeps/processor.cpp +49 -0
  6. data/.doc/ext/beeps/sawtooth_wave.cpp +66 -0
  7. data/.doc/ext/beeps/sine_wave.cpp +66 -0
  8. data/.doc/ext/beeps/sound.cpp +69 -0
  9. data/.doc/ext/beeps/square_wave.cpp +66 -0
  10. data/README.md +4 -0
  11. data/Rakefile +27 -0
  12. data/VERSION +1 -0
  13. data/beeps.gemspec +43 -0
  14. data/ext/beeps/beeps.cpp +48 -0
  15. data/ext/beeps/defs.h +13 -0
  16. data/ext/beeps/extconf.rb +25 -0
  17. data/ext/beeps/file_in.cpp +61 -0
  18. data/ext/beeps/native.cpp +41 -0
  19. data/ext/beeps/processor.cpp +50 -0
  20. data/ext/beeps/sawtooth_wave.cpp +69 -0
  21. data/ext/beeps/sine_wave.cpp +69 -0
  22. data/ext/beeps/sound.cpp +72 -0
  23. data/ext/beeps/square_wave.cpp +69 -0
  24. data/include/beeps.h +12 -0
  25. data/include/beeps/beeps.h +25 -0
  26. data/include/beeps/defs.h +23 -0
  27. data/include/beeps/exception.h +47 -0
  28. data/include/beeps/openal.h +34 -0
  29. data/include/beeps/processor.h +132 -0
  30. data/include/beeps/ruby.h +10 -0
  31. data/include/beeps/ruby/beeps.h +21 -0
  32. data/include/beeps/ruby/processor.h +86 -0
  33. data/include/beeps/ruby/sound.h +42 -0
  34. data/include/beeps/signals.h +53 -0
  35. data/include/beeps/sound.h +44 -0
  36. data/lib/beeps.rb +9 -0
  37. data/lib/beeps/autoinit.rb +10 -0
  38. data/lib/beeps/beeps.rb +49 -0
  39. data/lib/beeps/ext.rb +4 -0
  40. data/lib/beeps/module.rb +49 -0
  41. data/lib/beeps/processor.rb +60 -0
  42. data/lib/beeps/sound.rb +19 -0
  43. data/src/beeps.cpp +93 -0
  44. data/src/exception.cpp +43 -0
  45. data/src/openal.cpp +216 -0
  46. data/src/openal.h +25 -0
  47. data/src/processor.cpp +201 -0
  48. data/src/signals.cpp +90 -0
  49. data/src/sound.cpp +125 -0
  50. data/src/stk/include/Blit.h +151 -0
  51. data/src/stk/include/BlitSaw.h +148 -0
  52. data/src/stk/include/BlitSquare.h +170 -0
  53. data/src/stk/include/FileRead.h +141 -0
  54. data/src/stk/include/FileWvIn.h +195 -0
  55. data/src/stk/include/Generator.h +50 -0
  56. data/src/stk/include/SineWave.h +159 -0
  57. data/src/stk/include/Stk.h +589 -0
  58. data/src/stk/include/WvIn.h +46 -0
  59. data/src/stk/src/Blit.cpp +78 -0
  60. data/src/stk/src/BlitSaw.cpp +91 -0
  61. data/src/stk/src/BlitSquare.cpp +95 -0
  62. data/src/stk/src/FileRead.cpp +903 -0
  63. data/src/stk/src/FileWvIn.cpp +260 -0
  64. data/src/stk/src/SineWave.cpp +78 -0
  65. data/src/stk/src/Stk.cpp +395 -0
  66. data/test/helper.rb +17 -0
  67. data/test/test_beeps.rb +18 -0
  68. data/test/test_sound.rb +26 -0
  69. metadata +177 -0
@@ -0,0 +1,50 @@
1
+ #ifndef STK_GENERATOR_H
2
+ #define STK_GENERATOR_H
3
+
4
+ #include "Stk.h"
5
+
6
+ namespace stk {
7
+
8
+ /***************************************************/
9
+ /*! \class Generator
10
+ \brief STK abstract unit generator parent class.
11
+
12
+ This class provides limited common functionality for STK unit
13
+ generator sample-source subclasses. It is general enough to
14
+ support both monophonic and polyphonic output classes.
15
+
16
+ by Perry R. Cook and Gary P. Scavone, 1995--2014.
17
+ */
18
+ /***************************************************/
19
+
20
+ class Generator : public Stk
21
+ {
22
+ public:
23
+
24
+ //! Class constructor.
25
+ Generator( void ) { lastFrame_.resize( 1, 1, 0.0 ); };
26
+
27
+ //! Return the number of output channels for the class.
28
+ unsigned int channelsOut( void ) const { return lastFrame_.channels(); };
29
+
30
+ //! Return an StkFrames reference to the last output sample frame.
31
+ const StkFrames& lastFrame( void ) const { return lastFrame_; };
32
+
33
+ //! Fill the StkFrames object with computed sample frames, starting at the specified channel.
34
+ /*!
35
+ The \c channel argument plus the number of output channels must
36
+ be less than the number of channels in the StkFrames argument (the
37
+ first channel is specified by 0). However, range checking is only
38
+ performed if _STK_DEBUG_ is defined during compilation, in which
39
+ case an out-of-range value will trigger an StkError exception.
40
+ */
41
+ virtual StkFrames& tick( StkFrames& frames, unsigned int channel = 0 ) = 0;
42
+
43
+ protected:
44
+
45
+ StkFrames lastFrame_;
46
+ };
47
+
48
+ } // stk namespace
49
+
50
+ #endif
@@ -0,0 +1,159 @@
1
+ #ifndef STK_SINEWAVE_H
2
+ #define STK_SINEWAVE_H
3
+
4
+ const unsigned long TABLE_SIZE = 2048;
5
+
6
+ #include "Generator.h"
7
+
8
+ namespace stk {
9
+
10
+ /***************************************************/
11
+ /*! \class SineWave
12
+ \brief STK sinusoid oscillator class.
13
+
14
+ This class computes and saves a static sine "table" that can be
15
+ shared by multiple instances. It has an interface similar to the
16
+ WaveLoop class but inherits from the Generator class. Output
17
+ values are computed using linear interpolation.
18
+
19
+ The "table" length, set in SineWave.h, is 2048 samples by default.
20
+
21
+ by Perry R. Cook and Gary P. Scavone, 1995--2014.
22
+ */
23
+ /***************************************************/
24
+
25
+ class SineWave : public Generator
26
+ {
27
+ public:
28
+ //! Default constructor.
29
+ SineWave( void );
30
+
31
+ //! Class destructor.
32
+ ~SineWave( void );
33
+
34
+ //! Clear output and reset time pointer to zero.
35
+ void reset( void );
36
+
37
+ //! Set the data read rate in samples. The rate can be negative.
38
+ /*!
39
+ If the rate value is negative, the data is read in reverse order.
40
+ */
41
+ void setRate( StkFloat rate ) { rate_ = rate; };
42
+
43
+ //! Set the data interpolation rate based on a looping frequency.
44
+ /*!
45
+ This function determines the interpolation rate based on the file
46
+ size and the current Stk::sampleRate. The \e frequency value
47
+ corresponds to file cycles per second. The frequency can be
48
+ negative, in which case the loop is read in reverse order.
49
+ */
50
+ void setFrequency( StkFloat frequency );
51
+
52
+ //! Increment the read pointer by \e time in samples, modulo the table size.
53
+ void addTime( StkFloat time );
54
+
55
+ //! Increment the read pointer by a normalized \e phase value.
56
+ /*!
57
+ This function increments the read pointer by a normalized phase
58
+ value, such that \e phase = 1.0 corresponds to a 360 degree phase
59
+ shift. Positive or negative values are possible.
60
+ */
61
+ void addPhase( StkFloat phase );
62
+
63
+ //! Add a normalized phase offset to the read pointer.
64
+ /*!
65
+ A \e phaseOffset = 1.0 corresponds to a 360 degree phase
66
+ offset. Positive or negative values are possible.
67
+ */
68
+ void addPhaseOffset( StkFloat phaseOffset );
69
+
70
+ //! Return the last computed output value.
71
+ StkFloat lastOut( void ) const { return lastFrame_[0]; };
72
+
73
+ //! Compute and return one output sample.
74
+ StkFloat tick( void );
75
+
76
+ //! Fill a channel of the StkFrames object with computed outputs.
77
+ /*!
78
+ The \c channel argument must be less than the number of
79
+ channels in the StkFrames argument (the first channel is specified
80
+ by 0). However, range checking is only performed if _STK_DEBUG_
81
+ is defined during compilation, in which case an out-of-range value
82
+ will trigger an StkError exception.
83
+ */
84
+ StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
85
+
86
+ protected:
87
+
88
+ void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
89
+
90
+ static StkFrames table_;
91
+ StkFloat time_;
92
+ StkFloat rate_;
93
+ StkFloat phaseOffset_;
94
+ unsigned int iIndex_;
95
+ StkFloat alpha_;
96
+
97
+ };
98
+
99
+ inline StkFloat SineWave :: tick( void )
100
+ {
101
+ // Check limits of time address ... if necessary, recalculate modulo
102
+ // TABLE_SIZE.
103
+ while ( time_ < 0.0 )
104
+ time_ += TABLE_SIZE;
105
+ while ( time_ >= TABLE_SIZE )
106
+ time_ -= TABLE_SIZE;
107
+
108
+ iIndex_ = (unsigned int) time_;
109
+ alpha_ = time_ - iIndex_;
110
+ StkFloat tmp = table_[ iIndex_ ];
111
+ tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) );
112
+
113
+ // Increment time, which can be negative.
114
+ time_ += rate_;
115
+
116
+ lastFrame_[0] = tmp;
117
+ return lastFrame_[0];
118
+ }
119
+
120
+ inline StkFrames& SineWave :: tick( StkFrames& frames, unsigned int channel )
121
+ {
122
+ #if defined(_STK_DEBUG_)
123
+ if ( channel >= frames.channels() ) {
124
+ oStream_ << "SineWave::tick(): channel and StkFrames arguments are incompatible!";
125
+ handleError( StkError::FUNCTION_ARGUMENT );
126
+ }
127
+ #endif
128
+
129
+ StkFloat *samples = &frames[channel];
130
+ StkFloat tmp = 0.0;
131
+
132
+ unsigned int hop = frames.channels();
133
+ for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
134
+
135
+ // Check limits of time address ... if necessary, recalculate modulo
136
+ // TABLE_SIZE.
137
+ while ( time_ < 0.0 )
138
+ time_ += TABLE_SIZE;
139
+ while ( time_ >= TABLE_SIZE )
140
+ time_ -= TABLE_SIZE;
141
+
142
+ iIndex_ = (unsigned int) time_;
143
+ alpha_ = time_ - iIndex_;
144
+ tmp = table_[ iIndex_ ];
145
+ tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) );
146
+ *samples = tmp;
147
+
148
+ // Increment time, which can be negative.
149
+ time_ += rate_;
150
+ }
151
+
152
+ lastFrame_[0] = tmp;
153
+ return frames;
154
+ }
155
+
156
+ } // stk namespace
157
+
158
+ #endif
159
+
@@ -0,0 +1,589 @@
1
+ #ifndef STK_STK_H
2
+ #define STK_STK_H
3
+
4
+ #include <string>
5
+ #include <cstring>
6
+ #include <iostream>
7
+ #include <sstream>
8
+ #include <vector>
9
+ //#include <cstdlib>
10
+
11
+ /*! \namespace stk
12
+ \brief The STK namespace.
13
+
14
+ Most Stk classes are defined within the STK namespace. Exceptions
15
+ to this include the classes RtAudio and RtMidi.
16
+ */
17
+ namespace stk {
18
+
19
+ /***************************************************/
20
+ /*! \class Stk
21
+ \brief STK base class
22
+
23
+ Nearly all STK classes inherit from this class.
24
+ The global sample rate and rawwave path variables
25
+ can be queried and modified via Stk. In addition,
26
+ this class provides error handling and
27
+ byte-swapping functions.
28
+
29
+ The Synthesis ToolKit in C++ (STK) is a set of open source audio
30
+ signal processing and algorithmic synthesis classes written in the
31
+ C++ programming language. STK was designed to facilitate rapid
32
+ development of music synthesis and audio processing software, with
33
+ an emphasis on cross-platform functionality, realtime control,
34
+ ease of use, and educational example code. STK currently runs
35
+ with realtime support (audio and MIDI) on Linux, Macintosh OS X,
36
+ and Windows computer platforms. Generic, non-realtime support has
37
+ been tested under NeXTStep, Sun, and other platforms and should
38
+ work with any standard C++ compiler.
39
+
40
+ STK WWW site: http://ccrma.stanford.edu/software/stk/
41
+
42
+ The Synthesis ToolKit in C++ (STK)
43
+ Copyright (c) 1995--2014 Perry R. Cook and Gary P. Scavone
44
+
45
+ Permission is hereby granted, free of charge, to any person
46
+ obtaining a copy of this software and associated documentation files
47
+ (the "Software"), to deal in the Software without restriction,
48
+ including without limitation the rights to use, copy, modify, merge,
49
+ publish, distribute, sublicense, and/or sell copies of the Software,
50
+ and to permit persons to whom the Software is furnished to do so,
51
+ subject to the following conditions:
52
+
53
+ The above copyright notice and this permission notice shall be
54
+ included in all copies or substantial portions of the Software.
55
+
56
+ Any person wishing to distribute modifications to the Software is
57
+ asked to send the modifications to the original developer so that
58
+ they can be incorporated into the canonical version. This is,
59
+ however, not a binding provision of this license.
60
+
61
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
62
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
63
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
64
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
65
+ ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
66
+ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
67
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68
+ */
69
+ /***************************************************/
70
+
71
+ //#define _STK_DEBUG_
72
+
73
+ // Most data in STK is passed and calculated with the
74
+ // following user-definable floating-point type. You
75
+ // can change this to "float" if you prefer or perhaps
76
+ // a "long double" in the future.
77
+ typedef double StkFloat;
78
+
79
+ //! STK error handling class.
80
+ /*!
81
+ This is a fairly abstract exception handling class. There could
82
+ be sub-classes to take care of more specific error conditions ... or
83
+ not.
84
+ */
85
+ class StkError
86
+ {
87
+ public:
88
+ enum Type {
89
+ STATUS,
90
+ WARNING,
91
+ DEBUG_PRINT,
92
+ MEMORY_ALLOCATION,
93
+ MEMORY_ACCESS,
94
+ FUNCTION_ARGUMENT,
95
+ FILE_NOT_FOUND,
96
+ FILE_UNKNOWN_FORMAT,
97
+ FILE_ERROR,
98
+ PROCESS_THREAD,
99
+ PROCESS_SOCKET,
100
+ PROCESS_SOCKET_IPADDR,
101
+ AUDIO_SYSTEM,
102
+ MIDI_SYSTEM,
103
+ UNSPECIFIED
104
+ };
105
+
106
+ protected:
107
+ std::string message_;
108
+ Type type_;
109
+
110
+ public:
111
+ //! The constructor.
112
+ StkError(const std::string& message, Type type = StkError::UNSPECIFIED)
113
+ : message_(message), type_(type) {}
114
+
115
+ //! The destructor.
116
+ virtual ~StkError(void) {};
117
+
118
+ //! Prints thrown error message to stderr.
119
+ virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; }
120
+
121
+ //! Returns the thrown error message type.
122
+ virtual const Type& getType(void) { return type_; }
123
+
124
+ //! Returns the thrown error message string.
125
+ virtual const std::string& getMessage(void) { return message_; }
126
+
127
+ //! Returns the thrown error message as a C string.
128
+ virtual const char *getMessageCString(void) { return message_.c_str(); }
129
+ };
130
+
131
+
132
+ class Stk
133
+ {
134
+ public:
135
+
136
+ typedef unsigned long StkFormat;
137
+ static const StkFormat STK_SINT8; /*!< -128 to +127 */
138
+ static const StkFormat STK_SINT16; /*!< -32768 to +32767 */
139
+ static const StkFormat STK_SINT24; /*!< Lower 3 bytes of 32-bit signed integer. */
140
+ static const StkFormat STK_SINT32; /*!< -2147483648 to +2147483647. */
141
+ static const StkFormat STK_FLOAT32; /*!< Normalized between plus/minus 1.0. */
142
+ static const StkFormat STK_FLOAT64; /*!< Normalized between plus/minus 1.0. */
143
+
144
+ //! Static method that returns the current STK sample rate.
145
+ static StkFloat sampleRate( void ) { return srate_; }
146
+
147
+ //! Static method that sets the STK sample rate.
148
+ /*!
149
+ The sample rate set using this method is queried by all STK
150
+ classes that depend on its value. It is initialized to the
151
+ default SRATE set in Stk.h. Many STK classes use the sample rate
152
+ during instantiation. Therefore, if you wish to use a rate that
153
+ is different from the default rate, it is imperative that it be
154
+ set \e BEFORE STK objects are instantiated. A few classes that
155
+ make use of the global STK sample rate are automatically notified
156
+ when the rate changes so that internal class data can be
157
+ appropriately updated. However, this has not been fully
158
+ implemented. Specifically, classes that appropriately update
159
+ their own data when either a setFrequency() or noteOn() function
160
+ is called do not currently receive the automatic notification of
161
+ rate change. If the user wants a specific class instance to
162
+ ignore such notifications, perhaps in a multi-rate context, the
163
+ function Stk::ignoreSampleRateChange() should be called.
164
+ */
165
+ static void setSampleRate( StkFloat rate );
166
+
167
+ //! A function to enable/disable the automatic updating of class data when the STK sample rate changes.
168
+ /*!
169
+ This function allows the user to enable or disable class data
170
+ updates in response to global sample rate changes on a class by
171
+ class basis.
172
+ */
173
+ void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
174
+
175
+ //! Static method that returns the current rawwave path.
176
+ static std::string rawwavePath(void) { return rawwavepath_; }
177
+
178
+ //! Static method that sets the STK rawwave path.
179
+ static void setRawwavePath( std::string path );
180
+
181
+ //! Static method that byte-swaps a 16-bit data type.
182
+ static void swap16( unsigned char *ptr );
183
+
184
+ //! Static method that byte-swaps a 32-bit data type.
185
+ static void swap32( unsigned char *ptr );
186
+
187
+ //! Static method that byte-swaps a 64-bit data type.
188
+ static void swap64( unsigned char *ptr );
189
+
190
+ //! Static cross-platform method to sleep for a number of milliseconds.
191
+ static void sleep( unsigned long milliseconds );
192
+
193
+ //! Static method to check whether a value is within a specified range.
194
+ static bool inRange( StkFloat value, StkFloat min, StkFloat max ) {
195
+ if ( value < min ) return false;
196
+ else if ( value > max ) return false;
197
+ else return true;
198
+ }
199
+
200
+ //! Static function for error reporting and handling using c-strings.
201
+ static void handleError( const char *message, StkError::Type type );
202
+
203
+ //! Static function for error reporting and handling using c++ strings.
204
+ static void handleError( std::string message, StkError::Type type );
205
+
206
+ //! Toggle display of WARNING and STATUS messages.
207
+ static void showWarnings( bool status ) { showWarnings_ = status; }
208
+
209
+ //! Toggle display of error messages before throwing exceptions.
210
+ static void printErrors( bool status ) { printErrors_ = status; }
211
+
212
+ private:
213
+ static StkFloat srate_;
214
+ static std::string rawwavepath_;
215
+ static bool showWarnings_;
216
+ static bool printErrors_;
217
+ static std::vector<Stk *> alertList_;
218
+
219
+ protected:
220
+
221
+ static std::ostringstream oStream_;
222
+ bool ignoreSampleRateChange_;
223
+
224
+ //! Default constructor.
225
+ Stk( void );
226
+
227
+ //! Class destructor.
228
+ virtual ~Stk( void );
229
+
230
+ //! This function should be implemented in subclasses that depend on the sample rate.
231
+ virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
232
+
233
+ //! Add class pointer to list for sample rate change notification.
234
+ void addSampleRateAlert( Stk *ptr );
235
+
236
+ //! Remove class pointer from list for sample rate change notification.
237
+ void removeSampleRateAlert( Stk *ptr );
238
+
239
+ //! Internal function for error reporting that assumes message in \c oStream_ variable.
240
+ void handleError( StkError::Type type ) const;
241
+ };
242
+
243
+
244
+ /***************************************************/
245
+ /*! \class StkFrames
246
+ \brief An STK class to handle vectorized audio data.
247
+
248
+ This class can hold single- or multi-channel audio data. The data
249
+ type is always StkFloat and the channel format is always
250
+ interleaved. In an effort to maintain efficiency, no
251
+ out-of-bounds checks are performed in this class unless
252
+ _STK_DEBUG_ is defined.
253
+
254
+ Internally, the data is stored in a one-dimensional C array. An
255
+ indexing operator is available to set and retrieve data values.
256
+ Alternately, one can use pointers to access the data, using the
257
+ index operator to get an address for a particular location in the
258
+ data:
259
+
260
+ StkFloat* ptr = &myStkFrames[0];
261
+
262
+ Note that this class can also be used as a table with interpolating
263
+ lookup.
264
+
265
+ Possible future improvements in this class could include functions
266
+ to convert to and return other data types.
267
+
268
+ by Perry R. Cook and Gary P. Scavone, 1995--2014.
269
+ */
270
+ /***************************************************/
271
+
272
+ class StkFrames
273
+ {
274
+ public:
275
+
276
+ //! The default constructor initializes the frame data structure to size zero.
277
+ StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0 );
278
+
279
+ //! Overloaded constructor that initializes the frame data to the specified size with \c value.
280
+ StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels );
281
+
282
+ //! The destructor.
283
+ ~StkFrames();
284
+
285
+ // A copy constructor.
286
+ StkFrames( const StkFrames& f );
287
+
288
+ // Assignment operator that returns a reference to self.
289
+ StkFrames& operator= ( const StkFrames& f );
290
+
291
+ //! Subscript operator that returns a reference to element \c n of self.
292
+ /*!
293
+ The result can be used as an lvalue. This reference is valid
294
+ until the resize function is called or the array is destroyed. The
295
+ index \c n must be between 0 and size less one. No range checking
296
+ is performed unless _STK_DEBUG_ is defined.
297
+ */
298
+ StkFloat& operator[] ( size_t n );
299
+
300
+ //! Subscript operator that returns the value at element \c n of self.
301
+ /*!
302
+ The index \c n must be between 0 and size less one. No range
303
+ checking is performed unless _STK_DEBUG_ is defined.
304
+ */
305
+ StkFloat operator[] ( size_t n ) const;
306
+
307
+ //! Sum operator
308
+ /*!
309
+ The dimensions of the argument are expected to be the same as
310
+ self. No range checking is performed unless _STK_DEBUG_ is
311
+ defined.
312
+ */
313
+ StkFrames operator+(const StkFrames &frames) const;
314
+
315
+ //! Assignment by sum operator into self.
316
+ /*!
317
+ The dimensions of the argument are expected to be the same as
318
+ self. No range checking is performed unless _STK_DEBUG_ is
319
+ defined.
320
+ */
321
+ void operator+= ( StkFrames& f );
322
+
323
+ //! Assignment by product operator into self.
324
+ /*!
325
+ The dimensions of the argument are expected to be the same as
326
+ self. No range checking is performed unless _STK_DEBUG_ is
327
+ defined.
328
+ */
329
+ void operator*= ( StkFrames& f );
330
+
331
+ //! Channel / frame subscript operator that returns a reference.
332
+ /*!
333
+ The result can be used as an lvalue. This reference is valid
334
+ until the resize function is called or the array is destroyed. The
335
+ \c frame index must be between 0 and frames() - 1. The \c channel
336
+ index must be between 0 and channels() - 1. No range checking is
337
+ performed unless _STK_DEBUG_ is defined.
338
+ */
339
+ StkFloat& operator() ( size_t frame, unsigned int channel );
340
+
341
+ //! Channel / frame subscript operator that returns a value.
342
+ /*!
343
+ The \c frame index must be between 0 and frames() - 1. The \c
344
+ channel index must be between 0 and channels() - 1. No range checking
345
+ is performed unless _STK_DEBUG_ is defined.
346
+ */
347
+ StkFloat operator() ( size_t frame, unsigned int channel ) const;
348
+
349
+ //! Return an interpolated value at the fractional frame index and channel.
350
+ /*!
351
+ This function performs linear interpolation. The \c frame
352
+ index must be between 0.0 and frames() - 1. The \c channel index
353
+ must be between 0 and channels() - 1. No range checking is
354
+ performed unless _STK_DEBUG_ is defined.
355
+ */
356
+ StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
357
+
358
+ //! Returns the total number of audio samples represented by the object.
359
+ size_t size() const { return size_; };
360
+
361
+ //! Returns \e true if the object size is zero and \e false otherwise.
362
+ bool empty() const;
363
+
364
+ //! Resize self to represent the specified number of channels and frames.
365
+ /*!
366
+ Changes the size of self based on the number of frames and
367
+ channels. No element assignment is performed. No memory
368
+ deallocation occurs if the new size is smaller than the previous
369
+ size. Further, no new memory is allocated when the new size is
370
+ smaller or equal to a previously allocated size.
371
+ */
372
+ void resize( size_t nFrames, unsigned int nChannels = 1 );
373
+
374
+ //! Resize self to represent the specified number of channels and frames and perform element initialization.
375
+ /*!
376
+ Changes the size of self based on the number of frames and
377
+ channels, and assigns \c value to every element. No memory
378
+ deallocation occurs if the new size is smaller than the previous
379
+ size. Further, no new memory is allocated when the new size is
380
+ smaller or equal to a previously allocated size.
381
+ */
382
+ void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
383
+
384
+ //! Retrieves a single channel
385
+ /*!
386
+ Copies the specified \c channel into \c destinationFrames's \c destinationChannel. \c destinationChannel must be between 0 and destination.channels() - 1 and
387
+ \c channel must be between 0 and channels() - 1. destination.frames() must be >= frames().
388
+ No range checking is performed unless _STK_DEBUG_ is defined.
389
+ */
390
+ StkFrames& getChannel(unsigned int channel,StkFrames& destinationFrames, unsigned int destinationChannel) const;
391
+
392
+ //! Sets a single channel
393
+ /*!
394
+ Copies the \c sourceChannel of \c sourceFrames into the \c channel of self.
395
+ SourceFrames.frames() must be equal to frames().
396
+ No range checking is performed unless _STK_DEBUG_ is defined.
397
+ */
398
+ void setChannel(unsigned int channel,const StkFrames &sourceFrames,unsigned int sourceChannel);
399
+
400
+ //! Return the number of channels represented by the data.
401
+ unsigned int channels( void ) const { return nChannels_; };
402
+
403
+ //! Return the number of sample frames represented by the data.
404
+ unsigned int frames( void ) const { return (unsigned int)nFrames_; };
405
+
406
+ //! Set the sample rate associated with the StkFrames data.
407
+ /*!
408
+ By default, this value is set equal to the current STK sample
409
+ rate at the time of instantiation.
410
+ */
411
+ void setDataRate( StkFloat rate ) { dataRate_ = rate; };
412
+
413
+ //! Return the sample rate associated with the StkFrames data.
414
+ /*!
415
+ By default, this value is set equal to the current STK sample
416
+ rate at the time of instantiation.
417
+ */
418
+ StkFloat dataRate( void ) const { return dataRate_; };
419
+
420
+ private:
421
+
422
+ StkFloat *data_;
423
+ StkFloat dataRate_;
424
+ size_t nFrames_;
425
+ unsigned int nChannels_;
426
+ size_t size_;
427
+ size_t bufferSize_;
428
+
429
+ };
430
+
431
+ inline bool StkFrames :: empty() const
432
+ {
433
+ if ( size_ > 0 ) return false;
434
+ else return true;
435
+ }
436
+
437
+ inline StkFloat& StkFrames :: operator[] ( size_t n )
438
+ {
439
+ #if defined(_STK_DEBUG_)
440
+ if ( n >= size_ ) {
441
+ std::ostringstream error;
442
+ error << "StkFrames::operator[]: invalid index (" << n << ") value!";
443
+ Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
444
+ }
445
+ #endif
446
+
447
+ return data_[n];
448
+ }
449
+
450
+ inline StkFloat StkFrames :: operator[] ( size_t n ) const
451
+ {
452
+ #if defined(_STK_DEBUG_)
453
+ if ( n >= size_ ) {
454
+ std::ostringstream error;
455
+ error << "StkFrames::operator[]: invalid index (" << n << ") value!";
456
+ Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
457
+ }
458
+ #endif
459
+
460
+ return data_[n];
461
+ }
462
+
463
+ inline StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel )
464
+ {
465
+ #if defined(_STK_DEBUG_)
466
+ if ( frame >= nFrames_ || channel >= nChannels_ ) {
467
+ std::ostringstream error;
468
+ error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
469
+ Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
470
+ }
471
+ #endif
472
+
473
+ return data_[ frame * nChannels_ + channel ];
474
+ }
475
+
476
+ inline StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const
477
+ {
478
+ #if defined(_STK_DEBUG_)
479
+ if ( frame >= nFrames_ || channel >= nChannels_ ) {
480
+ std::ostringstream error;
481
+ error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
482
+ Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
483
+ }
484
+ #endif
485
+
486
+ return data_[ frame * nChannels_ + channel ];
487
+ }
488
+
489
+ inline StkFrames StkFrames::operator+(const StkFrames &f) const
490
+ {
491
+ #if defined(_STK_DEBUG_)
492
+ if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
493
+ std::ostringstream error;
494
+ error << "StkFrames::operator+: frames argument must be of equal dimensions!";
495
+ Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
496
+ }
497
+ #endif
498
+ StkFrames sum((unsigned int)nFrames_,nChannels_);
499
+ StkFloat *sumPtr = &sum[0];
500
+ const StkFloat *fptr = f.data_;
501
+ const StkFloat *dPtr = data_;
502
+ for (unsigned int i = 0; i < size_; i++) {
503
+ *sumPtr++ = *fptr++ + *dPtr++;
504
+ }
505
+ return sum;
506
+ }
507
+
508
+ inline void StkFrames :: operator+= ( StkFrames& f )
509
+ {
510
+ #if defined(_STK_DEBUG_)
511
+ if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
512
+ std::ostringstream error;
513
+ error << "StkFrames::operator+=: frames argument must be of equal dimensions!";
514
+ Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
515
+ }
516
+ #endif
517
+
518
+ StkFloat *fptr = &f[0];
519
+ StkFloat *dptr = data_;
520
+ for ( unsigned int i=0; i<size_; i++ )
521
+ *dptr++ += *fptr++;
522
+ }
523
+
524
+ inline void StkFrames :: operator*= ( StkFrames& f )
525
+ {
526
+ #if defined(_STK_DEBUG_)
527
+ if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
528
+ std::ostringstream error;
529
+ error << "StkFrames::operator*=: frames argument must be of equal dimensions!";
530
+ Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
531
+ }
532
+ #endif
533
+
534
+ StkFloat *fptr = &f[0];
535
+ StkFloat *dptr = data_;
536
+ for ( unsigned int i=0; i<size_; i++ )
537
+ *dptr++ *= *fptr++;
538
+ }
539
+
540
+ // Here are a few other useful typedefs.
541
+ typedef unsigned short UINT16;
542
+ typedef unsigned int UINT32;
543
+ typedef signed short SINT16;
544
+ typedef signed int SINT32;
545
+ typedef float FLOAT32;
546
+ typedef double FLOAT64;
547
+
548
+ // The default sampling rate.
549
+ const StkFloat SRATE = 44100.0;
550
+
551
+ // The default real-time audio input and output buffer size. If
552
+ // clicks are occuring in the input and/or output sound stream, a
553
+ // larger buffer size may help. Larger buffer sizes, however, produce
554
+ // more latency.
555
+ const unsigned int RT_BUFFER_SIZE = 512;
556
+
557
+ // The default rawwave path value is set with the preprocessor
558
+ // definition RAWWAVE_PATH. This can be specified as an argument to
559
+ // the configure script, in an integrated development environment, or
560
+ // below. The global STK rawwave path variable can be dynamically set
561
+ // with the Stk::setRawwavePath() function. This value is
562
+ // concatenated to the beginning of all references to rawwave files in
563
+ // the various STK core classes (ex. Clarinet.cpp). If you wish to
564
+ // move the rawwaves directory to a different location in your file
565
+ // system, you will need to set this path definition appropriately.
566
+ #if !defined(RAWWAVE_PATH)
567
+ #define RAWWAVE_PATH "../../rawwaves/"
568
+ #endif
569
+
570
+ const StkFloat PI = 3.14159265358979;
571
+ const StkFloat TWO_PI = 2 * PI;
572
+ const StkFloat ONE_OVER_128 = 0.0078125;
573
+
574
+ #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
575
+ #define __OS_WINDOWS__
576
+ #define __STK_REALTIME__
577
+ #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
578
+ #define __OS_LINUX__
579
+ #define __STK_REALTIME__
580
+ #elif defined(__IRIX_AL__)
581
+ #define __OS_IRIX__
582
+ #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
583
+ #define __OS_MACOSX__
584
+ #define __STK_REALTIME__
585
+ #endif
586
+
587
+ } // stk namespace
588
+
589
+ #endif