With Unity wrote a similar Tom cat auto-detect speaking function

Source: Internet
Author: User
Tags float max

Simply add the script to the camera.

Here is the script I wrote, there is a problem because it is automatic call detection call recording Unity call delay will appear some delay small problem, you can refer to my code change to do a button click on the recording end play recording or can be achieved

Using system.collections.generic;using unityengine;using system.collections;public class tom2:monobehaviour{//publi    c float Power = 1;    public float min = 0; Public AudioClip audioclip = null;//audioclip Audio clip//public Audiosource audiosource;//audiosource audio source public Boo    L audioclipworking = false;    public string audioisplaying;    public bool microphoneworking = FALSE; Private list<float> sampleslist;    Play data store public float Samplesmaxvalue;    public int cliplength;    private void Start () {sampleslist = new list<float> ();///Not fixed startrecord ();        private void Update () {audioisplaying = Audio.isPlaying.ToString ();        Screen.sleeptimeout = Sleeptimeout.neversleep; if (microphoneworking) {if (!                Microphone.isrecording (null)) {microphoneworking = false;            Judgerecord (); }} if (audioclipworking) {if (audio.IsPlaying = = False) {sampleslist.clear ();                Startrecord ();            Audioclipworking = false;        }}} private void Startrecord () {audio.clip = Microphone.start (null, FALSE, 1, 44100);        while (microphone.getposition (null) <= 0);    Microphoneworking = true;        } public void Judgerecord () {float max = 0; Float[] Tempsamples;        Temporary data storage Tempsamples = new float[audio.clip.samples * Audio.clip.channels];        Audio.clip.GetData (tempsamples, 0);            foreach (float s in tempsamples) {float m = mathf.abs (s);        if (Max < m) {max = m;//The maximum value of the audio data just recorded}} samplesmaxvalue = Max;            if (Max > 0.1) {//Judge someone to speak Startrecord ();        foreach (float e in tempsamples) {Sampleslist.add (e);//Save Data}//Record another second }       else {//unmanned speech if (audio.clip! = NULL && sampleslist! = null) {  if (Sampleslist.count >) {audioclip myclip = audioclip.create ("Tom",                    Sampleslist.count, 1, 44100, false, false);                    Myclip.setdata (Sampleslist.toarray (), 0);                                       Audio.clip = Myclip;                    Audio.loop = false;                    Audio.pitch = 1.2f; Audio.                    Play ();                Audioclipworking = true;                } else {Startrecord ();            }} else {Startrecord (); }}} private void Ongui () {GUI.        Label (New Rect (1, screen.width, +), "Samplesmaxvalue:" + samplesmaxvalue); Gui.        Label (New Rect (2, Screen.width, +), "Cliplength:" + cliplength); Gui. Label (NewRect (3, Screen.width, +), "audioisplaying:" + audioisplaying); Gui.        Label (New Rect (4, Screen.width, +), "audioclipworking:" + audioclipworking); Gui.        Label (New Rect (5, Screen.width, +), "microphoneworking:" + microphoneworking); Gui.        Label (New Rect (6, Screen.width, +), "Audio.clip.samples:" + audio.clip.samples); Gui.        Label (New Rect (7, Screen.width, +), "Audio.clip.length:" + audio.clip.length); Gui.        Label (New Rect (8, Screen.width, +), "Audio.clip.channels:" + audio.clip.channels); Gui.    Label (New Rect (8, Screen.width, +), "microphone.getposition:" + microphone.getposition (null)); }}

With Unity wrote a similar Tom cat auto-detect speaking function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.