读取音频文件的谱线

源码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LightController : MonoBehaviour {
	public AudioSource shenYuan;
	public Renderer light1, light2, light3, light4;
	public float dumpX=0.3f, dumpY=0.3f, dumpZ=0.3f;
	
	// 采样宽度
	public int sampWidth = 8;
	public float scaleAmount = 4;
	private float[] specTrum;
	private float startDetal;
	private List<Vector3> hights=new List<Vector3>();
	// Use this for initialization
	void Start () {
		
	}
	// Update is called once per frame
	float jiange=0f;
	void Update () {
		specTrum = audio.GetSpectrumData(sampWidth, 0, FFTWindow.BlackmanHarris);
		float totalValue = 0;
		int i = 0;
		while (i < sampWidth){
			totalValue = totalValue + specTrum[i];
			i++;
		}
		dumpZ = totalValue*scaleAmount;
		Debug.Log(dumpZ);
		startDetal+=Time.deltaTime;
		if(startDetal>0.1f){
			startDetal-=0.1f;
			jiange+=0.05f;
			hights.Add(new Vector3(jiange,dumpZ,0f));
		}
//      light1.material.SetFloat ("_DampZ", dumpZ);33we
//      light1.material.SetFloat ("_DampX", dumpX);
		//light1.material.SetFloat ("_DampY", dumpY);

//      light2.material.SetFloat ("_DampZ", dumpZ);
//      light2.material.SetFloat ("_DampX", dumpX);
		//light2.material.SetFloat ("_DampY", dumpY);
		
//      light3.material.SetFloat ("_DampZ", dumpZ);
//      light3.material.SetFloat ("_DampX", dumpX);
		//light3.material.SetFloat ("_DampY", dumpY);
		
//      light4.material.SetFloat ("_DampZ", dumpZ);
//      light4.material.SetFloat ("_DampX", dumpX);
		//light4.material.SetFloat ("_DampY", dumpY);
	}
	private void OnDrawGizmos(){
		for(int i=0;i<hights.Count;i++){
			Gizmos.DrawLine(hights[i],new Vector3(hights[i].x,-hights[i].y,0f));
		}

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值