Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resources.Load( pathToValidWAV ) as AudioClip contains only zeros

Discussion in 'Audio & Video' started by Pi_3-14, Dec 22, 2015.

  1. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    This is a fierce one.

    I am loading raw data from a .WAV like this:

    Code (csharp):
    1. AudioClip C = Resources.Load(filepath) as AudioClip;  assert(C!=null);
    2. raw_data = new Stereo(C);
    3.  
    4. public Stereo(AudioClip clip)
    5. {
    6.     if(clip.channels !=2){
    7.          Debug.Log("Error:Need stereo!");
    8.          return;
    9.      }
    10.  
    11.      int N=clip.samples;
    12.  
    13.      Samples=N;
    14.      L=new float[N];
    15.      R=new float[N];
    16.  
    17.      float[] wav=new float[N*clip.channels];
    18.  
    19.      // interleaved
    20.      clip.GetData(wav,0);
    21.  
    22.      for(inti=0;i<N;i++)
    23.      {
    24.          L[i]=wav[2*i];
    25.          R[i]=wav[2*i+1];
    26.      }
    27. }
    Inspecting the raw data, it is zeros everywhere.
    Inspecting the original WAV in audacity, it is some stereo waveform.

    Something is going wrong!

    I look at my import settings:

    can't see anything wrong there. I do have a lot of these files! >1000.

    Now the weird thing is I have a separate set of 'piano note' WAV files which were behaving the same way. Somehow toggling the import settings got them to load in okay, even though I was just setting a toggle and removing it again. So I tried the same trip here, no luck!

    Something is going really wrong. This was working with an earlier Unity5.x.

    Tomorrow I suppose I must try with an empty project, and see if I can get a minimal case. I'm rather worried it will work (why shouldn't it?) which would leave me having to rebuild my entire project piece by piece until I find one illegal move.

    Can anyone illuminate?

    π
     
  2. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    I've just submitted a bug report (attached).

    https://fogbugz.unity3d.com/default.asp?756384_i68c3nbe15l8jold

    While doing so, I found 3 more minor bugs:
    1. For my bug report I created a fresh project, then used Help -> Report a Bug, but after I had submitted it and closed Unity, it prompted me to save the scene. So I bet it has submitted an incomplete project.
    2. The 'Upload a File' button on this forum doesn't work on Firefox (OSX), I had to use Safari
    3. Trying to copy-paste code from MonoDevelop, it comes out with all SPACE/TAB removed. Fortunately I know of SHIFT+CTRL+OPT+v which works.
    Anyway, ignoring those, here is the problem:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class foo : MonoBehaviour
    7. {
    8.     void Start ()
    9.     {
    10.         AudioClip C = Resources.Load( "01" ) as AudioClip;
    11.  
    12.         if( C == null )
    13.             throw new System.Exception( "C == null" );
    14.  
    15.         float [] wav = new float[ C.samples * 2 ];
    16.  
    17.         C.GetData( wav, 0 );  // interleaved
    18.  
    19.         Debug.Log( "Len: " + wav.Length );
    20.  
    21.         bool allZeros = true;
    22.         for( int j=0; j<wav.Length; j++ )
    23.             if( wav[j] != 0 )
    24.                 allZeros = false;
    25.  
    26.         if( allZeros )
    27.             throw new System.Exception( "Error: All 0s" );
    28.  
    29.         Debug.Log("Success");
    30.     }
    31. }
    32.  
    That last exception always gets hit, even though I can see clearly that 01.WAV is clearly not composed entirely of zeros.
     

    Attached Files:

  3. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    I'm using 5.2.2

    I inserted the test part into my own load audioclip from resources script.

    Loaded from resources.
    Got Data.
    Not all zero's.

    The only time it doesn't work is when I set to stream from disk, and the error log tells me that is why it won't work.

    Code (csharp):
    1. AudioClip C = Resources.Load("01") as AudioClip;
    Are you sure that's a valid path to a file?
    From your first screenshot it should be:
    Code (csharp):
    1. AudioClip C = Resources.Load("002/01") as AudioClip;
    If it can't find your asset, then it will apparently just give you an empty audioclip. Are you sure it's picking up the asset?

    I have never used files as small as the ones you are using, and on the bottom right where it's supposed to show the waveform there isn't one. I assume you've tried reimporting the file?
     
    Last edited: Dec 24, 2015
  4. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    Further testing reveals that if I create a game object with an audio source component, and load the clip in at design time, it behaves properly.

    i.e. This code reports success:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class test3 : MonoBehaviour {
    5.  
    6.   // Use this for initialization
    7.   void Start ()
    8.   {
    9.     Debug.Log( test() );
    10.   }
    11.  
    12.   string test()
    13.   {
    14.       var audioSource = GetComponent<AudioSource>();
    15.  
    16.       AudioClip C = audioSource.clip;
    17.       if( C == null )
    18.         return "C == null";
    19.  
    20.       float [] wav = new float[ C.samples * 2 ];
    21.  
    22.       C.GetData( wav, 0 );  // interleaved
    23.       Debug.Log( "Len: " + wav.Length );
    24.  
    25.       for( int j=0; j<wav.Length; j++ )
    26.         if( wav[j] != 0 )
    27.           return "Success - Found a nonzero!";
    28.  
    29.       return "Error: All 0s";
    30.   }
    31. }
    However, loading in the audio clip at runtime results in failure:

    Code (csharp):
    1.  
    2.   string test2()
    3.   {
    4.     GameObject foo = new GameObject( "tempAudioClip" );
    5.     AudioSource audioSource = foo.AddComponent< AudioSource >();
    6.  
    7.     audioSource.clip = Resources.Load( "01" ) as AudioClip;
    8.     AudioClip C = audioSource.clip;
    9.     if( C == null )
    10.       return "C == null";
    11.  
    12.    float [] wav = new float[ C.samples * 2 ];
    13.  
    14.     C.GetData( wav, 0 );  // interleaved
    15.     Debug.Log( "Len: " + wav.Length );
    16.  
    17.     for( int j=0; j<wav.Length; j++ )
    18.       if( wav[j] != 0 )
    19.         return "Success - Found a nonzero!";
    20.  
    21.     return "Error: All 0s";
    22.   }
    It's worth noting that in all cases the length of the clip is correct. So it is not a case of 'not found'. It is correctly finding it and allocating memory. It is just failing to transfer the data into memory.

    π
     
    Last edited: Dec 24, 2015
  5. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    In my script I am assigning the audioclip to an audiosource and playing it before I do the test.
    In that scenario the data was loaded before I did the test.

    If I don't assign and play the audio then I run into the problem you are experiencing.
    If you check the loadState of the audioclip you will notice that it is not loaded.
    You can load it by calling LoadAudioData.

    Alternatively, the Preload Audio Data checkbox on the audioclip loads the data automatically.
     
    Last edited: Dec 24, 2015
  6. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    I have it working!

    The last move that got it working was going into the import settings for the audio clip, and changing "Load Type" from "compressed in memory" to "decompress on load".

    It occurred to me to try toggling this is setting after looking at the documentation for GetData:

    "Note that with compressed audio files, the sample data can only be retrieved when the Load Type is set to Decompress on Load in the audio importer. If this is not the case then the array will be returned with zeroes for all the sample values."

    I think this qualifies as a docbug, as my audio file is not compressed, yet the same constraint holds.

    Also 'decompress on load' and 'compressed in memory' are a little difficult to interpret for an asset that is a .WAV i.e. not compressed in the first place.

    π

    PS Additionally I have "preload audio data" checked and I am calling LoadAudioData before I call GetData. Probably overkill -- judging from NA-RA-KU's post.
     
  7. drumsareus

    drumsareus

    Joined:
    Apr 9, 2016
    Posts:
    1
    Thanks so much for this tip. I'm running 5.3.4 and this bug was killing me. I'm trying to import either ogg or wav, and I tried every combination of compression / file type. Sometimes it works and sometimes it doesn't. Toggling Compression Format seems to fix it for the first run, then it fails the next time. Is there a bug open for this?