Tuesday, January 23, 2007

What is ogg vorbis?

I'm glad that you asked. Ogg Vorbis is a format for music files like MP3. In many ways, I think that Ogg Vorbis is better. It produces higher quality output at a lower bit rate than MP3's. This means Ogg Vorbis files sound better and are smaller than MP3 files. In addition, Ogg Vobis is an open format and it uses open source software. This means that if you wanted to create an Ogg Vorbis player, or write a program that uses Ogg Vorbis, you don't need to pay any licensing fees on the technology. The MP3 format is patented by a German company (Fraunhofer Society) and they charge licensing fees to use it.

So, why isn't it more popular you may ask? I think it all comes down to timing. Ogg Vorbis was introduced much later than MP3, and several MP3 codecs have been released as free software for individual use. I'm hoping that Ogg Vorbis will gain momentum and eventually win out.

If you are interested in free and open audio compression you might also want to check out the Free Lossless Audio Codec (FLAC). The is no degradation to the quality of the sound and it is free and open like Ogg Vorbis.

Wednesday, January 10, 2007

Simulating classes in C

I have to admit, I like to program in C. In some ways, it is simpler than some of the newer, high level languages, and I really enjoy being closer to the machine code. Perhaps I'm a bit obsessive about efficiency. Still, I sometimes long for classes and objects in my C programs, (This is where you tell me that I should use C++ or Objective-C.) so I decided to figure out how I could simulate classes in plain old ANSI C.

It turned out to be quite easy. After I read up on function pointers, I created a struct which contains references to functions (kind of like class methods). Here's a simple example of what I'm talking about:

#include<stdio.h>

typedef struct {
int (*test)(int); /* This is the function pointer */
} functionholder;

/* At runtime, I will point to this function */
int thefunction(int x) {
printf("thefunction says %i\n", x);
return 0;
}

int main() {
functionholder fholder;
fholder.test = &thefunction;
(*fholder.test)(7);
return 0;
}

When I run the above, the program prints "thefunction says 7".
From there, I decided to create a family of structures which would simulate classes, objects, and allow polymorphic function calls.

One way to divide up data and methods in an OO way is to say that the class dictates the methods which can be used with the data in an object instance. So we could say that a class contains a list of functions. Using function pointers, the method called can be changed at runtime, as long as all of the functions have the same stack profile. Have I lost you yet? So what I needed to do was create a generic function prototype which abstracts the parameter list into a common format. While I was at it, I decided to lose the restriction that methods return only one value, so the results of a method call will be stored in a structure which is passed in as a parameter. (The method should probably be called a procedure instead of a function.) Perhaps it would be simpler to show you the code.

typedef struct {
void* type; /* This will point to a JSClass. */
void* data;
} JSObject;

typedef struct {
int object_count; /* number of objects in the array */
JSObject** objects; /* An array of pointers to JSObjects */
} JSListOfObjects;

typedef struct {
char* name;
/* Each JSClass contains a list of pointers to the methods which belong
* to that class. */
int method_count;
/* strings naming the functions (allow method lookup by string) */
char **method_names;
/* An array of pointers to functions */
void (**methods)(JSObject* x, JSListOfObjects* y, JSListOfObjects* z);
} JSClass;

I was going to discuss the functions I created to streamline "class" method invocation, but this post is getting a bit long. So I've decided to try something fun. I'm going to ask you, gentle reader, to suggest a design for a function which simplifies invocation of an object method. I will mail $1 (USD) to the person who posts the "best" working solution in the comments below. (I'll be compiling using gcc -ansi. Oh, and US residents only, I don't want to run into any strange rules.). In addition to a dollar, you'll have won bragging rights for the first of my blog challenges. After a week or two, I'll post my solution and we can all compare notes. Happy coding!

Saturday, January 06, 2007

A Clockwork Orange

I just finished reading A Clockwork Orange by Anthony Burgess and I would recommend it. If you decide to read it, be prepared for a rip roaring ride of emotions and strange slovos (words). At times, it can be difficult to stomach and after the second chapter, I was ready for the main character to be killed off. (I'd have tolchoked his merzky litso all on my oddy knocky, oh my brothers, 'til the krovvy flowed real horrorshow and his zoobies come all out.) There are some comic moments, (it can be a malankey bit silly) but at it's core, this book explores the deep topics of morality and free will (and all that cal). I've never viddied the movie, but I tend to think that it wouldn't come off so well. Some things just work better in print.