Mixedrealitytoolkit-unity: Expose MicStreamSelector to Unity

Created on 30 Jun 2016  路  12Comments  路  Source: microsoft/MixedRealityToolkit-Unity

Hi guys

Just wondering if someone has been working on making the HoloToolKit MicStreamSelector available from Unity ?
Something we would need here soonish so I thought I should ask in case there's already something available privately.

Thanks in advance
-s

All 12 comments

@andymule are you working on this currently?

Yeah should be done and checked in next week

Great news !
On Friday last week I gave it a quick go, here is the c# bindings that seem to work except for MicGetFrame that I ignored.
You probably have done that already anyway but just in case here it is:

``` c#
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

namespace HoloToolkit.Unity
{
public class MicStreamSelector
{
#region Public APIs

    public static int MicInitialize(int category, int fftsize, int numBuffers, int samplerate)
    {
        return DLLImports.MicInitialize(category, fftsize, numBuffers, samplerate);
    }

    public static int MicStartStream(bool keepData)
    {
        return DLLImports.MicStartStream(keepData);
    }

    public static int MicStopStream()
    {
        return DLLImports.MicStopStream();
    }

    public static int MicStartRecording(String filename)
    {
        return DLLImports.MicStartRecording(filename);
    }

    public static int MicPause()
    {
        return DLLImports.MicPause();
    }

    public static int MicResume()
    {
        return DLLImports.MicResume();
    }

    public static int MicSetGain(float gain)
    {
        return DLLImports.MicSetGain(gain);
    }

    public static int MicDestroy()
    {
        return DLLImports.MicDestroy();
    }

    #endregion

    #region Internal

    /// <summary>
    /// Raw MicStreamSelector.dll imports
    /// </summary>
    private class DLLImports
    {
        [DllImport("MicStreamSelector")]
        public static extern int MicInitialize(
            [In] int category,
            [In] int fftsize,
            [In] int numBuffers,
            [In] int samplerate);

        [DllImport("MicStreamSelector")]
        public static extern int MicStartStream(
            [In] bool keepData);

        [DllImport("MicStreamSelector")]
        public static extern int MicStopStream();

        [DllImport("MicStreamSelector")]
        public static extern int MicStartRecording(
            [In] String filename);

        [DllImport("MicStreamSelector")]
        public static extern int MicStopRecording(
            [In] String path);

        /*
        [DllImport("MicStreamSelector")]
        public static extern int MicGetFrame(// float * 
            [In] int length,
            [In] int numchannels
            );
        */

        [DllImport("MicStreamSelector")]
        public static extern int MicPause();

        [DllImport("MicStreamSelector")]
        public static extern int MicResume();

        [DllImport("MicStreamSelector")]
        public static extern int MicSetGain(
            [In] float gain);

        [DllImport("MicStreamSelector")]
        public static extern int MicDestroy();
    }

    #endregion
}

}
```

Here is a new version kindly improved by Dave Sullivan here in London

``` c#
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

if NETFX_CORE

using Windows.Storage;

endif

namespace HoloToolkit.Unity
{
public class MicStreamSelector
{
public enum ErrorCode
{
ALREADY_RUNNING = -10,
NO_AUDIO_DEVICE,
NO_INPUT_DEVICE,
ALREADY_RECORDING,
GRAPH_NOT_EXIST,
CHANNEL_COUNT_MISMATCH,
FILE_CREATION_PERMISSION_ERROR,
NOT_IMPLEMENTED = -1,
SUCCESS = 0
}

region Public APIs

    public static ErrorCode MicInitialize(int category, int fftsize, int numBuffers, int samplerate)
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicInitialize(category, fftsize, numBuffers, samplerate);

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicStartStream(bool keepData)
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicStartStream(keepData);

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicStopStream()
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicStopStream();

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicStartRecording(String filename)
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicStartRecording(filename);

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicStopRecording(ref String filename)
    {

if NETFX_CORE

        string originalFilename = filename;

        IntPtr msgBuffer = Marshal.AllocHGlobal(256);

        try
        {
            Marshal.WriteByte(msgBuffer, 0);
            DLLImports.MicStopRecording(msgBuffer);
            filename = Marshal.PtrToStringAnsi(msgBuffer);
        }
        finally
        {
            Marshal.FreeHGlobal(msgBuffer);
        }

        //Debug.LogFormat("New audio filename is {0}", filename);
        //filename = System.IO.Path.Combine(KnownFolders.MusicLibrary.Path, filename);

        var fileInfo = new FileInfo(filename);
        if (fileInfo.Exists)
        {
            return ErrorCode.SUCCESS;
        }

        return ErrorCode.FILE_CREATION_PERMISSION_ERROR;

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicPause()
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicPause();

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicResume()
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicResume();

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicSetGain(float gain)
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicSetGain(gain);

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

    public static ErrorCode MicDestroy()
    {

if NETFX_CORE

        return (ErrorCode)DLLImports.MicDestroy();

else

        return ErrorCode.NOT_IMPLEMENTED;

endif

    }

endregion

region Internal

    /// <summary>
    /// Raw MicStreamSelector.dll imports
    /// </summary>
    private class DLLImports
    {
        [DllImport("MicStreamSelector")]
        public static extern int MicInitialize(
            [In] int category,
            [In] int fftsize,
            [In] int numBuffers,
            [In] int samplerate);

        [DllImport("MicStreamSelector")]
        public static extern int MicStartStream(
            [In] bool keepData);

        [DllImport("MicStreamSelector")]
        public static extern int MicStopStream();

        [DllImport("MicStreamSelector")]
        public static extern int MicStartRecording(
            [In] String filename);

        [DllImport("MicStreamSelector")]
        public static extern void MicStopRecording(
            [In] IntPtr path);

        /*
        [DllImport("MicStreamSelector")]
        public static extern int MicGetFrame(// float * 
            [In] int length,
            [In] int numchannels
            );
        */

        [DllImport("MicStreamSelector")]
        public static extern int MicPause();

        [DllImport("MicStreamSelector")]
        public static extern int MicResume();

        [DllImport("MicStreamSelector")]
        public static extern int MicSetGain(
            [In] float gain);

        [DllImport("MicStreamSelector")]
        public static extern int MicDestroy();
    }

endregion

}

}
```

@andymule Looks like maybe we've got a couple implementations here. Reasons to wait on yours or should @stbertou just make a PR?

Thanks for this, Dave and Stephane. The MicStream code is still under some internal development, so I鈥檓 not sure how much of the changes will make it into HoloToolkit, but we鈥檒l look at it. In particular, I鈥檓 not sure why it鈥檚 returning NOT_IMPLEMENTED when you鈥檙e not running NETFX_CORE, as the code works fine running in the Unity editor on Windows 10, and having the code run in the Unity editor makes development much easier.

I'll let @Davesullivanatwork comment here but he told me the DLL was somehow not working from within the editor

@DanHolbert mind if I clean up your comment? It's full of the reply text from the email and is cluttering up this thread.

Done. I didn't realize that email replies were generating GitHub comments.

Regarding the DLL not working in editor, I know it's been under some revision lately. It's possible some of the revision addressed this, although I'd lean toward not. @Davesullivanatwork, do you have any details on what wasn't working?

@andymule is this done? Can we get a PR?

Hey guys, didn't see all this talk until now. There are official PRs out on this repo and a matching one going into the main Holotoolkit. It should all be good to go everywhere. Please use excessively and make changes as you see fit. Cheers!

Thanks @andymule! - #117

Was this page helpful?
0 / 5 - 0 ratings