Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose MicStreamSelector to Unity #86

Closed
stbertou opened this issue Jun 30, 2016 · 12 comments
Closed

Expose MicStreamSelector to Unity #86

stbertou opened this issue Jun 30, 2016 · 12 comments
Assignees

Comments

@stbertou
Copy link

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

@NeerajW
Copy link

NeerajW commented Jul 1, 2016

@andymule are you working on this currently?

@andymule
Copy link

andymule commented Jul 1, 2016

Yeah should be done and checked in next week

@stbertou
Copy link
Author

stbertou commented Jul 4, 2016

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:

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
    }
}

@stbertou
Copy link
Author

stbertou commented Jul 6, 2016

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

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
    }
}

@jwittner
Copy link
Member

jwittner commented Jul 6, 2016

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

@DanHolbert
Copy link

DanHolbert commented Jul 6, 2016

Thanks for this, Dave and Stephane. The MicStream code is still under some internal development, so I’m not sure how much of the changes will make it into HoloToolkit, but we’ll look at it. In particular, I’m not sure why it’s returning NOT_IMPLEMENTED when you’re 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.

@stbertou
Copy link
Author

stbertou commented Jul 7, 2016

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

@jwittner
Copy link
Member

jwittner commented Jul 7, 2016

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

@DanHolbert
Copy link

DanHolbert commented Jul 7, 2016

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?

@jwittner
Copy link
Member

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

@andymule
Copy link

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!

@jwittner
Copy link
Member

Thanks @andymule! - #117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants