前段时间做了一个变声软件,思路是麦克风录入声音保存到本地,然后把声音转化成与之对应类型的声音。
做的时候在网上看了好多博客,也去做语音识别大厂官网去查阅了相关资料,然后利用这个插件VoiceChangerFilter简单的完成了一些基础功能,如果想要完全智能化,推荐还是购买对接(例如:科大讯飞等),官网上边都有试用版本也可以接入SDK尝试一下。
//字节化成音频
public AudioClip ConvertBytesToClip(byte[] _bs)
{
//clip.length;
float[] samples = new float[_bs.Length / 2];
float rescaleFactor = 32767;
short st = 0;
float ft = 0;
for (int i = 0; i < _bs.Length; i += 2)
{
st = BitConverter.ToInt16(_bs, i);
ft = st / rescaleFactor;
samples[i / 2] = ft;
}
AudioClip cp = AudioClip.Create("clip", samples.Length, 1, audioFrequency, false);
cp.SetData(samples, 0);
return cp;
}
//开始录音
IEnumerator InitStart()
{
yield return null;
canRecord = true;
source.clip = null;
if (Microphone.devices.Length == 0)
{
canRecord = false;
//print("没有声音输入设备");
Debug.LogWarning("没有声音输入设备");
yield break;
}
sendt = 1.0f / SendRate;
recording = Microphone.Start(null, false, recordLengh, audioFrequency);
}
//停止录音
public void stopLuYin()
{
if (_IsStartRecord)
{
_IsStartRecord = false;
Microphone.End(null);
List<byte> lss = new List<byte>();
sendBuffer.ForEach((s) =>
{
lss.AddRange(s);
print(s);
});
if (lss.Count <= 0) return;
source.clip = ConvertBytesToClip(lss.ToArray());
//source.clip = recording;
SavWav.Save("test11", source.clip);
//MediaCtr.OBJS["变声按钮"].SetActive(true);
#if UNITY_IOS
// ... iOS项目编译
#elif UNITY_ANDROID
// ... apk 或 iOS项目编译
#elif UNITY_EDITOR
AssetDatabase.Refresh();
#endif
sendBuffer.Clear();
VoiceChangerDemoScene.instance.sampleVoiceClip = source.clip;
print("停止录音");
//MediaCtr.instance.OverLuYin();
}
else
{
print("已经停止录音了");
return;
}
MediaCtr.instance.OverLuYin();
}
效果图: