Urbano's Blog

Words from Alejandro U. Alvarez

RSS Feeds

  • Home
  • About me
  • Contact me
  • Plugins
    • Custom Fonts WordPress Plugin (v 5)
    • Nospam (v.1)
  • Advertise on Urbano’s Blog
  • Subscribe!

Restore iPhone without upgrading OS

May 3rd

Posted by alex in iPhone

No comments

Recovery mode

Recovery mode

If you need to restore your iPhone you'll notice iTunes tells you that upon restoring it, the latest version will be installed. This will be just fine for regular iPhones of course, but for the people who have a Jailbroken iPhone, it's not ok (At least until the newest version is ready for jailbreak)

Well don't panic since this is a really simple thing to do, first, download the iPhone firmware you want to restore into. For my example I will use the iPhone 3.1.2, so get the IPSW (iPhone Software) and then move on to the next step.

Now that we have the IPSW ready simple plug your iPhone to your computer, and when it shows up on iTunes Hold down the Shift key and press Restore. You will be asked for the location of your desired IPSW file, simply select the one you just downloaded and click Open. Your iPhone should now be restored without problems to that version. This is also how you could downgrade your iPhone OS if you accidentally updated it.

Hope you found this useful,
Alex

3g, 3gs, downgrade, firmware, iPhone, OS, restore

Play a sound via JavaScript

May 3rd

Posted by alex in Javascript

1 comment

Working in a project recently I found myself in the need of playing a sort of "alert" sound with JavaScript. It turns out there are not so many good options out there. The first thing that came to my mind was to use an embed and the Play and Stop JavaScript controls.

Well that only works in IE and some versions of Netscape so let's just forget about all those tags (embed, bgsound... etc)

So in my search for an easy solution I came across a very neat jQuery plugin that does exactly that job, the jQuery Sound Plugin by Jorn Zaefferer. I have been completely unable to find the plugin Website, so I will post it's code myself here. This plugin is a port from scriptaculous' version by Jules Gravinese.

How to use:

Using this to play a sound couldn't be easier, let's see an example with no parameters except for the file we want to play:

$.sound.play('files/sample.mp3')

This way the mp3 file sample will start playing. If we prefer the sound to start playing stopping the previous one, we can use tracks, this way we can play one sound per track, and every time we set a new sound for a certain track it stops the current sound there:

$.sound.play('files/sample.mp3',{
   track: "track1"
});

Other options are:

// timeout: Specify for how long the sound can play in milliseconds
$.sound.play('files/sample.mp3', {
   timeout: 4000
});

We can store a sound reference in an object so that we can later on stop it:

var sound = $.sound.play('files/sample.mp3');
sound.remove();

And finally we have the option to enable/disable sounds at all:

$.sound.enabled = false;
$.sound.enabled = true

The plugin code:

(function($) {
$.sound = {
	tracks: {},
	enabled: true,
	template: function(src) {
		return '<embed style="height:0" loop="false" src="' + src + '" autostart="true" hidden="true"/>';
	},
	play: function(url, options){
		if (!this.enabled)
			return;
		var settings = $.extend({
			url: url,
			timeout: 2000
		}, options);
		if(settings.track){
			if (this.tracks[settings.track]) {
				var current = this.tracks[settings.track];
				current.Stop && current.Stop();
				current.remove();
			}
		}
		var element = $.browser.msie
		  	? $('<bgsound/>').attr({
		        src: settings.url,
				loop: 1,
				autostart: true
		      })
		  	: $(this.template(settings.url));
		element.appendTo("body");
		if (settings.track) {
			this.tracks[settings.track] = element;
		}
		setTimeout(function() {
			element.remove();
		}, options.timeout)
		return element;
	}
};
})(jQuery);

I will probably minify this code before I upload it to the production server, so if I don't forget I will also post here the minified version,

Hope you found it useful,
Alex

javascript, js, pause, play, sound

C++ Internal Speaker Piano

Apr 28th

Posted by alex in C++

No comments

If you code in C++ you have probably used the internal speaker for some quick debug of your programs, in Windows, we can use the winapi header (windows.h) to access the built in function Beep():

Beep(frequency,duration);

Where frequency is in Hertz and duration in milliseconds.

So here is a quick something I did on C++ out of boredom, it basically uses your keyboard as a piano, playing each note using the PC Internal Speaker

Here is the source code:

#include<iostream>
#include <windows.h>
#include <conio.h>
 
using namespace std;
 
void p(char note, bool s, int dur,int sc){
     float freq;
     switch(sc){
         case 4:
              switch(note){
                  case 'C': (!s)?freq=130.81:freq=138.59; break;
                  case 'D': (!s)?freq=146.83:freq=155.56; break;
                  case 'E': (!s)?freq=164.81:freq=164.81; break;
                  case 'F': (!s)?freq=174.61:freq=185.00; break;
                  case 'G': (!s)?freq=196.00:freq=207.65; break;
                  case 'A': (!s)?freq=220.00:freq=233.08; break;
                  case 'B': (!s)?freq=246.94:freq=246.9; break;
             }
             break;
         case 5:
             switch(note){
                  case 'C': (!s)?freq=261.63:freq=277.18; break;
                  case 'D': (!s)?freq=293.66:freq=311.13; break;
                  case 'E': (!s)?freq=329.63:freq=329.63; break;
                  case 'F': (!s)?freq=349.23:freq=369.99; break;
                  case 'G': (!s)?freq=391.00:freq=415.30; break;
                  case 'A': (!s)?freq=440.00:freq=466.16; break;
                  case 'B': (!s)?freq=493.88:freq=493.88; break;
             }
             break;
     }
     Beep(freq,dur);   
}
void piano(char n){
    switch(n){
          case 'a': p('E',false,200,4); break;
          case 's': p('F',false,200,4); break;
          case 'd': p('G',false,200,4); break;
          case 'f': p('A',false,200,4); break;
          case 'g': p('B',false,200,4); break;
          case 'h': p('C',false,200,5); break;
          case 'j': p('D',false,200,5); break;
          case 'k': p('E',false,200,5); break;
          case 'l': p('F',false,200,5); break;
          case 'ñ': p('G',false,200,5); break;
          case '´': p('A',false,200,5); break;
          case 'ç': p('B',false,200,5); break;
          // Mayor
          case 'q': p('D',true,200,4); break;
          case 'w': p('E',true,200,4); break;
          case 'e': p('F',true,200,4); break;
          case 'r': p('G',true,200,4); break;
          case 't': p('A',true,200,4); break;
          case 'y': p('B',true,200,5); break;
          case 'u': p('C',true,200,5); break;
          case 'i': p('D',true,200,5); break;
          case 'o': p('E',true,200,5); break;
          case 'p': p('F',true,200,5); break;
          case '`': p('G',true,200,5); break;
          case '+': p('A',true,200,5); break;
     }
}
 
int main(){
    char m;
    while(m!='1'){
        if(kbhit()){
            m=getch();
            piano(m);
        }
    }
    return 0;
}

This will only work in computers with an internal speaker installed, yours probably has one if it "beeps" when it turns on. Although it may have it even if that doesn't happen, it doesn't work on Windows Vista x64 and Windows XP 64-Bit Edition though

Best regards,
Alex

cpp, internal, piano, speaker

Understanding C++ time complexity

Apr 21st

Posted by alex in C++

No comments

What functions are faster? Which ones are the most efficient? Understanding this will help you optimise your code and impove it's performance.

Each C++ function has a run time, and knowing that will allow you to combine the best methods always.

Here you have the different possible run times, explained and with an example of a function. (Note that they are ordered from fastest to slowest)

Constant time:

This is the fastest a function can work. An example is to measure an array's length:

// Using namespace std;
int a[32];
cout << a.size();

What this means is that no matter how large a is, it will always take the same amount of time to perform this operation.

Logarithmic time:

This is also in the fast group of times, there is a formula to get how much time it will actually take (That involves the logarithm of the size of the input)

An example of this would be a function that looks for a key in an array, returning it's index if found:

int binarySearch(int sortedArray[], int first, int last, int key) {
   // function:
   //   Searches sortedArray[first]..sortedArray[last] for key. 
   // returns: index of the matching element if it finds key,
   //         otherwise  -(index where it could be inserted)-1.
   // parameters:
   //   sortedArray in  array of sorted (ascending) values.
   //   first, last in  lower and upper subscript bounds
   //   key         in  value to search for.
   // returns:
   //   index of key, or -insertion_position -1 if key is not
   //                 in the array. This value can easily be
   //                 transformed into the position to insert it.

   while (first <= last) {
       int mid = (first + last) / 2;  // compute mid point.
       if (key > sortedArray[mid])
           first = mid + 1;  // repeat search in top half.
       else if (key < sortedArray[mid])
           last = mid - 1; // repeat search in bottom half.
       else
           return mid;     // found it. return position /////
   }
   return -(first + 1);    // failed to find key
}
Source: Fredosaurus.com

Linear time:

A bit slower than logarithmic time, but still fast. Functions that run in this type of time take a time proportional to the size of the input.

An example of this would be to iterate through an array:

int a[40];
// Fill a with data...
for(int i=0;i<40;i++){
     std::cout << a[i];
}

Linearithmic time:Quick Sort method

Well this one is a bit tricky, it's still fast, and it's run time depends on the size of the input but both linearly and logarithmically. All logarithmic functions that sort work this way, they are also linear because they depend on the size of the array to sort:

There are a lot of different ways of implementing the algorithm quicksort, here you have one sample demonstration of how it works.

Polynomial time:

This one is the slowest time inside the fast run times. It is the one that take up comparison sorts (Functions like insert, sort, select... )

Here you have a sample implementation of the function sort for arrays:

int array[] = { 23, 5, -10, 0, 0, 321, 1, 2, 99, 30 };
int elements = sizeof(array) / sizeof(array[0]);
std::sort(array, array + elements);
for (int i=0; i<elements; ++i)
     std::cout << array[i] << ' ';

Exponential time:

Algorithms and functions that run in this type of time are considered slow. This are algorithms of the type brute force, that is, algorithms that try all possibilities, like a solver for a Rubik Cube.

Factorial time:

This is the slowest of all, it corresponds to complex brute force algorithms, like an algorithm to solve the Travelling Salesman Problem by brute force.

I will probably add a bit more to this examples over time, but well to get a grasp of what these times mean I think this will be more than enough,

Alex

C++, constant, cpp, exponential, factorial, logarithmic, time

Display timezone-specific dates in PHP

Apr 17th

Posted by alex in PHP

No comments

It's common to have a website designed for one country (For example Spain or the UK) and have it in a server elsewhere (In the US for example). You will notice that sometimes when displaying a date this way, it shows the local time at the server!

Instead of manually correcting this time difference there is a much safer way of getting around this issue, using built-in PHP functions:

Take a look at this:

<?php
$timezone = new DateTimeZone( "Europe/Madrid" );
$date = new DateTime();
$date->setTimezone( $timezone );
echo  $date->format( 'H:ia  (D, M jS, Y)' );
?>

Which would generate the following output (At the time of writing of course!)

22:52pm (Sat, Apr 17th, 2010)

As you can see it is extremely easy to set up new timezones and to display dates for those zones specifically. You could even prompt your user for his/her own timezone, or add it as a specific setting for them :)

change, php, timezone
«12345»1020...Last »
  • Looking for something?

    • Popular posts
    • Archives
    • Tags
    • Categories
    • General talk (62)
      • Tips (6)
    • Graphic Design (30)
      • Branding (3)
      • Cool pics (5)
      • CSS (17)
      • Icons (2)
      • Photoshop (9)
    • iPhone (4)
    • OS (17)
      • Windows (16)
    • Programming (47)
      • AJAX (1)
      • C++ (8)
      • Javascript (10)
      • PHP (22)
      • XML (2)
    • Reviews (6)
    • Web-related (53)
      • Browsers (4)
      • Facebook (1)
      • Layout/Styling (9)
      • Security (1)
      • SEO (13)
      • Social Bookmarking (1)
      • Traffic building (5)
      • WordPress (10)
        • Plugin development (1)
    • Weird stuff (7)
      • Humor (2)
    ajax application best blog chat class codes control corners cpp css design english Firefox flash google handler ie image iPhone irc javascript jquery language photoshop php play plugins post redesign remote rotate rounded seo spanish tabs time top tutorial ui upload url web Windows WordPress
    • June 2010 (1)
    • May 2010 (6)
    • April 2010 (3)
    • March 2010 (11)
    • February 2010 (1)
    • January 2010 (2)
    • November 2009 (3)
    • October 2009 (3)
    • September 2009 (3)
    • August 2009 (2)
    • July 2009 (1)
    • June 2009 (3)
    • February 2009 (3)
    • January 2009 (3)
    • December 2008 (1)
    • November 2008 (7)
    • September 2008 (11)
    • August 2008 (7)
    • July 2008 (1)
    • June 2008 (10)
    • May 2008 (18)
    • April 2008 (25)
    • March 2008 (17)
    • PHP logging class v. 1.2 (25)
    • Bad words list (458 words) (20)
    • Best unobtrusive anti-spam technique (Not CAPTCHA) (19)
    • Country lists in most popular languages (14)
    • Did you mean… ? In php (13)
    • Best Photoshop brushes for web designers (12)
    • Quantum Universe Simulation – The Unique beings paradox (12)
    • Use Firefox to spell check your website (10)
    • sIFR – Use custom fonts in your website safely (8)
    • Creating an IRC bot in PHP from scratch (7)
  • Receive notifications!

    Your email:

     

  • About me

    I am Alex, a software and communications engeneering student. I currently live in the north of Spain.

    For the past years I have been working on software and application development, as well as sharing tips and tricks I find useful.

    Go to my homepage for more information ;)

© Alejandro U. Alvarez • All rights reserved
RSS Feeds XHTML 1.1 Top