-
Notifications
You must be signed in to change notification settings - Fork 3
/
noiseband.cpp
235 lines (171 loc) · 6.3 KB
/
noiseband.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//
// noiseband.cpp
//
// Mark Zadel, 2010
//
//
// bandlimited noise by a brute force, additive synthesis method
//
// alternatives: FFT^-1, or a bandpass filter on broadband noise
//
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// FIXME / TODO
// add -h help message
// seed random generator with srand (eg, say you want to do it twice for
// two-channel decorrelated noise)
// add option for 16 or 24-bit output instead of 64-bit floats
#include "SineWave.h"
#include "FileWrite.h"
#include <getopt.h>
#include <math.h>
#include <stdlib.h>
enum { WHITENOISE = 0, PINKNOISE };
static struct option long_options[] = {
{"length", required_argument, 0, 'l'},
{"startfreq", required_argument, 0, 's'},
{"endfreq", required_argument, 0, 'e'},
{"ppo", required_argument, 0, 'p'}, // partials per octave
{"numpartials", required_argument, 0, 'n'}, // total number of partials
{"white", no_argument, 0, 'w'},
{"pink", no_argument, 0, 'k'},
{"outfile", required_argument, 0, 'o'},
{0, 0, 0, 0}
};
void normalize( StkFrames& buffer )
{
// this code is from the STK FileWvIn class
size_t i;
StkFloat max = 0.0;
for ( i=0; i<buffer.size(); i++ ) {
if ( fabs( buffer[i] ) > max )
max = (StkFloat) fabs((double) buffer[i]);
}
std::cout << "normalize(): max amplitude found is " << max << std::endl;
if (max > 0.0) {
max = 1.0 / max;
for ( i=0; i<buffer.size(); i++ )
buffer[i] *= max;
}
}
StkFloat getlograndfreq( StkFloat lofreq, StkFloat hifreq ) {
StkFloat randval = rand() / (RAND_MAX + 1.0);
return pow(hifreq/lofreq, randval) * lofreq;
}
StkFloat getlinrandfreq( StkFloat lofreq, StkFloat hifreq ) {
StkFloat randval = rand() / (RAND_MAX + 1.0);
return randval * (hifreq-lofreq) + lofreq;
}
StkFloat numoctaves( StkFloat lofreq, StkFloat hifreq ) {
// return the number of octaves spanned by (lofreq,hifreq)
return log2( hifreq / lofreq );
}
int main( int argc, char **argv ) {
StkFloat lengthseconds = 2;
StkFloat lofreq = 10;
StkFloat hifreq = 22000;
StkFloat partialsperoctave = 1500.0;
unsigned long numpartials = 0;
unsigned int noisetype = WHITENOISE;
std::string outfilename( "outfile.wav" );
// -(option parsing)--------------------------------------------------------
int optionfound = -1;
unsigned char ppospecified = 1;
while( 1 ) {
int option_index = 0; // getopt_long stores the option index here.
optionfound = getopt_long (argc, argv, "l:s:e:p:n:wko:", long_options, &option_index);
// end of options
if (optionfound == -1)
break;
switch (optionfound) {
case 'l':
lengthseconds = atof( optarg );
break;
case 's':
lofreq = atof( optarg );
break;
case 'e':
hifreq = atof( optarg );
break;
case 'p':
partialsperoctave = atof(optarg);
ppospecified = 1;
break;
case 'n':
numpartials = atol(optarg);
ppospecified = 0;
break;
case 'w':
noisetype = WHITENOISE;
break;
case 'k':
noisetype = PINKNOISE;
break;
case 'o':
outfilename = optarg;
break;
default:
abort();
}
};
unsigned long numsamples = static_cast<unsigned long>(lengthseconds * Stk::sampleRate());
// need to do this at the end since it depends on lofreq/hifreq and can be
// overridden by --numpartials
if (ppospecified)
numpartials = static_cast<unsigned long>(partialsperoctave * numoctaves( lofreq, hifreq ));
if ( lofreq >= hifreq ) {
std::cout << "ERROR: low freq (" << lofreq
<< ") is not lower than high freq (" << hifreq
<< ")" << std::endl;
exit(0);
};
// -(main routine)----------------------------------------------------------
// final output
StkFrames output(0.0, numsamples, 1);
// sine ugen
SineWave thissine;
std::cout << "computing " << numpartials << " partials" << std::endl;
for ( unsigned long i = 0; i < numpartials; i++ ) {
// progress indicator
std::cout << "." << std::flush;
if ( i % 20 == 0 )
std::cout << " " << i << " " << std::flush;
if ( noisetype == WHITENOISE ) {
thissine.setFrequency( getlinrandfreq( lofreq, hifreq ) );
} else { // PINKNOISE
thissine.setFrequency( getlograndfreq( lofreq, hifreq ) );
}
// NB just changing the frequency in this way but using the same
// SineWave ugen effectively randomises the starting phase. we don't
// want all of the partials phase aligned at the start since that leads
// to a big amplitude spike (throwing off the normalisation step).
for ( unsigned long j = 0; j < numsamples; j++ ) {
// NB we're assuming here that we're not going to overflow the
// doubles. max I've seen is ~200.0, so we should be safe.
output[j] += thissine.tick();
}
}
std::cout << "done computing" << std::endl;
normalize( output );
std::cout << "done normalising" << std::endl;
// write to file
FileWrite outfile( outfilename, 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
outfile.write( output );
outfile.close();
std::cout << "wrote to " << outfilename << std::endl;
std::cout << "all done" << std::endl;
}
// vim:sw=4:ts=4:et:ai:ic: