Golang 基于chrome浏览器语音识别web演示系统WebHTK开发之 UI篇

前端之家收集整理的这篇文章主要介绍了Golang 基于chrome浏览器语音识别web演示系统WebHTK开发之 UI篇前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

没啥好说的,直接上现阶段的HTML代码,后续修改,再更新该篇博客

record.html:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta charset="utf-8"/>
  5. <Meta http-equiv="X-UA-Compatible" content="IE=edge"/>
  6. <Meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
  7. <Meta name="keywords" content="PONPON,HTK,Go语言"/>
  8. <Meta name="description" content="基于Beego开发语音识别演示系统"/>
  9. <Meta name="generator" content="PONPON" />
  10. <link href="/static/css/bootstrap.min.css" rel="stylesheet">
  11. <link href="/static/css/docs.css" rel="stylesheet">
  12. <link href="http://cdn.bootcss.com/highlight.js/7.3/styles/github.min.css" rel="stylesheet">
  13. <link rel="shortcut icon" href="/static/img/logoicon.jpg">
  14. <link rel="stylesheet" href="http://libs.baidu.com/fontawesome/4.0.3/css/font-awesome.min.css"/>
  15. <link rel="alternate" type="application/RSS+xml" href="/RSS.xml"/>
  16. <script type="text/javascript" src="/static/lib/recorder.js"> </script>
  17. <script type="text/javascript" src="/static/lib/jquery-1.10.1.min.js"> </script>
  18. <script type="text/javascript" src="/static/lib/recController.js"> </script>
  19. <title>WebHTK 演示系统</title>
  20. </head>
  21. <body>
  22. <nav id="header">
  23. <div class="container960 text-center" >
  24. <h3 id="header-h" class="center" >闽南语 - 语音识别演示系统</h3>
  25. <ul id="resultBox" class="center" style="padding-top:425px;font-size:20px;text-align: center;color:#FFC8B6;font-family:'微软雅黑'">
  26. <li >识别结果</li>
  27. </ul>
  28. <form style="padding-top:20px;">
  29. <a id="img" href="javascript://" >
  30. <img src="/static/img/aa.png" style="width:85px;height:85px;" alt=""/>
  31. </a>
  32. </form>
  33. <div id="message" style="padding-top:10px;font-size:16px;color:#F5FFFA;text-align: center;font-family:'微软雅黑'">点击麦克风,开始录音!</div>
  34. <script type="text/javascript">
  35. var recording = false;
  36. function test() {
  37. if (!recording) {
  38. document.getElementById("img").innerHTML="<img src='/static/img/a1.png' style='width:85px;height:85px;'' alt=''/>";
  39. toRecord();
  40. recording=true;
  41. }else{
  42. document.getElementById("img").innerHTML="<img src='/static/img/aa.png' style='width:85px;height:85px;'' alt=''/>";
  43. toSend();
  44. recording = false;
  45. }
  46. };
  47. function toRecord(){
  48. rec.record();
  49. var dd = ws.send("start");
  50. $("#message").text("再次点击,结束录音!");
  51. intervalKey = setInterval(function() {
  52. rec.exportWAV(function(blob) {
  53. rec.clear();
  54. ws.send(blob);
  55. });
  56. },300);
  57. }
  58. function toSend(){
  59. rec.stop();
  60. if (intervalKey == null) {
  61. $("#message").text("请先录音再发送!");
  62. return
  63. };
  64. ws.send(sampleRate);
  65. ws.send(channels);
  66. ws.send("stop");
  67. rec.clear();
  68. clearInterval(intervalKey);
  69. intervalKey = null;
  70. }
  71. </script>
  72. </div>
  73. </nav>
  74. <audio class="hide" controls autoplay></audio>
  75. </body>
  76. </html>


recorder.js:

  1. (function(window) {
  2. var WORKER_PATH = '/static/lib/recorderWorker.js';
  3. var Recorder = function(source,chan,cfg) {
  4. var config = cfg || {};
  5. var channels = chan || 1;
  6. var bufferLen = config.bufferLen || 8192;
  7. this.context = source.context;
  8. this.node = this.context.createJavaScriptNode(bufferLen,channels,channels);
  9. var worker = new Worker(config.workerPath || WORKER_PATH);
  10. worker.postMessage({
  11. command: 'init',config: {
  12. sampleRate: this.context.sampleRate
  13. }
  14. });
  15. var recording = false,currCallback;
  16. this.node.onaudioprocess = function(e) {
  17. if (!recording) return;
  18. worker.postMessage({
  19. command: 'record',buffer: [
  20. e.inputBuffer.getChannelData(0)
  21. ]
  22. });
  23. }
  24. this.configure = function(cfg) {
  25. for (var prop in cfg) {
  26. if (cfg.hasOwnProperty(prop)) {
  27. config[prop] = cfg[prop];
  28. }
  29. }
  30. }
  31. this.record = function() {
  32. recording = true;
  33. }
  34. this.stop = function() {
  35. recording = false;
  36. }
  37. this.clear = function() {
  38. worker.postMessage({
  39. command: 'clear'
  40. });
  41. }
  42. this.getBuffer = function(cb) {
  43. currCallback = cb || config.callback;
  44. worker.postMessage({
  45. command: 'getBuffer'
  46. })
  47. }
  48. this.exportWAV = function(cb,type) {
  49. currCallback = cb || config.callback;
  50. type = type || config.type || 'audio/wav';
  51. if (!currCallback) throw new Error('Callback not set');
  52. worker.postMessage({
  53. command: 'exportWAV',type: type
  54. });
  55. }
  56. worker.onmessage = function(e) {
  57. var blob = e.data;
  58. currCallback(blob);
  59. }
  60. source.connect(this.node);
  61. this.node.connect(this.context.destination);
  62. };
  63. window.Recorder = Recorder;
  64. })(window);


recorderWorker.js:


  1. var recLength = 0,recBuffersL = [],sampleRate;
  2. this.onmessage = function(e) {
  3. switch (e.data.command) {
  4. case 'init':
  5. init(e.data.config);
  6. break;
  7. case 'record':
  8. record(e.data.buffer);
  9. break;
  10. case 'exportWAV':
  11. exportWAV(e.data.type);
  12. break;
  13. case 'getBuffer':
  14. getBuffer();
  15. break;
  16. case 'clear':
  17. clear();
  18. break;
  19. }
  20. };
  21. function init(config) {
  22. sampleRate = config.sampleRate;
  23. }
  24. function record(inputBuffer) {
  25. recBuffersL.push(inputBuffer[0]);
  26. recLength += inputBuffer[0].length;
  27. }
  28. function exportWAV(type) {
  29. var bufferL = mergeBuffers(recBuffersL,recLength);
  30. var interleaved = interleave(bufferL);
  31. var dataview = encodeWAV(interleaved);
  32. var audioBlob = new Blob([dataview],{
  33. type: type
  34. });
  35. this.postMessage(audioBlob);
  36. }
  37. function getBuffer() {
  38. var buffers = [];
  39. buffers.push(mergeBuffers(recBuffersL,recLength));
  40. this.postMessage(buffers);
  41. }
  42. function clear(inputBuffer) {
  43. recLength = 0;
  44. recBuffersL = [];
  45. }
  46. function mergeBuffers(recBuffers,recLength) {
  47. var result = new Float32Array(recLength);
  48. var offset = 0;
  49. for (var i = 0; i < recBuffers.length; i++) {
  50. result.set(recBuffers[i],offset);
  51. offset += recBuffers[i].length;
  52. }
  53. return result;
  54. }
  55. function interleave(inputL) {
  56. var length;
  57. var result;
  58. var index = 0,inputIndex = 0;
  59. if (sampleRate == 48000) {
  60. length = inputL.length / 6;
  61. result = new Float32Array(length);
  62. while (index < length) {
  63. result[index++] = (inputL[inputIndex++] + inputL[inputIndex++] +
  64. inputL[inputIndex++] + inputL[inputIndex++] +
  65. inputL[inputIndex++] + inputL[inputIndex++]) / 6;
  66. }
  67. } else if (sampleRate == 44100) {
  68. length = inputL.length / 6;
  69. result = new Float32Array(length);
  70. while (index < length) {
  71. if (inputIndex % 12 == 0) {
  72. result[index++] = (inputL[inputIndex] + inputL[inputIndex++] +
  73. inputL[inputIndex++] + inputL[inputIndex++] +
  74. inputL[inputIndex++] + inputL[inputIndex++] +
  75. inputL[inputIndex++]) / 7;
  76. } else {
  77. result[index++] = (inputL[inputIndex++] + inputL[inputIndex++] +
  78. inputL[inputIndex++] + inputL[inputIndex++] +
  79. inputL[inputIndex++] + inputL[inputIndex++]) / 6;
  80. };
  81. }
  82. } else {
  83. length = inputL.length;
  84. result = new Float32Array(length);
  85. while (index < length) {
  86. result[index++] = inputL[inputIndex++];
  87. }
  88. };
  89. return result;
  90. }
  91. function floatTo16BitPCM(output,offset,input) {
  92. for (var i = 0; i < input.length; i++,offset += 2) {
  93. var s = Math.max(-1,Math.min(1,input[i]));
  94. output.setInt16(offset,s < 0 ? s * 0x8000 : s * 0x7FFF,true);
  95. }
  96. }
  97. function writeString(view,string) {
  98. for (var i = 0; i < string.length; i++) {
  99. view.setUint8(offset + i,string.charCodeAt(i));
  100. }
  101. }
  102. function encodeWAV(samples) {
  103. var buffer = new ArrayBuffer(samples.length * 2);
  104. var view = new DataView(buffer);
  105. floatTo16BitPCM(view,samples);
  106. return view;
  107. }


recController.js:

  1. var onFail = function(e) {
  2. console.log('Rejected!',e);
  3. };
  4. var onSuccess = function(s) {
  5. var context = new webkitAudioContext();
  6. var mediaStreamSource = context.createMediaStreamSource(s);
  7. rec = new Recorder(mediaStreamSource,channels);
  8. sampleRate = 8000;
  9. }
  10. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
  11. var rec;
  12. var intervalKey = null;
  13. var audio = document.querySelector('#audio');
  14. var sampleRate;
  15. var channels = 1;
  16. function startRecording() {
  17. if (navigator.getUserMedia) {
  18. navigator.getUserMedia({
  19. audio: true
  20. },onSuccess,onFail);
  21. } else {
  22. console.log('navigator.getUserMedia not present');
  23. }
  24. }
  25. startRecording();
  26. //--------------------
  27. var ws = new WebSocket('ws://' + window.location.host + '/join');
  28. ws.onopen = function() {
  29. console.log("Openened connection to websocket");
  30. };
  31. ws.onclose = function() {
  32. console.log("Close connection to websocket");
  33. }
  34. ws.onerror = function() {
  35. console.log("Cannot connection to websocket");
  36. }
  37. ws.onmessage = function(result) {
  38. var data = JSON.parse(result.data);
  39. console.log('识别结果:' + data.Pinyin);
  40. var result = document.getElementById("resultBox")
  41. result.getElementsByTagName("li")[0].innerHTML = data.Hanzi;
  42. document.getElementById("message").innerHTML = "点击麦克风,开始录音!";
  43. }


进入页面

wKioL1MV5_zT5fsBAAi_Ila2WRE976.jpg


正在录音:

wKiom1MV6CXzTifFAAiwJsgx_8k251.jpg


识别结果,目前还为将拼音转换为汉字,下图为“点亮星光”的显示结果图:

wKioL1MV6APT2sxZAAjIrySOhP0350.jpg

猜你在找的Go相关文章