c# - 如何在 Unity3D 中通过麦克风流式传输实时音频?

标签 c# unity3d network-programming audio-streaming microphone

到目前为止,这是主要代码的一部分(网络是单独完成的):

void Update()
{
    if (Network.connections.Length > 0) 
    {
        audioFloat = new float[audioInfo.clip.samples * audioInfo.clip.channels];
        audioInfo.clip.GetData (audioFloat, 0);
        networkView.RPC("PlayMicrophone", RPCMode.Others, ToByteArray(audioFloat), audioInfo.clip.channels);
    }
}

[RPC]
void PlayMicrophone(byte[] ba, int c)
{
    float[] flar = ToFloatArray (ba);
    audio.clip = AudioClip.Create ("test", flar.Length, c, 44100, true, false);

    audio.clip.SetData (flar, 0);
    audio.loop = true;
    while (!(Microphone.GetPosition("") > 0)) {}
    audio.Play();
}

void OnConnectedToServer()
{
    audioInfo.clip = Microphone.Start(null, true, 100, 44100);
}

// When you are the server and the client connected to 
void OnPlayerConnected(NetworkPlayer player)
{
    audioInfo.clip = Microphone.Start(null, true, 100, 44100);
}

public byte[] ToByteArray(float[] floatArray) {
    int len = floatArray.Length * 4;
    byte[] byteArray = new byte[len];
    int pos = 0;
    foreach (float f in floatArray) {
        byte[] data = System.BitConverter.GetBytes(f);
        System.Array.Copy(data, 0, byteArray, pos, 4);
        pos += 4;
    }
    return byteArray;
}

public float[] ToFloatArray(byte[] byteArray) {
    int len = byteArray.Length / 4;
    float[] floatArray = new float[len];
    for (int i = 0; i < byteArray.Length; i+=4) {
        floatArray[i/4] = System.BitConverter.ToSingle(byteArray, i);
    }
    return floatArray;
}

我知道它可能不会完美播放,但我期待听到哪怕是一秒钟的最轻微的声音,但它从未发生过。数据似乎可以通过 RPC 正常发送,但无法播放音频。

Update() 函数中,它将不断调用 RPC 以开始从麦克风播放。这发生在作为客户端或服务器建立连接之后。

由于除了字节数组之外不能发送任何数组,我将音频数据作为 float 组获取,然后转换为字节数组以发送给连接的其他人。运行代码时没有错误,但声音仍然没有播放。

想法是从各自玩家的麦克风收集完全实时流式传输 2 路音频。我觉得即使缓冲区只有 100 秒,它仍然应该播放声音到那个点。

当时机成熟时,我可能最终会为此使用循环缓冲区。声音还没有播放。

我尝试使用 Here 中的示例和 Here仍然无法产生结果。

最佳答案

我决定回到发布的代码here并尝试不同的东西。我所做的唯一区别是使用 FixedUpdate 而不是 Update 并在建立连接时在 FixedUpdate 内启动麦克风。使用 Update 时有很多卡顿,所以我选择以 0.5 秒的间隔使用 FixedUpdate,它起作用了。

int lastSample = 0;
void FixedUpdate()
{
    // If there is a connection
    if (Network.connections.Length > 0)
    {
        if (notRecording)
        {
            notRecording = false;
            sendingClip = Microphone.Start(null, true, 100, FREQUENCY);
            sending = true;
        }
        else if(sending)
        {
            int pos = Microphone.GetPosition(null);
            int diff = pos-lastSample;

            if (diff > 0)
            {
                float[] samples = new float[diff * sendingClip.channels];
                sendingClip.GetData (samples, lastSample);
                byte[] ba = ToByteArray (samples);
                networkView.RPC ("Send", RPCMode.Others, ba, sendingClip.channels);
                Debug.Log(Microphone.GetPosition(null).ToString());
            }
            lastSample = pos;
        }
    }
}

[RPC]
public void Send(byte[] ba, int chan) {
    float[] f = ToFloatArray(ba);
    audio.clip = AudioClip.Create("", f.Length, chan, FREQUENCY,true,false);
    audio.clip.SetData(f, 0);
    if (!audio.isPlaying) audio.Play();

}
// Used to convert the audio clip float array to bytes
public byte[] ToByteArray(float[] floatArray) {
    int len = floatArray.Length * 4;
    byte[] byteArray = new byte[len];
    int pos = 0;
    foreach (float f in floatArray) {
        byte[] data = System.BitConverter.GetBytes(f);
        System.Array.Copy(data, 0, byteArray, pos, 4);
        pos += 4;
    }
    return byteArray;
}
// Used to convert the byte array to float array for the audio clip
public float[] ToFloatArray(byte[] byteArray) {
    int len = byteArray.Length / 4;
    float[] floatArray = new float[len];
    for (int i = 0; i < byteArray.Length; i+=4) {
        floatArray[i/4] = System.BitConverter.ToSingle(byteArray, i);
    }
    return floatArray;
}

关于c# - 如何在 Unity3D 中通过麦克风流式传输实时音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31171714/

相关文章:

c# - 通过 Resharper 或 DevExpress 的方法进行依赖注入(inject)

c# - 将工作日缩写字符串(包括范围)转换为 List<DayOfWeek>

c# - Unity3D防止与鼠标事件发生冲突

c - 打印数据包内容的十六进制值

c# - 为什么使用 WebClient 的 UploadFileAsync 没有错误通知?

c# - 如何确定为接口(interface)的不同实现调用哪个存储库?

c# - 如何返回可为空的结构?

c# - 如何计算两点之间的值百分比值

network-programming - 将邮件卡在ZeroMQ中

c++ - 开源和跨平台多人游戏/网络库?