修复一些内容
This commit is contained in:
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:
|
Reference in New Issue
Block a user