/
LeanAudio.cs
418 lines (338 loc) · 14.7 KB
/
LeanAudio.cs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using UnityEngine;
using System.Collections.Generic;
public class LeanAudioStream {
public int position = 0;
public AudioClip audioClip;
public float[] audioArr;
public LeanAudioStream( float[] audioArr ){
this.audioArr = audioArr;
}
public void OnAudioRead(float[] data) {
int count = 0;
while (count < data.Length) {
data[count] = audioArr[this.position];
position++;
count++;
}
}
public void OnAudioSetPosition(int newPosition) {
this.position = newPosition;
}
}
/**
* Create Audio dynamically and easily playback
*
* @class LeanAudio
* @constructor
*/
public class LeanAudio : object {
public static float MIN_FREQEUNCY_PERIOD = 0.000115f;
public static int PROCESSING_ITERATIONS_MAX = 50000;
public static float[] generatedWaveDistances;
public static int generatedWaveDistancesCount = 0;
private static float[] longList;
public static LeanAudioOptions options(){
if(generatedWaveDistances==null){
generatedWaveDistances = new float[ PROCESSING_ITERATIONS_MAX ];
longList = new float[ PROCESSING_ITERATIONS_MAX ];
}
return new LeanAudioOptions();
}
public static LeanAudioStream createAudioStream( AnimationCurve volume, AnimationCurve frequency, LeanAudioOptions options = null ){
if(options==null)
options = new LeanAudioOptions();
options.useSetData = false;
int generatedWavePtsLength = createAudioWave( volume, frequency, options);
createAudioFromWave( generatedWavePtsLength, options );
return options.stream;
}
/**
* Create dynamic audio from a set of Animation Curves and other options.
*
* @method createAudio
* @param {AnimationCurve} volumeCurve:AnimationCurve describing the shape of the audios volume (from 0-1). The length of the audio is dicated by the end value here.
* @param {AnimationCurve} frequencyCurve:AnimationCurve describing the width of the oscillations between the sound waves in seconds. Large numbers mean a lower note, while higher numbers mean a tighter frequency and therefor a higher note. Values are usually between 0.01 and 0.000001 (or smaller)
* @param {LeanAudioOptions} options:LeanAudioOptions You can pass any other values in here like vibrato or the frequency you would like the sound to be encoded at. See <a href="LeanAudioOptions.html">LeanAudioOptions</a> for more details.
* @return {AudioClip} AudioClip of the procedurally generated audio
* @example
* AnimationCurve volumeCurve = new AnimationCurve( new Keyframe(0f, 1f, 0f, -1f), new Keyframe(1f, 0f, -1f, 0f));<br>
* AnimationCurve frequencyCurve = new AnimationCurve( new Keyframe(0f, 0.003f, 0f, 0f), new Keyframe(1f, 0.003f, 0f, 0f));<br>
* AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.32f,0f,0f)} ));<br>
*/
public static AudioClip createAudio( AnimationCurve volume, AnimationCurve frequency, LeanAudioOptions options = null ){
if(options==null)
options = new LeanAudioOptions();
int generatedWavePtsLength = createAudioWave( volume, frequency, options);
// Debug.Log("generatedWavePtsLength:"+generatedWavePtsLength);
return createAudioFromWave( generatedWavePtsLength, options );
}
private static int createAudioWave( AnimationCurve volume, AnimationCurve frequency, LeanAudioOptions options ){
float time = volume[ volume.length - 1 ].time;
int listLength = 0;
// List<float> list = new List<float>();
// generatedWaveDistances = new List<float>();
// float[] vibratoValues = new float[ vibrato.Length ];
float passed = 0f;
for(int i = 0; i < PROCESSING_ITERATIONS_MAX; i++){
float f = frequency.Evaluate(passed);
if(f<MIN_FREQEUNCY_PERIOD)
f = MIN_FREQEUNCY_PERIOD;
float height = volume.Evaluate(passed + 0.5f*f);
if(options.vibrato!=null){
for(int j=0; j<options.vibrato.Length; j++){
float peakMulti = Mathf.Abs( Mathf.Sin( 1.5708f + passed * (1f/options.vibrato[j][0]) * Mathf.PI ) );
float diff = (1f-options.vibrato[j][1]);
peakMulti = options.vibrato[j][1] + diff*peakMulti;
height *= peakMulti;
}
}
// Debug.Log("i:"+i+" f:"+f+" passed:"+passed+" height:"+height+" time:"+time);
if(passed + 0.5f*f>=time)
break;
if(listLength >= PROCESSING_ITERATIONS_MAX-1){
Debug.LogError("LeanAudio has reached it's processing cap. To avoid this error increase the number of iterations ex: LeanAudio.PROCESSING_ITERATIONS_MAX = "+(PROCESSING_ITERATIONS_MAX*2));
break;
}else{
int distPoint = listLength / 2;
//generatedWaveDistances.Add( f );
passed += f;
generatedWaveDistances[ distPoint ] = passed;
//Debug.Log("distPoint:"+distPoint+" passed:"+passed);
//list.Add( passed );
//list.Add( i%2==0 ? -height : height );
longList[ listLength ] = passed;
longList[ listLength + 1 ] = i%2==0 ? -height : height;
}
listLength += 2;
}
listLength += -2;
generatedWaveDistancesCount = listLength / 2;
/*float[] wave = new float[ listLength ];
for(int i = 0; i < wave.Length; i++){
wave[i] = longList[i];
}*/
return listLength;
}
private static AudioClip createAudioFromWave( int waveLength, LeanAudioOptions options ){
float time = longList[ waveLength - 2 ];
float[] audioArr = new float[ (int)(options.frequencyRate*time) ];
int waveIter = 0;
float subWaveDiff = longList[waveIter];
float subWaveTimeLast = 0f;
float subWaveTime = longList[waveIter];
float waveHeight = longList[waveIter+1];
for(int i = 0; i < audioArr.Length; i++){
float passedTime = (float)i / (float)options.frequencyRate;
if(passedTime > longList[waveIter] ){
subWaveTimeLast = longList[waveIter];
waveIter += 2;
subWaveDiff = longList[waveIter] - longList[waveIter-2];
waveHeight = longList[waveIter+1];
// Debug.Log("passed wave i:"+i);
}
subWaveTime = passedTime - subWaveTimeLast;
float ratioElapsed = subWaveTime / subWaveDiff;
float value = Mathf.Sin( ratioElapsed * Mathf.PI );
if(options.waveStyle==LeanAudioOptions.LeanAudioWaveStyle.Square){
if(value>0f)
value = 1f;
if(value<0f)
value = -1f;
}else if(options.waveStyle==LeanAudioOptions.LeanAudioWaveStyle.Sawtooth){
float sign = value > 0f ? 1f : -1f;
if(ratioElapsed<0.5f){
value = (ratioElapsed*2f)*sign;
}else{ // 0.5f - 1f
value = (1f - ratioElapsed)*2f*sign;
}
}else if(options.waveStyle==LeanAudioOptions.LeanAudioWaveStyle.Noise){
float peakMulti = (1f-options.waveNoiseInfluence) + Mathf.PerlinNoise(0f, passedTime * options.waveNoiseScale ) * options.waveNoiseInfluence;
/*if(i<25){
Debug.Log("passedTime:"+passedTime+" peakMulti:"+peakMulti+" infl:"+options.waveNoiseInfluence);
}*/
value *= peakMulti;
}
//if(i<25)
// Debug.Log("passedTime:"+passedTime+" value:"+value+" ratioElapsed:"+ratioElapsed+" subWaveTime:"+subWaveTime+" subWaveDiff:"+subWaveDiff);
value *= waveHeight;
if(options.modulation!=null){
for(int k=0; k<options.modulation.Length; k++){
float peakMulti = Mathf.Abs( Mathf.Sin( 1.5708f + passedTime * (1f/options.modulation[k][0]) * Mathf.PI ) );
float diff = (1f-options.modulation[k][1]);
peakMulti = options.modulation[k][1] + diff*peakMulti;
// if(k<10){
// Debug.Log("k:"+k+" peakMulti:"+peakMulti+" value:"+value+" after:"+(value*peakMulti));
// }
value *= peakMulti;
}
}
audioArr[i] = value;
// Debug.Log("pt:"+pt+" i:"+i+" val:"+audioArr[i]+" len:"+audioArr.Length);
}
int lengthSamples = audioArr.Length;
#if UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
bool is3dSound = false;
AudioClip audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, options.frequencyRate, is3dSound, false);
#else
AudioClip audioClip = null;
if(options.useSetData){
audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, options.frequencyRate, false, null, OnAudioSetPosition);
audioClip.SetData(audioArr, 0);
}else{
options.stream = new LeanAudioStream(audioArr);
// Debug.Log("len:"+audioArr.Length+" lengthSamples:"+lengthSamples+" freqRate:"+options.frequencyRate);
audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, options.frequencyRate, false, options.stream.OnAudioRead, options.stream.OnAudioSetPosition);
options.stream.audioClip = audioClip;
}
#endif
return audioClip;
}
private static void OnAudioSetPosition(int newPosition) {
}
public static AudioClip generateAudioFromCurve( AnimationCurve curve, int frequencyRate = 44100 ){
float curveTime = curve[ curve.length - 1 ].time;
float time = curveTime;
float[] audioArr = new float[ (int)(frequencyRate*time) ];
// Debug.Log("curveTime:"+curveTime+" AudioSettings.outputSampleRate:"+AudioSettings.outputSampleRate);
for(int i = 0; i < audioArr.Length; i++){
float pt = (float)i / (float)frequencyRate;
audioArr[i] = curve.Evaluate( pt );
// Debug.Log("pt:"+pt+" i:"+i+" val:"+audioArr[i]+" len:"+audioArr.Length);
}
int lengthSamples = audioArr.Length;//(int)( (float)frequencyRate * curveTime );
#if UNITY_3_5 || UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
bool is3dSound = false;
AudioClip audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, frequencyRate, is3dSound, false);
#else
AudioClip audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, frequencyRate, false);
#endif
audioClip.SetData(audioArr, 0);
return audioClip;
}
public static AudioSource play( AudioClip audio, float volume ){
AudioSource audioSource = playClipAt(audio, Vector3.zero);
audioSource.volume = volume;
return audioSource;
}
public static AudioSource play( AudioClip audio ){
return playClipAt( audio, Vector3.zero );
}
public static AudioSource play( AudioClip audio, Vector3 pos ){
return playClipAt( audio, pos );
}
public static AudioSource play( AudioClip audio, Vector3 pos, float volume ){
// Debug.Log("audio length:"+audio.length);
AudioSource audioSource = playClipAt(audio, pos);
audioSource.minDistance = 1f;
//audioSource.pitch = pitch;
audioSource.volume = volume;
return audioSource;
}
public static AudioSource playClipAt( AudioClip clip, Vector3 pos ) {
GameObject tempGO = new GameObject(); // create the temp object
tempGO.transform.position = pos; // set its position
AudioSource aSource = tempGO.AddComponent<AudioSource>(); // add an audio source
aSource.clip = clip; // define the clip
aSource.Play(); // start the sound
GameObject.Destroy(tempGO, clip.length); // destroy object after clip duration
return aSource; // return the AudioSource reference
}
public static void printOutAudioClip( AudioClip audioClip, ref AnimationCurve curve, float scaleX = 1f ){
// Debug.Log("Audio channels:"+audioClip.channels+" frequency:"+audioClip.frequency+" length:"+audioClip.length+" samples:"+audioClip.samples);
float[] samples = new float[audioClip.samples * audioClip.channels];
audioClip.GetData(samples, 0);
int i = 0;
Keyframe[] frames = new Keyframe[samples.Length];
while (i < samples.Length) {
frames[i] = new Keyframe( (float)i * scaleX, samples[i] );
++i;
}
curve = new AnimationCurve( frames );
}
}
/**
* Pass in options to LeanAudio
*
* @class LeanAudioOptions
* @constructor
*/
public class LeanAudioOptions : object {
public enum LeanAudioWaveStyle{
Sine,
Square,
Sawtooth,
Noise
}
public LeanAudioWaveStyle waveStyle = LeanAudioWaveStyle.Sine;
public Vector3[] vibrato;
public Vector3[] modulation;
public int frequencyRate = 44100;
public float waveNoiseScale = 1000;
public float waveNoiseInfluence = 1f;
public bool useSetData = true;
public LeanAudioStream stream;
public LeanAudioOptions(){}
/**
* Set the frequency for the audio is encoded. 44100 is CD quality, but you can usually get away with much lower (or use a lower amount to get a more 8-bit sound).
*
* @method setFrequency
* @param {int} frequencyRate:int of the frequency you wish to encode the AudioClip at
* @return {LeanAudioOptions} LeanAudioOptions describing optional values
* @example
* AnimationCurve volumeCurve = new AnimationCurve( new Keyframe(0f, 1f, 0f, -1f), new Keyframe(1f, 0f, -1f, 0f));<br>
* AnimationCurve frequencyCurve = new AnimationCurve( new Keyframe(0f, 0.003f, 0f, 0f), new Keyframe(1f, 0.003f, 0f, 0f));<br>
* AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.32f,0f,0f)} ).setFrequency(12100) );<br>
*/
public LeanAudioOptions setFrequency( int frequencyRate ){
this.frequencyRate = frequencyRate;
return this;
}
/**
* Set details about the shape of the curve by adding vibrato modulations through it (alters the peak values giving it a wah-wah effect). You can add as many as you want to sculpt out more detail in the sound wave.
*
* @method setVibrato
* @param {Vector3[]} vibratoArray:Vector3[] The first value is the period in seconds that you wish to have the vibrato wave fluctuate at. The second value is the minimum height you wish the vibrato wave to dip down to (default is zero). The third is reserved for future effects.
* @return {LeanAudioOptions} LeanAudioOptions describing optional values
* @example
* AnimationCurve volumeCurve = new AnimationCurve( new Keyframe(0f, 1f, 0f, -1f), new Keyframe(1f, 0f, -1f, 0f));<br>
* AnimationCurve frequencyCurve = new AnimationCurve( new Keyframe(0f, 0.003f, 0f, 0f), new Keyframe(1f, 0.003f, 0f, 0f));<br>
* AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.32f,0.3f,0f)} ).setFrequency(12100) );<br>
*/
public LeanAudioOptions setVibrato( Vector3[] vibrato ){
this.vibrato = vibrato;
return this;
}
/*
public LeanAudioOptions setModulation( Vector3[] modulation ){
this.modulation = modulation;
return this;
}*/
public LeanAudioOptions setWaveSine(){
this.waveStyle = LeanAudioWaveStyle.Sine;
return this;
}
public LeanAudioOptions setWaveSquare(){
this.waveStyle = LeanAudioWaveStyle.Square;
return this;
}
public LeanAudioOptions setWaveSawtooth(){
this.waveStyle = LeanAudioWaveStyle.Sawtooth;
return this;
}
public LeanAudioOptions setWaveNoise(){
this.waveStyle = LeanAudioWaveStyle.Noise;
return this;
}
public LeanAudioOptions setWaveStyle( LeanAudioWaveStyle style ){
this.waveStyle = style;
return this;
}
public LeanAudioOptions setWaveNoiseScale( float waveScale ){
this.waveNoiseScale = waveScale;
return this;
}
public LeanAudioOptions setWaveNoiseInfluence( float influence ){
this.waveNoiseInfluence = influence;
return this;
}
}