Sunday, 26 February 2012

Getting started with Java MIDI

This post is about getting started with the Java MIDI Api.

I'll try and step through creating a simple class that will let you add notes to a track, then let you play that track. This will allow most

If you want in depth information about everything, this site is great:

http://docs.oracle.com/javase/tutorial/sound/MIDI-synth.html

First things first, create a MusicPlayer class:


import javax.sound.midi.Track;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class MusicPlayer {

        public Sequence currentSong;
        public Track currentTrack;
        public Sequencer player;

        public MusicPlayer()
        {
                currentSong = null;
                currentTrack = null;
                player = null;
        }
}



The second thing is learn how to write code on blogger....

No, the second thing is to add a method to create the sequencer we will use to play the music.
If you're looking for more information about the sequencer timing thing:
http://docs.oracle.com/javase/1.4.2/docs/api/javax/sound/midi/Sequence.html


    public Sequencer createPlayer()
    {
            Sequencer cPlayer = null;
           try
           {
                   cPlayer = MidiSystem.getSequencer();
                   cPlayer.open();
                    // 16 counts per quarter note, so 4/4 = 64 counts;
                   Sequence seq = new Sequence(Sequence.PPQ, 16);
                   currentSong = seq;
                   currentTrack = currentSong.createTrack();
           }
           catch (Exception e)
           {
                   e.printStackTrace();
            }
           return cPlayer;
    }



import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class MusicPlayer {
   
    public Sequence currentSong;
    public Track currentTrack;
    public Sequencer player;

    public MusicPlayer()
    {
            currentSong = null;
            currentTrack = null;
            player = createPlayer();
    }
   
    public void play()
    {
            if(currentSong != null)
            {
                   try
                    {
                            player.setSequence(currentSong);
                            player.start();
                    }
                    catch (Exception e) {
                            e.printStackTrace();
                    }
            }
    }
   
    public void addNote(int pitch, int volume, int time, int duration)
    {
            try {
                    if(duration < 1)
                    {
                            duration = 1;
                    }
                    ShortMessage a = new ShortMessage();
                    // First part is what you want to happen, the 4 is channel (instrument pretty much)
                    // Pitch is which note (60 for middle C, 61 C#, 62 D etc)
                    // Volume is how loud (relatively) the note should be played.
                    a.setMessage(ShortMessage.NOTE_ON, 4, pitch, volume);
                    MidiEvent noteOn = new MidiEvent (a, time);
                    currentTrack.add(noteOn);
                    ShortMessage b = new ShortMessage();
                    b.setMessage(ShortMessage.NOTE_OFF, 4, pitch, volume);
                    MidiEvent noteOff = new MidiEvent (b, time + duration);
                    currentTrack.add(noteOff);
           } catch (InvalidMidiDataException e) {
                    e.printStackTrace();
            }
    }
   
    // If I was smart this would be an actual song rather than bad noise!
    public void createDemoSong()
    {
            // Pitch, Volume, StartTime, Duration
            // StartTime is in the same time scheme as the sequencer
            // So 0 starts beginning first bar, 64 seconds, 128 third etc
            // Because our sequencer is set to 64 counts a bar
            this.addNote(60, 60, 0, 16);
            this.addNote(50, 60, 16, 16);
            this.addNote(62, 60, 32, 32);
            this.addNote(54, 60, 64, 16);
            this.addNote(56, 60, 80, 16);
            this.addNote(44, 60, 96, 16);
            this.addNote(76, 60, 112, 16);
            this.addNote(47, 60, 128, 16);
            this.addNote(48, 60, 144, 32);
    }
   
    public Sequencer createPlayer()
    {
            Sequencer cPlayer = null;
            try
            {
                    cPlayer = MidiSystem.getSequencer();
                    cPlayer.open();
                    // 16 counts per quarter note, so 4/4 = 64 counts;
                    Sequence seq = new Sequence(Sequence.PPQ, 16);
                    currentSong = seq;
                    currentTrack = currentSong.createTrack();
            }
            catch (Exception e)
            {
                    e.printStackTrace();
            }
            return cPlayer;
    }
}

Not a game update

Time for an update:
(I may be doing this update instead of working on that update of the game I promised, whoops)

Flying Kakapo: The Game currently has 2 sales, and 3 of those are still installed according to Google, so that's confusing, but atleast I don't have to panic about lots of people having trouble with it not working on their phone.
The sales aren't even family members! (but they are friends hehe)

The update I'm currently working on will fix a couple of bugs, and add Arcade Mode. It's a shorter, sharper experience, for when you don't have half an hour available to get those big scores...

Katie and I went busking yesterday, and had a great time. We made only $8 from our hour twenty playing, but got great responses from everyone. Half the money earned was people impressed with the hats we were wearing (hats Katie made!) so that was nice :)

Katie's been taking photos of her hats to post so they should be visible soon.

I also wandered around the Nelson Saturday Market and got an idea of prices for woodworking, and spoke to some very friendly locals who've invited me to come along to a meeting of the Nelson Woodturners Club. I'll be going hopefully, I need advice about which lathe to buy as I'm keen to get started.

In the programming side of things, I have way to many projects going.

I've gained permission to use some images for a New Zealand Sign Language quiz I'm working on, so hopefully I'll be getting that out soon. It's just the alphabet to start with, but Katie and I are having fun using it to communicate when I'm hiding behind my headphones. It will of course be free!

A fun project that isn't very pratical is my attempts to write a program that will write music for me. I've been learning about the Java Midi api. I'm also working on a post explaining how to get going with the api as I'm likely to forget...
Hopefully it will end up being useful to someone.
Katie isn't very impressed by it's output so far, she thinks she can do better :P


Another game I'm working on is a language learning game, I should do a post about that soon too. Yeah, I will, trust me! At the moment I'm thinking of it like the original Pokemon games, but where you have to learn a language as you go, it should be interesting :)

As always, leave a comment or twit at me on twitter!

Random fact: The country with the third most views on this blog is the Czech Republic (if you read this, leave a comment I'm curious!).

Monday, 20 February 2012

Flying Kakapo: The Game


Well I've finally released the game I started working on two weeks ago.

You can check it out here: Flying Kakapo: The Game

This game was concieved when working on my as yet unreleased Charades game, all it had in it was one movie, Gone With The Wind. When you clicked next it added a number that increased everytime you tapped the screen. I remember getting up to 180 before I realised what I was doing.
It's amazing how something so simple can be addictive.

This game initially started as just tapping the screen and seeing how high you could get it, well, before you got bored I guess. You can see it looks a little more developed than that. Lots of creep.

I added an object with Box2D that you could move around by tapping. Then made it fall. Then made a little bird graphic for it.

Now the game contains combos for tapping it numerous times in a row without missing, falling items to collect and a combo reset for smacking him into walls.

Altogether quite a fun game and fits well within the 'chocolate bar' market of $0.99 games.

Not expecting it to be an amazing seller, but certainly a game I'm happy with and a good learning experience.

I'll be blogging all details about it's experience out there in the world as time passes.



And so it begins

Well, the move has been completed.

Here I am in sunny Nelson, for which there is a severe weather warning for the next few days!

For a while now I've been telling myself that everything starts once I get to Nelson.
Yesterday (Monday) I went to the gym and set everything up, then went to work fixing bugs and getting my first mobile app ready for release.

Some might have viewed this as procrastinating on my blog post, and they might be right.

It seems a bit silly to post my first post on my second day of work but atleast I'm posting today! (ignoring the fact it is now 7pm as I write this).

So where do I stand?

Programming: -$168 NZD
I'm currently at -$138 from buying a web address and hosting, then another -$30 from registering as an Android Dev. Hopefully I'll start digging my way back up to break even when I release my game, even if just really slowly.

Woodworking: +$1,150 NZD
Currently at $1,150 NZD for woodworking, that looks pretty good right? Unfortunately that includes a $1,000 loan from my parents (which my father is trying to make into a gift, as parents like to do, should I allow this? Hmmmmm). $100 is from my godmother (Thanks Liz!) and $50 from my Uncle and Aunt for my birthday last year. So depending on how you look at it I'm either at positive $1,150 or negative $850. I prefer the first.

Soon as I can get some time in town (currently living 16km out of town) I'll look at buying some tools. First on my list are a router, jigsaw and drill. I'm planning on making some childrens rocking chairs first, to sell at the local market. You can see one in the background behind the chess table on my woodworking page.

Music: $0
Currently sitting at $0 for music, and I was going to say it's not likely to change anytime soon, but Katie has threatened to drag me out busking this weekend in the hopes of collecting the end of the tourist section. She seems quite serious as she had us practicing this afternoon (one reason why this post is so late).

We have been out busking once before, at christmas, and made $11 which we promptly spent on icecream at Penguinos which may or may not be the entire reason I moved to Nelson. The icecream is so good my sister once drove 5 hours to try and get some, only to find it didn't open til later in the day, and then had to drive 5 hours home.

So that's where things sit currently, and this is where I sit!




Wednesday, 8 February 2012

My Evil Plan

The move is progressing slowly, done some calling around and found it would cost around $650 to move our stuff to Nelson (bed, bikes, couple of chairs etc). A problem is that Katie's place is fairly rural, involving a small bridge so lots of stress and complications.

After some discussing (and perhaps some guilting) my sister and her husband are going to drive up with us, with a trailer full of our stuff, then take the trailer back down the next day, saving us $200 of relocation fee for the trailer.

This is something I am much more comfortable with, less stress! I've offered them the difference in costs so it might not be cheaper but lessss stressssssssssssssssssssssss. (And now I'm a snake...)

My grand plan for this year:

1): Earn $10,000 income (not profit) between now and new years.
2): Release 10 Mobile Apps that get atleast 1000 downloads and $1,000 income over the 12 months following their release
3): Enjoy myself!

I think $10,000 is too low of a target, and Katie thinks it's too high, so we settled on it. I'm guessing at this point 90% of the income will come from programming, 10% from woodworking, and 0.000001% from music.

The mobile apps will be interesting, I have a list of 30 odd apps to work on, I'm sure I will release more than 10, but most of them probably won't do very well. Only having to get 1,000 total downloads shouldn't be too hard, it'll just involve a lot of guilting people I know (and you, my dear readers!).

Enjoying myself shouldn't be too hard either, that's why I have three different things to concentrate on, if I'm not enjoying programming, I'll do woodworking, or music. And if I'm enjoying none of them, I'll play games :D

So I feel fairly optimistic at the moment and I'm looking forward til after the move so I can throw myself fully into this. I'll hopefully cover more of my plans in my next posts.

For now, a preview of what I've been working on the last two days:

Friday, 3 February 2012

A Little Background

So time for a little background.

Well I'm currently twenty five going on seventeen, I have a Bachelor of Science, Majoring in Computer Science and I live in New Zealand.

I've been living in Christchurch for the last 8 years, since I moved down here to attend University.

I went to uni to get a degree in physics. Over a period of five years I did a various assortment of courses from astrophysics to philosophy. In the end I got my degree in computer science when the University decided my philosophy of religion course counted as science credits.

And so there I was with a shiney new degree in hand and not much of a clue what to do with it.

So I did what any young person would do, spent a year sitting about the flat playing computer games and burning through what little savings I had. I did eventually get around to applying at one talent agency and for one job. Neither of which I heard back from if I recall correctly.

At that new years I finally made a resolution to do something, so I did some googling and found a local company I'd heard a friend talk about that made computer games.
I wandered through the door there about as soon as they opened after the holiday period. It turned out they were having a problem with their internet, so the boss didn't have much to do and was able to interview me.
I got a call back later that day asking if I would be keen to help them with some game testing.
It wasn't really a job in most senses of the word, as I didn't actually get paid for a good two months. But such is the life in the gaming industry, and I was enjoying what I did.
Thus began my work as a games tester, programmer and designer.
(I actually have my name in the credits as a game tester for Kung Fu Funk and Doc Clock).

Once I got bumped up to programming I spent most of my time working on Crystalink for Facebook which I quite enjoyed.
After a year and a half I found I wasn't enjoying myself as much, and was beginning to dread work each day. So I took the leap and handing in my resignation, staying on untl the next project milestone. I then did something totally random and took a six month carpentry Furniture and Joinery course.

It was a massive change to go from lounging in a chair all day spaced out as I dealt with some problem to working all day on my feet with whirling blades of death attached to everything.
I can't say I was any good at it, but I enjoyed my time in the course, and managed to get through with all fingers still attached, though I had a good go at melting my thumb on the last day. and left behind a fair bit of skin and blood in various amounts.


Then an opening came for Katie to help her brother back in her home city of Nelson, and as we weren't doing anything much we decided it was a good time to move.

So we're about to move to sunny Nelson (which recently suffered serious floodings just before we were there for christmas, I'm fairly sure the sunny thing is a con, as it always rains when I'm there). It seems kinda wierd to have stayed here through the aftershocks, only moving when they've mostly died down.

Mostly it's just time for a change.

Thursday, 2 February 2012

Jamie's Journalistic Journey towards Financial Freedom!

This year I'm going to try out being self-employed, earning my existence through woodworking, programming for mobile phones and music.

A big step but now seems like a good time to try it, my girlfriend (Katie) and I are moving cities. At the moment I'm actually on holiday in the Queen Charlotte Sounds, but once we get back the move beings in earnest.

And through this blog I'm going to cover all the nitty gritty details of my efforts. I find blogs that post actual information about how they're getting by, and how much money they're making fascinating, so this is going to be my attempt.

So let us hope I make something to talk about!

Over the next short while I'll be messing around with my website (http://www.flyingkakapo.co.nz) and adding some information to this blog.

Meanwhile here's what I'm enjoying: