K-Gater, Part 1


I might as well expand the purpose of this blog, rather than start up yet another one (I have 4 already).  Initially, the idea of the WordPress ThreeStepsOverJapan blog was to just act as a place for holding my reviews of the Gakken Otona no Kagaku kit reviews.  When I started it, I had enough material to post an entry a day for well over a month, and that dovetailed nicely with the release of the next kit at the time.  However, after I caught up with the past reviews and all of the current Gakken news, the activity here dropped off because it’s 3 to 6 months between new kits, and often there’s no news from Gakken for months at a time.  So, to fill the gap, I started reviewing the 50 Famous People mook series.  This keeps TSOJ somewhat active, but since the mooks only come out once a week and I only want to buy about half of them, I’m only able to post a review once a week, generally Monday mornings my time.

However, I decided to take on a new programming project for myself, and I’d rather talk about it here, than trying to trim it down and attempting to submit it as a tutorial to some Java reference site.  So, I’ll chop the code up into moderately large chunks and post a section, with ramblings, every couple of days, or so.  (I don’t seem to be able to get WordPress to keep the formatting, though, so the code may turn out unreadable.)

I’ll use this first post to get some ranting out of the way, and to lay the groundwork for the later code.

First, What is “the project”?

A few months ago, I bought the Gakken SX-150 Mark II, and after playing with it for a while, I realized that I really wanted to own a bigger, more powerful synthesizer.  When I broke my foot, I had a lot of free time on my hands, and the desire to get a synth became overwhelming.  However, at that time, I couldn’t get around, and I didn’t want to buy something off of Amazon without at least being able to handle it a bit, first.  So, after my foot healed enough that I could move around in the shopping mall on crutches, I visited a small music shop in the train station near my apartment.  After very little consideration, I settled on the Korg Kaossilator Pro (K-Pro).  It’s small, easy to hold when I’m sitting on the bed, doesn’t take up much room (my apartment is tiny) and lets me play tunes right out of the box.  I also like the fact that I can use the LFO section of the Mark II as a filter for the K-Pro output.

However, the so-called “8×8” touchscreen matrix is set up to make it really hard to hit the same note twice, especially if you’re alternating between two notes that are kind of far apart.  In fact, the screen is divided up into 128×128 cells (although, depending on the selected instrument, not all of the cells will be for separate notes).  This not only affects repeatability, but it also means that the K-Pro is not 100% MIDI-compatible.  Which leads to the second issue.  I wanted to hook up a small MIDI keyboard to the K-Pro, and every Korg forum that I visited said the same thing – won’t work.  The MIDI Note_On/Note_Off messages only go from 0 to 127, and with the K-Pro you have 128 times 128 notes.  Instead, Korg implemented CC messages (control codes), and you need 3 CC messages just to play one note: Set the X address, set the Y address, specify the note as On or Off.

So, I looked around some more and while a couple people wrote little mapper programs in C++ or Basic, I wanted to have something a little more powerful and under my own control.  Hence, “the project”: a MIDI Mapper for interfacing the K-Pro to a MIDI controller keyboard.

Why Java?

I’m cheap.  I don’t want to pay for a copy of C++ or Basic just to write some code for my own personal use.  And, because VBScript and Perl don’t provide the kind of GUI I want, the choices for “free” are kind of limited.  Actually, I really like Flash, but it’s priced way out of my reach, so that just leaves Java.

I don’t like Java much.  It’s quirky.  Some of the code doesn’t work as advertised.  I can not figure out how to make serializing objects work for saving them to a file.  The Oracle documentation is hard to navigate, and many of the tutorials are too trivial or incomplete to be useful in the real world, forcing me to do a lot of trial and error that I’d rather avoid. Screen layout for the GUI is also a bear if I’m going to have lots of buttons and sliders.  However, it is free, and after a huge amount of yelling and swearing, I eventually can get something marginally close to what I want.

Why Netbeans?

As mentioned above, handwriting raw Java code to make a complex GUI is just too much work.  As long as you’re careful, and remember to break the screen up into N-S-E-W-middle and put the components into the correct panels, Netbeans won’t automatically screw up the layout on you too often.  Plus, it is useful for automating certain steps such as creating action listeners.  And, it’s free.

One of the biggest drawbacks to using Netbeans, though, is that you’re blocked from touching the action listener names.  If you write raw Java code, it’s easy to make a for-loop and create 30 buttons in just a few lines of code.  Very simple and compact.  With Netbeans, every single component is spelled out one at a time, and if five buttons do pretty much the same job, you’ve got 5 sets of duplicated method calls where all that changes between them is an ID number (this is being called by button 1, this now being called by button 2, etc.) I’m mentioning this now, to explain why my source code listing is so stupidly long.

Why “K-Gater: The Project?”

I want a keyboard to drive the K-Pro. I need something that will convert between MIDI Note_On/Note_Off messages to CC messages.  I couldn’t find a tutorial that showed EVERY SINGLE LINE OF CODE needed for driving external MIDI devices.  Most of the documentation gets right up to making the connection to the external device, then switches over to using the default Java software synthesizer at exactly the wrong point.  So, I’m writing this mostly for my own education, and posting it for anyone that cares.

Several professional musicians on the Korg forums had stated that if you connect a computer to the K-Pro, that you’re turning it into just an over-priced synth, and there are a lot of other synths on the market that are a lot better for the money.  This is true.  But, if you’ve got the K-Pro, you’ve got the computer, and you’ve got the keyboard, you might as well hook them all up and get a little more mileage out of all of them together.  Their claim that the primary strength of the K-Pro is the funky x-by-y touchscreen, does hold true, and you can still use the touchscreen to play notes, even after adding the laptop connection.

Why’s this code look so sucky?

I’m not a trained, professional Java programmer.  I’m self-taught, and I’m still learning.  I don’t mind having my code tweaked to be more standardized, or made more elegant.  And I don’t mind being shown better ways to do things (in fact, I’d prefer it).  But, please don’t call me names or insult my code because it offends you.  Thanks.

Why K-Gater?

One of the complaints professional musicians have with the K-Pro is that Korg removed most of the arpeggiator functionality. The arpeggiator, also called a gate arpeggiator, or just “arp”, is a circuit that causes the note that you’re currently pressing to turn on and off at variable rates and with selectable patterns.  That is, rather than pushing the keys really fast yourself, you just hold one key down, and the gate arp plays the pattern for you (such as going up 3 notes one at a time, or down 3 notes in half a second).  With the K-Pro, the arp only plays the one note you’re currently selecting and just gives you one of two options: To change the rate with a fixed on/off ratio; or change the ratio of note on to note off time at a fixed play rate.

I figured that if I was going to make a MIDI mapper in Java, that I might as well extend the gate arp functionality to allow for any kind of pattern I can think of.  This is a little different from a sequencer, in that the sequencer records the Note_On/Note_Off messages and allows you to reassign the channel to a different instrument or voice.  With an arp, you can have a 0, 1, 2, 3 pattern or a 0, -4, -2, 3 pattern, and then change the resulting output by pressing a different note (key), changing the step sizes. or changing the rate or on/off ratio.  And, because this is all in software on a computer, I could make the entire song into one long arp pattern, set the rate to 1/32 seconds and loop it while playing different keys.  But that sounds like too much work, so I won’t.

However, because the CC messages are specific to the K-Pro and the regular Kaossilator, the K-Gater program is hardcoded for just the K-Pro.  And, I’ll be further hardcoding it when I decide on what MIDI controller keyboard to buy.

What, you don’t have the keyboard yet and you’re already writing this blog?

Yup. the K-Gater is already functional without the external keyboard.  I’m happy with the ability for selecting instruments, creating arp patterns, and saving the whole setup as a patch file.  I can change the note through a text field and that lets me amuse myself with the results of different patterns and arp rates.  I’m seriously considering getting a Roland A-300 Pro, which I can then connect to some real DAW software to use it to its full potential.  The K-Pro will then be used as a phrase synth as originally intended.  The nice thing about the Roland is that it has enough controls and pads that it’d be a very simple matter to read the incoming MIDI messages from it and turn them into Java component .doClick() instructions for the different buttons and combo boxes of the K-Gater.  That is, adding the Java code to the K-Gater after buying the Roland should take less than a day.