Friday 24 April 2020

filter media stream

/**
    * filter media stream to reduce noise and more
    * if not filtered or have any problem then
    * return original stream itself
    *
    * @param stream    MediaStream   Source to Video tag
    * @return MediaStream
    */
    async function filterMediaStream(stream) {
      
      try {
        //filter media stream
        var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
        var source = audioCtx.createMediaStreamSource(stream);

        var delay = audioCtx.createDelay(1.5);

        // Create a biquadfilter
        var biquadFilter = audioCtx.createBiquadFilter();

        biquadFilter.Q.value = 8.5;
        biquadFilter.frequency.value = 550;//350 - default value
        biquadFilter.gain.value = 1;
        biquadFilter.type = 'bandpass';

        source.connect(biquadFilter);
        biquadFilter.connect(delay);
        delay.connect(audioCtx.destination);
        //assign user media          
        return await stream;
      } catch (error) {
        return null
        console.log('Error with media filtering: 'error.messageerror.name);
        //if u have problem with filtering media stream 
        //then return the original stream copy
      }
    }

Wednesday 15 April 2020

error invalid_scope


 Check the
 
const OAUTH_SCOPES='openid profile offline_access user.read calendars.read';
 


http://localhost/wteam/?error=invalid_scope&error_description=The%20provided%20value%20for%20the%20input%20parameter%20%27scope%27%20is%20not%20valid.%20The%20scope%20%27OnlineMeetings.ReadWrite%27%20is%20not%20valid.&state=b26c7e96816d6a543452c52f4ef03d4e

GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem

1. https://curl.haxx.se/ca/cacert.pem

download cacert.pem file
 
2. php.ini

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo ="C:/wamp64/www/wteam/cacert.pem"

3. restart services : Done
 
( ! ) Fatal error: Uncaught GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in C:\wamp64\www\wteam\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 201
( ! ) GuzzleHttp\Exception\RequestException: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in C:\wamp64\www\wteam\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php on line 201
Call Stack
#TimeMemoryFunctionLocation
10.0070367272{main}( )...\index.php:0
20.0595528752AuthController->callback( )...\index.php:86
30.0595529128League\OAuth2\Client\Provider\GenericProvider->getAccessToken( )...\index.php:70
40.0710609440League\OAuth2\Client\Provider\GenericProvider->getParsedResponse( )...\AbstractProvider.php:537
50.0710609440League\OAuth2\Client\Provider\GenericProvider->getResponse( )...\AbstractProvider.php:621
60.0710609440GuzzleHttp\Client->send( )...\AbstractProvider.php:608
70.4408647328GuzzleHttp\Promise\RejectedPromise->wait( )...\Client.php:130

Monday 13 April 2020

Benchmark between Symfony HttpFoundation and Nette Http RequestFactory

<?php

require_once 'vendor/autoload.php';
$s = 0;
$n = 0;

$bench = new Ubench;
for ($i=0; $i < 10000; $i++) {
   
$bench->start();
$request = Symfony\Component\HttpFoundation\Request::createFromGlobals();
$bench->end();

// Get elapsed time and memory
$s += $bench->getTime(true); // elapsed microtime in float

$bench->start();
$request = new Nette\Http\RequestFactory;
$request->fromGlobals();
$bench->end();

$n += $bench->getTime(true); // elapsed microtime in float

}

echo "<br>Symfony average request: " . $s/10000 . '<br>';
echo "Nette average request: " . $n/10000;


Result: Average request Time

Welcome to the framework
Symfony average request : 0.0016007466554642
Nette average request: 0.0020953632354736

        "symfony/http-foundation":"5.0.7",

symfony/http-foundation is the clear winner


        "symfony/http-foundation":"5.0.7",

        "nette/http""^3.0"

Sunday 12 April 2020

Benchmark between Symfony DependencyInjection ContainerBuilder AND League Container

<?php

require_once 'vendor/autoload.php';
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;


class Bar {}

class Foo
{
    public $bar;

    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
}

   
$bench = new Ubench;

$bench->start();


$container = new League\Container\Container;

$container->add(\Foo::class)->addArgument(\Bar::class);
$container->add(\Bar::class);

$foo = $container->get(\Foo::class);

var_dump($foo instanceof \Foo);      // true
var_dump($foo->bar instanceof \Bar); // true
$bench->end();
echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>League';
// Get elapsed time and memory
echo  '<br>' . $bench->getTime() . '<br>'; // 156ms or 1.123s
echo $bench->getTime(true) . '<br>'; // elapsed microtime in float

echo $bench->getMemoryPeak() . 'Peak memory<br>';

echo $bench->getMemoryPeak(true) . '<br>'; // memory peak in bytes

// Returns the memory usage at the end mark
echo $bench->getMemoryUsage() . '<br>';


echo '<br>';
echo '<br>';
echo '<br>';
echo '<br>Symfony';
$bench->start();

$containerBuilder = new ContainerBuilder();
$containerBuilder
    ->register('bar', 'Bar');


$containerBuilder
    ->register('foo', 'Foo')
    ->addArgument(new Reference('bar'));

$foo = $containerBuilder->get('foo');

var_dump($foo instanceof \Foo);      // true
var_dump($foo->bar instanceof \Bar); // true
$bench->end();

// Get elapsed time and memory
echo  '<br>' . $bench->getTime() . '<br>'; // 156ms or 1.123s
echo $bench->getTime(true) . '<br>'; // elapsed microtime in float

echo $bench->getMemoryPeak() . 'Peak memory<br>';

echo $bench->getMemoryPeak(true) . '<br>'; // memory peak in bytes

// Returns the memory usage at the end mark
echo $bench->getMemoryUsage() . '<br>';

 Result:

League

C:\wamp64\www\mecrud\index1.php:33:boolean true
C:\wamp64\www\mecrud\index1.php:34:boolean true

6ms
0.0061249732971191
Peak memory : 2097152

Symfony

C:\wamp64\www\mecrud\index1.php:68:boolean true
C:\wamp64\www\mecrud\index1.php:69:boolean true

6ms
0.0063478946685791
Peak memory: 2097152



Composer require

 "require": {
        "php""^7.2.5",

        "league/container""^3.3",
        "symfony/dependency-injection""^5.0"
    },
    "require-dev": { 
        "devster/ubench""~2.0.0"
    },


Multiple run gives mixed result. I decided to use league (https://container.thephpleague.com/).

Friday 10 April 2020

Benchmark between PHLAK\Config and Noodlehaus\Config Object

Benchmark between PHLAK\Config and Noodlehaus\Config Object

<?php
echo 'Welcome to the framework';

require_once 'vendor/autoload.php';

$bench = new Ubench;

$bench->start();

// // Execute some code
use Noodlehaus\Config;
use Noodlehaus\Parser\Json;
//use PHLAK\Config\Config;
// Load a single file mutiple time
//for ($i=0; $i < 2333; $i++) {
    //$config = Config::load('composer.json');
    $config = new Config('test.json');
//}




$bench->end();


use PHLAK\Config\Config;
0.0043570995330811
Peak memory: 2097152


Noodlehaus\Config Object
0.0045328140258789
Peak memory : 2097152



Both equally doing good a very small performance with PHLAK, so i decided to use PHLAK.

with direct php array in PHLAK
0.0019919872283936
Peak memory: 2097152


i am going to use php array directly or json if required

Sunday 5 April 2020

TinyMCE example with toggle using on blur event

<script type="text/javascript">
    $(document).ready(function(){
             
            function add(){
                       return  tinyMCE.init({
                        selector: "div#questionEdit textarea",
                        plugins: [
                            "advlist autolink lists link image charmap print preview anchor",
                            "searchreplace visualblocks code fullscreen",
                            "insertdatetime media table paste imagetools wordcount"
                        ],
                        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
                        media_dimensions: false,
                        
                        browser_spellcheck : true,
                        media_live_embeds: true,
                        mediaembed_max_width: 450,
                        height : 150,
                        extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|style],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style|face]",

                        theme_advanced_toolbar_location: "top",
                        theme_advanced_toolbar_align: "left",


                        setup: function(editor) {

                            editor.on('blur', function(e) {
                                var questionId = $(".questionEditLayout .questionNoLayout").attr('id');
                                var description = $("#" +questionId).attr('description');
                                //var questionContent = $(this).val();
                                var questionContent =editor.getContent();

                                if(questionContent=='' ||questionContent == null ){
                                    if(description){
                                        $('.questionEditLayout #questionEdit').slideDown('slow').before('<p class="panelerror">Description should not empty!</p>');
                                        setTimeout(function () {
                                            $('.panelerror').slideUp("slow", function(){
                                                $(this).remove();
                                            });
                                        }, 3000);
                                    }else{
                                $('.questionEditLayout #questionEdit').slideDown('slow').before('<p class="panelerror">Questions should not empty!</p>');
                                    setTimeout(function () {
                                        $('.panelerror').slideUp("slow", function(){
                                            $(this).remove();
                                        });
                                    }, 3000);
                                }
                                }

                                /*else if(questionContent.length>1500){
                                    $('.questionEditLayout #questionEdit').slideDown('slow').before('<p class="panelerror">Maximum length of hte question should be 1500!</p>');
                                    setTimeout(function () {
                                        $('.panelerror').slideUp("slow", function(){
                                            $(this).remove();
                                        });
                                    }, 3000);
                                }*/
                                else{
                                    $("#" + questionId + " .questionListContainer div.error").hide();
                                }

                                //$("div#dragContainer #addDescription").append(question);
                                if(description){
                                    $(".quizLayout #"+questionId+" div.addDescription").html(questionContent);
                                }else{

                                    $(".quizLayout #"+questionId+" h2.questionLabel").html(questionContent);
                                }
                                //on focuss out remove tini mce
                                editor.remove();  
                            });
                        }
                });  
   
            };
            
            //add tinymce on click
            $("textarea#question_editor").click(function(){
                return add();
            });



    });
</script>