Unity使用SoundFlow对音频文件降噪

SoundFlow 地址 https://github.com/LSXPrime/SoundFlow.git
soundflow-unity 地址 https://github.com/xue-fei/soundflow-unity.git

由于SoundFlow 的依赖问题,无法直接使用dll,且源码C#版本太高无法直接用,所以进行了一些修改。

音频文件降噪具体实现

using SoundFlow.Abstracts;
using SoundFlow.Backends.MiniAudio;
using SoundFlow.Extensions.WebRtc.Apm;
using SoundFlow.Extensions.WebRtc.Apm.Components;
using SoundFlow.Providers;
using SoundFlow.Structs;
using System.IO;
using System.Linq;
using UnityEngine;

public class UnityNoiseSuppressor : MonoBehaviour
{
    private AudioEngine audioEngine;

    // Start is called before the first frame update
    void Start()
    {
        audioEngine = new MiniAudioEngine();
        AudioFormat Format = AudioFormat.Unity;

        string filePath = Application.dataPath + "/StreamingAssets/mix.wav";
        using var inputStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        using var dataProvider = new StreamDataProvider(audioEngine, Format, inputStream);
        var noiseSuppressor = new NoiseSuppressor(
            dataProvider: dataProvider,
            audioFormat: Format,
            suppressionLevel: NoiseSuppressionLevel.VeryHigh
        );

        var cleanData = noiseSuppressor.ProcessAll();
        float[] data = cleanData.ToArray();
        SaveClip(1, 16000, data, Application.streamingAssetsPath + "/test.wav");
        // Dispose noise suppressor and encoder
        noiseSuppressor.Dispose();
        dataProvider.Dispose();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public static void SaveClip(int channels, int frequency, float[] data, string filePath)
    {
        using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
        {
            using (BinaryWriter writer = new BinaryWriter(fileStream))
            {
                // 写入RIFF头部标识
                writer.Write("RIFF".ToCharArray());
                // 写入文件总长度(后续填充)
                writer.Write(0);
                writer.Write("WAVE".ToCharArray());
                // 写入fmt子块
                writer.Write("fmt ".ToCharArray());
                writer.Write(16); // PCM格式块长度
                writer.Write((short)1); // PCM编码类型
                writer.Write((short)channels);
                writer.Write(frequency);
                writer.Write(frequency * channels * 2); // 字节率
                writer.Write((short)(channels * 2)); // 块对齐
                writer.Write((short)16); // 位深度
                                         // 写入data子块
                writer.Write("data".ToCharArray());
                writer.Write(data.Length * 2); // 音频数据字节数
                                               // 写入PCM数据(float转为short)
                foreach (float sample in data)
                {
                    writer.Write((short)(sample * 32767));
                }
                // 返回填充文件总长度
                fileStream.Position = 4;
                writer.Write((int)(fileStream.Length - 8));
            }
        }
    }
}

原音频
在这里插入图片描述
降噪后
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地狱为王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值