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 package org.openimaj.audio.generation;
31
32 import org.openimaj.audio.AudioFormat;
33 import org.openimaj.audio.SampleChunk;
34 import org.openimaj.audio.generation.Synthesizer.FMOptions;
35 import org.openimaj.audio.generation.Synthesizer.OscillatorOptions;
36 import org.openimaj.audio.samples.SampleBuffer;
37 import org.openimaj.audio.samples.SampleBufferFactory;
38
39
40
41
42
43
44
45
46 public interface Oscillator
47 {
48
49
50
51
52
53
54
55 public static class SineOscillator implements Oscillator
56 {
57 @Override
58 public SampleChunk getSampleChunk( final int length, final double time,
59 final double freq, final int gain, final AudioFormat format )
60 {
61
62 final double samplesPerWave = format.getSampleRateKHz()*1000d/freq;
63
64
65
66
67 final double p = 2*Math.PI*((freq*time)-Math.floor(freq*time));
68
69
70 final SampleBuffer sb = SampleBufferFactory.createSampleBuffer(
71 format, length );
72
73
74 final double z = 2*Math.PI/samplesPerWave;
75 for( int i = 0; i < length; i++ )
76 sb.set( i, (float)(Math.sin( i*z+p )*gain) );
77
78 return sb.getSampleChunk();
79 }
80
81 @Override
82 public OscillatorOptions getOptions()
83 {
84 return null;
85 }
86 };
87
88
89
90
91
92
93
94
95 public static class SquareOscillator implements Oscillator
96 {
97 @Override
98 public SampleChunk getSampleChunk( final int length, final double time,
99 final double freq, final int gain, final AudioFormat format )
100 {
101 final SampleBuffer sb = SampleBufferFactory.createSampleBuffer(
102 format, length );
103
104 final double samplesPerWave = format.getSampleRateKHz()*1000d/freq;
105
106
107 final int p = (int)( samplesPerWave *
108 ((freq*time)-Math.floor(freq*time)));
109
110 for( int i = 0; i < length; i++ )
111 {
112 final int x = (i+p) % (int)samplesPerWave;
113 if( x > samplesPerWave/2 )
114 sb.set( i, gain );
115 else sb.set( i, -gain );
116 }
117
118 return sb.getSampleChunk();
119 }
120
121 @Override
122 public OscillatorOptions getOptions()
123 {
124 return null;
125 }
126 };
127
128
129
130
131
132
133
134
135 public static class SawOscillator implements Oscillator
136 {
137 @Override
138 public SampleChunk getSampleChunk( final int length, final double time,
139 final double freq, final int gain, final AudioFormat format )
140 {
141 final SampleBuffer sb = SampleBufferFactory.createSampleBuffer(
142 format, length );
143
144 final double samplesPerWave = format.getSampleRateKHz()*1000d/freq;
145
146
147 final int p = (int)( samplesPerWave *
148 ((freq*time)-Math.floor(freq*time)));
149
150 for( int i = 0; i < length; i++ )
151 {
152 final int x = (i+p) % (int)samplesPerWave;
153 sb.set( i, (float)(x*(gain/samplesPerWave)) );
154 }
155
156 return sb.getSampleChunk();
157 }
158
159 @Override
160 public OscillatorOptions getOptions()
161 {
162 return null;
163 }
164 };
165
166
167
168
169
170
171
172
173 public static class NoiseOscillator implements Oscillator
174 {
175 @Override
176 public SampleChunk getSampleChunk( final int length, final double time,
177 final double freq, final int gain, final AudioFormat format )
178 {
179 final SampleBuffer sb = SampleBufferFactory.createSampleBuffer(
180 format, length );
181 for( int i = 0; i < sb.size(); i++ )
182 sb.set( i, (float)(Math.random() * Integer.MAX_VALUE) );
183 return sb.getSampleChunk();
184 }
185
186 @Override
187 public OscillatorOptions getOptions()
188 {
189 return null;
190 }
191 };
192
193
194
195
196
197
198
199
200 public static class DummyOscillator implements Oscillator
201 {
202 @Override
203 public SampleChunk getSampleChunk( final int length, final double time,
204 final double freq, final int gain, final AudioFormat format )
205 {
206 final SampleBuffer sb = SampleBufferFactory.createSampleBuffer(
207 format, length );
208 return sb.getSampleChunk();
209 }
210
211 @Override
212 public OscillatorOptions getOptions()
213 {
214 return null;
215 }
216 };
217
218
219
220
221
222
223
224
225
226
227
228 public static class FrequencyModulatedOscillator implements Oscillator
229 {
230
231 private final FMOptions options = new FMOptions();
232
233 @Override
234 public SampleChunk getSampleChunk(final int length, final double time,
235 final double freq, final int gain, final AudioFormat format)
236 {
237
238
239
240
241 final SampleBuffer sb = this.getOptions().modulator.nextSampleChunk().getSampleBuffer();
242
243
244
245 double minTime = time;
246 double maxTime = time + length;
247 final double step = format.getSampleRateKHz();
248 final double amp = this.getOptions().modulatorAmplitude;
249 for( int i = 0; i < sb.size(); i++ )
250 {
251 final float f = sb.get(i);
252 final double t = time + step*i + f*amp;
253 minTime = Math.min( t, minTime );
254 maxTime = Math.max( t, maxTime );
255 }
256
257
258 final int cl = (int)(maxTime - minTime);
259 final double ct = minTime;
260
261
262
263
264 final SampleBuffer carrierBuffer = this.getOptions().carrier.getOscillator()
265 .getSampleChunk( cl, ct, freq, gain, format ).getSampleBuffer();
266
267
268 for( int i = 0; i < sb.size(); i++ )
269 {
270
271 final float f = sb.get(i);
272 final double t = time + step*i + f*amp;
273
274
275 final int index = (int)((t - minTime)/step);
276
277 sb.set( i, carrierBuffer.get( index ) );
278
279
280
281
282
283
284
285 }
286
287 return sb.getSampleChunk();
288 }
289
290 @Override
291 public FMOptions getOptions()
292 {
293 return this.options;
294 }
295 };
296
297
298
299
300
301
302
303
304
305
306 public abstract SampleChunk getSampleChunk( int length, double time,
307 double freq, int gain, AudioFormat format );
308
309
310
311
312
313 public abstract OscillatorOptions getOptions();
314 }