Latest Entries »

Optimizing the PHP MP3 waveform generator

Waveform generated using Dancing With Paris' "(Boardwalk)" MP3.

After some great comments in the original PHP MP3 Waveform Generator blog post, I decided to go ahead and do my best to optimize the script as much as possible. This included, as a commenter named Bruce pointed out, that it wasn’t very efficient on memory to perform a extraction of a large set of amplitude data points for the waveform only to discard them later when drawing on the canvas (given the DETAIL constant setting).

My revised script now estimates the number of amplitude points of the resulting decoded WAV file (data size) and extracts those points while at the same time plotting them onto the canvas. This eliminates the need for a second loop and reduces overall memory and performance.

UPDATE 2012-12-08: I’ve also added support for stereo (2-channel) waveform processing!

View full article »

“phrames” framework/ORM now on github

While in the midst of readying some fairly large projects at work, I haven’t had too much time to progress with my last update of my “phrames” ORM/framework. However, I am happy to announce (to whoever is willing to listen) that I’ve published my work to github! I’m really hoping that, first and foremost, this project can gain a little more ground to those that might be interested in it. Progress has been slow and steady, and quite obviously more a personal effort than anything, but I’ve been loving some of the feedback I’ve gotten. I believe that if there’s just 1 person out that that uses my code, contributes to it on github, or even just learns something from what I’ve offered then mission accomplished.

That being said, I’ve more been a lone developer for essentially all of my career to date. My experience with version control systems is limited at best, especially on a multi-developer level. I’ve only ever used it for keeping track of my personal changes/commits and versions, so if you would be so gracious as to make some changes to my project and share them with me, please bear with me while I learn and adjust to these new methods.

Find my project phrames on github!

It’s been a long time since I have last posted and since I have had the time to develop my django-like PHP framework/ORM. However in last few days I was able to crack down and get quite a lot done from where I left off.

I last left off considering what my ultimate plan was: do I want to sit and study the entire django codebase and create a line-for-line duplicate? Not really, but I was able to use the source for a lot of reference points. Please keep in mind that, more than a serious project, this iss more about an experiment in exploring some of the deeper functionality in PHP. I haven’t had a chance to refactor any of my code, so some of it is pure garbage, but I’ve commented as much as I could that didn’t seem immediately obvious. If this inspires one person or if one person uses even one class or one method of my code, then mission accomplished!

I’m now calling my ORM “phrames” (as in PHP Framework)… not original, but among the millions of PHP frameworks it wasn’t already taken. View full article »

In my last post I was looking for a way to give simple images transparent backgrounds. I noted that PHP GD’s imagefill function wasn’t quite what I was looking for as it only replaced exact colours based on your starting image coordinates. I gave this problem some brief thought and came up with a slightly better algorithm using colour distance calculation and simple recursion.

View full article »

I needed a quick and dirty script (that I took a few minutes to clean up and throw together in a class) that can take a basic JPEG and turn it into a transparent PNG around the edges. I called it “Transparentize” — only because I couldn’t find any proper term for this.

I looked into PHP GD library’s imagefill function but it wasn’t quite giving me the results I needed. It only filled for exact colours, and in a JPEG with millions of colours, there are a number of variations of what the average set of eyes perceives as “white.” I need something where a threshold could be specified and find the edges of an image on a high contract background (for example, a basic black square on a white background).

View full article »

After some research, discussion and a bit of hacking together some poorly organized code, I’ve come up with a revised interface from my previous post An experiment: Django Framework-like querying/model interface in PHP.

Now there existed a few problems I was well aware that I wanted to address if I was going to spend any more time on a good, clean interface that promoted rapid development and readable code:

  • It was dependent on a PECL extension that was nearly 4 years old
  • It only worked with PHP 5.2.x (as per above)
  • The actual query-building interface wasn’t pretty and hindered readability

I sat down and revised a good portion of the old code I had put together and came up with something that fixes these problems. I wrote a new collection of classes with PHP 5.3 in mind and it uses a lot of the new language features (especially late static bindings and the __callStatic magic method). Now, because of the use of these new features my code is now dependent on PHP version 5.3 (developed using 5.3.1) — but, I suppose it’s better to code looking to the future rather than being locked into an older version. View full article »

I have continued with this experiment and created a new querying interface compatible with PHP 5.3 and has no dependent PECL extensions: Experiment update: revised Django-like PHP (5.3 compatible) querying interface.

So this is an experiment I’ve had in the making for quite some time. I’ve been working for years trying to build a reusable “generic object” querying framework that I made my PHP applications easier to maintain and faster to develop. I originally started with a small library of classes, including a ‘GenericObject’ class and a ‘Collection’, which allowed for proper iteration using the Iterator interface — all wrapped around a very messy Factory design pattern.

Up until about a year, I used hacked and modified versions of these classes for all of my projects and was getting tired of the mess. After doing some casual research I finally came across the Django Framework and found the object creation and database querying quite phenomenal as a programming interface. I didn’t want to spend years trying to get my Python up to par with my abilities with PHP, so I decided to try and replicate it. For the last year, again, I’ve been using a pretty hacked up version of what seemed to me like a “Django-like” interface, combined together with my old GenericObject and Collection classes from long before. With this, I was previously able to perform really simple database queries with an interface like so:

$users = User::get(array("fullname__contains" => "Andrew"));

foreach($users as $user) {
  // ...
}

View full article »

Map of Canada chart with province value colour coding.

This is PHP class I developed for work when I needed a Canada-specific map chart in some reporting tools I was creating. Obviously Google’s Map Charts API would’ve been my first choice, but it didn’t support Canada as it’s zoom level for reporting province-based data sets.

This is where I sat down, thought about a quick and dirty way to create this chart and went to work. I was able to create a chart as show on the right, with full control of output dimensions and colours.

Before I jump right into the full code, I want give a brief explanation of some of the techniques I used to accomplish this task.

View full article »

So here’s a quick couple snippets of code that will allow you to retrieve the coordinates of two locations using Google Maps API — no API key necessary.

This first function will return the coordinates of specific address (I find pretty much anything works, whether it’s a full address with street number, or just a postal code, or simply a city name) as an array of coordinates:

function getCoordinates($address) {
  $url = "http://maps.google.com/maps/geo?q=" . urlencode($address)
         . "&output=json";
  $result = file_get_contents($url);
  $result = json_decode($result, 1);
  if (isset($result['Placemark'])) {
    list($lat, $long) = $result['Placemark'][0]['Point']['coordinates'];
  } else {
    $lat = $long = false;
  }
  return array($lat, $long);
}

View full article »

Generating MP3 waveforms with PHP

UPDATE: After some great comments, I have optimized the performance of this script and made some brief commentary available here. I have also updated any source code links below to the new github project page.

I don’t quite know what gave me the desire to ever generate audio (specifically MP3) waveforms with PHP — it’s not something I’ll likely ever use — but after doing some research and coming up a bit short I decided to make it happen.

Waveform generated using Dancing With Paris' "(Boardwalk)" MP3.

A few days of Googling, I did come up with a couple solutions but none seemed to really produce the results I wanted. All I was looking to generate was a simple waveform like that in Cubase or other recording software. I don’t need axis labels or left/right audio splits, just a simple mono waveform from an MP3. I also didn’t want to go through hundreds of audio files and convert them into WAV files just to do this — if I was going to do this as an experiment, I wanted it on the fly. I did come across some interesting links though:

View full article »

Powered by WordPress | Theme: Motion by 85ideas.