-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Comments
@andymule are you working on this currently? |
Yeah should be done and checked in next week |
Great news ! 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 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
}
} |
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. |
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! |
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
The text was updated successfully, but these errors were encountered: