Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Saturday 3 August 2019

Speech to Voice in Website using Javascript

Speech to Voice in Website using Javascript

W3C Community introduced the Web Speech API specification. Its a wonderful feature to introduce voice enabled commands on your website which works in Chrome Browser.

Convert Speech to Text

Below code just convert voice speech to text that you can use to make any search on your website or to navigate to another link on your website. Also you can make simple Bot on your website using this feature.

window.SpeechRecognition = window.webkitSpeechRecognition || window.SpeechRecognition;
console.log(window.SpeechRecognition);
if ('SpeechRecognition' in window{
    const recognition = new window.SpeechRecognition();
    recognition.onresult = (event) => {
        let speechToText = event.results[0][0].transcript;
        console.log("speech text", speechToText);
    }

    recognition.start();
    console.log('Ready to receive a command.');
} else {
    alert("speaking not working");
    // speech recognition API not supported
}



Below code is used to output speech on Chrome browser.
speechSynthesis.getVoices(); method is used to get voices from various countries accents.

var m = 'Hi How are you?';
if ('speechSynthesis' in window{
    const msg = new SpeechSynthesisUtterance(m);
    const voices = window.speechSynthesis.getVoices();
    // msg.voice = voices[9];
    //msg.voice = voices[4];
    console.log("speaking: " + m);
}

Saturday 10 February 2018

Check Uploaded File Size And Type JQuery

Check Uploaded File Size And Type JQuery

We upload files in web applications, sometimes we require to get uploaded file size and type at client side. I am sharing my JavaScript code to get uploaded file size and type at client side.

jQuery("input[name='member-file']").change(function () {
    var type = '';
    var iSize = 0;
    var error = true;
    if (jQuery.browser.msie{
        var objFSO = new ActiveXObject("Scripting.FileSystemObject");
        var sPath = jQuery(this)[0].value;
        var objFile = objFSO.getFile(sPath);
        iSize = objFile.size;
        iSize = iSize / 1024;
        type = objFile.type;
    } else {
        iSize = (jQuery(this)[0].files[0].size / 1024);
        type = jQuery(this)[0].files[0].type;
    }

    var TypeArr = [
        'video/mp4',
        'image/jpg',
        'image/jpeg',
        'image/gif',
        'image/png'
    ];

    jQuery(TypeArr).each(function (i, l) {
        if (type == l{
            error = false;
            return false;
        }
    });

    if (error == false{
        if (iSize > 0{
            iSize = (Math.round((iSize / 1024* 100/ 100)
        }

        if (iSize > 10{
            error = true;
        }
    }

    if (error == true{
        // show error
        return false;
    } else {
        // no error
        return true;
    }
});

Monday 10 November 2014

Webcam Broadcast using WebRTC and JavaScript

Webcam Broadcast using WebRTC and JavaScript


Introduction:

This document is created to describe technologies used and process flow of Video Broadcasting project.

Prerequisites:

Basic knowledge: HTML5, CSS, JQuery/JavaScript, experience in Online Chat application
Experience of Node.js would also be useful.
Installed on your development machine:
  • Google Chrome or Firefox.
  • Code editor.
  • Web cam.
  • Node.js with socket.io and express.

Technologies:

Node.JS

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. We used Node.js for making signalling server.

Download latest node.js - http://nodejs.org/download/. Install Node.Js using windows binary

WebRTC

Its a free, open project that enables web browsers with Real-Time Communications (RTC) capabilities via simple JavaScript APIs. The WebRTC components have been optimized to best serve this purpose. 

Socket.IO

Aims to make Realtime Apps possible in every browser and mobile device.
WEBRTC Video Conference chat app requires some modules i.e. socket.io, express

INSTALLATION

1. create folder videoconf
2. open command prompt. Go to path of videoconf folder.
3. command to install socket.io – [path to nodejs folder]/npm install socket.io
4. command to install express - [path to nodejs folder]/npm install express

Note: socket.io and express modules will install by node.js in folder videoconf/node_modules.

Node.js automatically assigns Apache’s Server Host to Socket.io.js and will use as follows:

Client side: 
script src = "http://locahost/socket.io/socket.io.js" > < /script>


Server.js

Server.js file is used for Communication/Chat Server. Server.js makes socket connections, receives messages to and from clients, can run any Javascript functionality on Server side like making AJAX requests from Server side only, converting video formats using FFMPEG command.

Server.js sample:

var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
    socket.emit('news', {
        hello: 'world'
    });
    socket.on('my other event', function (data) {
        console.log(data);
    });
});


Note: any unique port can assign Multiple servers can create on one platform using multiple server.js files but need to give different ports.

5. RUN SERVER - [path to nodejs folder]/node [path to webchat folder]/server.js.
6. Server will run in command prompt
7. on Linux to run server in background, I used: nohup command

Command to run any Linux command oin background:

nohup node server.js

client.js sample:

<script src="http://localhost/socket.io/socket.io.js"></script>

<!-- socket.io.js will run on your localhost and this path is automatically set by node.js. Its node.js magic. -->

var socket = io.connect('http://localhost');
socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', {
        my: 'data'
    });
});

navigator.getUserMedia

navigator.getUserMedia function of Mozilla and Chrome bowser us used to open webcam in the browser.


RTCPeerConnection.js

Javascript class RTCPeerConnection is the WebRTC API for video and audio calling.



HTML5

HTML5 is used at client side to get stream and give stream to Video element.

Process flow for Webcam Broadcasting:

1.    We run server.js file on server using node command[described above]. The code is in javascript. We can use jquery also we need to install jquery module in node. We assign one unique port in server.js which is used to make connection on client side.
2.    On client side A user using HTML5, JQuery and Socket.io creates socket connection on server using unique port
3.    Also A user make global object of Conference class [conference.js].
4.    On connect method, A user asked to open the webcam.
5.    Once browser gets A user’s webcam stream, stream get attach with conference attach using config.attachstream method.
6.    Conference object sends offer to other peer.
7.    When other peer B user connects to server with above procedure, he gets offer sent by A user peer connection and also receives A user’s stream
8.    B user in return sends answer to A user that he joins him. But we are not asking B user to open the webcam because he will be viewer only.
9.    Once both users get joined and B User receives A user’s webcam stream using socket.getremotestream function, and puts stream in video element.
10.  B user starts viewing A’s webcam.
11.  A’s stream stops when user call leave function of conference.
12.  If A user close his browser, his stream will stop sending.
13.  We can notify B user and other peer connections that host has closed his webcam
14.  Any number of peer connections can be made. We need to test load.

File Structure in Fleshcast for Webcam Broadcasting:

1.            Server.js - placed on root
2.            RTCPeerConnection.js and conference.js JS files placed in assets/js folder
3.            HTML5 and Javascript code to make connection and open webcam is done in golive method in sitemodels controller.

References:

Create .ICS file using PHP code

Recently worked on creating a .ics file in PHP after a very long time, code so thought to share with everybody. Please find below the comple...