This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2009-01-17 01:50:56

AddZero
Scratcher
Registered: 2007-08-11
Posts: 100+

midi music import/player -online performance problems.

http://scratch.mit.edu/projects/AddZero/383557#

Well, I need simple midi import for another project I have, so this project spawned and I got a bit carried away.

It seems to work pretty well off line.
I'd be interested in how this works for you and any performance tips to get this faster online.

Does lists bog down the online player?

Thanks!

project notes wrote:

This plays a song from Super Mario Galaxy using the "play note" block.

*Works when downloaded!*

I made this to test importing a midi file into scratch.
Let me know how it works for you, and if it does work online for you.

Supports most basic midi features:
*multiple notes at once
*variable instrument per note. (user changeable too.)
*variable volume per note.
*tempo change
(note warp not possible)

Problems:
Slow/broken online. (I think limiting the number of array hits would help but would make the code harder to read.)
Does not play drum sets yet.

The file is imported from:
27500_Comet-Observatory.mid
The music is by Nintendo.
the midi was created by "usatraveler"
http://www.hamienet.com/midi27500_Comet-Observatory.html

How it works:
A sprite can play only one midi note at a time.  So, this works by having a bunch of "player" sprites.

The "player feeder" assigns a note to play to the 1st player listed on the "available-players" list, then removes it from that list.  (It keeps feeding till the "available players" is empty, then it waits till it's not.)

Each "player" sets its volume and instrument that was assigned it and waits till the timer is greater than its time to play then plays.
It then re-ads itself to the "available-players" list and waits for another note to be assigned to it.

The midi file was converted to the Cmedia file format (using Rose Garden (a Linux only program- there is likely Windows programs that can do this too), which is a text document that lists all the notes and when they play...  I used a spreadsheet to convert the notes to note number and sort them by time.  (They're grouped by instrument originally.)

Feel free to do whatever you like with this.

Last edited by AddZero (2009-01-17 01:51:50)


http://scratch.mit.edu/static/icons/buddy/524717_med.png?t=2010-06-15+09%3A48%3A36

Offline

 

#2 2009-01-17 02:16:09

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: midi music import/player -online performance problems.

Wow  this is  amazing, sounds just like the real thing.

Too bad the online player messes everything up.


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#3 2009-01-17 13:36:36

AddZero
Scratcher
Registered: 2007-08-11
Posts: 100+

Re: midi music import/player -online performance problems.

paddle2see wrote:

It would be great if we had a program that could convert directly from MIDI format to the multiple list format you have developed here...

That would be cool.  Perhaps it could be written in python.  Here is a midi library:  http://www.mxm.dk/products/public/pythonmidi

Right now the process involves allot of steps:
- open midifile in Rose Garden
- export to Cmedia
(Example- Here's what a scale from middle C looks like)

Code:

;; Csound score file written by Rosegarden

;; Copyright note:
;; Unknown

;; Segment: "Acoustic Grand Piano"
;; on Track: ""
;;
;; Inst    Time    Dur    Pitch    Vely
;; ----    ----    ---    -----    ----
;; Event type: clefchange
   i0    0    1    8.00    100    
   i0    1    1    8.02    100    
   i0    2    1    8.04    100    
   i0    3    1    8.05    100    
   i0    4    1    8.07    100    
   i0    5    1    8.09    100    
   i0    6    1    8.11    100    
   i0    7    1    9.00    100    
;; Event type: rest
;; Event type: rest
;; Event type: rest

t 0 120

e

- open Cmedia file in openoffice.org as a Tab delimited spreadsheet file. (Excel should also work)
- sort all the notes by time.
- convert the (octive.key) note format to key numbers

Code:

=((D1-ROUNDDOWN(D1))*100)+(ROUNDDOWN(D1)*12)-36

so "9.04" (9th octave, 4th note) becomes "76"

convert the instrument to a number, plus 1 (to reference list item numbers)

Code:

=RIGHT(A1;LEN(A1)-4)+1

so "   i0" becomes 1

- Then save each relevant column to a text file.

- the tempo needs special attention:

Code:

t 0 170 18 170 18 140 24 140 24 170

becomes:

Code:

0
170
18
140
24
170

("0 170" tells it that at beat 0, set the tempo to 170 bpm.  I'm not sure why we need "18 170" yet. Perhaps if we were to play the music backwards?)  (I found that my tempo setting code in scratch is broken.)

- import all these files to their respective lists in the midi lists.

paddle2see wrote:

where can I find a description of the MIDI format?

http://www.sonicspot.com/guide/midifiles.html
But it's a binary format, so I'd use some library made for reading it.
I listed python above, here's one made for php, http://staff.dasdeck.de/valentin/midi/

I forgot to mention this:  This scratch player will start to lag at about 10 to 30 notes per second.  So it wont work on fast songs.  That's because the "player feeder" can't go any faster.  So some thoughts were to simplify it by sending only the midi note number to the players- (I should have thought of that sooner.)  so the players look up what they need from the midi lists--  and perhaps thread the "player feeder".  divide it up into a couple/few scripts.  Also It would be much faster if the tempo calculations were done before import. 

And it would be faster if it was tailored for piano only songs. 
This would be a fun one  smile   http://www.hamienet.com/midi25986_Athletic-Theme.html
or anything from this site:  http://www.piano-midi.de/debuss.htm  I like "Andante très expressif"


I'll look forward to what you all do with this idea.

Last edited by AddZero (2009-01-17 14:51:16)


http://scratch.mit.edu/static/icons/buddy/524717_med.png?t=2010-06-15+09%3A48%3A36

Offline

 

#4 2009-01-17 17:07:11

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: midi music import/player -online performance problems.

Very nice write up!   I have an interest in this area but I don't have a MIDI producing device right now.  There must be music programs that produce a MIDI output file?


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#5 2009-01-17 21:33:38

AddZero
Scratcher
Registered: 2007-08-11
Posts: 100+

Re: midi music import/player -online performance problems.

Paddle2See wrote:

I have an interest in this area but I don't have a MIDI producing device right now.  There must be music programs that produce a MIDI output file?

I don't have a midi keyboard connected.  All I've done with this project so far is just convert midi files I found around the internet. 
I'm not familiar with windows sequencers enough to recommend one. here's some: http://www.hitsquad.com/smm/cat/MIDI_SEQUENCERS/
Often you can just draw the notes like your scratch program.
For converting midi to play in scratch, I think the python library is a good bet.

Last edited by AddZero (2009-01-18 00:31:32)


http://scratch.mit.edu/static/icons/buddy/524717_med.png?t=2010-06-15+09%3A48%3A36

Offline

 

#6 2009-01-21 12:51:16

awsomemaster
Scratcher
Registered: 2008-04-10
Posts: 83

Re: midi music import/player -online performance problems.

dose any body know how to import pics and sprints? like so i could take a pic with my camra and put it on scratch?


scar the REAL 3d fps we've all been waiting for

Offline

 

#7 2009-01-21 15:17:23

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: midi music import/player -online performance problems.

awsomemaster wrote:

dose any body know how to import pics and sprints? like so i could take a pic with my camra and put it on scratch?

If it is a compatible graphic format, you can probably just drag the photo file over to an open Scratch project and drop it in Costumes area of a sprite.

By the way...your post has nothing to do with the topic.  Please post only to topics that relate to your post or make a new thread.  Thank you.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer