Unity音频生成波浪线

29 篇文章 0 订阅

传入一个音频生成波浪线效果如下图:

 

using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public AudioClip audioClip;

    public RawImage _rawImage; 
    // Start is called before the first frame update
    void Start()
    {
        _rawImage.texture = BakeAudioWaveform(audioClip);
    }

// 传入一个AudioClip 会将AudioClip上挂载的音频文件生成频谱到一张Texture2D上
public static Texture2D BakeAudioWaveform(AudioClip _clip) {
        int resolution = 20;	// 控制生成波浪线的高度
        int width = 1920;		// 这个是最后生成的Texture2D图片的宽度
        int height = 200;		// 这个是最后生成的Texture2D图片的高度

        resolution = _clip.frequency / resolution;

        float[] samples = new float[_clip.samples * _clip.channels];
        _clip.GetData(samples, 0);

        float[] waveForm = new float[(samples.Length / resolution)];

        float min = 0;
        float max = 0;
        bool inited = false;

        for (int i = 0; i < waveForm.Length; i++) {
            waveForm[i] = 0;

            for (int j = 0; j < resolution; j++) {
                waveForm[i] += Mathf.Abs(samples[(i * resolution) + j]);
            }

            if (!inited) {
                min = waveForm[i];
                max = waveForm[i];
                inited = true;
            } else {
                if (waveForm[i] < min) {
                    min = waveForm[i];
                }

                if (waveForm[i] > max) {
                    max = waveForm[i];
                }
            }
            //waveForm[i] /= resolution;
        }


        Color backgroundColor = Color.black;
        Color waveformColor = Color.green;
        Color[] blank = new Color[width * height];
        Texture2D texture = new Texture2D(width, height);

        for (int i = 0; i < blank.Length; ++i) {
            blank[i] = backgroundColor;
        }

        texture.SetPixels(blank, 0);

        float xScale = (float)width / (float)waveForm.Length;

        int tMid = (int)(height / 2.0f);
        float yScale = 1;

        if (max > tMid) {
            yScale = tMid / max;
        }

        for (int i = 0; i < waveForm.Length; ++i) {
            int x = (int)(i * xScale);
            int yOffset = (int)(waveForm[i] * yScale);
            int startY = tMid - yOffset;
            int endY = tMid + yOffset;

            for (int y = startY; y <= endY; ++y) {
                texture.SetPixel(x, y, waveformColor);
            }
        }

        texture.Apply();
        return texture;
    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将文本转换为音频,您需要使用文字转语音引擎。有许多不同的文字转语音引擎可供选择,例如Google Text-to-Speech API,IBM Watson Text-to-Speech API,Nuance Text-to-Speech等等。这些引擎都有不同的接口可以使用,您需要查看每个引擎的文档以了解如何使用它。 一般情况下,您需要向文字转语音引擎发送HTTP请求,并收到包含生成音频文件的响应。您可以使用Unity的WWW类来发送HTTP请求,并将音频文件保存到本地。使用Unity的Audio Source组件,您可以播放保存的音频文件。 以下是一个基本示例代码,演示如何使用Google Text-to-Speech API将文本转换为音频,并使用Unity的WWW类将音频文件保存到本地: ``` using UnityEngine; using System.Collections; public class TextToSpeech : MonoBehaviour { // The text to be converted to audio public string text = "Hello, world!"; // The Google Text-to-Speech API endpoint public string url = "https://texttospeech.googleapis.com/v1beta1/text:synthesize"; // Your Google Cloud API key public string apiKey = "YOUR_GOOGLE_CLOUD_API_KEY"; // The audio format (eg. "MP3", "WAV") public string audioFormat = "MP3"; // The output audio file path public string audioFilePath = "output/audio.mp3"; IEnumerator Start() { // Convert the text to audio using the Google Text-to-Speech API WWWForm form = new WWWForm(); form.AddField("input", "{\"text\":\"" + text + "\"}"); form.AddField("voice", "{\"languageCode\":\"en-US\",\"name\":\"en-US-Wavenet-F\"}"); form.AddField("audioConfig", "{\"audioEncoding\":\"" + audioFormat + "\"}"); byte[] rawData = form.data; WWW www = new WWW(url, rawData, new Dictionary<string,string> {{"Content-Type", "application/json"}, {"X-Goog-Api-Key", apiKey}}); yield return www; // Save the audio file to disk string filePath = Application.dataPath + "/" + audioFilePath; System.IO.File.WriteAllBytes(filePath, www.bytes); // Load the audio file and play it AudioClip clip = WavUtility.ToAudioClip(filePath); AudioSource.PlayClipAtPoint(clip, Vector3.zero); } } ``` 请注意,此代码片段仅用于演示目的,您需要根据自己的需要进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值