Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C# 4.0

Fun with Google Speech Recognition Service

Rate me:
Please Sign up or sign in to vote.
4.14/5 (7 votes)
22 Apr 2024CPOL2 min read 81.5K   10.3K   21  
Create text issues in Redmine by using Google speech recognition service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace vtmblip.wave
{
    public class Recorder
    {
        private unsafe void DataArrived(IntPtr data, int size)
        {
            cb.Upload(data.ToPointer(), size);
        }

        WaveLib.WaveInRecorder m_Recorder;
        //VorbisEnc.VorbisEnc ve;
        VorbisEnc.FlacEncoder ve;
        VorbisEnc.CircleBuffer cb;
        IntPtr filepath;

        public unsafe Recorder(string tempfilepath)
        {
            cb = new VorbisEnc.CircleBuffer();
            //ve = new VorbisEnc.VorbisEnc();
            ve = new VorbisEnc.FlacEncoder();
            //byte[] data = Encoding.GetBytes(tempfilepath);
            //filepath = System.Runtime.InteropServices.Marshal.AllocHGlobal(data.Length + 1);
            
            //System.Runtime.InteropServices.Marshal.Copy(data, 0, filepath, data.Length);
            //ve.Initialise((sbyte*)filepath.ToPointer());
            ve.Initialise((sbyte*)System.Runtime.InteropServices.Marshal.StringToHGlobalUni(tempfilepath).ToPointer());
            //System.Runtime.InteropServices.Marshal.FreeHGlobal(filepath);//need anymore?

            System.Threading.Thread th = new System.Threading.Thread(EncodeData);

            WaveLib.WaveFormat fmt = new WaveLib.WaveFormat(16000, 16, 1);
            m_Recorder = new WaveLib.WaveInRecorder(-1, fmt, 4096, 4, new WaveLib.BufferDoneEventHandler(DataArrived));

            th.Start();
        }
        ~Recorder()
        {
            //System.Runtime.InteropServices.Marshal.FreeHGlobal(filepath);
        }
        bool StopThread;
        public bool AllDone = false;
        public void Stop()
        {
            m_Recorder.Dispose();
            StopThread = true;
            cb.Dispose();
        }
        unsafe void EncodeData()
        {
            IntPtr datax = System.Runtime.InteropServices.Marshal.AllocHGlobal(4096);
            sbyte* data = (sbyte*)datax.ToPointer();
            while (!StopThread)
            {
                System.Threading.Thread.Sleep(10);
                while (cb.getNeedForUpdate() < 4096 * 4)
                {
                    cb.Download(data, 4096);
                    ve.Encode(data, 4096);
                }
            }
            System.Runtime.InteropServices.Marshal.FreeHGlobal(datax);
            ve.Close();

            AllDone = true;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions