Archive for the ‘Programming’ Category.

Create m3u playlist from directory list

This little command script iterates all sub folders to create an m3u playlist containing mp3 files. The m3u playlist can then be played using winamp.

To create the command script, open a text editor and add the following line.

dir *.mp3 /b /s > "playlist.m3u"

Next save the file as create-playlist.cmd. To execute place the command script in the same folder as the mp3s and double click create-playlist.cmd, this will generate playlist.m3u. Then open playlist.m3u in winamp.

Sound Buttons

Sound Buttons are a technique used to teach children to read. This function generates the sound buttons for a given word, uing the Jolly Phonics system. This approach breaks each word into the groups of letter sounds, of which there are 42 letter sounds.

/**
 * Generates phonics for a given word.
 * This function uses the Jolly Phonics, synthetic phonics programme.    
 * @param input The input word
 * @return array of phonemes
 */   
function phonomes($input)
{
  // create the output array
  $output = array();
  
  // create the Phoneme arrays
  $phoneme3 = array('ear','igh');
  $phoneme2 = array('ai','ar','ch','ck','ee','er','ie','ng','oa','oi','oo','or','ou','ow','qu','sh','th','ue','ue','ur');
  
  // process the input word
  $word =strtolower($input);
  $word = trim($word);
  $letterCount = strlen($word);
  $index = 0;
  
  // iterate each letter in the word searching for phonemes
  while($index <$letterCount)
  {
    if (in_array(substr($word, $index, 3), $phoneme3))
    {
      // found a three letter phoneme
      $output[] = substr($word,$index, 3);
      $index += 3;
    }
    else if (in_array(substr($word, $index, 2), $phoneme2))
    {
      // found a two letter phoneme
      $output[] = substr($word,$index, 2);
      $index += 2;
    }
    else
    {
      //found a single letter phoneme
      $output[] = substr($word,$index, 1);
      $index++;    
    }   
  }
  return $output;
}

To test the phonomes function use

foreach(array('this','moth','that','three','them','thin','quick','quilt','liquid','squid') as $test)
{
  print "$test = " . implode(' - ', phonomes($test)) . "
"; }

This produces the following output

this = th - i - s
moth = m - o - th
that = th - a - t
three = th - r - ee
them = th - e - m
thin = th - i - n
quick = qu - i - ck
quilt = qu - i - l - t
liquid = l - i - qu - i - d
squid = s - qu - i - d

To see this function working please visit Dylan Seaford

Convert time in seconds to HH:MM:SS

## Convert time in seconds to hours:minutes:seconds
# @param sec Time in seconds
# @return The time in hh:mm:ss format
def SecToTime(Sec): 
  H = int(Sec / 3600)
  M = int(Sec / 60 - H * 60)
  S = int(Sec - (H * 3600 + M * 60))

  if len(str(H)) == 1: time = "0" + str(H) + ":"
  else: time = str(H) + ":"
 
  if len(str(M)) == 1: time = time + "0" + str(M) + ":"
  else: time = time + str(M) + ":"

  if len(str(S)) == 1: time = time + "0" + str(S) 
  else: time = time + str(S) 
  
  return time
  pass

Get Web Page Content as a String with C#

This function dowloads the html content of a webpage and returns the content as a string.

public static string GetWebPageAsString(string url) {
    HttpWebRequest httpWebRequest =(HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    Stream stream = httpWebResponse.GetResponseStream();
    StreamReader streamReader = new StreamReader(stream, Encoding.ASCII);
    return streamReader.ReadToEnd();
}

Recursive delete files and folders using C#

This function will Recursively delete all files and folders.

private void DeleteAllFiles(string sPath)
{
    foreach(string strFile in Directory.GetFiles(sPath))
    {
        File.Delete(strFile);
    }
    foreach (string strDir in Directory.GetDirectories(sPath))
    {
        DeleteAllFiles(strDir);
    }
    Directory.Delete(sPath);
}

Create a autocompleting textbox using C#

The code example below creates an autocompleting textbox, with the possible values Andy, Andrew, Dylan and Kyle.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace autoComplete
{
    public partial class Form1 : Form
    {
        AutoCompleteStringCollection nameCollection = new AutoCompleteStringCollection();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            nameCollection.Add("Andy");
            nameCollection.Add("Andrew");
            nameCollection.Add("Dylan");
            nameCollection.Add("Kyle");

            txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtName.AutoCompleteCustomSource = nameCollection;
        }
    }
}

Create arraylist

Create an ArrayList and add three elements.

using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList list = new ArrayList();
        list.Add("One");
        list.Add("Two");
        list.Add("Three");
    }
}

Wood Cells

Analysis of wood cells using halcon.

read_image (Woodcells1, 'C:\\Program Files\\MVTec\\HALCON\\images\\woodcell.png')
X := 20
threshold (Woodcells1, CellBoarder, 0, 120)
get_image_pointer1 (Woodcells1, Pointer, Type, Width, Height)
open_file ('wood_cells.dat', 'output', FileHandle)
for i := 0 to Width-X-1 by 1
    clip_region (CellBoarder, Part, 0, i, Height-1, i+X)
    area_center (Part, Area, Row, Col)
    fwrite_string (FileHandle, i + ' ' + (Area * 100.0 / (X * Height)))
    fnew_line (FileHandle)
endfor
close_file (FileHandle)

Convert a date and time to UTC

/** Converts a date and time to UTC
* /param date The date in the format dd/mm/yyyy
* /param time The time in the format HH/mm/ss
* /return utc
*/   

function utc($inputDate, $inputTime)
{
$pieces = explode("/", $inputDate);
$dateus = $pieces[2]."-".$pieces[1]."-".$pieces[0];
$stringtime = "$dateus $inputTime";
return strtotime($stringtime);

}