• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JesseSTG · Jun 22, 2019 at 06:26 PM · scripting problemeditor-scriptingassetaudioclipimporter

AudioClip generated by an external process and loaded into the editor has no samples

I'm working on a custom ScriptedImporter -- let's call it LmmsImporter -- that loads project files from the LMMS sequencer as AudioClips. I don't parse the project file; instead, I expect users to install LMMS, configure its location, then store their project files inside their Assets directory. After the asset settings are configured, the import process looks like this:

  1. Create command-line arguments based on the properties offered by LmmsImporter

  2. Get a temporary path inside the project's Temp directory (not the OS's equivalent)

  3. Spawn an LMMS process that renders the imported project file to the path given by (2), with the arguments given by (1).

  4. Wait for the process to complete.

  5. Load the AudioClip at "runtime" (i.e. in an editor script, not as an imported asset) with `UnityWebRequestMultimedia.GetAudioClip`

  6. Clone the AudioClip.

  7. Set the cloned AudioClip to be the LMMS project asset's main object, as described here.

  8. Save the imported asset, then delete the original audio file generated by (3).

Everything up to but not including step 7 works as expected. In fact, even step 7 appears to work as expected. But when I open up the imported AudioClip, it has no samples. When I try to play the preview in the editor, I get this error in the editor console:

Error: Cannot create FMOD::Sound instance for resource , (Error loading file. )
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

What makes this weird is that:

  • The sound is properly rendered by LMMS and saved to the filesystem. Nothing unusual happens here.

  • Playing the AudioClip inside the importer script immediately after it's loaded (i.e. while still in OnImportAsset) with `AudioSource.PlayAtPoint` works as expected.

  • The data is properly cloned; I checked, the arrays are equal.

  • I do not receive any errors or exceptions except those that I've already described, not even from LMMS.

This is an excerpt of my code with the parts I think are most relevant:

using System; using System.Diagnostics; using System.IO; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using UnityEditor; using UnityEditor.Experimental.AssetImporters;

[ScriptedImporter(1, new[] { "mmp", "mmpz" })] public class LmmsImporter : ScriptedImporter { // Properties omitted for brevity

 public override void OnImportAsset(AssetImportContext ctx)
 {
     var relativeTempPath = FileUtil.GetUniqueTempPathInProject();
     var projectDir = Path.GetDirectoryName(Application.dataPath);
     var absoluteAssetPath = Path.Combine(projectDir, assetPath);
     var tempPath = Path.Combine(projectDir, relativeTempPath);

     try
     {
         using (Process lmms = new Process())
         {
             // Process invocation details and error handling omitted for brevity
             // ...one successfully rendered WAV, OGG, or MP3 file later...

             using (var request = UnityWebRequestMultimedia.GetAudioClip(new Uri(tempPath), audioFormat))
             {
                 var handler = request.downloadHandler as DownloadHandlerAudioClip;
                 handler.compressed = false;
                 handler.streamAudio = false;
                 request.timeout = LOAD_MEDIA_TIMEOUT;

                 var operation = request.SendWebRequest();

                 while (!(request.isHttpError || request.isNetworkError || handler.isDone)) ;
                 // Let the request finish, unless we get an error (possibly including a timeout)

                 var clip = DownloadHandlerAudioClip.GetContent(request);

                 clip.LoadAudioData();

                 var assetName = Path.GetFileNameWithoutExtension(assetPath);
                 var clone = AudioClip.Create("Clip", clip.samples, clip.channels, clip.frequency, false);

                 using (var so = new SerializedObject(clone))
                 {
                     var samples = new float[clip.samples * clip.channels];

                     clip.GetData(samples, 0);
                     clone.SetData(samples, 0);

                     // Everything above this line works fine

                     ctx.AddObjectToAsset("AudioClip", clone);
                     ctx.SetMainObject(clone);

                     so.ApplyModifiedProperties();

                     EditorUtility.SetDirty(clone);
                 }
             }
         }
     }
     catch (Exception e)
     {
         ctx.LogImportError($"Failed import with {e.GetType()}: {e.Message}");
     }
 }
 private const int LOAD_MEDIA_TIMEOUT = 5; // seconds

}

I'm using Unity 2019.1.7f1 Personal, on Ubuntu 19.04. Is this a bug, or am I doing something wrong?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JesseSTG · Jun 25, 2019 at 03:16 PM

It appears that this is a bug. I've reported this to Unity and they have reproduced it. The ticket can be found here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

198 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Connecting oracle with unity tutorial, Steps or a way? 1 Answer

Adding an element to List a .asset file of ScriptableObject 0 Answers

How to : Editor.OnPreviewGUI Implementation 0 Answers

asset wont accept attached script 0 Answers

Editor class "Texture Importer" + Apply import settings question 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges