コード例 #1
0
        private async Task <string> SpeechRecognizeAsync(string filename)
        {
            //将wav文件post到服务器进行语音识别
            //将识别回来的文本写入文本框
            wav wave = await WaveAccess(filename);

            Int16[] wavs = wave.wavs;

            int    fs       = wave.fs;
            string wavs_str = "";

            string[] tmp_strs = new string[wavs.Length];
            for (int i = 0; i < wavs.Length; i++)
            {
                //tmp_strs[i] = "&wavs=" + wavs[i].ToString();
                tmp_strs[i] = wavs[i].ToString();
            }
            wavs_str = string.Join("&wavs=", tmp_strs);


            //string r = await PostDataAsync(url, "qwertasd", wavs_str, fs.ToString());

            string r = await post2(url, "qwertasd", fs.ToString(), wavs_str);

            return(r);
        }
コード例 #2
0
        /// <summary>
        /// 读取wav文件
        /// </summary>
        /// <param name="filename"></param>
        private async Task <wav> WaveAccess(string filename)
        {
            try
            {
                byte[] riff       = new byte[4];
                byte[] riffSize   = new byte[4];
                byte[] waveID     = new byte[4];
                byte[] junkID     = new byte[4];
                bool   hasjunk    = false;
                byte[] junklength = new byte[4];

                byte[] fmtID           = new byte[4];
                byte[] cksize          = new byte[4];
                uint   waveType        = 0;
                byte[] channel         = new byte[2];
                byte[] sample_rate     = new byte[4];
                byte[] bytespersec     = new byte[4];
                byte[] blocklen_sample = new byte[2];
                byte[] bitNum          = new byte[2];
                byte[] unknown         = new byte[2];
                byte[] dataID          = new byte[4]; //52
                byte[] dataLength      = new byte[4]; //56 个字节

                //string longFileName = filepath;

                //FileStream fs = new FileStream(filepath, FileMode.Open);
                //Windows.Storage.StorageFolder s = Windows.ApplicationModel.Package.Current.InstalledLocation;
                //FileStream fs;

                //StorageFolder storageFolder = Package.Current.InstalledLocation;
                StorageFolder storageFolder = ApplicationData.Current.LocalFolder;

                StorageFile storageFile = await storageFolder.GetFileAsync(filename);

                IRandomAccessStream fileStream = await storageFile.OpenAsync(FileAccessMode.Read);

                Stream s = fileStream.AsStream();



                BinaryReader bread = new BinaryReader(s);
                //BinaryReader bread = new BinaryReader(fs);
                riff = bread.ReadBytes(4); // RIFF

                if (BitConverter.ToUInt32(bytesReserve(riff), 0) != 0x52494646)
                {
                    Exception e = new Exception("该文件不是WAVE文件");
                    throw e;
                }

                riffSize = bread.ReadBytes(4); // 文件剩余长度

                if (BitConverter.ToUInt32(riffSize, 0) != bread.BaseStream.Length - bread.BaseStream.Position)
                {
                    //Exception e = new Exception("该WAVE文件损坏,文件长度与标记不一致");
                    //throw e;
                }

                waveID = bread.ReadBytes(4);

                if (BitConverter.ToUInt32(bytesReserve(waveID), 0) != 0x57415645)
                {
                    Exception e = new Exception("该文件不是WAVE文件");
                    throw e;
                }

                byte[] tmp = bread.ReadBytes(4);

                if (BitConverter.ToUInt32(bytesReserve(tmp), 0) == 0x4A554E4B)
                {
                    //包含junk标记的wav
                    junkID     = tmp;
                    hasjunk    = true;
                    junklength = bread.ReadBytes(4);
                    uint junklen = BitConverter.ToUInt32(junklength, 0);
                    //将不要的junk部分读出
                    bread.ReadBytes((int)junklen);

                    //读fmt 标记
                    fmtID = bread.ReadBytes(4);
                }
                else if (BitConverter.ToUInt32(bytesReserve(tmp), 0) == 0x666D7420)
                {
                    fmtID = tmp;
                }
                else
                {
                    Exception e = new Exception("无法找到WAVE文件的junk和fmt标记");
                    throw e;
                }



                if (BitConverter.ToUInt32(bytesReserve(fmtID), 0) != 0x666D7420)
                {
                    //fmt 标记
                    Exception e = new Exception("无法找到WAVE文件fmt标记");
                    throw e;
                }

                cksize = bread.ReadBytes(4);
                uint p_data_start = BitConverter.ToUInt32(cksize, 0);
                int  p_wav_start  = (int)p_data_start + 8;

                waveType = bread.ReadUInt16();

                if (waveType != 1)
                {
                    // 非pcm格式,暂不支持
                    Exception e = new Exception("WAVE文件不是pcm格式,暂时不支持");
                    throw e;
                }

                //声道数
                channel = bread.ReadBytes(2);

                //采样频率
                sample_rate = bread.ReadBytes(4);
                int fs = (int)BitConverter.ToUInt32(sample_rate, 0);

                //每秒钟字节数
                bytespersec = bread.ReadBytes(4);

                //每次采样的字节大小,2为单声道,4为立体声道
                blocklen_sample = bread.ReadBytes(2);

                //每个声道的采样精度,默认16bit
                bitNum = bread.ReadBytes(2);

                tmp = bread.ReadBytes(2);
                //寻找da标记
                while (BitConverter.ToUInt16(bytesReserve(tmp), 0) != 0x6461)
                {
                    tmp = bread.ReadBytes(2);
                }
                tmp = bread.ReadBytes(2);

                if (BitConverter.ToUInt16(bytesReserve(tmp), 0) != 0x7461)
                {
                    //ta标记
                    Exception e = new Exception("无法找到WAVE文件data标记");
                    throw e;
                }

                //wav数据byte长度
                uint DataSize = bread.ReadUInt32();
                //计算样本数
                long NumSamples = (long)DataSize / 2;

                if (NumSamples == 0)
                {
                    NumSamples = (bread.BaseStream.Length - bread.BaseStream.Position) / 2;
                }
                //if (BitConverter.ToUInt32(notDefinition, 0) == 18)
                //{
                //    unknown = bread.ReadBytes(2);
                //}
                //dataID = bread.ReadBytes(4);

                Int16[] data = new Int16[NumSamples];

                for (int i = 0; i < NumSamples; i++)
                {
                    //读入2字节有符号整数
                    data[i] = bread.ReadInt16();
                }

                s.Dispose();

                bread.Dispose();

                wav wave = new wav();
                wave.wavs = data;
                wave.fs   = fs;
                return(wave);
            }
            catch (System.Exception ex)
            {
                //return null;
                throw ex;
            }
        }