Last active
October 7, 2019 22:40
-
-
Save thephucit/cb1a6a2c1537be8c4325d405b9fe61ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| *@author: The Phuc | |
| *@since: 06/01/2017 | |
| */ | |
| (function(window, angular, undefined) { | |
| 'use strict'; | |
| var ngRouteModule = angular.module('ssStream', ['ng']).provider('$ssStream', $ssStream); | |
| function $ssStream() { | |
| window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext; | |
| navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; | |
| var context = new AudioContext(); | |
| var session = {audio: true, video: false}; | |
| var bufferSize = 16384; | |
| /*convert Int16Array to Float32Array*/ | |
| var decode = function(raw) { | |
| var data = new Int16Array(raw); | |
| var output = new Float32Array(data.length); | |
| for (var i = 0; i < data.length; i++) { | |
| var int = data[i]; | |
| var float = (int >= 0x8000) ? -(0x10000 - int) / 0x8000 : int / 0x7FFF; | |
| output[i] = float; | |
| } | |
| return output; | |
| }; | |
| /*convert Float32Array to Int16Array*/ | |
| var encode = function(buffer) { | |
| var l = buffer.length; | |
| var buf = new Int16Array(l); | |
| while (l--) | |
| buf[l] = Math.min(1, buffer[l]) * 0x7FFF; | |
| return buf.buffer; | |
| }; | |
| this.$get = function() { | |
| return { | |
| listen: function(callback) { | |
| navigator.getUserMedia(session, function(stream) { | |
| var audioInput = context.createMediaStreamSource(stream); | |
| var recorder = context.createScriptProcessor(bufferSize, 1, 1); | |
| recorder.onaudioprocess = function(stream) { | |
| var left = stream.inputBuffer.getChannelData(0); | |
| callback(encode(left)); | |
| }; | |
| audioInput.connect(recorder); | |
| recorder.connect(context.destination); | |
| }, function(er){ | |
| console.log(er.message); | |
| }); | |
| }, | |
| say: function(raw) { | |
| var data = decode(raw); | |
| console.log(data); | |
| var source = context.createBufferSource(); | |
| source.connect(context.destination); | |
| var buffer = context.createBuffer(1, data.length, context.sampleRate); | |
| buffer.getChannelData(0).set(data); | |
| source.buffer = buffer; | |
| source.start(0); | |
| }, | |
| }; | |
| }; | |
| } | |
| })(window, window.angular); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| *@author: The Phuc | |
| *@since: 06/01/2017 | |
| */ | |
| // client side | |
| var socket = io.connect('192.168.2.200:3000'); | |
| var session = {audio: true, video: false}; | |
| window.AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext; | |
| navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; | |
| var context = new AudioContext(); | |
| navigator.getUserMedia(session, function(stream) { | |
| var audioInput = context.createMediaStreamSource(stream); | |
| var bufferSize = 16384;//16384 | |
| var recorder = context.createScriptProcessor(bufferSize, 1, 1); | |
| recorder.onaudioprocess = function recorderProcess(e) { | |
| var left = e.inputBuffer.getChannelData(0); | |
| socket.emit('broadcast', convertFloat32ToInt16(left)); | |
| }; | |
| audioInput.connect(recorder); | |
| recorder.connect(context.destination); | |
| }, function(er){ | |
| console.log(er.message); | |
| }); | |
| function convertFloat32ToInt16(buffer) { | |
| l = buffer.length; | |
| buf = new Int16Array(l); | |
| while (l--) | |
| buf[l] = Math.min(1, buffer[l]) * 0x7FFF; | |
| return buf.buffer; | |
| } | |
| function convertInt16ToFloat32(raw) { | |
| var data = new Int16Array(raw); | |
| var output = new Float32Array(data.length); | |
| for (var i = 0; i < data.length; i++) { | |
| var int = data[i]; | |
| var float = (int >= 0x8000) ? -(0x10000 - int) / 0x8000 : int / 0x7FFF; | |
| output[i] = float; | |
| } | |
| return output; | |
| } | |
| socket.on('broadcast', function(raw) { | |
| var data = convertInt16ToFloat32(raw); | |
| console.log(data); | |
| var source = context.createBufferSource(); | |
| source.connect(context.destination); | |
| var buffer = context.createBuffer(1, data.length, context.sampleRate); | |
| buffer.getChannelData(0).set(data); | |
| source.buffer = buffer; | |
| source.start(0); | |
| }); | |
| // server side | |
| var io = require('socket.io')(3000); | |
| io.on('connection', function(socket) { | |
| console.log('user connected'); | |
| socket.on('broadcast', function(buffer) { | |
| socket.broadcast.emit('broadcast', buffer); | |
| }); | |
| socket.on('disconnect', function() { | |
| console.log('user disconnected'); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| *@author: The Phuc | |
| *@since: 06/01/2017 | |
| */ | |
| $ssStream.listen(function(data) { | |
| Socket.emit('broadcast', data); | |
| }); | |
| Socket.on('broadcast', function(raw) { | |
| $ssStream.say(raw); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment