Grauw’s web spot

JavaScript enumeration pattern

September 25th, 2012

Although often better handled with subclasses and polymorphism, enumerations can be a convenient light-weight way to indicate some kind of state or non-boolean flag.

When one makes an enumeration in JavaScript, it is typically an old-fashioned list of constants with integer number values:

var MyEnum = {};
MyEnum.FIRST = 0;
MyEnum.SECOND = 1;
MyEnum.THIRD = 2;

A downside of this approach is that it is not type-safe; these values can not be distinguished from any other number. Additionally, it is possible to cheat by hardcoding a value somewhere, which makes code less readable and refactoring a lot harder.

There is a better way however:

function MyEnum() {}
MyEnum.FIRST = new MyEnum();
MyEnum.SECOND = new MyEnum();
MyEnum.THIRD = new MyEnum();

When one of these is passed to a method its type can be checked using instanceof or a function like checkTypes(). And because these are unique object references they are impossible to specify without using the constants.

Additionally, because this effectively defines a class it comes with a lot of power. E.g. you can pass meta-data and handy functions:

function MyEnum(name) { this._name = name; }
MyEnum.prototype.toString = function() { return this._name; };

MyEnum.FIRST = new MyEnum("FIRST");
MyEnum.SECOND = new MyEnum("SECOND");
MyEnum.THIRD = new MyEnum("THIRD");

Finally, as you discover you want to move more and more functionality onto this enumeration, it is easy to upgrade the enumeration to a full-fledged set of subclasses:

function First() {
    MyEnum.call(this, "FIRST");
}
extend(MyEnum, First);

function Second() {
    MyEnum.call(this, "SECOND");
}
extend(MyEnum, Second);

function Third() {
    MyEnum.call(this, "THIRD");
}
extend(MyEnum, Third);

Grauw

0 comments [reply]

Someone contacted me asking what sysex message to send to the Roland GAIA SH-01 synthesizer to request a MIDI dump of the patch parameters. This so that he could set up his DAW to record the dump, and play it back in a project to initialise the machine.

Let’s start by pointing out that Roland published a “GAIA SH-01 MIDI Implementation” manual on their US site, which basically contains all the necessary information. It can be found here:

» GAIA SH-01 :: Manuals :: Support :: Roland

In this case, we are looking for the “Data Request 1 RQ1 (11H)” message on page 4. Additionally, on page 8 we can find a number of tables which describe the Gaia’s internal memory layout. The topmost table tells us that the temporary patch address is 10 00 00 00 (note: 7-bit), and the second one that the patch content goes from 10 00 00 00 up to 10 00 1D 00.

So, to request a complete dump of the temporary patch, we want to request 1D 00 bytes starting at address 01 00 00 00. When we fill in the data request fields, we get a sysex message that looks like this:

F0 41 7F 00 00 41 11 10 00 00 00 00 00 1D 00 53 F7

If we send this message, the Gaia will respond with the complete patch contents, split up into 25 sysex messages (one for each parameter block).

Now all we have to do is record this response, and replay it to the Gaia when we want to restore it to this state. The input format is exactly the same as the format it outputs.

For an easy way to trigger this particular message in the Gaia Tool, go to the patch you want to dump, set your MIDI editor to record, and then press the “Reload” button at the top right of the temporary patch panel.

Grauw

0 comments [reply]

3D model animations

January 14th, 2011

In 2006 I took the 3D modelling course at Utrecht University. As part of this course there were two exercises that involved modelling something, and I noticed I had never posted about these!

Now mind you, I’m not a graphics guy, so don’t expect too much. Also I notice that some of the renders have aliasing and artefacts; I would create new higher-quality renders but I don’t have a copy of Maya anymore. Nevertheless, they don’t look too shabby if I say so myself :).

The first animation is a recreation of my fan:

See this PDF for a nice step-by-step walkthrough, including a photo of the original. Slightly higher quality avi file here. Maya source file here.

The second animation is of a toy train. This one I created together with Bert:

See this PDF for some details about how the animation is made and some screenshots. Slightly higher quality avi file here.

Grauw

1 comment [reply]

Roland GAIA SH-01 A little while ago I bought a Roland GAIA SH-01 synthesizer. It’s got three oscillators, an effect section, and almost everything is directly accessible from the front panel. Fiddling around creating new sounds is a lot of fun, and it sounds good!

Now, Roland is also planning to release a software package to edit sounds on the GAIA from your PC, which provides some extra possibilities, however it isn’t out yet and won’t be free.

But I found that Roland has made available a MIDI implementation document for the GAIA, also documenting a protocol to access its internal memory. So what I did is I started working on a little tool that can talk to the GAIA. It’s still in its early stages, but already it exposes some very interesting information. For example, I was able to extract a list of preset patch names — should come in handy!

There are currently two menus to issue commands and a log to show results. The first menu lets you play some test notes and send GM on/off control messages (looks like they just reset the GM though). The second menu is more interesting, it allows you to dump the system settings, as well as the settings of the currently selected patch (including edits). So now you can retrieve the exact patch settings, share them, reposition the controls, whatever!

Some plans for the future are to show the parameters in a little more visually appealing and structured way, and to eventually allow you to edit the parameters. I also want to add the ability to set the tempo to a specific BPM. An editor for arpeggios is also on the wish-list, and also there’s a bunch of reserved memory locations that may have something good lurking in them.

» Download the GAIA tool here

The source code is available. Consider it Apache 2.0 licensed for now, although I didn’t add license headers yet. If you want to contribute please go ahead, but let me know if you do. If you make any changes please contribute them back!

Update: As of version 0.1 it is possible to edit arpeggios, and as of version 0.3 you can organise your patches in a library. A first for the GAIA!

Grauw

55 comments [reply]

Earlier posts
TitleDateComments
WhiskyJuly 13th, 201010
MSX Computer Magazine online archiveMay 2nd, 20100
Mercurial shallow clones and repository sizesApril 9th, 20102
Eclipse community awards finalistMarch 18th, 20101
The view port element is… which?February 16th, 20100
What’s wrong with feature detection?February 8th, 20100
Object-oriented programming in JavaScriptFebruary 1st, 20100
Saving time with MercurialJanuary 5th, 20102
Var and function in JavaScriptDecember 30th, 20090
Planet MSX pipeSeptember 24th, 20091

» All blog entries…

» Blog feed