修复一些内容
This commit is contained in:
8
Convention/[Runtime]/Audio.meta
Normal file
8
Convention/[Runtime]/Audio.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e45c91269dcbefe429bcfe99acfc7fc8
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
182
Convention/[Runtime]/Audio/SpectrogramRenderer - 副本.xcsx
Normal file
182
Convention/[Runtime]/Audio/SpectrogramRenderer - 副本.xcsx
Normal file
@@ -0,0 +1,182 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using Unity.Collections;
|
||||
|
||||
namespace Convention
|
||||
{
|
||||
/// <summary>
|
||||
/// 频谱图渲染器 - 横轴时间,纵轴频率,颜色深度表示声音强度
|
||||
/// </summary>
|
||||
public class SpectrogramRenderer : MonoBehaviour
|
||||
{
|
||||
[Header("Audio Source")]
|
||||
public AudioClip audioClip;
|
||||
public int fftSize = 256; // FFT窗口大小,必须是2的幂
|
||||
public int timeResolution = 10; // 时间分辨率,每秒分析多少次
|
||||
|
||||
[Header("Render Texture")]
|
||||
public RenderTexture spectrogramTexture;
|
||||
public RenderTextureFormat textureFormat = RenderTextureFormat.ARGB32;
|
||||
public int textureWidth = 512; // 时间轴分辨率
|
||||
public int textureHeight = 256; // 频率轴分辨率
|
||||
|
||||
[Header("Display Settings")]
|
||||
public float intensityScale = 100f;
|
||||
public Color lowIntensityColor = Color.black;
|
||||
public Color highIntensityColor = Color.red;
|
||||
|
||||
private float[][] spectrogramData;
|
||||
private int currentTimeIndex = 0;
|
||||
private Texture2D bufferTexture;
|
||||
|
||||
void Start()
|
||||
{
|
||||
InitializeSpectrogramTexture();
|
||||
if (audioClip != null)
|
||||
{
|
||||
StartCoroutine(ProcessAudioClip());
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeSpectrogramTexture()
|
||||
{
|
||||
if (spectrogramTexture == null)
|
||||
{
|
||||
spectrogramTexture = new RenderTexture(textureWidth, textureHeight, 0, textureFormat);
|
||||
spectrogramTexture.Create();
|
||||
}
|
||||
|
||||
bufferTexture = new Texture2D(textureWidth, textureHeight, TextureFormat.ARGB32, false);
|
||||
|
||||
// 初始化为黑色
|
||||
Color[] pixels = new Color[textureWidth * textureHeight];
|
||||
for (int i = 0; i < pixels.Length; i++)
|
||||
{
|
||||
pixels[i] = Color.black;
|
||||
}
|
||||
bufferTexture.SetPixels(pixels);
|
||||
bufferTexture.Apply();
|
||||
}
|
||||
|
||||
IEnumerator ProcessAudioClip()
|
||||
{
|
||||
if (audioClip == null)
|
||||
{
|
||||
Debug.LogError("AudioClip is null!");
|
||||
yield break;
|
||||
}
|
||||
|
||||
float[] samples = new float[audioClip.samples * audioClip.channels];
|
||||
audioClip.GetData(samples, 0);
|
||||
|
||||
int sampleRate = audioClip.frequency;
|
||||
int samplesPerAnalysis = sampleRate / timeResolution;
|
||||
int totalAnalyses = Mathf.CeilToInt((float)samples.Length / samplesPerAnalysis);
|
||||
|
||||
spectrogramData = new float[totalAnalyses][];
|
||||
|
||||
for (int i = 0; i < totalAnalyses; i++)
|
||||
{
|
||||
int startSample = i * samplesPerAnalysis;
|
||||
int endSample = Mathf.Min(startSample + fftSize, samples.Length);
|
||||
|
||||
// 创建FFT窗口数据
|
||||
float[] windowData = new float[fftSize];
|
||||
for (int j = 0; j < fftSize && startSample + j < endSample; j++)
|
||||
{
|
||||
if (startSample + j < samples.Length)
|
||||
{
|
||||
// 应用汉宁窗
|
||||
float windowValue = 0.5f * (1f - Mathf.Cos(2f * Mathf.PI * j / (fftSize - 1)));
|
||||
windowData[j] = samples[startSample + j] * windowValue;
|
||||
}
|
||||
}
|
||||
|
||||
// 执行FFT
|
||||
spectrogramData[i] = PerformFFT(windowData);
|
||||
|
||||
// 渲染当前频谱到纹理
|
||||
RenderSpectrumToTexture(spectrogramData[i], i);
|
||||
|
||||
// 每处理几帧就让出控制权,避免卡顿
|
||||
if (i % 10 == 0)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 最终更新渲染纹理
|
||||
Graphics.CopyTexture(bufferTexture, spectrogramTexture);
|
||||
Debug.Log("频谱图生成完成!");
|
||||
}
|
||||
|
||||
float[] PerformFFT(float[] timeData)
|
||||
{
|
||||
// 简化的FFT实现 - 实际项目中建议使用更优化的FFT库
|
||||
float[] magnitudes = new float[fftSize / 2];
|
||||
|
||||
for (int k = 0; k < fftSize / 2; k++)
|
||||
{
|
||||
float real = 0f;
|
||||
float imag = 0f;
|
||||
|
||||
for (int n = 0; n < fftSize; n++)
|
||||
{
|
||||
float angle = -2f * Mathf.PI * k * n / fftSize;
|
||||
real += timeData[n] * Mathf.Cos(angle);
|
||||
imag += timeData[n] * Mathf.Sin(angle);
|
||||
}
|
||||
|
||||
magnitudes[k] = Mathf.Sqrt(real * real + imag * imag) / fftSize;
|
||||
}
|
||||
|
||||
return magnitudes;
|
||||
}
|
||||
|
||||
void RenderSpectrumToTexture(float[] spectrum, int timeIndex)
|
||||
{
|
||||
if (bufferTexture == null || spectrum == null)
|
||||
return;
|
||||
|
||||
// 计算在纹理中的x坐标
|
||||
int x = Mathf.FloorToInt((float)timeIndex / spectrogramData.Length * textureWidth);
|
||||
x = Mathf.Clamp(x, 0, textureWidth - 1);
|
||||
|
||||
// 渲染频谱数据到纹理的一列
|
||||
for (int y = 0; y < textureHeight; y++)
|
||||
{
|
||||
float frequency = (float)y / textureHeight;
|
||||
int spectrumIndex = Mathf.FloorToInt(frequency * spectrum.Length);
|
||||
spectrumIndex = Mathf.Clamp(spectrumIndex, 0, spectrum.Length - 1);
|
||||
|
||||
float intensity = spectrum[spectrumIndex] * intensityScale;
|
||||
intensity = Mathf.Clamp01(intensity);
|
||||
|
||||
Color pixelColor = Color.Lerp(lowIntensityColor, highIntensityColor, intensity);
|
||||
bufferTexture.SetPixel(x, y, pixelColor);
|
||||
}
|
||||
}
|
||||
|
||||
public void GenerateSpectrogram()
|
||||
{
|
||||
if (audioClip == null)
|
||||
{
|
||||
Debug.LogError("请先设置AudioClip!");
|
||||
return;
|
||||
}
|
||||
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(ProcessAudioClip());
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (bufferTexture != null)
|
||||
{
|
||||
DestroyImmediate(bufferTexture);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d25fa7ac86d50849b05d7ea4eb2c741
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
127
Convention/[Runtime]/Audio/SpectrogramRenderer.cs
Normal file
127
Convention/[Runtime]/Audio/SpectrogramRenderer.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using Unity.Collections;
|
||||
|
||||
namespace Convention
|
||||
{
|
||||
/// <summary>
|
||||
/// 频谱图渲染器 - 横轴时间,纵轴频率,颜色深度表示声音强度
|
||||
/// </summary>
|
||||
public class SpectrogramRenderer : MonoBehaviour
|
||||
{
|
||||
[Header("Audio Source")]
|
||||
public AudioClip clip;
|
||||
public int Step = 100;
|
||||
|
||||
[Header("Render Texture")]
|
||||
public RenderTexture spectrogramTexture;
|
||||
|
||||
[Header("Display Settings")]
|
||||
public Color lowIntensityColor = Color.black;
|
||||
public Color midIntensityColor = Color.red;
|
||||
public Color highIntensityColor = Color.white;
|
||||
public float PowMode = 5f;
|
||||
|
||||
private Texture2D bufferTexture;
|
||||
|
||||
void Start()
|
||||
{
|
||||
InitializeSpectrogramTexture();
|
||||
if (clip != null)
|
||||
{
|
||||
StartCoroutine(ProcessAudioClip());
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeSpectrogramTexture()
|
||||
{
|
||||
bufferTexture = new Texture2D(spectrogramTexture.width, spectrogramTexture.height, TextureFormat.ARGB32, false);
|
||||
|
||||
// 初始化为黑色
|
||||
Color[] pixels = new Color[spectrogramTexture.width * spectrogramTexture.height];
|
||||
for (int i = 0; i < pixels.Length; i++)
|
||||
{
|
||||
pixels[i] = Color.black;
|
||||
}
|
||||
bufferTexture.SetPixels(pixels);
|
||||
bufferTexture.Apply();
|
||||
}
|
||||
|
||||
IEnumerator ProcessAudioClip()
|
||||
{
|
||||
var samplesCount = clip.samples;
|
||||
Debug.Log("ProcessAudioClip Start", this);
|
||||
|
||||
var data = new float[spectrogramTexture.width + 10][];
|
||||
for (int i = 0; i < spectrogramTexture.width + 10; i++)
|
||||
{
|
||||
data[i] = new float[spectrogramTexture.height];
|
||||
for (int j = 0; j < spectrogramTexture.height; j++)
|
||||
data[i][j] = -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < samplesCount; )
|
||||
{
|
||||
var current = new float[spectrogramTexture.height];
|
||||
int x = Mathf.FloorToInt(i * (spectrogramTexture.width / (float)samplesCount));
|
||||
clip.GetData(current, i);
|
||||
for (int j = 0; j < spectrogramTexture.height; j++)
|
||||
{
|
||||
data[x][j] = Mathf.Max(data[x][j], current[j]);
|
||||
}
|
||||
for (int y = 0; y < spectrogramTexture.height; y++)
|
||||
{
|
||||
var last = bufferTexture.GetPixel(x, y);
|
||||
var t = (data[x][y] + 1f) * 0.5f;
|
||||
Color pixelColor = Color.white;
|
||||
if (t < 0.5f)
|
||||
pixelColor = Color.Lerp(lowIntensityColor, midIntensityColor, Mathf.Log(t*100, PowMode)*0.01f);
|
||||
else
|
||||
pixelColor = Color.Lerp(midIntensityColor, highIntensityColor, Mathf.Log(t*100, PowMode)*0.01f);
|
||||
pixelColor = new(Mathf.Max(last.r, pixelColor.r), Mathf.Max(last.g, pixelColor.g),
|
||||
Mathf.Max(last.b, pixelColor.b), Mathf.Max(last.a, pixelColor.a));
|
||||
bufferTexture.SetPixel(x, y, pixelColor);
|
||||
}
|
||||
if (x % 100 == 0)
|
||||
{
|
||||
bufferTexture.Apply();
|
||||
Debug.Log($"ProcessAudioClip: {i}-{i * 100 / samplesCount}%");
|
||||
yield return null;
|
||||
}
|
||||
i += Step;
|
||||
}
|
||||
//for (int x = 0; x < spectrogramTexture.width; x++)
|
||||
//{
|
||||
// for (int y = 0; y < spectrogramTexture.height; y++)
|
||||
// {
|
||||
// var last = bufferTexture.GetPixel(x, y);
|
||||
// var t = (data[x][y] + 1f) * 0.5f;
|
||||
// Color pixelColor = Color.Lerp(lowIntensityColor, highIntensityColor, Mathf.Pow(t, PowMode));
|
||||
// pixelColor = new(Mathf.Max(last.r, pixelColor.r), Mathf.Max(last.g, pixelColor.g),
|
||||
// Mathf.Max(last.b, pixelColor.b), Mathf.Max(last.a, pixelColor.a));
|
||||
// bufferTexture.SetPixel(x, y, pixelColor);
|
||||
// }
|
||||
// if (x % 100 == 0)
|
||||
// {
|
||||
// Debug.Log($"ProcessAudioClip Render: {x}-{x * 100 / spectrogramTexture.width}%");
|
||||
// yield return null;
|
||||
// }
|
||||
//}
|
||||
|
||||
// 最终更新渲染纹理
|
||||
bufferTexture.Apply();
|
||||
Graphics.CopyTexture(bufferTexture, spectrogramTexture);
|
||||
Debug.Log("频谱图生成完成!");
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (bufferTexture != null)
|
||||
{
|
||||
DestroyImmediate(bufferTexture);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
11
Convention/[Runtime]/Audio/SpectrogramRenderer.cs.meta
Normal file
11
Convention/[Runtime]/Audio/SpectrogramRenderer.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81765945edc9f7f4ea0d96345d7bea36
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
429
Convention/[Runtime]/AudioSystem.cs
Normal file
429
Convention/[Runtime]/AudioSystem.cs
Normal file
@@ -0,0 +1,429 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using UnityEngine.XR;
|
||||
|
||||
namespace Convention
|
||||
{
|
||||
public abstract class BasicAudioSystem : MonoBehaviour
|
||||
{
|
||||
public abstract void LoadAudioClip(AudioClip clip);
|
||||
public abstract AudioClip GetAudioClip();
|
||||
public abstract float GetClockTime();
|
||||
|
||||
public AudioClip CurrentClip
|
||||
{
|
||||
get => GetAudioClip();
|
||||
set => LoadAudioClip(value);
|
||||
}
|
||||
public float CurrentTime => GetClockTime();
|
||||
|
||||
public abstract bool IsPlaying();
|
||||
|
||||
public abstract void Stop();
|
||||
public abstract void Pause();
|
||||
public abstract void Play();
|
||||
|
||||
public abstract void SetPitch(float pitch);
|
||||
public abstract void SetSpeed(float speed);
|
||||
public abstract void SetVolume(float volume);
|
||||
|
||||
public void PrepareToOtherScene()
|
||||
{
|
||||
StartCoroutine(ClockOnJump());
|
||||
IEnumerator ClockOnJump()
|
||||
{
|
||||
transform.parent = null;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
for (float now = 0; now < 1; now += UnityEngine.Time.deltaTime)
|
||||
{
|
||||
this.SetVolume(1 - now);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
if (samples.Length != spectrumLength) samples = new float[spectrumLength];
|
||||
if (bands.Length != BandCount) bands = new float[BandCount];
|
||||
if (freqBands.Length != BandCount) freqBands = new float[BandCount];
|
||||
if (bandBuffers.Length != BandCount) bandBuffers = new float[BandCount];
|
||||
if (bufferDecrease.Length != BandCount) bufferDecrease = new float[BandCount];
|
||||
if (bandHighest.Length != BandCount) bandHighest = new float[BandCount];
|
||||
if (normalizedBands.Length != BandCount) normalizedBands = new float[BandCount];
|
||||
if (normalizedBandBuffers.Length != BandCount) normalizedBandBuffers = new float[BandCount];
|
||||
if (sampleCount.Length != BandCount) sampleCount = new int[BandCount];
|
||||
}
|
||||
|
||||
public void Sampling()
|
||||
{
|
||||
if (samples.Length != spectrumLength) samples = new float[spectrumLength];
|
||||
if (bands.Length != BandCount) bands = new float[BandCount];
|
||||
if (freqBands.Length != BandCount) freqBands = new float[BandCount];
|
||||
if (bandBuffers.Length != BandCount) bandBuffers = new float[BandCount];
|
||||
if (bufferDecrease.Length != BandCount) bufferDecrease = new float[BandCount];
|
||||
if (bandHighest.Length != BandCount) bandHighest = new float[BandCount];
|
||||
if (normalizedBands.Length != BandCount) normalizedBands = new float[BandCount];
|
||||
if (normalizedBandBuffers.Length != BandCount) normalizedBandBuffers = new float[BandCount];
|
||||
if (sampleCount.Length != BandCount) sampleCount = new int[BandCount];
|
||||
|
||||
InjectSampling();
|
||||
}
|
||||
|
||||
protected abstract void InjectSampling();
|
||||
|
||||
public static AudioType GetAudioType(string path)
|
||||
{
|
||||
return Path.GetExtension(path) switch
|
||||
{
|
||||
"wav" => AudioType.WAV,
|
||||
"mp3" => AudioType.MPEG,
|
||||
"ogg" => AudioType.OGGVORBIS,
|
||||
_ => AudioType.UNKNOWN
|
||||
};
|
||||
}
|
||||
|
||||
public void LoadOnResource(string path)
|
||||
{
|
||||
LoadAudioClip(Resources.Load<AudioClip>(path));
|
||||
}
|
||||
public void LoadOnUrl(string url)
|
||||
{
|
||||
LoadOnUrl(url, GetAudioType(url));
|
||||
}
|
||||
public void LoadOnUrl(string url, AudioType audioType)
|
||||
{
|
||||
StartCoroutine(LoadAudio(url, audioType));
|
||||
}
|
||||
public IEnumerator LoadAudio(string path, AudioType audioType)
|
||||
{
|
||||
UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(path, audioType);
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(request);
|
||||
CurrentClip = audioClip;
|
||||
}
|
||||
else Debug.LogError(request.result);
|
||||
}
|
||||
|
||||
public enum SpectrumLength
|
||||
{
|
||||
Spectrum64, Spectrum128, Spectrum256, Spectrum512, Spectrum1024, Spectrum2048, Spectrum4096, Spectrum8192
|
||||
}
|
||||
|
||||
public enum BufferDecreasingType
|
||||
{
|
||||
Jump, Slide, Falling
|
||||
}
|
||||
|
||||
public enum BufferIncreasingType
|
||||
{
|
||||
Jump, Slide
|
||||
}
|
||||
[Header("MusicSampler")]
|
||||
public SpectrumLength SpectrumCount = SpectrumLength.Spectrum256;
|
||||
protected int spectrumLength => (int)Mathf.Pow(2, ((int)SpectrumCount + 6));
|
||||
public float[] samples = new float[64];
|
||||
protected int[] sampleCount = new int[8];
|
||||
public uint BandCount = 8;
|
||||
public BufferDecreasingType decreasingType = BufferDecreasingType.Jump;
|
||||
public float decreasing = 0.003f;
|
||||
public float DecreaseAcceleration = 0.2f;
|
||||
public BufferIncreasingType increasingType = BufferIncreasingType.Jump;
|
||||
public float increasing = 0.003f;
|
||||
public float[] bands = new float[8];
|
||||
protected float[] freqBands = new float[8];
|
||||
protected float[] bandBuffers = new float[8];
|
||||
protected float[] bufferDecrease = new float[8];
|
||||
public float average
|
||||
{
|
||||
get
|
||||
{
|
||||
float average = 0;
|
||||
for (int i = 0; i < BandCount; i++)
|
||||
{
|
||||
average += normalizedBands[i];
|
||||
}
|
||||
average /= BandCount;
|
||||
return average;
|
||||
}
|
||||
}
|
||||
|
||||
protected float[] bandHighest = new float[8];
|
||||
public float[] normalizedBands = new float[8];
|
||||
protected float[] normalizedBandBuffers = new float[8];
|
||||
|
||||
protected void GetSampleCount()
|
||||
{
|
||||
float acc = (((float)((int)SpectrumCount + 6)) / BandCount);
|
||||
int sum = 0;
|
||||
int last = 0;
|
||||
for (int i = 0; i < BandCount - 1; i++)
|
||||
{
|
||||
int pow = (int)Mathf.Pow(2, acc * (i));
|
||||
sampleCount[i] = pow - sum;
|
||||
if (sampleCount[i] < last) sampleCount[i] = last;
|
||||
sum += sampleCount[i];
|
||||
last = sampleCount[i];
|
||||
}
|
||||
sampleCount[BandCount - 1] = samples.Length - sum;
|
||||
}
|
||||
}
|
||||
|
||||
public class AudioSystem : BasicAudioSystem
|
||||
{
|
||||
[SerializeField] private AudioSource source;
|
||||
|
||||
public AudioSource Source
|
||||
{
|
||||
get
|
||||
{
|
||||
if (source == null)
|
||||
source = this.SeekComponent<AudioSource>();
|
||||
if (source == null)
|
||||
source = this.gameObject.AddComponent<AudioSource>();
|
||||
return source;
|
||||
}
|
||||
set
|
||||
{
|
||||
Stop();
|
||||
source = value;
|
||||
}
|
||||
}
|
||||
|
||||
public AudioMixer Mixer = null;
|
||||
|
||||
public override AudioClip GetAudioClip()
|
||||
{
|
||||
return Source.clip;
|
||||
}
|
||||
public override float GetClockTime()
|
||||
{
|
||||
return (float)Source.timeSamples / (float)Source.clip.frequency;
|
||||
}
|
||||
|
||||
public override bool IsPlaying()
|
||||
{
|
||||
return Source.isPlaying;
|
||||
}
|
||||
|
||||
public override void LoadAudioClip(AudioClip clip)
|
||||
{
|
||||
Source.clip = clip;
|
||||
}
|
||||
|
||||
public override void Stop()
|
||||
{
|
||||
Source.Stop();
|
||||
}
|
||||
public override void Pause()
|
||||
{
|
||||
Source.Pause();
|
||||
}
|
||||
public override void Play()
|
||||
{
|
||||
Source.Play();
|
||||
}
|
||||
|
||||
public override void SetPitch(float pitch)
|
||||
{
|
||||
Source.pitch = pitch;
|
||||
}
|
||||
public override void SetSpeed(float speed)
|
||||
{
|
||||
SetSpeed(speed, 1.0f, "Master", "MasterPitch", "PitchShifterPitch", true);
|
||||
}
|
||||
public override void SetVolume(float volume)
|
||||
{
|
||||
Source.volume = volume;
|
||||
}
|
||||
|
||||
public void SetSpeed(float speed,
|
||||
float TargetPitchValue,
|
||||
string TargetGroupName,
|
||||
string TargetPitch_Attribute_Name,
|
||||
string TargetPitchshifterPitch_Attribute_Name,
|
||||
bool IsIgnoreWarning = false)
|
||||
{
|
||||
if (Mixer != null)
|
||||
{
|
||||
if (TargetPitchValue > 0)
|
||||
{
|
||||
Source.pitch = 1;
|
||||
Mixer.SetFloat(TargetPitch_Attribute_Name, TargetPitchValue);
|
||||
float TargetPitchshifterPitchValue = 1.0f / TargetPitchValue;
|
||||
Mixer.SetFloat(TargetPitchshifterPitch_Attribute_Name, TargetPitchshifterPitchValue);
|
||||
Source.outputAudioMixerGroup = Mixer.FindMatchingGroups(TargetGroupName)[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
Source.pitch = -1;
|
||||
Mixer.SetFloat(TargetPitch_Attribute_Name, -TargetPitchValue);
|
||||
float TargetPitchshifterPitchValue = -1.0f / TargetPitchValue;
|
||||
Mixer.SetFloat(TargetPitchshifterPitch_Attribute_Name, TargetPitchshifterPitchValue);
|
||||
Source.outputAudioMixerGroup = Mixer.FindMatchingGroups(TargetGroupName)[0];
|
||||
}
|
||||
}
|
||||
else if (IsIgnoreWarning == false)
|
||||
{
|
||||
Debug.LogWarning("you try to change an Audio's speed without AudioMixer, which will cause it to change its pitch");
|
||||
SetPitch(speed);
|
||||
}
|
||||
}
|
||||
|
||||
private void GetSpectrums()
|
||||
{
|
||||
Source.GetSpectrumData(samples, 0, FFTWindow.Blackman);
|
||||
}
|
||||
|
||||
|
||||
private void GetFrequencyBands()
|
||||
{
|
||||
int counter = 0;
|
||||
for (int i = 0; i < BandCount; i++)
|
||||
{
|
||||
float average = 0;
|
||||
for (int j = 0; j < sampleCount[i]; j++)
|
||||
{
|
||||
average += samples[counter] * (counter + 1);
|
||||
counter++;
|
||||
}
|
||||
average /= sampleCount[i];
|
||||
freqBands[i] = average * 10;
|
||||
}
|
||||
}
|
||||
|
||||
private void GetNormalizedBands()
|
||||
{
|
||||
for (int i = 0; i < BandCount; i++)
|
||||
{
|
||||
if (freqBands[i] > bandHighest[i])
|
||||
{
|
||||
bandHighest[i] = freqBands[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void GetBandBuffers(BufferIncreasingType increasingType, BufferDecreasingType decreasingType)
|
||||
{
|
||||
for (int i = 0; i < BandCount; i++)
|
||||
{
|
||||
if (freqBands[i] > bandBuffers[i])
|
||||
{
|
||||
switch (increasingType)
|
||||
{
|
||||
case BufferIncreasingType.Jump:
|
||||
bandBuffers[i] = freqBands[i];
|
||||
bufferDecrease[i] = decreasing;
|
||||
break;
|
||||
case BufferIncreasingType.Slide:
|
||||
bufferDecrease[i] = decreasing;
|
||||
bandBuffers[i] += increasing;
|
||||
break;
|
||||
}
|
||||
if (freqBands[i] < bandBuffers[i]) bandBuffers[i] = freqBands[i];
|
||||
}
|
||||
if (freqBands[i] < bandBuffers[i])
|
||||
{
|
||||
switch (decreasingType)
|
||||
{
|
||||
case BufferDecreasingType.Jump:
|
||||
bandBuffers[i] = freqBands[i];
|
||||
break;
|
||||
case BufferDecreasingType.Falling:
|
||||
bandBuffers[i] -= decreasing;
|
||||
break;
|
||||
case BufferDecreasingType.Slide:
|
||||
bandBuffers[i] -= bufferDecrease[i];
|
||||
bufferDecrease[i] *= 1 + DecreaseAcceleration;
|
||||
break;
|
||||
}
|
||||
if (freqBands[i] > bandBuffers[i]) bandBuffers[i] = freqBands[i]; ;
|
||||
}
|
||||
bands[i] = bandBuffers[i];
|
||||
if (bandHighest[i] == 0) continue;
|
||||
normalizedBands[i] = (freqBands[i] / bandHighest[i]);
|
||||
normalizedBandBuffers[i] = (bandBuffers[i] / bandHighest[i]);
|
||||
if (normalizedBands[i] > normalizedBandBuffers[i])
|
||||
{
|
||||
switch (increasingType)
|
||||
{
|
||||
case BufferIncreasingType.Jump:
|
||||
normalizedBandBuffers[i] = normalizedBands[i];
|
||||
bufferDecrease[i] = decreasing;
|
||||
break;
|
||||
case BufferIncreasingType.Slide:
|
||||
bufferDecrease[i] = decreasing;
|
||||
normalizedBandBuffers[i] += increasing;
|
||||
break;
|
||||
}
|
||||
if (normalizedBands[i] < normalizedBandBuffers[i]) normalizedBandBuffers[i] = normalizedBands[i];
|
||||
}
|
||||
if (normalizedBands[i] < normalizedBandBuffers[i])
|
||||
{
|
||||
switch (decreasingType)
|
||||
{
|
||||
case BufferDecreasingType.Jump:
|
||||
normalizedBandBuffers[i] = normalizedBands[i];
|
||||
break;
|
||||
case BufferDecreasingType.Falling:
|
||||
normalizedBandBuffers[i] -= decreasing;
|
||||
break;
|
||||
case BufferDecreasingType.Slide:
|
||||
normalizedBandBuffers[i] -= bufferDecrease[i];
|
||||
bufferDecrease[i] *= 1 + DecreaseAcceleration;
|
||||
break;
|
||||
}
|
||||
if (normalizedBands[i] > normalizedBandBuffers[i]) normalizedBandBuffers[i] = normalizedBands[i];
|
||||
}
|
||||
normalizedBands[i] = normalizedBandBuffers[i];
|
||||
}
|
||||
}
|
||||
|
||||
private void BandNegativeCheck()
|
||||
{
|
||||
for (int i = 0; i < BandCount; i++)
|
||||
{
|
||||
if (bands[i] < 0)
|
||||
{
|
||||
bands[i] = 0;
|
||||
}
|
||||
if (normalizedBands[i] < 0)
|
||||
{
|
||||
normalizedBands[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InjectSampling()
|
||||
{
|
||||
GetSpectrums();
|
||||
GetFrequencyBands();
|
||||
GetNormalizedBands();
|
||||
GetBandBuffers(increasingType, decreasingType);
|
||||
BandNegativeCheck();
|
||||
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
float[] spectrum = new float[256];
|
||||
AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.Rectangular);
|
||||
for (int i = 1; i < spectrum.Length - 1; i++)
|
||||
{
|
||||
Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red);
|
||||
Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan);
|
||||
Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green);
|
||||
Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.blue);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
11
Convention/[Runtime]/AudioSystem.cs.meta
Normal file
11
Convention/[Runtime]/AudioSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92ba7bb6320d6934e82e24c449692e71
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user