hengyang_client/wb_unity_pro/Assets/Scripts/Platform/Voice.cs

162 lines
4.5 KiB
C#
Raw Normal View History

2025-06-10 16:11:48 +08:00
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Collections.Generic;
using Game;
using LuaInterface;
public static class Voice
{
public const int SamplingRate = 8000;
public static string VoiceUpload;
public static string VoiceDownload;
static AudioClip _clip;
static Dictionary<string, string> cacheRecordUrlMap = new Dictionary<string, string>();
static Dictionary<string, AudioClip> cacheRecordClipMap = new Dictionary<string, AudioClip>();
public static void BeginRecord()
{
Microphone.End(null);
2025-06-17 20:26:53 +08:00
Debug.Log("BeginRecord");
2025-06-10 16:11:48 +08:00
_clip = Microphone.Start(null, false, 20, SamplingRate);
}
public static bool EndRecord(string desk, string userid, LuaFunction callback)
{
int audioLength;
int lastPos = Microphone.GetPosition(null);
if (Microphone.IsRecording(null))
{
audioLength = lastPos / SamplingRate;
}
else
{
audioLength = 10;
}
var pos = Microphone.GetPosition(null);
Microphone.End(null);
if (audioLength < 1.0f)
{
_clip = null;
return false;
}
2025-06-17 20:26:53 +08:00
Debug.Log("pos");
Debug.Log(pos);
2025-06-10 16:11:48 +08:00
var samples = new float[pos];
_clip.GetData(samples, 0);
var mfilename = string.Concat(DateTime.Now.Ticks + UnityEngine.Random.Range(100, 999), desk, userid);
var bytes = new byte[samples.Length * 2];
int rescaleFactor = 32767; //to convert float to Int16
for (int i = 0; i < samples.Length; i++)
{
short temshort = (short)(samples[i] * rescaleFactor);
byte[] temdata = BitConverter.GetBytes(temshort);
bytes[i * 2] = temdata[0];
bytes[i * 2 + 1] = temdata[1];
}
string url = null;
cacheRecordUrlMap.TryGetValue(userid, out url);
if (url != mfilename)
{
if (!string.IsNullOrEmpty(url))
{
cacheRecordClipMap.Remove(url);
}
AudioClip clip = AudioClip.Create(mfilename, samples.Length, 1, SamplingRate, false);
clip.SetData(samples, 0);
cacheRecordClipMap[mfilename] = clip;
cacheRecordUrlMap[userid] = mfilename;
}
_clip = null;
GameApplication.Instance.StartCoroutine(UpLoad(bytes, mfilename, callback));
return true;
}
static IEnumerator UpLoad(byte[] data, string filename, LuaFunction callback)
{
var cdata = BestHTTP.Decompression.Zlib.DeflateStream.CompressBuffer(data);
WWWForm form = new WWWForm();
form.AddField("name", filename);
form.AddBinaryData("post", cdata, filename);
WWW www = new WWW(Voice.VoiceUpload, form);
yield return www;
if (callback != null)
{
callback.Call(filename);
callback.Dispose();
}
}
public static void CrealRecord()
{
if (_clip != null)
{
_clip.UnloadAudioData();
_clip = null;
}
cacheRecordUrlMap.Clear();
cacheRecordClipMap.Clear();
}
public static void DownLoad(string url, LuaFunction callback)
{
AudioClip clip = null;
cacheRecordClipMap.TryGetValue(url, out clip);
if (clip != null)
{
if (callback != null)
{
callback.Call(clip);
callback.Dispose();
}
return;
}
GameApplication.Instance.StartCoroutine(DownloadVoice(url, callback));
}
static IEnumerator DownloadVoice(string url, LuaFunction callback)
{
string murl = VoiceDownload + url;
WWW www = new WWW(murl);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
var data = www.bytes;
data = BestHTTP.Decompression.Zlib.DeflateStream.UncompressBuffer(data);
AudioClip clip = AudioClip.Create(url, data.Length / 2, 1, SamplingRate, false);
float[] samples = new float[data.Length / 2];
float rescaleFactor = 32767;
for (int i = 0; i < samples.Length; i++)
{
samples[i] = BitConverter.ToInt16(data, i * 2) / rescaleFactor;
}
clip.SetData(samples, 0);
clip.name = url;
if (callback != null)
{
callback.Call(clip);
callback.Dispose();
}
}
}
}