├── README.md └── esp32 └── esp32_remote_mic2 ├── I2S.cpp ├── I2S.h ├── Wav.cpp ├── Wav.h ├── data ├── index.html ├── jquery-3.4.1.min.js ├── live.js ├── player.js └── style.css └── esp32_remote_mic2.ino /README.md: -------------------------------------------------------------------------------- 1 | ESP32 as Remote MIC 2 | 3 | 一.功能:
4 | esp32 网页麦克风终端
5 | 项目参考 https://github.com/paranerd/simplecam 项目进行的ESP32版本移植,原项目可以通过网页监听树莓派上传入的图像和声音.
6 | esp32也能实现图像声音传输功能,目前只开发了声音功能.
7 | 使用场景:单元楼大门声音对话,监听防盗, 作为智能音箱远程附件等
8 | 9 | 二.硬件:
10 | ESP32+ INMP441(I2S麦克风模块)
11 | 推荐硬件:
12 | A.普通ESP32+INMP441麦克风模块 成本低,需要几根杜邦线连接, 显得不整洁
13 | INMP441 <---> ESP32 , 接线定义见I2S.h
14 | SCK <---> IO14
15 | WS <---> IO27
16 | SD <---> IO2
17 | L/R <---> GND
18 | 19 | B.ESP-EYE 小巧,隐蔽性好,价格有些贵
20 | 21 | C.TTGO T-Camera Plus ESP32 体积有些大,不隐蔽
22 | 23 | D.TTGO T-Watch 加一块带MIC扩展板 成本略高,隐蔽性好
24 | 25 | 三.使用方法:
26 | 1.打开网页浏览器, 输入访问地址 http://192.168.1.100
27 | 2.网页打开后有一个切换声音是否播放按钮, 点击后浏览器开始接收并播放声音.
28 | 29 | 注:
30 | 1.暂不支付多并发,同一时间只允许一个客户端
31 | 2.必须用支持html5的浏览器, IE不支持html,所以不可以用. pc上可以用chrome浏览器, 手机上内置的浏览器基本上都支持html5
32 | 3.如果电脑播放声音为杂音, 电脑控制面板找到声音设置,将声音播放格式改成48000hz
33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/I2S.cpp: -------------------------------------------------------------------------------- 1 | void I2S_Init(i2s_mode_t MODE, int SAMPLE_RATE, i2s_bits_per_sample_t BPS) { 2 | i2s_config_t i2s_config = { 3 | .mode = (i2s_mode_t)(I2S_MODE_MASTER | MODE), 4 | .sample_rate = SAMPLE_RATE, 5 | .bits_per_sample = BPS, 6 | .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,//只获取单声道 左声道 7 | .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB), 8 | .intr_alloc_flags = 0, 9 | .dma_buf_count = 8, 10 | .dma_buf_len = 64, 11 | .use_apll = false 12 | }; 13 | i2s_pin_config_t pin_config; 14 | pin_config.bck_io_num = PIN_I2S_BCLK; 15 | pin_config.ws_io_num = PIN_I2S_LRC; 16 | if (MODE == I2S_MODE_RX) { 17 | pin_config.data_out_num = I2S_PIN_NO_CHANGE; 18 | pin_config.data_in_num = PIN_I2S_DIN; 19 | } 20 | else if (MODE == I2S_MODE_TX) { 21 | pin_config.data_out_num = PIN_I2S_DOUT; 22 | pin_config.data_in_num = I2S_PIN_NO_CHANGE; 23 | } 24 | i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); 25 | i2s_set_pin(I2S_NUM_0, &pin_config); 26 | //最终设置: 16k, 16位,单声道 27 | //1.0.3 rc1 以后的版本不要调用下句,否则I2S不可用 28 | //i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, BPS, I2S_CHANNEL_MONO); 29 | } 30 | 31 | int I2S_Read(char* data, int numData) { 32 | return i2s_read_bytes(I2S_NUM_0, (char *)data, numData, portMAX_DELAY); 33 | } 34 | 35 | void I2S_Write(char* data, int numData) { 36 | i2s_write_bytes(I2S_NUM_0, (const char *)data, numData, portMAX_DELAY); 37 | } 38 | 39 | void I2S_uninstall() 40 | { 41 | i2s_driver_uninstall(I2S_NUM_0); 42 | } 43 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/I2S.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "freertos/FreeRTOS.h" 3 | #include "freertos/task.h" 4 | #include "driver/i2s.h" 5 | #include "esp_system.h" 6 | #define PIN_I2S_BCLK 14 7 | #define PIN_I2S_LRC 27 8 | #define PIN_I2S_DIN 2 9 | #define PIN_I2S_DOUT -1 10 | 11 | // This I2S specification : 12 | // - LRC high is channel 2 (right). 13 | // - LRC signal transitions once each word. 14 | // - DATA is valid on the CLOCK rising edge. 15 | // - Data bits are MSB first. 16 | // - DATA bits are left-aligned with respect to LRC edge. 17 | // - DATA bits are right-shifted by one with respect to LRC edges. 18 | // It's valid for ADMP441 (microphone) and MAX98357A (speaker). 19 | // It's not valid for SPH0645LM4H(microphone) and WM8960(microphon/speaker). 20 | // 21 | // - 44100Hz 22 | // - stereo 23 | 24 | /// @parameter MODE : I2S_MODE_RX or I2S_MODE_TX 25 | /// @parameter BPS : I2S_BITS_PER_SAMPLE_16BIT or I2S_BITS_PER_SAMPLE_32BIT 26 | void I2S_Init(i2s_mode_t MODE, int SAMPLE_RATE, i2s_bits_per_sample_t BPS); 27 | 28 | /// I2S_Read() for I2S_MODE_RX 29 | /// @parameter data: pointer to buffer 30 | /// @parameter numData: buffer size 31 | /// @return Number of bytes read 32 | int I2S_Read(char* data, int numData); 33 | 34 | /// I2S_Write() for I2S_MODE_TX 35 | /// @param data: pointer to buffer 36 | /// @param numData: buffer size 37 | void I2S_Write(char* data, int numData); 38 | 39 | void I2S_uninstall(); 40 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/Wav.cpp: -------------------------------------------------------------------------------- 1 | #include "Wav.h" 2 | 3 | void CreateWavHeader(byte* header, long waveDataSize){ 4 | header[0] = 'R'; 5 | header[1] = 'I'; 6 | header[2] = 'F'; 7 | header[3] = 'F'; 8 | unsigned int fileSizeMinus8 = waveDataSize + 44 - 8; 9 | header[4] = (byte)(fileSizeMinus8 & 0xFF); 10 | header[5] = (byte)((fileSizeMinus8 >> 8) & 0xFF); 11 | header[6] = (byte)((fileSizeMinus8 >> 16) & 0xFF); 12 | header[7] = (byte)((fileSizeMinus8 >> 24) & 0xFF); 13 | header[8] = 'W'; 14 | header[9] = 'A'; 15 | header[10] = 'V'; 16 | header[11] = 'E'; 17 | header[12] = 'f'; 18 | header[13] = 'm'; 19 | header[14] = 't'; 20 | header[15] = ' '; 21 | header[16] = 0x10; // linear PCM 22 | header[17] = 0x00; 23 | header[18] = 0x00; 24 | header[19] = 0x00; 25 | header[20] = 0x01; // linear PCM 26 | header[21] = 0x00; 27 | header[22] = 0x01; // monoral 28 | header[23] = 0x00; 29 | header[24] = 0x80; // sampling rate 16000 30 | header[25] = 0x3E; 31 | header[26] = 0x00; 32 | header[27] = 0x00; 33 | header[28] = 0x00; // Byte/sec = 16000x2x1 = 32000 34 | header[29] = 0x7D; 35 | header[30] = 0x00; 36 | header[31] = 0x00; 37 | header[32] = 0x02; // 16bit monoral 38 | header[33] = 0x00; 39 | header[34] = 0x10; // 16bit 40 | header[35] = 0x00; 41 | header[36] = 'd'; 42 | header[37] = 'a'; 43 | header[38] = 't'; 44 | header[39] = 'a'; 45 | header[40] = (byte)(waveDataSize & 0xFF); 46 | header[41] = (byte)((waveDataSize >> 8) & 0xFF); 47 | header[42] = (byte)((waveDataSize >> 16) & 0xFF); 48 | header[43] = (byte)((waveDataSize >> 24) & 0xFF); 49 | } 50 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/Wav.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 16bit, monoral, 16000Hz, linear PCM 4 | void CreateWavHeader(byte* header, long waveDataSize); // size of header is 44 5 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Audio Esp32 5 | 6 | 7 | 8 | Audio Esp32 9 |
10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/data/jquery-3.4.1.min.js: -------------------------------------------------------------------------------- 1 | /*! jQuery v3.4.1 | (c) JS Foundation and other contributors | jquery.org/license */ 2 | !function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(C,e){"use strict";var t=[],E=C.document,r=Object.getPrototypeOf,s=t.slice,g=t.concat,u=t.push,i=t.indexOf,n={},o=n.toString,v=n.hasOwnProperty,a=v.toString,l=a.call(Object),y={},m=function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},x=function(e){return null!=e&&e===e.window},c={type:!0,src:!0,nonce:!0,noModule:!0};function b(e,t,n){var r,i,o=(n=n||E).createElement("script");if(o.text=e,t)for(r in c)(i=t[r]||t.getAttribute&&t.getAttribute(r))&&o.setAttribute(r,i);n.head.appendChild(o).parentNode.removeChild(o)}function w(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?n[o.call(e)]||"object":typeof e}var f="3.4.1",k=function(e,t){return new k.fn.init(e,t)},p=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;function d(e){var t=!!e&&"length"in e&&e.length,n=w(e);return!m(e)&&!x(e)&&("array"===n||0===t||"number"==typeof t&&0+~]|"+M+")"+M+"*"),U=new RegExp(M+"|>"),X=new RegExp($),V=new RegExp("^"+I+"$"),G={ID:new RegExp("^#("+I+")"),CLASS:new RegExp("^\\.("+I+")"),TAG:new RegExp("^("+I+"|[*])"),ATTR:new RegExp("^"+W),PSEUDO:new RegExp("^"+$),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+R+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},Y=/HTML$/i,Q=/^(?:input|select|textarea|button)$/i,J=/^h\d$/i,K=/^[^{]+\{\s*\[native \w/,Z=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,ee=/[+~]/,te=new RegExp("\\\\([\\da-f]{1,6}"+M+"?|("+M+")|.)","ig"),ne=function(e,t,n){var r="0x"+t-65536;return r!=r||n?t:r<0?String.fromCharCode(r+65536):String.fromCharCode(r>>10|55296,1023&r|56320)},re=/([\0-\x1f\x7f]|^-?\d)|^-$|[^\0-\x1f\x7f-\uFFFF\w-]/g,ie=function(e,t){return t?"\0"===e?"\ufffd":e.slice(0,-1)+"\\"+e.charCodeAt(e.length-1).toString(16)+" ":"\\"+e},oe=function(){T()},ae=be(function(e){return!0===e.disabled&&"fieldset"===e.nodeName.toLowerCase()},{dir:"parentNode",next:"legend"});try{H.apply(t=O.call(m.childNodes),m.childNodes),t[m.childNodes.length].nodeType}catch(e){H={apply:t.length?function(e,t){L.apply(e,O.call(t))}:function(e,t){var n=e.length,r=0;while(e[n++]=t[r++]);e.length=n-1}}}function se(t,e,n,r){var i,o,a,s,u,l,c,f=e&&e.ownerDocument,p=e?e.nodeType:9;if(n=n||[],"string"!=typeof t||!t||1!==p&&9!==p&&11!==p)return n;if(!r&&((e?e.ownerDocument||e:m)!==C&&T(e),e=e||C,E)){if(11!==p&&(u=Z.exec(t)))if(i=u[1]){if(9===p){if(!(a=e.getElementById(i)))return n;if(a.id===i)return n.push(a),n}else if(f&&(a=f.getElementById(i))&&y(e,a)&&a.id===i)return n.push(a),n}else{if(u[2])return H.apply(n,e.getElementsByTagName(t)),n;if((i=u[3])&&d.getElementsByClassName&&e.getElementsByClassName)return H.apply(n,e.getElementsByClassName(i)),n}if(d.qsa&&!A[t+" "]&&(!v||!v.test(t))&&(1!==p||"object"!==e.nodeName.toLowerCase())){if(c=t,f=e,1===p&&U.test(t)){(s=e.getAttribute("id"))?s=s.replace(re,ie):e.setAttribute("id",s=k),o=(l=h(t)).length;while(o--)l[o]="#"+s+" "+xe(l[o]);c=l.join(","),f=ee.test(t)&&ye(e.parentNode)||e}try{return H.apply(n,f.querySelectorAll(c)),n}catch(e){A(t,!0)}finally{s===k&&e.removeAttribute("id")}}}return g(t.replace(B,"$1"),e,n,r)}function ue(){var r=[];return function e(t,n){return r.push(t+" ")>b.cacheLength&&delete e[r.shift()],e[t+" "]=n}}function le(e){return e[k]=!0,e}function ce(e){var t=C.createElement("fieldset");try{return!!e(t)}catch(e){return!1}finally{t.parentNode&&t.parentNode.removeChild(t),t=null}}function fe(e,t){var n=e.split("|"),r=n.length;while(r--)b.attrHandle[n[r]]=t}function pe(e,t){var n=t&&e,r=n&&1===e.nodeType&&1===t.nodeType&&e.sourceIndex-t.sourceIndex;if(r)return r;if(n)while(n=n.nextSibling)if(n===t)return-1;return e?1:-1}function de(t){return function(e){return"input"===e.nodeName.toLowerCase()&&e.type===t}}function he(n){return function(e){var t=e.nodeName.toLowerCase();return("input"===t||"button"===t)&&e.type===n}}function ge(t){return function(e){return"form"in e?e.parentNode&&!1===e.disabled?"label"in e?"label"in e.parentNode?e.parentNode.disabled===t:e.disabled===t:e.isDisabled===t||e.isDisabled!==!t&&ae(e)===t:e.disabled===t:"label"in e&&e.disabled===t}}function ve(a){return le(function(o){return o=+o,le(function(e,t){var n,r=a([],e.length,o),i=r.length;while(i--)e[n=r[i]]&&(e[n]=!(t[n]=e[n]))})})}function ye(e){return e&&"undefined"!=typeof e.getElementsByTagName&&e}for(e in d=se.support={},i=se.isXML=function(e){var t=e.namespaceURI,n=(e.ownerDocument||e).documentElement;return!Y.test(t||n&&n.nodeName||"HTML")},T=se.setDocument=function(e){var t,n,r=e?e.ownerDocument||e:m;return r!==C&&9===r.nodeType&&r.documentElement&&(a=(C=r).documentElement,E=!i(C),m!==C&&(n=C.defaultView)&&n.top!==n&&(n.addEventListener?n.addEventListener("unload",oe,!1):n.attachEvent&&n.attachEvent("onunload",oe)),d.attributes=ce(function(e){return e.className="i",!e.getAttribute("className")}),d.getElementsByTagName=ce(function(e){return e.appendChild(C.createComment("")),!e.getElementsByTagName("*").length}),d.getElementsByClassName=K.test(C.getElementsByClassName),d.getById=ce(function(e){return a.appendChild(e).id=k,!C.getElementsByName||!C.getElementsByName(k).length}),d.getById?(b.filter.ID=function(e){var t=e.replace(te,ne);return function(e){return e.getAttribute("id")===t}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n=t.getElementById(e);return n?[n]:[]}}):(b.filter.ID=function(e){var n=e.replace(te,ne);return function(e){var t="undefined"!=typeof e.getAttributeNode&&e.getAttributeNode("id");return t&&t.value===n}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n,r,i,o=t.getElementById(e);if(o){if((n=o.getAttributeNode("id"))&&n.value===e)return[o];i=t.getElementsByName(e),r=0;while(o=i[r++])if((n=o.getAttributeNode("id"))&&n.value===e)return[o]}return[]}}),b.find.TAG=d.getElementsByTagName?function(e,t){return"undefined"!=typeof t.getElementsByTagName?t.getElementsByTagName(e):d.qsa?t.querySelectorAll(e):void 0}:function(e,t){var n,r=[],i=0,o=t.getElementsByTagName(e);if("*"===e){while(n=o[i++])1===n.nodeType&&r.push(n);return r}return o},b.find.CLASS=d.getElementsByClassName&&function(e,t){if("undefined"!=typeof t.getElementsByClassName&&E)return t.getElementsByClassName(e)},s=[],v=[],(d.qsa=K.test(C.querySelectorAll))&&(ce(function(e){a.appendChild(e).innerHTML="",e.querySelectorAll("[msallowcapture^='']").length&&v.push("[*^$]="+M+"*(?:''|\"\")"),e.querySelectorAll("[selected]").length||v.push("\\["+M+"*(?:value|"+R+")"),e.querySelectorAll("[id~="+k+"-]").length||v.push("~="),e.querySelectorAll(":checked").length||v.push(":checked"),e.querySelectorAll("a#"+k+"+*").length||v.push(".#.+[+~]")}),ce(function(e){e.innerHTML="";var t=C.createElement("input");t.setAttribute("type","hidden"),e.appendChild(t).setAttribute("name","D"),e.querySelectorAll("[name=d]").length&&v.push("name"+M+"*[*^$|!~]?="),2!==e.querySelectorAll(":enabled").length&&v.push(":enabled",":disabled"),a.appendChild(e).disabled=!0,2!==e.querySelectorAll(":disabled").length&&v.push(":enabled",":disabled"),e.querySelectorAll("*,:x"),v.push(",.*:")})),(d.matchesSelector=K.test(c=a.matches||a.webkitMatchesSelector||a.mozMatchesSelector||a.oMatchesSelector||a.msMatchesSelector))&&ce(function(e){d.disconnectedMatch=c.call(e,"*"),c.call(e,"[s!='']:x"),s.push("!=",$)}),v=v.length&&new RegExp(v.join("|")),s=s.length&&new RegExp(s.join("|")),t=K.test(a.compareDocumentPosition),y=t||K.test(a.contains)?function(e,t){var n=9===e.nodeType?e.documentElement:e,r=t&&t.parentNode;return e===r||!(!r||1!==r.nodeType||!(n.contains?n.contains(r):e.compareDocumentPosition&&16&e.compareDocumentPosition(r)))}:function(e,t){if(t)while(t=t.parentNode)if(t===e)return!0;return!1},D=t?function(e,t){if(e===t)return l=!0,0;var n=!e.compareDocumentPosition-!t.compareDocumentPosition;return n||(1&(n=(e.ownerDocument||e)===(t.ownerDocument||t)?e.compareDocumentPosition(t):1)||!d.sortDetached&&t.compareDocumentPosition(e)===n?e===C||e.ownerDocument===m&&y(m,e)?-1:t===C||t.ownerDocument===m&&y(m,t)?1:u?P(u,e)-P(u,t):0:4&n?-1:1)}:function(e,t){if(e===t)return l=!0,0;var n,r=0,i=e.parentNode,o=t.parentNode,a=[e],s=[t];if(!i||!o)return e===C?-1:t===C?1:i?-1:o?1:u?P(u,e)-P(u,t):0;if(i===o)return pe(e,t);n=e;while(n=n.parentNode)a.unshift(n);n=t;while(n=n.parentNode)s.unshift(n);while(a[r]===s[r])r++;return r?pe(a[r],s[r]):a[r]===m?-1:s[r]===m?1:0}),C},se.matches=function(e,t){return se(e,null,null,t)},se.matchesSelector=function(e,t){if((e.ownerDocument||e)!==C&&T(e),d.matchesSelector&&E&&!A[t+" "]&&(!s||!s.test(t))&&(!v||!v.test(t)))try{var n=c.call(e,t);if(n||d.disconnectedMatch||e.document&&11!==e.document.nodeType)return n}catch(e){A(t,!0)}return 0":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(e){return e[1]=e[1].replace(te,ne),e[3]=(e[3]||e[4]||e[5]||"").replace(te,ne),"~="===e[2]&&(e[3]=" "+e[3]+" "),e.slice(0,4)},CHILD:function(e){return e[1]=e[1].toLowerCase(),"nth"===e[1].slice(0,3)?(e[3]||se.error(e[0]),e[4]=+(e[4]?e[5]+(e[6]||1):2*("even"===e[3]||"odd"===e[3])),e[5]=+(e[7]+e[8]||"odd"===e[3])):e[3]&&se.error(e[0]),e},PSEUDO:function(e){var t,n=!e[6]&&e[2];return G.CHILD.test(e[0])?null:(e[3]?e[2]=e[4]||e[5]||"":n&&X.test(n)&&(t=h(n,!0))&&(t=n.indexOf(")",n.length-t)-n.length)&&(e[0]=e[0].slice(0,t),e[2]=n.slice(0,t)),e.slice(0,3))}},filter:{TAG:function(e){var t=e.replace(te,ne).toLowerCase();return"*"===e?function(){return!0}:function(e){return e.nodeName&&e.nodeName.toLowerCase()===t}},CLASS:function(e){var t=p[e+" "];return t||(t=new RegExp("(^|"+M+")"+e+"("+M+"|$)"))&&p(e,function(e){return t.test("string"==typeof e.className&&e.className||"undefined"!=typeof e.getAttribute&&e.getAttribute("class")||"")})},ATTR:function(n,r,i){return function(e){var t=se.attr(e,n);return null==t?"!="===r:!r||(t+="","="===r?t===i:"!="===r?t!==i:"^="===r?i&&0===t.indexOf(i):"*="===r?i&&-1:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;function j(e,n,r){return m(n)?k.grep(e,function(e,t){return!!n.call(e,t,e)!==r}):n.nodeType?k.grep(e,function(e){return e===n!==r}):"string"!=typeof n?k.grep(e,function(e){return-1)[^>]*|#([\w-]+))$/;(k.fn.init=function(e,t,n){var r,i;if(!e)return this;if(n=n||q,"string"==typeof e){if(!(r="<"===e[0]&&">"===e[e.length-1]&&3<=e.length?[null,e,null]:L.exec(e))||!r[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(r[1]){if(t=t instanceof k?t[0]:t,k.merge(this,k.parseHTML(r[1],t&&t.nodeType?t.ownerDocument||t:E,!0)),D.test(r[1])&&k.isPlainObject(t))for(r in t)m(this[r])?this[r](t[r]):this.attr(r,t[r]);return this}return(i=E.getElementById(r[2]))&&(this[0]=i,this.length=1),this}return e.nodeType?(this[0]=e,this.length=1,this):m(e)?void 0!==n.ready?n.ready(e):e(k):k.makeArray(e,this)}).prototype=k.fn,q=k(E);var H=/^(?:parents|prev(?:Until|All))/,O={children:!0,contents:!0,next:!0,prev:!0};function P(e,t){while((e=e[t])&&1!==e.nodeType);return e}k.fn.extend({has:function(e){var t=k(e,this),n=t.length;return this.filter(function(){for(var e=0;e\x20\t\r\n\f]*)/i,he=/^$|^module$|\/(?:java|ecma)script/i,ge={option:[1,""],thead:[1,"","
"],col:[2,"","
"],tr:[2,"","
"],td:[3,"","
"],_default:[0,"",""]};function ve(e,t){var n;return n="undefined"!=typeof e.getElementsByTagName?e.getElementsByTagName(t||"*"):"undefined"!=typeof e.querySelectorAll?e.querySelectorAll(t||"*"):[],void 0===t||t&&A(e,t)?k.merge([e],n):n}function ye(e,t){for(var n=0,r=e.length;nx",y.noCloneChecked=!!me.cloneNode(!0).lastChild.defaultValue;var Te=/^key/,Ce=/^(?:mouse|pointer|contextmenu|drag|drop)|click/,Ee=/^([^.]*)(?:\.(.+)|)/;function ke(){return!0}function Se(){return!1}function Ne(e,t){return e===function(){try{return E.activeElement}catch(e){}}()==("focus"===t)}function Ae(e,t,n,r,i,o){var a,s;if("object"==typeof t){for(s in"string"!=typeof n&&(r=r||n,n=void 0),t)Ae(e,s,n,r,t[s],o);return e}if(null==r&&null==i?(i=n,r=n=void 0):null==i&&("string"==typeof n?(i=r,r=void 0):(i=r,r=n,n=void 0)),!1===i)i=Se;else if(!i)return e;return 1===o&&(a=i,(i=function(e){return k().off(e),a.apply(this,arguments)}).guid=a.guid||(a.guid=k.guid++)),e.each(function(){k.event.add(this,t,i,r,n)})}function De(e,i,o){o?(Q.set(e,i,!1),k.event.add(e,i,{namespace:!1,handler:function(e){var t,n,r=Q.get(this,i);if(1&e.isTrigger&&this[i]){if(r.length)(k.event.special[i]||{}).delegateType&&e.stopPropagation();else if(r=s.call(arguments),Q.set(this,i,r),t=o(this,i),this[i](),r!==(n=Q.get(this,i))||t?Q.set(this,i,!1):n={},r!==n)return e.stopImmediatePropagation(),e.preventDefault(),n.value}else r.length&&(Q.set(this,i,{value:k.event.trigger(k.extend(r[0],k.Event.prototype),r.slice(1),this)}),e.stopImmediatePropagation())}})):void 0===Q.get(e,i)&&k.event.add(e,i,ke)}k.event={global:{},add:function(t,e,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,v=Q.get(t);if(v){n.handler&&(n=(o=n).handler,i=o.selector),i&&k.find.matchesSelector(ie,i),n.guid||(n.guid=k.guid++),(u=v.events)||(u=v.events={}),(a=v.handle)||(a=v.handle=function(e){return"undefined"!=typeof k&&k.event.triggered!==e.type?k.event.dispatch.apply(t,arguments):void 0}),l=(e=(e||"").match(R)||[""]).length;while(l--)d=g=(s=Ee.exec(e[l])||[])[1],h=(s[2]||"").split(".").sort(),d&&(f=k.event.special[d]||{},d=(i?f.delegateType:f.bindType)||d,f=k.event.special[d]||{},c=k.extend({type:d,origType:g,data:r,handler:n,guid:n.guid,selector:i,needsContext:i&&k.expr.match.needsContext.test(i),namespace:h.join(".")},o),(p=u[d])||((p=u[d]=[]).delegateCount=0,f.setup&&!1!==f.setup.call(t,r,h,a)||t.addEventListener&&t.addEventListener(d,a)),f.add&&(f.add.call(t,c),c.handler.guid||(c.handler.guid=n.guid)),i?p.splice(p.delegateCount++,0,c):p.push(c),k.event.global[d]=!0)}},remove:function(e,t,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,v=Q.hasData(e)&&Q.get(e);if(v&&(u=v.events)){l=(t=(t||"").match(R)||[""]).length;while(l--)if(d=g=(s=Ee.exec(t[l])||[])[1],h=(s[2]||"").split(".").sort(),d){f=k.event.special[d]||{},p=u[d=(r?f.delegateType:f.bindType)||d]||[],s=s[2]&&new RegExp("(^|\\.)"+h.join("\\.(?:.*\\.|)")+"(\\.|$)"),a=o=p.length;while(o--)c=p[o],!i&&g!==c.origType||n&&n.guid!==c.guid||s&&!s.test(c.namespace)||r&&r!==c.selector&&("**"!==r||!c.selector)||(p.splice(o,1),c.selector&&p.delegateCount--,f.remove&&f.remove.call(e,c));a&&!p.length&&(f.teardown&&!1!==f.teardown.call(e,h,v.handle)||k.removeEvent(e,d,v.handle),delete u[d])}else for(d in u)k.event.remove(e,d+t[l],n,r,!0);k.isEmptyObject(u)&&Q.remove(e,"handle events")}},dispatch:function(e){var t,n,r,i,o,a,s=k.event.fix(e),u=new Array(arguments.length),l=(Q.get(this,"events")||{})[s.type]||[],c=k.event.special[s.type]||{};for(u[0]=s,t=1;t\x20\t\r\n\f]*)[^>]*)\/>/gi,qe=/\s*$/g;function Oe(e,t){return A(e,"table")&&A(11!==t.nodeType?t:t.firstChild,"tr")&&k(e).children("tbody")[0]||e}function Pe(e){return e.type=(null!==e.getAttribute("type"))+"/"+e.type,e}function Re(e){return"true/"===(e.type||"").slice(0,5)?e.type=e.type.slice(5):e.removeAttribute("type"),e}function Me(e,t){var n,r,i,o,a,s,u,l;if(1===t.nodeType){if(Q.hasData(e)&&(o=Q.access(e),a=Q.set(t,o),l=o.events))for(i in delete a.handle,a.events={},l)for(n=0,r=l[i].length;n")},clone:function(e,t,n){var r,i,o,a,s,u,l,c=e.cloneNode(!0),f=oe(e);if(!(y.noCloneChecked||1!==e.nodeType&&11!==e.nodeType||k.isXMLDoc(e)))for(a=ve(c),r=0,i=(o=ve(e)).length;r").attr(n.scriptAttrs||{}).prop({charset:n.scriptCharset,src:n.url}).on("load error",i=function(e){r.remove(),i=null,e&&t("error"===e.type?404:200,e.type)}),E.head.appendChild(r[0])},abort:function(){i&&i()}}});var Vt,Gt=[],Yt=/(=)\?(?=&|$)|\?\?/;k.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var e=Gt.pop()||k.expando+"_"+kt++;return this[e]=!0,e}}),k.ajaxPrefilter("json jsonp",function(e,t,n){var r,i,o,a=!1!==e.jsonp&&(Yt.test(e.url)?"url":"string"==typeof e.data&&0===(e.contentType||"").indexOf("application/x-www-form-urlencoded")&&Yt.test(e.data)&&"data");if(a||"jsonp"===e.dataTypes[0])return r=e.jsonpCallback=m(e.jsonpCallback)?e.jsonpCallback():e.jsonpCallback,a?e[a]=e[a].replace(Yt,"$1"+r):!1!==e.jsonp&&(e.url+=(St.test(e.url)?"&":"?")+e.jsonp+"="+r),e.converters["script json"]=function(){return o||k.error(r+" was not called"),o[0]},e.dataTypes[0]="json",i=C[r],C[r]=function(){o=arguments},n.always(function(){void 0===i?k(C).removeProp(r):C[r]=i,e[r]&&(e.jsonpCallback=t.jsonpCallback,Gt.push(r)),o&&m(i)&&i(o[0]),o=i=void 0}),"script"}),y.createHTMLDocument=((Vt=E.implementation.createHTMLDocument("").body).innerHTML="
",2===Vt.childNodes.length),k.parseHTML=function(e,t,n){return"string"!=typeof e?[]:("boolean"==typeof t&&(n=t,t=!1),t||(y.createHTMLDocument?((r=(t=E.implementation.createHTMLDocument("")).createElement("base")).href=E.location.href,t.head.appendChild(r)):t=E),o=!n&&[],(i=D.exec(e))?[t.createElement(i[1])]:(i=we([e],t,o),o&&o.length&&k(o).remove(),k.merge([],i.childNodes)));var r,i,o},k.fn.load=function(e,t,n){var r,i,o,a=this,s=e.indexOf(" ");return-1").append(k.parseHTML(e)).find(r):e)}).always(n&&function(e,t){a.each(function(){n.apply(this,o||[e.responseText,t,e])})}),this},k.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(e,t){k.fn[t]=function(e){return this.on(t,e)}}),k.expr.pseudos.animated=function(t){return k.grep(k.timers,function(e){return t===e.elem}).length},k.offset={setOffset:function(e,t,n){var r,i,o,a,s,u,l=k.css(e,"position"),c=k(e),f={};"static"===l&&(e.style.position="relative"),s=c.offset(),o=k.css(e,"top"),u=k.css(e,"left"),("absolute"===l||"fixed"===l)&&-1<(o+u).indexOf("auto")?(a=(r=c.position()).top,i=r.left):(a=parseFloat(o)||0,i=parseFloat(u)||0),m(t)&&(t=t.call(e,n,k.extend({},s))),null!=t.top&&(f.top=t.top-s.top+a),null!=t.left&&(f.left=t.left-s.left+i),"using"in t?t.using.call(e,f):c.css(f)}},k.fn.extend({offset:function(t){if(arguments.length)return void 0===t?this:this.each(function(e){k.offset.setOffset(this,t,e)});var e,n,r=this[0];return r?r.getClientRects().length?(e=r.getBoundingClientRect(),n=r.ownerDocument.defaultView,{top:e.top+n.pageYOffset,left:e.left+n.pageXOffset}):{top:0,left:0}:void 0},position:function(){if(this[0]){var e,t,n,r=this[0],i={top:0,left:0};if("fixed"===k.css(r,"position"))t=r.getBoundingClientRect();else{t=this.offset(),n=r.ownerDocument,e=r.offsetParent||n.documentElement;while(e&&(e===n.body||e===n.documentElement)&&"static"===k.css(e,"position"))e=e.parentNode;e&&e!==r&&1===e.nodeType&&((i=k(e).offset()).top+=k.css(e,"borderTopWidth",!0),i.left+=k.css(e,"borderLeftWidth",!0))}return{top:t.top-i.top-k.css(r,"marginTop",!0),left:t.left-i.left-k.css(r,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var e=this.offsetParent;while(e&&"static"===k.css(e,"position"))e=e.offsetParent;return e||ie})}}),k.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(t,i){var o="pageYOffset"===i;k.fn[t]=function(e){return _(this,function(e,t,n){var r;if(x(e)?r=e:9===e.nodeType&&(r=e.defaultView),void 0===n)return r?r[i]:e[t];r?r.scrollTo(o?r.pageXOffset:n,o?n:r.pageYOffset):e[t]=n},t,e,arguments.length)}}),k.each(["top","left"],function(e,n){k.cssHooks[n]=ze(y.pixelPosition,function(e,t){if(t)return t=_e(e,n),$e.test(t)?k(e).position()[n]+"px":t})}),k.each({Height:"height",Width:"width"},function(a,s){k.each({padding:"inner"+a,content:s,"":"outer"+a},function(r,o){k.fn[o]=function(e,t){var n=arguments.length&&(r||"boolean"!=typeof e),i=r||(!0===e||!0===t?"margin":"border");return _(this,function(e,t,n){var r;return x(e)?0===o.indexOf("outer")?e["inner"+a]:e.document.documentElement["client"+a]:9===e.nodeType?(r=e.documentElement,Math.max(e.body["scroll"+a],r["scroll"+a],e.body["offset"+a],r["offset"+a],r["client"+a])):void 0===n?k.css(e,t,i):k.style(e,t,n,i)},s,n?e:void 0,n)}})}),k.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,n){k.fn[n]=function(e,t){return 0 { 53 | // console.log("audioQueue") 54 | // console.log(this.audioQueue.length()) 55 | if (this.audioQueue.length()) { 56 | 57 | e.outputBuffer.getChannelData(0).set(this.audioQueue.read(this.config.bufferSize)); 58 | } 59 | else { 60 | e.outputBuffer.getChannelData(0).set(this.silence); 61 | } 62 | } 63 | 64 | this.scriptNode.connect(this.audioCtx.destination); 65 | } 66 | 67 | //将传入的16000频率的声音数据转成48000频率的声音数据 68 | interleave(e){ 69 | let t = e.length; 70 | let sampleRate = 16000.0 ; 71 | let outputSampleRate = 48000.0; 72 | let s = 0; 73 | let o = sampleRate / outputSampleRate; 74 | let u = Math.ceil(t * outputSampleRate / sampleRate); 75 | let a = new Float32Array(u); 76 | let i=0; 77 | for (i = 0; i < u; i++) { 78 | a[i] = e[Math.floor(s)]; 79 | s += o; 80 | } 81 | return a; 82 | } 83 | 84 | //MDN上说,audioporcess缓冲区的数据是,非交错的32位线性PCM,标称范围介于-1和之间+1, 85 | //即32位浮点缓冲区,每个样本介于-1.0和1.0之间。 86 | //http://www.i2geek.com/mobile/articleContent.php?id=19544 87 | //https://webaudio.github.io/web-audio-api/#APIOverview 88 | write(chunk) { 89 | //将传入的blob转换成 Float32Array对象 90 | var reader = new FileReader(); 91 | var my_audioQueue= this.audioQueue; 92 | var my_int16ToFloat32= this.int16ToFloat32 93 | var my_interleave= this.interleave; 94 | reader.onload = function(e) { 95 | //console.log(reader.result); 96 | let new_chunk=reader.result; 97 | 98 | //A.如果输入是: 16000hz 16位声音数据 99 | //let testDataInt = new Int8Array(new_chunk); 100 | let testDataInt = new Int16Array(new_chunk); 101 | //16位对象转32位对象 102 | let array = my_int16ToFloat32(testDataInt,0,testDataInt.length); 103 | //16000hz 转 48000hz 104 | array= my_interleave(array); 105 | //console.log(array.length,array); 106 | console.log("get wav bytes:",array.length); 107 | //B.如果输入是: 48000hz 16位声音数据 108 | /* 109 | let array = new Float32Array(new_chunk); 110 | console.log(array.length,array); 111 | */ 112 | my_audioQueue.write(array); 113 | }; 114 | reader.readAsArrayBuffer(chunk); 115 | } 116 | 117 | 118 | 119 | stop() { 120 | this.audioQueue.clear(); 121 | this.scriptNode.disconnect(); 122 | this.scriptNode = null; 123 | } 124 | 125 | isPlaying() { 126 | return !! this.scriptNode; 127 | } 128 | 129 | int16ToFloat32(inputArray, startIndex, length) { 130 | var output = new Float32Array(inputArray.length-startIndex); 131 | for (var i = startIndex; i < length; i++) { 132 | var int = inputArray[i]; 133 | // If the high bit is on, then it is a negative number, and actually counts backwards. 134 | var float = (int >= 0x8000) ? -(0x10000 - int) / 0x8000 : int / 0x7FFF; 135 | output[i] = float; 136 | } 137 | return output; 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/data/style.css: -------------------------------------------------------------------------------- 1 | .video { 2 | margin-left: auto; 3 | margin-right: auto; 4 | height: 480px; 5 | width: 640px; 6 | display: block; 7 | } 8 | -------------------------------------------------------------------------------- /esp32/esp32_remote_mic2/esp32_remote_mic2.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "I2S.h" 4 | #include "Wav.h" 5 | #include 6 | #include 7 | #include 8 | 9 | //注意: 10 | // 除了编译烧写本程序,还需要使用 ESP32 SKetch Data Upload 将data目录的一些网页文件等上传至 SPIFFS 11 | 12 | //声音流: 16000hz, 16位 单声道 pcm数据 32KB/S 256kbps/s 13 | //设置语句如下: 14 | // I2S_Init(I2S_MODE_RX, 16000, I2S_BITS_PER_SAMPLE_16BIT); 15 | 16 | const char *ssid = "CMCC-r3Ff"; 17 | const char *password = "9999900000"; 18 | AsyncWebServer server(80); 19 | WebSocketsServer webSocket = WebSocketsServer(1331); 20 | const int numCommunicationData = 8000; 21 | //数组:8000字节缓冲区 22 | char communicationData[numCommunicationData]; //1char=8bits 1byte=8bits 1int=8*2 1long=8*4 23 | char buff[100]; 24 | 25 | //这个psram版本就是提前分配了一个内存区,专用于存放声音数据,调用百度声音识别时不需要借助于SD卡做为交换. 26 | //wav文件头的44个字节, 为base64 处理需要, 必须是6的倍数, 多加点数据 27 | 28 | const int headerSize = 44; 29 | byte wav_head[headerSize]; 30 | 31 | uint8_t connect_no = -1; //当前连接号 32 | bool sound_skip = false; //控制发送声音html5中断跳出 33 | 34 | // Callback: receiving any WebSocket message 35 | void onWebSocketEvent(uint8_t client_num, 36 | WStype_t type, 37 | uint8_t * payload, 38 | size_t length) { 39 | String tmpstr ; 40 | int loop1; 41 | int sound_second = 0; 42 | 43 | int16_t val16 = 0; 44 | uint8_t val1, val2; 45 | int32_t tmpval = 0; 46 | 47 | //注意:esp32 对于wssocket 在同一函数中,不支持多并发 48 | //但同一函数在执行时,可以并发另一函数事件 49 | //所以如果在供给一个函数mic输出时, 其它用户是进入不了的! 50 | 51 | int32_t low_sound = 10; //声音下限 低于此值的声音数据截掉 52 | int32_t high_sound = 5000; //声音上限 高于此值的声音数据截掉 53 | 54 | int add_sound = 40; //音量加倍系数 55 | 56 | File wav_file ; 57 | 58 | switch (type) { 59 | case WStype_DISCONNECTED: 60 | Serial.printf("client:[%u] Disconnected!\n", client_num); 61 | Serial.printf("client:[%u] last connect_no=[%u] \n", client_num, connect_no); 62 | 63 | if (connect_no == client_num) 64 | { 65 | sound_skip = true; 66 | Serial.printf("client:[%u] trigger stop wav\n",client_num); 67 | } 68 | break; 69 | 70 | // New client has connected 71 | case WStype_CONNECTED: 72 | { 73 | IPAddress ip = webSocket.remoteIP(client_num); 74 | Serial.printf("client:[%u] Connection from %s\n", client_num,ip.toString().c_str()); 75 | //Serial.println(); 76 | } 77 | break; 78 | 79 | // Handle text messages from client 80 | case WStype_TEXT: 81 | 82 | /* 83 | Serial.printf("client:[%u] begin send test sound...\n",client_num); 84 | 85 | wav_file = SPIFFS.open("/cn_word.wav", FILE_READ); 86 | //改成文件直接读取,测试效果 87 | while (wav_file.available()) 88 | { 89 | int readnum = wav_file.read((uint8_t *)communicationData, numCommunicationData); 90 | if (readnum > 0) 91 | { 92 | webSocket.sendBIN(client_num, (uint8_t *)communicationData, readnum); 93 | } 94 | else 95 | break; 96 | } 97 | wav_file.close(); 98 | 99 | Serial.printf("client:[%u] end send test sound...\n",client_num); 100 | */ 101 | 102 | // Print out raw message 103 | //Serial.printf("client:[%u] [%u] seconds: %s\n",client_num, length, payload); 104 | sprintf(buff, "%s", payload); 105 | 106 | for (int loop1 = 0; loop1 < length; loop1++) 107 | { 108 | char ch = payload[loop1]; 109 | tmpstr += ch; 110 | } 111 | //Serial.println("tmpstr=" + tmpstr); 112 | sound_second = tmpstr.toInt(); 113 | //Serial.println(""); 114 | Serial.printf("client:[%u] request [%u] seconds\n", client_num,sound_second); 115 | 116 | Serial.printf("client:[%u] begin send wav sound ...\n", client_num ); 117 | 118 | if (sound_second > 0) 119 | { 120 | //先输出wav文件头 121 | //html5不需要,注意会产生嘀嘀噪音 122 | //如果用于html5播放端, wav head数据不需要传输 123 | //8000*4是一秒的wav数据量 124 | CreateWavHeader(wav_head, 8000 * sound_second * 4); 125 | webSocket.sendBIN(client_num, (uint8_t *)wav_head, headerSize); 126 | //send_wavhead = true; 127 | } 128 | 129 | //如果声音秒数是负数,表示中断上次连接 130 | if (sound_second < 0) 131 | { 132 | Serial.printf("client:[%u] sound_second<0\n",client_num); 133 | sound_skip = true; 134 | break; 135 | } 136 | 137 | //1. 0 表示不限时间输出 138 | if (sound_second == 0) 139 | { 140 | 141 | connect_no = client_num; 142 | sound_skip = false; 143 | //esp32的线程功能局限,一次只能服务1个网页 144 | //如果无限循环输出声音,则当html5客户端关闭后,新的连接不能进来,导致esp32必须重启 145 | //建议一次 30秒或60秒,即使 html5客户端关闭,时间到后也会自动关闭本次连接! 146 | //Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. 147 | 148 | 149 | //1/4秒 8000字节 150 | //每秒要循环4次读取音频数据 151 | //for 指定几秒就传输几秒数据 152 | //for ( loop1 = 0; loop1 < sound_second * 4; loop1++) 153 | //无限循环输出,直到当前html5连接主动关闭(比如网页关闭) 154 | while (true) 155 | { 156 | // yield(); 157 | if (sound_skip == true) 158 | { 159 | Serial.printf("client:[%u] skip send wav sound ...\n",client_num); 160 | break; 161 | } 162 | I2S_Read(communicationData, numCommunicationData); 163 | 164 | //for 提升音量 165 | for (int loop1 = 0; loop1 < numCommunicationData / 2 ; loop1++) 166 | { 167 | val1 = communicationData[loop1 * 2]; 168 | val2 = communicationData[loop1 * 2 + 1] ; 169 | val16 = val1 + val2 * 256; 170 | 171 | //add_sound 配置成40最合适 172 | //乘以40 :音量提升20db 173 | tmpval = val16 * add_sound; 174 | if (abs(tmpval) > 32767 ) 175 | { 176 | if (val16 > 0) 177 | tmpval = 32767; 178 | else 179 | tmpval = -32767; 180 | } 181 | 182 | //对声音设置上下限 183 | //防止噪音 184 | //if (abs(tmpval) > high_sound) 185 | // tmpval = high_sound; 186 | 187 | //Serial.println(String(val1) + " " + String(val2) + " " + String(val16) + " " + String(tmpval)); 188 | communicationData[loop1 * 2] = (byte)(tmpval & 0xFF); 189 | communicationData[loop1 * 2 + 1] = (byte)((tmpval >> 8) & 0xFF); 190 | } 191 | 192 | /* 193 | //for 排除噪音 194 | for (int loop1 = 0; loop1 < numCommunicationData / 2 ; loop1++) 195 | { 196 | val1 = communicationData[loop1 * 2]; 197 | val2 = communicationData[loop1 * 2 + 1] ; 198 | val16 = val1 + val2 * 256; 199 | 200 | //将高于指定音量,低于指定音量的数据排除 201 | 202 | tmpval = val16 * 1; 203 | //高于上限的噪音去除 204 | if (abs(tmpval) > high_sound ) 205 | { 206 | if (val16 > 0) 207 | tmpval = high_sound; 208 | else 209 | tmpval = -high_sound; 210 | } 211 | 212 | //低于下限的噪音,去除 213 | if (abs(tmpval) < low_sound ) 214 | { 215 | tmpval = 0; 216 | } 217 | 218 | //Serial.println(String(val1) + " " + String(val2) + " " + String(val16) + " " + String(tmpval)); 219 | communicationData[loop1 * 2] = (byte)(tmpval & 0xFF); 220 | communicationData[loop1 * 2 + 1] = (byte)((tmpval >> 8) & 0xFF); 221 | } 222 | */ 223 | 224 | //传输声音文件主体 225 | webSocket.sendBIN(client_num, (uint8_t *)communicationData, numCommunicationData); 226 | //webSocket.broadcastBIN((uint8_t *)communicationData, numCommunicationData, false); 227 | } 228 | } 229 | //2.有限时间输出 230 | else 231 | { 232 | connect_no = client_num; 233 | sound_skip = false; 234 | //esp32的线程功能局限,一次只能服务1个网页 235 | //如果无限循环输出声音,则当html5客户端关闭后,新的连接不能进来,导致esp32必须重启 236 | //建议一次 30秒或60秒,即使 html5客户端关闭,时间到后也会自动关闭本次连接! 237 | //Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. 238 | 239 | //1/4秒 8000字节 240 | //每秒要循环4次读取音频数据 241 | //for 指定几秒就传输几秒数据 242 | for ( loop1 = 0; loop1 < sound_second * 4; loop1++) 243 | //无限循环输出,直到当前html5连接主动关闭(比如网页关闭) 244 | //while (true) 245 | { 246 | // yield(); 247 | if (sound_skip == true) 248 | { 249 | Serial.printf("client:[%u] skip wav sound ...\n", client_num); 250 | break; 251 | } 252 | I2S_Read(communicationData, numCommunicationData); 253 | 254 | //for 提升音量 255 | for (int loop1 = 0; loop1 < numCommunicationData / 2 ; loop1++) 256 | { 257 | val1 = communicationData[loop1 * 2]; 258 | val2 = communicationData[loop1 * 2 + 1] ; 259 | val16 = val1 + val2 * 256; 260 | 261 | //add_sound 配置成40最合适 262 | //乘以40 :音量提升20db 263 | tmpval = val16 * add_sound; 264 | if (abs(tmpval) > 32767 ) 265 | { 266 | if (val16 > 0) 267 | tmpval = 32767; 268 | else 269 | tmpval = -32767; 270 | } 271 | 272 | //对声音设置上下限 273 | //防止噪音 274 | //if (abs(tmpval) > high_sound) 275 | // tmpval = high_sound; 276 | 277 | //Serial.println(String(val1) + " " + String(val2) + " " + String(val16) + " " + String(tmpval)); 278 | communicationData[loop1 * 2] = (byte)(tmpval & 0xFF); 279 | communicationData[loop1 * 2 + 1] = (byte)((tmpval >> 8) & 0xFF); 280 | } 281 | 282 | /* 283 | //for 排除噪音 284 | for (int loop1 = 0; loop1 < numCommunicationData / 2 ; loop1++) 285 | { 286 | val1 = communicationData[loop1 * 2]; 287 | val2 = communicationData[loop1 * 2 + 1] ; 288 | val16 = val1 + val2 * 256; 289 | 290 | //将高于指定音量,低于指定音量的数据排除 291 | 292 | tmpval = val16 * 1; 293 | //高于上限的噪音去除 294 | if (abs(tmpval) > high_sound ) 295 | { 296 | if (val16 > 0) 297 | tmpval = high_sound; 298 | else 299 | tmpval = -high_sound; 300 | } 301 | 302 | //低于下限的噪音,去除 303 | if (abs(tmpval) < low_sound ) 304 | { 305 | tmpval = 0; 306 | } 307 | 308 | //Serial.println(String(val1) + " " + String(val2) + " " + String(val16) + " " + String(tmpval)); 309 | communicationData[loop1 * 2] = (byte)(tmpval & 0xFF); 310 | communicationData[loop1 * 2 + 1] = (byte)((tmpval >> 8) & 0xFF); 311 | } 312 | */ 313 | 314 | //传输声音文件主体 315 | webSocket.sendBIN(client_num, (uint8_t *)communicationData, numCommunicationData); 316 | //webSocket.broadcastBIN((uint8_t *)communicationData, numCommunicationData, false); 317 | } 318 | } 319 | 320 | Serial.printf("client:[%u] end send wav sound ...\n", client_num ); 321 | 322 | break; 323 | 324 | // For everything else: do nothing 325 | case WStype_BIN: 326 | case WStype_ERROR: 327 | case WStype_FRAGMENT_TEXT_START: 328 | case WStype_FRAGMENT_BIN_START: 329 | case WStype_FRAGMENT: 330 | case WStype_FRAGMENT_FIN: 331 | default: 332 | break; 333 | } 334 | } 335 | 336 | 337 | // Callback: send homepage 338 | void onIndexRequest(AsyncWebServerRequest * request) { 339 | IPAddress remote_ip = request->client()->remoteIP(); 340 | Serial.println(" [" + remote_ip.toString() + 341 | "] HTTP GET request of " + request->url()); 342 | request->send(SPIFFS, "/index.html", "text/html"); 343 | } 344 | 345 | // Callback: send style sheet 346 | void onCSSRequest(AsyncWebServerRequest * request) { 347 | IPAddress remote_ip = request->client()->remoteIP(); 348 | //Serial.println("[" + remote_ip.toString() + 349 | // "] HTTP GET request of " + request->url()); 350 | request->send(SPIFFS, "/style.css", "text/css"); 351 | } 352 | 353 | void onjs_jqueryRequest(AsyncWebServerRequest * request) { 354 | IPAddress remote_ip = request->client()->remoteIP(); 355 | // Serial.println("[" + remote_ip.toString() + 356 | // "] HTTP GET request of " + request->url()); 357 | request->send(SPIFFS, "/jquery-3.4.1.min.js", "text/javascript"); 358 | } 359 | 360 | void onjs_liveRequest(AsyncWebServerRequest * request) { 361 | IPAddress remote_ip = request->client()->remoteIP(); 362 | //Serial.println("[" + remote_ip.toString() + 363 | // "] HTTP GET request of " + request->url()); 364 | request->send(SPIFFS, "/live.js", "text/javascript"); 365 | } 366 | 367 | void onjs_playerRequest(AsyncWebServerRequest * request) { 368 | IPAddress remote_ip = request->client()->remoteIP(); 369 | //Serial.println("[" + remote_ip.toString() + 370 | // "] HTTP GET request of " + request->url()); 371 | request->send(SPIFFS, "/player.js", "text/javascript"); 372 | } 373 | 374 | //void onjs_socketRequest(AsyncWebServerRequest *request) { 375 | // IPAddress remote_ip = request->client()->remoteIP(); 376 | // Serial.println("[" + remote_ip.toString() + 377 | // "] HTTP GET request of " + request->url()); 378 | // request->send(SPIFFS, "/socket.io.min.js", "text/javascript"); 379 | //} 380 | 381 | // Callback: send 404 if requested file does not exist 382 | void onPageNotFound(AsyncWebServerRequest * request) { 383 | IPAddress remote_ip = request->client()->remoteIP(); 384 | //Serial.println("[" + remote_ip.toString() + 385 | // "] HTTP GET request of " + request->url()); 386 | request->send(404, "text/plain", "Not found"); 387 | } 388 | 389 | 390 | void setup() { 391 | Serial.begin(115200); 392 | 393 | // Make sure we can read the file system 394 | if ( !SPIFFS.begin()) { 395 | Serial.println("Error mounting SPIFFS"); 396 | while (1); 397 | } 398 | 399 | //I2S_BITS_PER_SAMPLE_8BIT 配置的话,下句会报错, 400 | //最小必须配置成I2S_BITS_PER_SAMPLE_16BIT 401 | //I2S_Init(I2S_MODE_RX, 16000, I2S_BITS_PER_SAMPLE_16BIT); 402 | 403 | I2S_Init(I2S_MODE_RX, 16000, I2S_BITS_PER_SAMPLE_16BIT); 404 | connectwifi(); 405 | 406 | // On HTTP request for root, provide index.html file 407 | server.on("/", HTTP_GET, onIndexRequest); 408 | 409 | // On HTTP request for style sheet, provide style.css 410 | server.on("/style.css", HTTP_GET, onCSSRequest); 411 | 412 | server.on("/jquery-3.4.1.min.js", HTTP_GET, onjs_jqueryRequest); 413 | server.on("/live.js", HTTP_GET, onjs_liveRequest); 414 | server.on("/player.js", HTTP_GET, onjs_playerRequest); 415 | // server.on("/socket.io.min.js", HTTP_GET, onjs_socketRequest); 416 | 417 | // Handle requests for pages that do not exist 418 | server.onNotFound(onPageNotFound); 419 | 420 | // Start web server 421 | server.begin(); 422 | 423 | // Start WebSocket server and assign callback 424 | webSocket.begin(); 425 | webSocket.onEvent(onWebSocketEvent); 426 | } 427 | 428 | void connectwifi() 429 | { 430 | if (WiFi.status() == WL_CONNECTED) return; 431 | WiFi.disconnect(); 432 | delay(200); 433 | WiFi.config(IPAddress(192, 168, 1, 100), //设置静态IP位址 434 | IPAddress(192, 168, 1, 1), 435 | IPAddress(255, 255, 255, 0), 436 | IPAddress(192, 168, 1, 1) 437 | ); 438 | WiFi.mode(WIFI_STA); 439 | Serial.println("Connecting to WIFI"); 440 | WiFi.begin(ssid, password); 441 | while ((!(WiFi.status() == WL_CONNECTED))) { 442 | delay(1000); 443 | Serial.print("."); 444 | } 445 | Serial.println("Connected"); 446 | Serial.println("My Local IP is : "); 447 | Serial.println(WiFi.localIP()); 448 | } 449 | 450 | 451 | void loop() { 452 | connectwifi(); 453 | // Look for and handle WebSocket data 454 | webSocket.loop(); 455 | } 456 | --------------------------------------------------------------------------------