草庐IT

Unity运行时加载外部mp3/wav音频

GrimRaider 2023-09-01 原文

本文介绍Unity开发中,在运行时加载外部音频(mp3/wav)的方法,非 WWW 或 UnityWebRequest,需要www方式的同学请自行baidu

参考库:

  1. NAudio:功能全,但仅限windows平台
  2. NLayer:读取mp3音频文件并解析,正好满足需求;

github地址(都是NAudio名下的)

  1. NAudio: GitHub - naudio/NAudio: Audio and MIDI library for .NET
  2. NLayer: GitHub - naudio/NLayer: MPEG 1 & 2 Decoder for Layers 1, 2, & 3

NAudio

        输入:mp3文件

        输出:AudioClip

// Use NAudio
// file => mp3 byte[] => {NAudio} => wav byte[] => AudioClip

using System;
using System.IO;
using NAudio.Wave;
// parse mp3
AudioClip LoadMp3Audio(byte[] audioBytes, string fileName)
{
    MemoryStream mp3stream = new MemoryStream(audioBytes);
    Mp3FileReader mp3audio = new Mp3FileReader(mp3stream);
    WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(mp3audio);
    MemoryStream outputStream = new MemoryStream();
    using (WaveFileWriter waveFileWriter = new WaveFileWriter(outputStream, waveStream.WaveFormat))
    {
        byte[] bytes = new byte[waveStream.Length];
        waveStream.Position = 0;
        waveStream.Read(bytes, 0, Convert.ToInt32(waveStream.Length));
        waveFileWriter.Write(bytes, 0, bytes.Length);
        waveFileWriter.Flush();
    }
    byte[] wavBytes = outputStream.ToArray();
    return LoadWavAudio(wavBytes, fileName);
}
// parse wav
AudioClip LoadWavAudio(byte[] audioBytes, string fileName)
{
    // WAV 自定义wav解析类
    WAV wav = new WAV(audioBytes);
    AudioClip audioClip;
    if (wav.ChannelCount == 2)
    {
        audioClip = AudioClip.Create(fileName, wav.SampleCount, 2, wav.Frequency, false);
        audioClip.SetData(wav.StereoChannel, 0);
    }
    else
    {
        audioClip = AudioClip.Create(fileName, wav.SampleCount, 1, wav.Frequency, false);
        audioClip.SetData(wav.LeftChannel, 0);
    }
    return audioClip;
}
// 逻辑不完整,但是能用,所以只要你和代码一个能跑就可以了
public class WAV
{

    // convert two bytes to one float in the range -1 to 1
    static float bytesToFloat(byte firstByte, byte secondByte)
    {
        // convert two bytes to one short (little endian)
        short s = (short)((secondByte << 8) | firstByte);
        // convert to range from -1 to (just below) 1
        return s / 32768.0F;
    }

    static int bytesToInt(byte[] bytes, int offset = 0)
    {
        int value = 0;
        for (int i = 0; i < 4; i++)
        {
            value |= ((int)bytes[offset + i]) << (i * 8);
        }
        return value;
    }
    // properties
    public float[] LeftChannel { get; internal set; }
    public float[] RightChannel { get; internal set; }
    public float[] StereoChannel { get; internal set; }
    public int ChannelCount { get; internal set; }
    public int SampleCount { get; internal set; }
    public int Frequency { get; internal set; }


    public WAV(byte[] wav)
    {
        // Determine if mono or stereo
        ChannelCount = wav[22];     // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels

        // Get the frequency
        Frequency = bytesToInt(wav, 24);

        // Get past all the other sub chunks to get to the data subchunk:
        int pos = 12;   // First Subchunk ID from 12 to 16

        // Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
        while (!(wav[pos] == 100 && wav[pos + 1] == 97 && wav[pos + 2] == 116 && wav[pos + 3] == 97))
        {
            pos += 4;
            int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
            pos += 4 + chunkSize;
        }
        pos += 8;

        // Pos is now positioned to start of actual sound data.
        SampleCount = (wav.Length - pos) / 2;     // 2 bytes per sample (16 bit sound mono)
        if (ChannelCount == 2) SampleCount /= 2;        // 4 bytes per sample (16 bit stereo)

        // Allocate memory (right will be null if only mono sound)
        LeftChannel = new float[SampleCount];
        if (ChannelCount == 2) RightChannel = new float[SampleCount];
        else RightChannel = null;

        // Write to double array/s:
        int i = 0;
        while (pos < wav.Length)
        {
            LeftChannel[i] = bytesToFloat(wav[pos], wav[pos + 1]);
            pos += 2;
            if (ChannelCount == 2)
            {
                RightChannel[i] = bytesToFloat(wav[pos], wav[pos + 1]);
                pos += 2;
            }
            i++;
        }

        //Merge left and right channels for stereo sound
        if (ChannelCount == 2)
        {
            StereoChannel = new float[SampleCount * 2];
            //Current position in our left and right channels
            int channelPos = 0;
            //After we've changed two values for our Stereochannel, we want to increase our channelPos
            short posChange = 0;

            for (int index = 0; index < (SampleCount * 2); index++)
            {

                if (index % 2 == 0)
                {
                    StereoChannel[index] = LeftChannel[channelPos];
                    posChange++;
                }
                else
                {
                    StereoChannel[index] = RightChannel[channelPos];
                    posChange++;
                }
                //Two values have been changed, so update our channelPos
                if (posChange % 2 == 0)
                {
                    if (channelPos < SampleCount)
                    {
                        channelPos++;
                        //Reset the counter for next iterations
                        posChange = 0;
                    }
                }
            }
        }
        else
        {
            StereoChannel = null;
        }

        Debug.Log($"mpegFile.Length={wav.Length},ChannelCount={ChannelCount},lenSample={LeftChannel.Length}");
    }
}

NLayer

// NLayer
// file => mp3 byte[] => {MpegFile} => {ReadSamples} => sample float[] => AudioClip
// 重点是ReadSamples方法的使用

using NLayer;

AudioClip LoadMp3Audio(byte[] audioBytes, string fileName)
{
    System.IO.Stream memStream = new System.IO.MemoryStream(audioBytes);
    var mpegFile = new MpegFile(memStream);
    int lengthSamples = (int)(mpegFile.Length / sizeof(float) / mpegFile.Channels);
    float[] samples = new float[lengthSamples * mpegFile.Channels];
    int readCount = mpegFile.ReadSamples(samples, 0, lengthSamples * mpegFile.Channels);
    AudioClip ac = AudioClip.Create(fileName, lengthSamples, mpegFile.Channels, mpegFile.SampleRate, false);
    ac.SetData(samples, 0);
    return ac;
}
// wav加载同NAudio部分

参考:

  1. https://www.jianshu.com/p/bf20e69f2cc4
  2. Audio - Import mp3 at runtime? - Unity Forum
  3. unity-load-mp3-at-runtime/Assets/NLayer at master · greggman/unity-load-mp3-at-runtime · GitHub
  4. (Solved)Load a 16bit .Wav file WITHOUT using WWW (at runtime) - Unity Forum

改进加载效果:

https://github.com/Cysharp/UniTask

有关Unity运行时加载外部mp3/wav音频的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  4. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  7. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  8. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  9. ruby-on-rails - 无法让 rspec、spork 和调试器正常运行 - 2

    GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'

  10. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

随机推荐