Saturday, September 23, 2017

Guitar Fretboard Notes in PHP

Jump to here to see how the specific strings are populated with correct note values.

<?php


$notes = [ 
 ['C' , '16.35'] , 
 ['C♯' , '17.32'] , 
 ['D' , '18.35'] , 
 ['E♭' , '19.45'] , 
 ['E' , '20.60'] , 
 ['F' , '21.83'] , 
 ['F♯' , '23.12'] , 
 ['G' , '24.50'] , 
 ['G♯' , '25.96'] , 
 ['A' , '27.50'] , 
 ['B♭' , '29.14'] , 
 ['B' , '30.87'] , 
 ['C' , '32.70'] , 
 ['C♯' , '34.65'] , 
 ['D' , '36.71'] , 
 ['E♭' , '38.89'] , 
 ['E' , '41.20'] , 
 ['F' , '43.65'] , 
 ['F♯' , '46.25'] , 
 ['G' , '49.00'] , 
 ['G♯' , '51.91'] , 
 ['A' , '55.00'] , 
 ['B♭' , '58.27'] , 
 ['B' , '61.74'] , 
 ['C' , '65.41'] , 
 ['C♯' , '69.30'] , 
 ['D' , '73.42'] , 
 ['E♭' , '77.78'] , 
 ['E' , '82.41'] , 
 ['F' , '87.31'] , 
 ['F♯' , '92.50'] , 
 ['G' , '98.00'] , 
 ['G♯' , '103.80'] , 
 ['A' , '110.00'] , 
 ['B♭' , '116.50'] , 
 ['B' , '123.50'] , 
 ['C' , '130.80'] , 
 ['C♯' , '138.60'] , 
 ['D' , '146.80'] , 
 ['E♭' , '155.60'] , 
 ['E' , '164.80'] , 
 ['F' , '174.60'] , 
 ['F♯' , '185.00'] , 
 ['G' , '196.00'] , 
 ['G♯' , '207.70'] , 
 ['A' , '220.00'] , 
 ['B♭' , '233.10'] , 
 ['B' , '246.90'] , 
 ['C' , '261.60'] , 
 ['C♯' , '277.20'] , 
 ['D' , '293.70'] , 
 ['E♭' , '311.10'] , 
 ['E' , '329.60'] , 
 ['F' , '349.20'] , 
 ['F♯' , '370.00'] , 
 ['G' , '392.00'] , 
 ['G♯' , '415.30'] , 
 ['A' , '440.00'] , 
 ['B♭' , '466.20'] , 
 ['B' , '493.90'] , 
 ['C' , '523.30'] , 
 ['C♯' , '554.40'] , 
 ['D' , '587.30'] , 
 ['E♭' , '622.30'] , 
 ['E' , '659.30'] , 
 ['F' , '698.50'] , 
 ['F♯' , '740.00'] , 
 ['G' , '784.00'] , 
 ['G♯' , '830.60'] , 
 ['A' , '880.00'] , 
 ['B♭' , '932.30'] , 
 ['B' , '987.80'] , 
 ['C' , '1047.00'] , 
 ['C♯' , '1109.00'] , 
 ['D' , '1175.00'] , 
 ['E♭' , '1245.00'] , 
 ['E' , '1319.00'] , 
 ['F' , '1397.00'] , 
 ['F♯' , '1480.00'] , 
 ['G' , '1568.00'] , 
 ['G♯' , '1661.00'] , 
 ['A' , '1760.00'] , 
 ['B♭' , '1865.00'] , 
 ['B' , '1976.00'] , 
 ['C' , '2093.00'] , 
 ['C♯' , '2217.00'] , 
 ['D' , '2349.00'] , 
 ['E♭' , '2489.00'] , 
 ['E' , '2637.00'] , 
 ['F' , '2794.00'] , 
 ['F♯' , '2960.00'] , 
 ['G' , '3136.00'] , 
 ['G♯' , '3322.00'] , 
 ['A' , '3520.00'] , 
 ['B♭' , '3729.00'] , 
 ['B' , '3951.00'] , 
 ['C' , '4186.00'] , 
 ['C♯' , '4435.00'] , 
 ['D' , '4699.00'] , 
 ['E♭' , '4978.00'] , 
 ['E' , '5274.00'] , 
 ['F' , '5588.00'] , 
 ['F♯' , '5920.00'] , 
 ['G' , '6272.00'] , 
 ['G♯' , '6645.00'] , 
 ['A' , '7040.00'] , 
 ['B♭' , '7459.00'] , 
 ['B' , '7902.00']  
    ] ;

function wrapNote($input) {
         $letter=$input[0];
         $hz=$input[1];                     
         return "<span class='note ".$hz."'>".$letter."</span> <span class='note '>".$hz."</span>";
                    }
 


 foreach($notes as $key=>$note) {
  switch($notes[$key][1]) {
    case 82.41:
       $E=$key;
       break;
    case 110.00:
       $a=$key;
        break;
    case 146.80:
       $d=$key;
       break; 
    case 196.00:
       $g=$key;
       break;
       case 246.90:
       $b=$key;
       break;
    case 329.60:
       $e=$key;
       break;    
            }
           }
   

$E = array_slice($notes, $E, 13); 
$a = array_slice($notes, $a, 13); 
$d = array_slice($notes, $d, 13); 
$g = array_slice($notes, $g, 13); 
$e = array_slice($notes, $e, 13); 

foreach($E as $key=>$value) {
           print wrapNote($e[$key]);
           print wrapNote($b[$key]);
           print wrapNote($g[$key]);
           print wrapNote($d[$key]);
           print wrapNote($a[$key]);
           print wrapNote($E[$key]);
           print "<br />";
           print "<br />"; 
          }




Tuesday, August 8, 2017

Javascript Memory Limit and Usage in Chrome



<script>
// Thank you stackoverflow user B. Gruenbaum
console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
// will give you the JS heap size
console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
// how much you're currently using
</script>

Slightly off topic from post title, the following may be an issue in the problem set being thought on in regards to memory limits: JSON object sizes.
In my scenario I had static reference data in CSV format. I converted to JSON files ( e.g. materials.json).  Here is standalone PHP script you can place in directory of JSON files and get file sizes and javascript memory info all together.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<meta name="format-detection" ></meta>
<title>JS Memory Size</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<?php
foreach(glob('*.json') as $file) {
   print "<script type='text/javascript' src='".$file."' ></script>".PHP_EOL;
}
?>

<script>
$(document).ready(function(){
  console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
  // will give you the JS heap size
  console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
  // how much you're currently using
  $('#jsHeapSizeLimit').text(performance.memory.jsHeapSizeLimit);
  $('#usedJSHeapSize').text(performance.memory.usedJSHeapSize);
});
</script>
<style>
td {
align:left;
border:1px dotted #000000;
padding:5px;
}
</style>
</head>
<body>
<h1>JS Memory Size</h1>
<table><tr>
<th>Size</th><th>Info</th>
</tr>
 <td id='jsHeapSizeLimit'></td><td>Size Limit</td>
</tr><tr>
 <td id='usedJSHeapSize'></td><td>Currently Used</td>
</tr>
<?php
foreach(glob('*.json') as $file) {
   print "<tr><td>".filesize($file)."</td><td>".$file."</td></tr>".PHP_EOL;
}
?>
</table>
</body>
</html>
<?php
die();
?>

Thursday, June 8, 2017

Dynamically create input name and id via associated label text

var groupA = "";
$('label').each(function(index, item) {
var a = $(this).text();
a = a.replace(/\W/g, '');
a = a.replace(/\s/g, '');
a = a.toLowerCase();
groupA = $(this).attr('group');
$('input:text').each(function(index,item) {
if ($(this).attr('group') == groupA) {
$(this).attr('name', a + $(this).attr('typeB') );
$(this).attr('id', a + $(this).attr('typeB') );
}
});
});

Tuesday, April 4, 2017

Force use of HTTPS with Javascript

<script>
location.protocol=='https:' ? console.log(window.location.href) : replace('https:'+location.hostname+location.pathname);
</script>

Friday, March 17, 2017

See inventory of function in javascript files

Place this script in same directory your javascript files are, view in browser.
(and yes I know there is grep -n 'function' *.js but I like things I can see in browser)
 <?php
header("Content-Type:text/plain") ;
$files=glob('*.js');
$searchfor = 'function';
foreach($files as $file) {
print $file.PHP_EOL;
$contents=file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $contents, $matches)){
  print count($matches[0])." total functions".PHP_EOL;
  print implode("\n", $matches[0]);
}
}
?>

Example out:

main.js
37 total functions
function checkExists(myVar) {
function consoleLogTime(input) {
function debugMsg(input) {
function getQuerystring(key, default_)
function selectCustomer(input) {
function listCreate(arrayValues, arrayText, myClass) {
function optionListCreate(arrayValues, arrayText) {
function validateEmail(x) {
function checkEmailExists(email) {
      success: function(result) {
      error: function(result) {
function passwordReset(email) {
      success: function(result) {
      error: function(result) {
function listArray(myArray) { 
function getJsonTable(url, table, tag) {
 $.getJSON(url+'?table='+table, function(data){
        $.each(data, function(index,item) {
$(document).ready(function(){
     success: function(result) {
     error: function(result) {
$('#signin').click(function() {
$('#signup').click(function() {
$("#passwordReset").click(function() { 
$("#emailB").blur(function() { 
$('.mytype').click(function() {
$("#passwordconfirmB").keyup(function() { 
$("#passwordconfirmB").blur(function() { 
$("#workorder").ready(function() { 
$("select").ready(function() { 
$("#bids").change(function() { 
$("#about").click(function() { 
$('.submit').click(function() {
      success: function(result) {
      error: function(result) {
$("#selectacustomer").click(function() { 
$(".customerSelect").click(function() { 


Thursday, February 2, 2017

Wednesday, February 1, 2017

Web Browser Sniffing

function get_browser_name($user_agent)
{
    if (strpos($user_agent, 'opera') || strpos($user_agent, 'opr/')) return 'opera';
    elseif (strpos($user_agent, 'edge')) return 'edge';
    elseif (strpos($user_agent, 'chrome')) return 'chrome';
    elseif (strpos($user_agent, 'safari')) return 'safari';
    elseif (strpos($user_agent, 'firefox')) return 'firefox';
    elseif (strpos($user_agent, 'msie') || strpos($user_agent, 'trident/7')) return 'internet explorer';
    elseif (strpos($user_agent, 'ipad')) return 'ipad';
    
    return 'Other';
}

$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$browser = get_browser_name($userAgent);

(javascript)
var userAgent = '';
var browser = '';
console.log('userAgent '+userAgent);
console.log('browser '+browser);

Tuesday, January 17, 2017

Javascript checkExists

function checkExists(myVar) {

 if (typeof myVar === 'undefined')  { return false;  }
 if ( myVar === null ) { return false;  }
 if ( myVar.length == 0) { return false; } 
 
 return true;
 
}

Friday, January 13, 2017

MYSQL Query for Timestamp Age in Hours

SELECT `route_id` FROM `route` where `is_active`=1
AND TIMESTAMPDIFF(HOUR,`dt_update`,CURRENT_TIMESTAMP())< 48;

Monday, January 9, 2017

Javascript misterClean string cleaner

function misterClean(x) {
   x = x.replace(/^\s+|\s+$/gm,''); // remove end whitespace
   x = x.replace(/(\r\n|\n|\r)/gm,""); // remove line breaks
   return x;
}