Skip to content

Instantly share code, notes, and snippets.

@tatmos
Created November 2, 2017 05:03
Show Gist options
  • Save tatmos/e6f1b410148c04e215f8aff0bd1ceb05 to your computer and use it in GitHub Desktop.
Save tatmos/e6f1b410148c04e215f8aff0bd1ceb05 to your computer and use it in GitHub Desktop.
ちょっと昔(2013年)に書いていたノリノリ勇者のSoundManager.cs 。ADX2LEとWEBPLAYERで鳴らせるようにしていた様子 音量で変形する線とか
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SoundManager : MonoBehaviour {
public Material lineMaterial;
LevelLine bus0LevelLine = new LevelLine();
#if UNITY_WEBPLAYER
AudioSource bgm;
AudioSource bgm_Beat;
AudioSource voice;
AudioSource se;
AudioSource impact;
#else
CriAtomSource bgm;
CriAtomSource bgm_Beat;
CriAtomSource voice;
CriAtomSource se;
CriAtomSource impact;
#endif
public enum SoundEventName
{
Opening,
StartBGM,
Imademasita,
KeyGet,
ItemPop,
DoorOpen,
DoorClose,
YushaDash,
TaruImpact,
TzuboImpact,
ItemDrop,
TzuboImpactKoukyu,
OusamaResult,
OpeningStory,
Select,
Norinoriyusha
}
private static SoundManager instance = null;
public static SoundManager Instance {
get { return SoundManager.instance; }
}
void Awake()
{
if( instance == null)
{
instance = this;
}else{
Destroy( this );
}
}
// Use this for initialization
void Start () {
GameObject p1 = new GameObject();
p1.transform.parent = this.transform;
bus0LevelLine.init(p1,0,Color.white,Color.white,true,lineMaterial);
AudioSetup();
}
void AudioSetup()
{
#if UNITY_WEBPLAYER
if(bgm == null)bgm = gameObject.AddComponent<AudioSource>();
//bgm.cueSheet = "CueSheet_0";
if(bgm_Beat == null)bgm_Beat= gameObject.AddComponent<AudioSource>();
//bgm_Beat.cueSheet = "CueSheet_0";
voice = gameObject.AddComponent<AudioSource>();
//voice.cueSheet = "CueSheet_0";
se = gameObject.AddComponent<AudioSource>();
//se.cueSheet = "CueSheet_0";
impact = gameObject.AddComponent<AudioSource>();
//impact.cueSheet = "CueSheet_0";
bgm.rolloffMode = AudioRolloffMode.Linear;
bgm_Beat.rolloffMode = AudioRolloffMode.Linear;
voice.rolloffMode = AudioRolloffMode.Linear;
se.rolloffMode = AudioRolloffMode.Linear;
impact.rolloffMode = AudioRolloffMode.Linear;
#else
if(bgm == null)bgm = gameObject.AddComponent<CriAtomSource>();
bgm.cueSheet = "CueSheet_0";
if(bgm_Beat == null)bgm_Beat= gameObject.AddComponent<CriAtomSource>();
bgm_Beat.cueSheet = "CueSheet_0";
voice = gameObject.AddComponent<CriAtomSource>();
voice.cueSheet = "CueSheet_0";
se = gameObject.AddComponent<CriAtomSource>();
se.cueSheet = "CueSheet_0";
impact = gameObject.AddComponent<CriAtomSource>();
impact.cueSheet = "CueSheet_0";
CriAtom.SetBusAnalyzer(true); // バス解析器を有効化
#endif
}
static int beatNo = 0; //ビート数
static int lastBeatNo = 0; //最後のビート数
static int barNo = 0; // 小節数
static int beatNo_BloackStart = 0;
public float level = 0f; //振幅レベル
float lastLevel = 0f;
static Color bgColor = Color.white;
// Update is called once per frame
void Update () {
Update_sobaMeter();
}
public float[] samples = new float[256];
void Update_sobaMeter()
{
#if UNITY_WEBPLAYER
samples = audio.GetSpectrumData(128,1,FFTWindow.Triangle);
level = samples[3];
lastLevel = level;
if(lastLevel > 0.1f){
{
barNo++;
bgColor = new Color((beatNo*0.125f)+Random.Range(0f,0.5f),(beatNo*0.125f)+Random.Range(0f,0.5f),(beatNo*0.125f)+Random.Range(0f,0.5f));
Camera.main.backgroundColor = bgColor; //カメラの背景カラー変更
}
}
#else
bus0LevelLine.Update();
// Bus
CriAtomExAsr.BusAnalyzerInfo lBusInfo = CriAtom.GetBusAnalyzerInfo(0);// Lch
level = lBusInfo.peakLevels[0];
if(level > 0.1f + lastLevel || level < lastLevel - 0.1f){
beatNo = (int)(lBusInfo.peakLevels[0]*5f);
}
lastLevel = level;
if(lastLevel > 0.5f){
{
barNo++;
bgColor = new Color((beatNo*0.125f)+Random.Range(0f,0.5f),(beatNo*0.125f)+Random.Range(0f,0.5f),(beatNo*0.125f)+Random.Range(0f,0.5f));
Camera.main.backgroundColor = bgColor; //カメラの背景カラー変更
}
}
#endif
}
//---------------イベント-------------------------------------------------------------------------
public delegate void BeatUpdate (); // ビートイベントコールバック登録用デリゲート
static public BeatUpdate evBeatUpdate; // ビートイベントコールバック登録用
static int requestCount = 0;
static public void StartSoundEvent(SoundEventName name)
{
requestCount++;
switch(name)
{
case SoundEventName.DoorClose:
SoundManager.instance.PlayDoorClose();
break;
case SoundEventName.DoorOpen:
SoundManager.instance.PlayDoorOpen();
break;
case SoundEventName.Imademasita:
SoundManager.instance.PlayImademasita();
break;
case SoundEventName.ItemPop:
SoundManager.instance.PlayItemPop();
break;
case SoundEventName.KeyGet:
SoundManager.instance.PlayKeyGet();
break;
case SoundEventName.Opening:
SoundManager.instance.PlayOpening();
break;
case SoundEventName.StartBGM:
SoundManager.instance.StartBGM();
break;
case SoundEventName.TaruImpact:
SoundManager.instance.PlayTaruImpact();
break;
case SoundEventName.TzuboImpact:
SoundManager.instance.PlayTzuboImpact();
break;
case SoundEventName.YushaDash:
SoundManager.instance.PlayYushaDash();
break;
case SoundEventName.ItemDrop:
SoundManager.instance.PlayItemDrop();
break;
case SoundEventName.TzuboImpactKoukyu:
SoundManager.instance.PlayTzuboImpactKoukyu();
break;
case SoundEventName.OusamaResult:
SoundManager.instance.PlayOusamaResult();
break;
case SoundEventName.OpeningStory:
SoundManager.instance.PlayOpeningStory();
break;
case SoundEventName.Select:
SoundManager.instance.PlaySelect();
break;
case SoundEventName.Norinoriyusha:
SoundManager.instance.PlayNorinoriyusha();
break;
}
}
Dictionary<string,AudioClip> audioClipList = new Dictionary<string, AudioClip>();
AudioClip GetAudioClipOrLoad(string path)
{
if(audioClipList.ContainsKey(path)){
return audioClipList[path];
} else
{
audioClipList.Add(path,Resources.Load(path, typeof(AudioClip)) as AudioClip);
}
return audioClipList[path];
}
void PlayOpening()
{
if(bgm == null)AudioSetup();
#if UNITY_WEBPLAYER
bgm.clip = GetAudioClipOrLoad("AudioForWeb/Materials/BGM/opening");
bgm.loop = false;
#else
bgm.cueName = "opening";
#endif
bgm.Play();
}
void StartBGM()
{
if(bgm_Beat == null)AudioSetup();
#if UNITY_WEBPLAYER
bgm_Beat.clip = GetAudioClipOrLoad("AudioForWeb/Materials/BGM/beat");
bgm_Beat.loop = true;
#else
bgm_Beat.cueName = "beat";
#endif
StartCoroutine("StartBGMEvent",1f);
}
private IEnumerator StartBGMEvent(float time)
{
yield return new WaitForSeconds(time);
#if UNITY_WEBPLAYER
bgm_Beat.clip = GetAudioClipOrLoad("AudioForWeb/Materials/BGM/beat");
#else
bgm_Beat.cueName = "beatsetup1";
bgm_Beat.Play();
bgm_Beat.cueName = "beatsetup2";
bgm_Beat.Play();
bgm_Beat.cueName = "beat";
#endif
yield return new WaitForSeconds(time);
bgm_Beat.Play();
}
void PlayImademasita()
{
#if UNITY_WEBPLAYER
voice.clip = GetAudioClipOrLoad("AudioForWeb/Materials/Voice/imademasita");
#else
voice.cueName = "imademasita";
#endif
voice.Play();
}
void PlayKeyGet()
{
#if UNITY_WEBPLAYER
se.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/KeyGet");
#else
se.cueName = "KeyGet";
#endif
se.Play();
}
void PlayItemPop()
{
#if UNITY_WEBPLAYER
se.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/ItemPop");
#else
se.cueName = "ItemPop";
#endif
se.Play();
}
void PlayDoorOpen()
{
#if UNITY_WEBPLAYER
se.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/doorOpen");
#else
se.cueName = "doorOpen";
#endif
se.Play();
}
void PlayDoorClose()
{
#if UNITY_WEBPLAYER
se.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/doorClose");
#else
se.cueName = "doorClose";
#endif
se.Play();
}
void PlayYushaDash()
{
#if UNITY_WEBPLAYER
se.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/yushaDash");
#else
se.cueName = "yushaDash";
#endif
se.Play();
}
void PlayTaruImpact()
{#if UNITY_WEBPLAYER
impact.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/taruImpact");
#else
impact.cueName = "taruImpact";
#endif
impact.Play();
}
void PlayTzuboImpact()
{#if UNITY_WEBPLAYER
impact.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/tzuboImpact");
#else
impact.cueName = "tzuboImpact";
#endif
impact.Play();
}
void PlayTzuboImpactKoukyu()
{#if UNITY_WEBPLAYER
se.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/tzuboImpact2");
#else
se.cueName = "tzuboImpactKoukyu";
#endif
se.Play();
}
void PlayItemDrop()
{#if UNITY_WEBPLAYER
impact.clip = GetAudioClipOrLoad("AudioForWeb/Materials/SE/ItemDrop");
#else
impact.cueName = "ItemDrop";
#endif
impact.Play();
}
void PlayOusamaResult()
{
bgm_Beat.Stop();
#if UNITY_WEBPLAYER
bgm.clip = GetAudioClipOrLoad("AudioForWeb/Materials/BGMResult/ousama_result");
#else
bgm.cueName = "Ousama_result";
#endif
bgm.Play();
}
void PlayOpeningStory()
{
bgm_Beat.Stop();
#if UNITY_WEBPLAYER
bgm.clip = GetAudioClipOrLoad("AudioForWeb/Materials/BGMResult/openingStory");
bgm.loop = true;
#else
bgm.cueName = "OpeningStory";
#endif
bgm.Play();
}
void PlaySelect()
{
#if UNITY_WEBPLAYER
bgm.clip = GetAudioClipOrLoad("AudioForWeb/Materials/sword/sword");
#else
se.cueName = "Select";
#endif
se.Play();
}
void PlayNorinoriyusha()
{
#if UNITY_WEBPLAYER
voice.clip = GetAudioClipOrLoad("AudioForWeb/Materials/Voice/Norinoriyusha");
#else
voice.cueName = "Norinoriyusha";
#endif
voice.Play();
}
void OnGUI()
{
/*
if(GUILayout.Button("Norinoriyusha")){
SoundManager.StartSoundEvent(SoundEventName.Norinoriyusha);
}
GUILayout.BeginHorizontal();
if(bgm_Beat != null){
GUILayout.Label("BGM_Beat "+ bgm_Beat.time.ToString());
}
GUILayout.Label("Request Count "+ requestCount.ToString());
if(GUILayout.Button("OpeningStory")){
SoundManager.StartSoundEvent(SoundEventName.OpeningStory);
}
if(GUILayout.Button("Ousama_result")){
SoundManager.StartSoundEvent(SoundEventName.OusamaResult);
}
if(GUILayout.Button("Select")){
SoundManager.StartSoundEvent(SoundEventName.Select);
}
GUILayout.EndHorizontal();
/*
if(GUILayout.Button("Opening")){
SoundManager.StartSoundEvent(SoundEventName.Opening);
}
if(GUILayout.Button("BGM")){
SoundManager.StartSoundEvent(SoundEventName.StartBGM);
}
if(GUILayout.Button("imademasita")){
SoundManager.StartSoundEvent(SoundEventName.Imademasita);
}
if(GUILayout.Button("KeyGet")){
SoundManager.StartSoundEvent(SoundEventName.KeyGet);
}
if(GUILayout.Button("ItemPop")){
SoundManager.StartSoundEvent(SoundEventName.ItemPop);
}
if(GUILayout.Button("doorOpen")){
SoundManager.StartSoundEvent(SoundEventName.DoorOpen);
}
if(GUILayout.Button("doorClose")){
SoundManager.StartSoundEvent(SoundEventName.DoorClose);
}
if(GUILayout.Button("yushaDash")){
SoundManager.StartSoundEvent(SoundEventName.YushaDash);
}
if(GUILayout.Button("taruImpact")){
SoundManager.StartSoundEvent(SoundEventName.TaruImpact);
}
if(GUILayout.Button("tzuboImpact")){
SoundManager.StartSoundEvent(SoundEventName.TzuboImpact);
}
if(GUILayout.Button("ItemDrop")){
SoundManager.StartSoundEvent(SoundEventName.ItemDrop);
}
if(GUILayout.Button("tzuboImpactKoukyu")){
SoundManager.StartSoundEvent(SoundEventName.TzuboImpactKoukyu);
}
*/
}
}
public class LevelLine// : MonoBehaviour
{
List<float> levelInfoList = new List<float>();
LineRenderer lineRenderer;
int lengthOfLineRenderer = 4;
int busNo = 0;
bool autoReduction = false;
int seek = 0;
public void init(GameObject gameObject,int busNo,Color c1,Color c2,bool autoReduction,Material lineMaterial)
{
this.autoReduction = autoReduction;
this.busNo = busNo;
lineRenderer = gameObject.AddComponent<LineRenderer>();
//lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
lineRenderer.material = new Material(lineMaterial);// (Shader.Find("Self-illumin/Additive"));
lineRenderer.SetColors(c1, c2);
lineRenderer.SetWidth(0.5f,0.5f);
lineRenderer.SetVertexCount(lengthOfLineRenderer);
lineRenderer.transform.localScale= new Vector3(0.5f,0.5f,0.5f);
for(int i = 0;i<lengthOfLineRenderer;i++){
levelInfoList.Add(0f);
}
}
public void Update()
{
#if UNITY_WEBPLAYER
// Bus
seek++;
// Lch
levelInfoList[seek%(levelInfoList.Count)] = SoundManager.Instance.samples[3];//(float)lBusInfo.peakLevels[0]*1F;
// Rch
levelInfoList[(seek+(levelInfoList.Count/2))%(levelInfoList.Count)] = SoundManager.Instance.samples[3];//(float)lBusInfo.peakLevels[1]*1F;
for(int i = 0;i<lengthOfLineRenderer;i++){
Vector3 pos = new Vector3(
((float)Screen.width/lengthOfLineRenderer*0.0005f)*(i-lengthOfLineRenderer/2f)+0.6f,// + (float)(Screen.width*1/3),
0.25f*levelInfoList[(i)%(levelInfoList.Count) ]
-0.35f ,-0.20f);//busNo);
lineRenderer.SetPosition(i, pos);
if(this.autoReduction){
levelInfoList[Mathf.Abs((i-1)%(levelInfoList.Count))] *=0.92f;
}
}
#else
// Bus
CriAtomExAsr.BusAnalyzerInfo lBusInfo = CriAtom.GetBusAnalyzerInfo(this.busNo);
seek++;
// Lch
levelInfoList[seek%(levelInfoList.Count)] = (float)lBusInfo.peakLevels[0]*1F;
// Rch
levelInfoList[(seek+(levelInfoList.Count/2))%(levelInfoList.Count)] = (float)lBusInfo.peakLevels[1]*1F;
for(int i = 0;i<lengthOfLineRenderer;i++){
Vector3 pos = new Vector3(
((float)Screen.width/lengthOfLineRenderer*0.0005f)*(i-lengthOfLineRenderer/2f)+0.6f,// + (float)(Screen.width*1/3),
0.25f*levelInfoList[(i)%(levelInfoList.Count) ]
-0.35f ,-0.20f);//busNo);
lineRenderer.SetPosition(i, pos);
if(this.autoReduction){
levelInfoList[Mathf.Abs((i-1)%(levelInfoList.Count))] *=0.92f;
}
}
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment