Search Unity

  1. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice

Audio format importing

Discussion in '2020.1 Beta' started by khos, Mar 28, 2020.

  1. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,078
    Hi,

    Would it be ok to ask a question here (apologies if in incorrect forum section), regarding Audio format importing.
    Older Unity versions only allow for .wav file audio import (at runtime) to my understanding, not .mp3 I believe due to licensing issues..
    Will the 2020 version allow this, or can this be handled differently?
     
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    2,478
  3. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,078
    Hello LeonhardP, many thanks for your help. I will proceed to look for a sample script/how to, to be able to load external mp3's into my game scene. The intent was to use a asset/plugin called audioimporter but if there is a native/easier way I would prefer that.
     
  4. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    458
    You can just drop the file into the project pane as with any other asset. Or are you talking about loading them at runtime?
     
  5. khos

    khos

    Joined:
    May 10, 2016
    Posts:
    1,078
    Hi,,I need to load at runtime.
     
  6. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    138
    You can use CSCore or NAudio to retrieve the samples from the audio file, which you feed into the PCMReaderCallback delegate on a runtime-created AudioClip. Here's some example code taken from a project I'm working on, it uses the ISampleSource API from CSCore:

    Code (CSharp):
    1. void PcmSetPositionCallback(int position)
    2. {
    3.     if (isDisposed || !SampleSource.CanSeek)
    4.         return;
    5.  
    6.     SampleSource.Position = (long)position * SampleSource.WaveFormat.Channels;
    7. }
    8.  
    9. void PcmReaderCallback(float[] samples)
    10. {
    11.     if (isDisposed)
    12.         return;
    13.  
    14.     for (int offset = 0, remaining = samples.Length; remaining > 0;)
    15.     {
    16.         int n = SampleSource.Read(samples, offset, remaining);
    17.  
    18.         if (n <= 0)
    19.             break;
    20.  
    21.         offset    += n;
    22.         remaining -= n;
    23.     }
    24. }
    Hope that's useful.
     
unityunity