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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

NatMic - Native Microphone API

Discussion in 'Assets and Asset Store' started by Lanre, Sep 17, 2016.

  1. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    177
  2. tomas12343

    tomas12343

    Joined:
    Dec 19, 2019
    Posts:
    10
    Thank you for your responce!

    Unfortunately the app hasn't been updated for 2 years and I don't think is compatible with the current version of Unity. Do you have anything else to suggest?
     
  3. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    177
    I believe it is compatible or it wouldn't be on the asset store. I'm using 2019.2.11f1.

    Really, as I say, I found using Unity's Microphone class directly worked for me - and RARE app does that, so you can use its code as an example.

    https://docs.unity3d.com/ScriptReference/Microphone.html
     
  4. tomas12343

    tomas12343

    Joined:
    Dec 19, 2019
    Posts:
    10
    I have already used the microphone class but it has a lot of latency. That's why I was searching for an alternative..
     
  5. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    35
    Hello! Im trying this out and works well so far. But how do i get it to play for android/ios. The sample script called VoiceRecorder.cs has this line

    Code (CSharp):
    1. // Play the recording in the scene
    2. Application.OpenURL(audioRecorder.FilePath);
    which does nothing on my phone but works well in editor. Advice?
     
  6. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    35
    Nvm. Got it to work using UWR :

    Code (CSharp):
    1. IEnumerator GetAudio(string url)
    2.     {
    3.         // Start a download of the given URL
    4.         string extension = Path.GetExtension(url);
    5.  
    6.         UnityWebRequest www = new UnityWebRequest();
    7.  
    8.        if (extension == ".wav")
    9.         {
    10.             www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV);
    11.         }
    12.         // Wait for download to complete
    13.         yield return www.SendWebRequest();
    14.  
    15.         if (www.isNetworkError)
    16.         {
    17.             Debug.Log(www.error);
    18.         }
    19.         else
    20.         {
    21.             audioClip = DownloadHandlerAudioClip.GetContent(www);
    22.         }
    23.         // having audio here. Do with it whatever you want...
    24.         audioSource.clip = audioClip;
    25.         audioSource.Play();
    26.     }
     
    Lanre likes this.
  7. ndmrzk

    ndmrzk

    Joined:
    Jul 25, 2018
    Posts:
    35
    But why does the permission prompt not come out? Does it not come with? I have to create it myself? This plugin is $50 just to record audio but no permission? The documentation needs work.
     
  8. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    NatMic ducks audio coming from other apps.
     
    edee1337 likes this.
  9. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    Unity has permissions API's already, so NatMic doesn't make it redundant. This also applies on Android only, as iOS will automatically request permissions the first time the microphone is accessed.
     
  10. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    Hey Colin. Sorry for the radio silence; I've been caught up in so much work across several projects (including NatMic). NatMic and NatCam are being brought together into a single API as they are two parts of essentially the same thing; I'm working on a mixer device implementation which will hopefully work properly; and also considering building some infrastructure to make NatMic pipe audio into an AudioClip. Again I apologize for the silence; it's been very difficult keeping up with it all.
     
    Colin_MacLeod likes this.
  11. tgrayston

    tgrayston

    Joined:
    Aug 30, 2012
    Posts:
    16
    Hi @Lanre, does/could NatMic allow selection of which specific microphone to use on Android/iOS? iPhone's (and probably most other phones too) have 3 mics - base (for phone calls and voice recording), front (for front-facing videos) and rear (for rear-facing videos).

    Unity's Microphone class only reveals one device ("iPhone audio input") which seems to use the base microphone. My use-case involves placing the device on a piano's music stand which tends to muffle that microphone input so I'd like to try others.

    Also I'm using a pitch detection algorithm which usually works ok, but sometimes performs poorly. When I listen into what's being recorded it's often garbled. I'm guessing NatMic could help with that, assuming I can hook it up to the pitch detection algorithm I'm using...?

    Thanks!
     
  12. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    iOS does not give granular access to each of these microphones. See the discussion on this page. Or do you have a source that mentions otherwise?
    NatMic provides the microphone audio as floating-point linear PCM sample buffers (float[]), as they are provided by the native API. If your pitch detection algorithm can take this format as input, then you're all set.
     
  13. tgrayston

    tgrayston

    Joined:
    Aug 30, 2012
    Posts:
    16
    Thanks Lanre. Having selected the built-in microphone port it appears you can then select the inputDataSource to feed to that port. From the See Also section of the page you linked to, setInputDataSource:
    Haven't tried any of this, have very little native coding experience!

    Thanks, I'll double check what the algorithm uses. :)
     
    Lanre likes this.
  14. vn_man

    vn_man

    Joined:
    Jun 7, 2017
    Posts:
    23
    hi Lanre,
    How do I change volume of AudioDevice?
    btw, I have a scene with an AudioSource, when mute AudioSource, record sound OK, but when unmute the sound is noise and looping
    so NatMic can work with AudioSource or not?
     
  15. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    You can't change the volume of an AudioDevice; there isn't such a concept. As for working with an audio source, you would need to record from a microphone that uses echo cancellation in order to prevent the audio feedback you are hearing.
     
  16. vn_man

    vn_man

    Joined:
    Jun 7, 2017
    Posts:
    23
    "You can't change the volume of an AudioDevice; there isn't such a concept" - OK, but Can mute/unmute AudioDevice while recording? if can, how to do it?
    ===
    "you would need to record from a microphone that uses echo cancellation"-> it is setting hardware or setting in code?
    thanks so much!
     
  17. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    Each device has an `EchoCancellation` property. Check the available devices to see if there is any device that has this property as true.
     
  18. vn_man

    vn_man

    Joined:
    Jun 7, 2017
    Posts:
    23
    Can mute/unmute AudioDevice while recording? if can, how to do it?
    thanks!
     
    Lanre likes this.
  19. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    This isn't trivial. You would have to manually zero out the sample buffers coming in from the microphone.
     
  20. vn_man

    vn_man

    Joined:
    Jun 7, 2017
    Posts:
    23
    thank Lanre,
    I change code to:

    public void OnSampleBuffer(float[] sampleBuffer, int sampleRate, int channelCount, long timestamp)
    {
    if (mute)
    Array.Clear(sampleBuffer, 0, sampleBuffer.Length);
    videoRecorder.CommitSamples(sampleBuffer, recordingClock.Timestamp);
    }

    and it work ok :D!
     
    Lanre likes this.
  21. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    Make sure to upgrade to NatDevice 1.0.0 (from NatMic) and NatCorder 1.7.0 on the Asset Store.
     
  22. purplelilgirlmakesgames

    purplelilgirlmakesgames

    Joined:
    Jan 9, 2015
    Posts:
    9
    Hi,

    Is NatMic no longer supported? I updated NatCorder, is there a way for me to still use NatMic for audio, or do I have to buy NatDevice?

    Thanks.
     
  23. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    NatMic (along with NatCam) has been succeeded by NatDevice. The upgrade from either NatCam or NatMic to NatDevice is heavily discounted for current customers.
     
  24. purplelilgirlmakesgames

    purplelilgirlmakesgames

    Joined:
    Jan 9, 2015
    Posts:
    9
    thats great, thank you.
     
    Lanre likes this.
  25. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    867
    Im using NatCorder and NatMIC on IOS 13.6 on iPhone 7, when i start a recording the whole UI freezes until the recording is over, but it actually records the camera and mic. This works perfectly on Android without any freeze.

    Can anybody tell me how to fix it?
     
    Last edited: Jul 28, 2020
  26. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    This is a bug in Unity 2018. It was fixed in Unity 2019.2.
     
  27. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    867
    So you mean to tell me your product never worked for IOS the whole time until release 2019.2?
     
    Last edited: Jul 30, 2020
  28. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    The seemingly affected some Unity 2018 builds and not others. At the time I was using 2018.3 and didn't face any issues, whereas some other developers did. I finally got word that 2019.2 fixed it, and upgraded. As I said, this is a bug caused by Unity Engine; it's out of my control. I have no clue what causes it, and no clue how to fix it. The exact same code works in 2019.2+.
     
  29. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    867
    Great excuse you have there, some people it worked for 2018.3 and others dont, wow cool.

    I bought NATCORDER and NATMIC more than a year ago thinking it would work for Android and IOS both, i been using it on Android only and it worked fine for over a year. I was just able to buy a mac and iPhone now only for the purpose of converting it to IOS, but now finding out i just waste my time and money because it never worked for IOS the whole time, really cool. I guess the joke is on me.

    My app cant be upgraded to 2019, because many packages i bought dont work on 2019. So i guess im screwed. Thanks
     
  30. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    I think a larger concern of yours should be your inability to upgrade to 2019, given that the 2019 LTS has been released and we're moving to the 2020 release cycle. You're going to have to upgrade at some point, right? As I mentioned, the issue has nothing to do with NatCorder; all we're doing is re-rendering the game camera to a RenderTexture so we can grab video frames to record (all the code is right there in the CameraInput script, you can look at it and tinker with it). And as I've also mentioned, Unity fixed whatever was causing the issue in 2019.2. I'm not sure what you expect me to do.
     
  31. digitalbath101

    digitalbath101

    Joined:
    Feb 12, 2013
    Posts:
    1
    Hi @Lanre, I've just bought NatSuite and am looking to use the Native mic support to drive animations on an iOS device.

    can you recommend the best way to get FFT data from the sampleBuffer array in the OnSampleBuffer method?

    thanks
     
  32. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950
    Hey there. NatDevice doesn't have any FFT functions. You can use open source projects like this one to get the spectrum data from the audio stream.
     
  33. Fangh

    Fangh

    Joined:
    Apr 19, 2013
    Posts:
    217
  34. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,950