You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

418 lines
15 KiB

7 years ago
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class LeanAudioStream {
  4. public int position = 0;
  5. public AudioClip audioClip;
  6. public float[] audioArr;
  7. public LeanAudioStream( float[] audioArr ){
  8. this.audioArr = audioArr;
  9. }
  10. public void OnAudioRead(float[] data) {
  11. int count = 0;
  12. while (count < data.Length) {
  13. data[count] = audioArr[this.position];
  14. position++;
  15. count++;
  16. }
  17. }
  18. public void OnAudioSetPosition(int newPosition) {
  19. this.position = newPosition;
  20. }
  21. }
  22. /**
  23. * Create Audio dynamically and easily playback
  24. *
  25. * @class LeanAudio
  26. * @constructor
  27. */
  28. public class LeanAudio : object {
  29. public static float MIN_FREQEUNCY_PERIOD = 0.000115f;
  30. public static int PROCESSING_ITERATIONS_MAX = 50000;
  31. public static float[] generatedWaveDistances;
  32. public static int generatedWaveDistancesCount = 0;
  33. private static float[] longList;
  34. public static LeanAudioOptions options(){
  35. if(generatedWaveDistances==null){
  36. generatedWaveDistances = new float[ PROCESSING_ITERATIONS_MAX ];
  37. longList = new float[ PROCESSING_ITERATIONS_MAX ];
  38. }
  39. return new LeanAudioOptions();
  40. }
  41. public static LeanAudioStream createAudioStream( AnimationCurve volume, AnimationCurve frequency, LeanAudioOptions options = null ){
  42. if(options==null)
  43. options = new LeanAudioOptions();
  44. options.useSetData = false;
  45. int generatedWavePtsLength = createAudioWave( volume, frequency, options);
  46. createAudioFromWave( generatedWavePtsLength, options );
  47. return options.stream;
  48. }
  49. /**
  50. * Create dynamic audio from a set of Animation Curves and other options.
  51. *
  52. * @method createAudio
  53. * @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.
  54. * @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)
  55. * @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.
  56. * @return {AudioClip} AudioClip of the procedurally generated audio
  57. * @example
  58. * AnimationCurve volumeCurve = new AnimationCurve( new Keyframe(0f, 1f, 0f, -1f), new Keyframe(1f, 0f, -1f, 0f));<br>
  59. * AnimationCurve frequencyCurve = new AnimationCurve( new Keyframe(0f, 0.003f, 0f, 0f), new Keyframe(1f, 0.003f, 0f, 0f));<br>
  60. * AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.32f,0f,0f)} ));<br>
  61. */
  62. public static AudioClip createAudio( AnimationCurve volume, AnimationCurve frequency, LeanAudioOptions options = null ){
  63. if(options==null)
  64. options = new LeanAudioOptions();
  65. int generatedWavePtsLength = createAudioWave( volume, frequency, options);
  66. // Debug.Log("generatedWavePtsLength:"+generatedWavePtsLength);
  67. return createAudioFromWave( generatedWavePtsLength, options );
  68. }
  69. private static int createAudioWave( AnimationCurve volume, AnimationCurve frequency, LeanAudioOptions options ){
  70. float time = volume[ volume.length - 1 ].time;
  71. int listLength = 0;
  72. // List<float> list = new List<float>();
  73. // generatedWaveDistances = new List<float>();
  74. // float[] vibratoValues = new float[ vibrato.Length ];
  75. float passed = 0f;
  76. for(int i = 0; i < PROCESSING_ITERATIONS_MAX; i++){
  77. float f = frequency.Evaluate(passed);
  78. if(f<MIN_FREQEUNCY_PERIOD)
  79. f = MIN_FREQEUNCY_PERIOD;
  80. float height = volume.Evaluate(passed + 0.5f*f);
  81. if(options.vibrato!=null){
  82. for(int j=0; j<options.vibrato.Length; j++){
  83. float peakMulti = Mathf.Abs( Mathf.Sin( 1.5708f + passed * (1f/options.vibrato[j][0]) * Mathf.PI ) );
  84. float diff = (1f-options.vibrato[j][1]);
  85. peakMulti = options.vibrato[j][1] + diff*peakMulti;
  86. height *= peakMulti;
  87. }
  88. }
  89. // Debug.Log("i:"+i+" f:"+f+" passed:"+passed+" height:"+height+" time:"+time);
  90. if(passed + 0.5f*f>=time)
  91. break;
  92. if(listLength >= PROCESSING_ITERATIONS_MAX-1){
  93. 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));
  94. break;
  95. }else{
  96. int distPoint = listLength / 2;
  97. //generatedWaveDistances.Add( f );
  98. passed += f;
  99. generatedWaveDistances[ distPoint ] = passed;
  100. //Debug.Log("distPoint:"+distPoint+" passed:"+passed);
  101. //list.Add( passed );
  102. //list.Add( i%2==0 ? -height : height );
  103. longList[ listLength ] = passed;
  104. longList[ listLength + 1 ] = i%2==0 ? -height : height;
  105. }
  106. listLength += 2;
  107. }
  108. listLength += -2;
  109. generatedWaveDistancesCount = listLength / 2;
  110. /*float[] wave = new float[ listLength ];
  111. for(int i = 0; i < wave.Length; i++){
  112. wave[i] = longList[i];
  113. }*/
  114. return listLength;
  115. }
  116. private static AudioClip createAudioFromWave( int waveLength, LeanAudioOptions options ){
  117. float time = longList[ waveLength - 2 ];
  118. float[] audioArr = new float[ (int)(options.frequencyRate*time) ];
  119. int waveIter = 0;
  120. float subWaveDiff = longList[waveIter];
  121. float subWaveTimeLast = 0f;
  122. float subWaveTime = longList[waveIter];
  123. float waveHeight = longList[waveIter+1];
  124. for(int i = 0; i < audioArr.Length; i++){
  125. float passedTime = (float)i / (float)options.frequencyRate;
  126. if(passedTime > longList[waveIter] ){
  127. subWaveTimeLast = longList[waveIter];
  128. waveIter += 2;
  129. subWaveDiff = longList[waveIter] - longList[waveIter-2];
  130. waveHeight = longList[waveIter+1];
  131. // Debug.Log("passed wave i:"+i);
  132. }
  133. subWaveTime = passedTime - subWaveTimeLast;
  134. float ratioElapsed = subWaveTime / subWaveDiff;
  135. float value = Mathf.Sin( ratioElapsed * Mathf.PI );
  136. if(options.waveStyle==LeanAudioOptions.LeanAudioWaveStyle.Square){
  137. if(value>0f)
  138. value = 1f;
  139. if(value<0f)
  140. value = -1f;
  141. }else if(options.waveStyle==LeanAudioOptions.LeanAudioWaveStyle.Sawtooth){
  142. float sign = value > 0f ? 1f : -1f;
  143. if(ratioElapsed<0.5f){
  144. value = (ratioElapsed*2f)*sign;
  145. }else{ // 0.5f - 1f
  146. value = (1f - ratioElapsed)*2f*sign;
  147. }
  148. }else if(options.waveStyle==LeanAudioOptions.LeanAudioWaveStyle.Noise){
  149. float peakMulti = (1f-options.waveNoiseInfluence) + Mathf.PerlinNoise(0f, passedTime * options.waveNoiseScale ) * options.waveNoiseInfluence;
  150. /*if(i<25){
  151. Debug.Log("passedTime:"+passedTime+" peakMulti:"+peakMulti+" infl:"+options.waveNoiseInfluence);
  152. }*/
  153. value *= peakMulti;
  154. }
  155. //if(i<25)
  156. // Debug.Log("passedTime:"+passedTime+" value:"+value+" ratioElapsed:"+ratioElapsed+" subWaveTime:"+subWaveTime+" subWaveDiff:"+subWaveDiff);
  157. value *= waveHeight;
  158. if(options.modulation!=null){
  159. for(int k=0; k<options.modulation.Length; k++){
  160. float peakMulti = Mathf.Abs( Mathf.Sin( 1.5708f + passedTime * (1f/options.modulation[k][0]) * Mathf.PI ) );
  161. float diff = (1f-options.modulation[k][1]);
  162. peakMulti = options.modulation[k][1] + diff*peakMulti;
  163. // if(k<10){
  164. // Debug.Log("k:"+k+" peakMulti:"+peakMulti+" value:"+value+" after:"+(value*peakMulti));
  165. // }
  166. value *= peakMulti;
  167. }
  168. }
  169. audioArr[i] = value;
  170. // Debug.Log("pt:"+pt+" i:"+i+" val:"+audioArr[i]+" len:"+audioArr.Length);
  171. }
  172. int lengthSamples = audioArr.Length;
  173. #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
  174. bool is3dSound = false;
  175. AudioClip audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, options.frequencyRate, is3dSound, false);
  176. #else
  177. AudioClip audioClip = null;
  178. if(options.useSetData){
  179. audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, options.frequencyRate, false, null, OnAudioSetPosition);
  180. audioClip.SetData(audioArr, 0);
  181. }else{
  182. options.stream = new LeanAudioStream(audioArr);
  183. // Debug.Log("len:"+audioArr.Length+" lengthSamples:"+lengthSamples+" freqRate:"+options.frequencyRate);
  184. audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, options.frequencyRate, false, options.stream.OnAudioRead, options.stream.OnAudioSetPosition);
  185. options.stream.audioClip = audioClip;
  186. }
  187. #endif
  188. return audioClip;
  189. }
  190. private static void OnAudioSetPosition(int newPosition) {
  191. }
  192. public static AudioClip generateAudioFromCurve( AnimationCurve curve, int frequencyRate = 44100 ){
  193. float curveTime = curve[ curve.length - 1 ].time;
  194. float time = curveTime;
  195. float[] audioArr = new float[ (int)(frequencyRate*time) ];
  196. // Debug.Log("curveTime:"+curveTime+" AudioSettings.outputSampleRate:"+AudioSettings.outputSampleRate);
  197. for(int i = 0; i < audioArr.Length; i++){
  198. float pt = (float)i / (float)frequencyRate;
  199. audioArr[i] = curve.Evaluate( pt );
  200. // Debug.Log("pt:"+pt+" i:"+i+" val:"+audioArr[i]+" len:"+audioArr.Length);
  201. }
  202. int lengthSamples = audioArr.Length;//(int)( (float)frequencyRate * curveTime );
  203. #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
  204. bool is3dSound = false;
  205. AudioClip audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, frequencyRate, is3dSound, false);
  206. #else
  207. AudioClip audioClip = AudioClip.Create("Generated Audio", lengthSamples, 1, frequencyRate, false);
  208. #endif
  209. audioClip.SetData(audioArr, 0);
  210. return audioClip;
  211. }
  212. public static AudioSource play( AudioClip audio, float volume ){
  213. AudioSource audioSource = playClipAt(audio, Vector3.zero);
  214. audioSource.volume = volume;
  215. return audioSource;
  216. }
  217. public static AudioSource play( AudioClip audio ){
  218. return playClipAt( audio, Vector3.zero );
  219. }
  220. public static AudioSource play( AudioClip audio, Vector3 pos ){
  221. return playClipAt( audio, pos );
  222. }
  223. public static AudioSource play( AudioClip audio, Vector3 pos, float volume ){
  224. // Debug.Log("audio length:"+audio.length);
  225. AudioSource audioSource = playClipAt(audio, pos);
  226. audioSource.minDistance = 1f;
  227. //audioSource.pitch = pitch;
  228. audioSource.volume = volume;
  229. return audioSource;
  230. }
  231. public static AudioSource playClipAt( AudioClip clip, Vector3 pos ) {
  232. GameObject tempGO = new GameObject(); // create the temp object
  233. tempGO.transform.position = pos; // set its position
  234. AudioSource aSource = tempGO.AddComponent<AudioSource>(); // add an audio source
  235. aSource.clip = clip; // define the clip
  236. aSource.Play(); // start the sound
  237. GameObject.Destroy(tempGO, clip.length); // destroy object after clip duration
  238. return aSource; // return the AudioSource reference
  239. }
  240. public static void printOutAudioClip( AudioClip audioClip, ref AnimationCurve curve, float scaleX = 1f ){
  241. // Debug.Log("Audio channels:"+audioClip.channels+" frequency:"+audioClip.frequency+" length:"+audioClip.length+" samples:"+audioClip.samples);
  242. float[] samples = new float[audioClip.samples * audioClip.channels];
  243. audioClip.GetData(samples, 0);
  244. int i = 0;
  245. Keyframe[] frames = new Keyframe[samples.Length];
  246. while (i < samples.Length) {
  247. frames[i] = new Keyframe( (float)i * scaleX, samples[i] );
  248. ++i;
  249. }
  250. curve = new AnimationCurve( frames );
  251. }
  252. }
  253. /**
  254. * Pass in options to LeanAudio
  255. *
  256. * @class LeanAudioOptions
  257. * @constructor
  258. */
  259. public class LeanAudioOptions : object {
  260. public enum LeanAudioWaveStyle{
  261. Sine,
  262. Square,
  263. Sawtooth,
  264. Noise
  265. }
  266. public LeanAudioWaveStyle waveStyle = LeanAudioWaveStyle.Sine;
  267. public Vector3[] vibrato;
  268. public Vector3[] modulation;
  269. public int frequencyRate = 44100;
  270. public float waveNoiseScale = 1000;
  271. public float waveNoiseInfluence = 1f;
  272. public bool useSetData = true;
  273. public LeanAudioStream stream;
  274. public LeanAudioOptions(){}
  275. /**
  276. * 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).
  277. *
  278. * @method setFrequency
  279. * @param {int} frequencyRate:int of the frequency you wish to encode the AudioClip at
  280. * @return {LeanAudioOptions} LeanAudioOptions describing optional values
  281. * @example
  282. * AnimationCurve volumeCurve = new AnimationCurve( new Keyframe(0f, 1f, 0f, -1f), new Keyframe(1f, 0f, -1f, 0f));<br>
  283. * AnimationCurve frequencyCurve = new AnimationCurve( new Keyframe(0f, 0.003f, 0f, 0f), new Keyframe(1f, 0.003f, 0f, 0f));<br>
  284. * AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.32f,0f,0f)} ).setFrequency(12100) );<br>
  285. */
  286. public LeanAudioOptions setFrequency( int frequencyRate ){
  287. this.frequencyRate = frequencyRate;
  288. return this;
  289. }
  290. /**
  291. * 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.
  292. *
  293. * @method setVibrato
  294. * @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.
  295. * @return {LeanAudioOptions} LeanAudioOptions describing optional values
  296. * @example
  297. * AnimationCurve volumeCurve = new AnimationCurve( new Keyframe(0f, 1f, 0f, -1f), new Keyframe(1f, 0f, -1f, 0f));<br>
  298. * AnimationCurve frequencyCurve = new AnimationCurve( new Keyframe(0f, 0.003f, 0f, 0f), new Keyframe(1f, 0.003f, 0f, 0f));<br>
  299. * AudioClip audioClip = LeanAudio.createAudio(volumeCurve, frequencyCurve, LeanAudio.options().setVibrato( new Vector3[]{ new Vector3(0.32f,0.3f,0f)} ).setFrequency(12100) );<br>
  300. */
  301. public LeanAudioOptions setVibrato( Vector3[] vibrato ){
  302. this.vibrato = vibrato;
  303. return this;
  304. }
  305. /*
  306. public LeanAudioOptions setModulation( Vector3[] modulation ){
  307. this.modulation = modulation;
  308. return this;
  309. }*/
  310. public LeanAudioOptions setWaveSine(){
  311. this.waveStyle = LeanAudioWaveStyle.Sine;
  312. return this;
  313. }
  314. public LeanAudioOptions setWaveSquare(){
  315. this.waveStyle = LeanAudioWaveStyle.Square;
  316. return this;
  317. }
  318. public LeanAudioOptions setWaveSawtooth(){
  319. this.waveStyle = LeanAudioWaveStyle.Sawtooth;
  320. return this;
  321. }
  322. public LeanAudioOptions setWaveNoise(){
  323. this.waveStyle = LeanAudioWaveStyle.Noise;
  324. return this;
  325. }
  326. public LeanAudioOptions setWaveStyle( LeanAudioWaveStyle style ){
  327. this.waveStyle = style;
  328. return this;
  329. }
  330. public LeanAudioOptions setWaveNoiseScale( float waveScale ){
  331. this.waveNoiseScale = waveScale;
  332. return this;
  333. }
  334. public LeanAudioOptions setWaveNoiseInfluence( float influence ){
  335. this.waveNoiseInfluence = influence;
  336. return this;
  337. }
  338. }