Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Kirl on August 23, 2011

Title: help structuring a virtual piano player
Post by: Kirl on August 23, 2011
I'm working on a virtual keyboard player and I've got it playing a ditty, but I'm unsure how to structure the music data so that it's easy to read/write the sheet music, and to later add more info without cluttering the music data. Currently my 1st few notes look like this:

Code: [Select]
var notes = [ [9], [12,5], [12,7,9], [12,5], [12,7,9,"a"], [14,"a"], [13,5,"a"], [12,"a"], [11,7,9], [12,5], [7, 9], [12,3], [12,7,5],
[10,3, "a"], [12,"a"], [11,7,5,"a"], [10,"a"], [11,0], [9,7,4], [8,-3], [4,1,-1] ];

It only stores keys and the "a" signifes 1/2 notes (quite clumsily), Ideally I'd like to be able to store all info present on sheet music. but the array is quite an obtuse mess already (and it's only the 1st few notes).

Any ideas for how to hold the music data (like key, duration rests, etc) in a somewhat more readable format?
Title: Re: help structuring a virtual piano player
Post by: combatking0 on August 24, 2011
What do the other numbers in the array of arrays represent?

Perhaps having a standard format would be beneficial, such as:

Code: [Select]
var notes = [ [k,d,r,h], [k,d,r,h], ... etc  ];

Where:
k is replaced by numerical data which represents the key
d is replaced by numerical data which represents the duration
r is replaced by numerical data which represents the rest following the note
h is replaced by boolean data which represents the presence of a half note

For example

Code: [Select]
[9,4,2,false]

would use note 9 for 4 time units (beats?) followed by a rest of 2 time units and is not a half note.

My knowledge of music is limited, but hopefully my interpretation will be useful in helping you to find a working solution.
Title: Re: help structuring a virtual piano player
Post by: Jim on August 25, 2011
What about MIDI?
Jim
Title: Re: help structuring a virtual piano player
Post by: Raizor on August 26, 2011
I'd probably look into MIDI for a start. It may well be overkill for what you're planning, but should at least give you some good ideas for a starting point for a custom format.

It might also be worth looking at piano tablature as a basis for your format, although that might be too far the other way and prove too simplistic (no rest periods, note strength etc).

Another option may be to base what you're doing on Mod/XM (or something similar) and just replace the samples with your own keyboard notes.
Title: Re: help structuring a virtual piano player
Post by: padman on August 26, 2011
Dunno if that's any help at all but I'll post it anyway.  ;)

There's a C example for playing midi via your PC keyboard (and some other stuff regarding midi) in the book "Programming Windows" by Charles Petzold. Maybe you can nick something from the examples.

Sourcecode(s) can be found there: ftp://ftp.charlespetzold.com/ProgWin5/Chap22/ (ftp://ftp.charlespetzold.com/ProgWin5/Chap22/)
(The BachTocc and KBMidi are the ones I'm referring to.)
Title: Re: help structuring a virtual piano player
Post by: hellfire on August 26, 2011
On computers musical patterns have always been laid out differently.
That's because it's hardly possible to get all symbols from sheet-music notation into an array-structure (just think about Glissando or Vibrato).
Midi, for example, doesn't even know the length of a note but just schedules a key-released signal afterwards.
Title: Re: help structuring a virtual piano player
Post by: Kirl on August 26, 2011
Thanks for your input all!   :cheers:

I've been reading up on MIDI for how it structures information, but didn't really find the miracle solution I was looking for. Actually using midi in Flash requires some nasty work arounds.

The piano player is working surprisingly well as it is though, even without duration info and the rest which I thought would be necessary. Got some cool experiments like random music generation planned, something I've been thinking about long before I was programming. Also read a lot about the finer points of music notation and been really enjoying myself with this project. Read and done a lot of stuff I never really got around to doing!

@ck0
All numbers are notes, starting at the lowest C (0), multiple numbers/keys in an array are played together. I loop through the notes array with an interval.
Title: Re: help structuring a virtual piano player
Post by: combatking0 on August 26, 2011
I see, that would allow you to output multiple notes simultaneously. That sounds like a good idea.