Translating javascript

About Truespace Archives

These pages are a copy of the official truespace forums prior to their removal somewhere around 2011.

They are retained here for archive purposes only.

Translating javascript // Scriptorium

1  |  

Post by jamesmc // Dec 19, 2007, 10:03pm

jamesmc
Total Posts: 2566
Working on a project and have found one of the parts for that project at the following Web Page.

http://www.cournia.com/classwork/cpsc808/proj5/

The goal of my project is to take midi files and translate them into a javascript which would take wav files and translate them into note off/note on functions.

The next step would be to assign them to a particular midi instrument and its respective range of a music dynamic (song)

Then, to write a controller that could kept in the library which in turn could be used for procedural animation.

...............

However, the example the Web Author gives for his script is written in Maya Embedded Language (MEL) which is Perl based.
(1) Writing pseudo code from his interface would be one way and tedious.
(2) Having a translator from MEL to javascript would certainly be nice, but probably ain't gonna happen.
(3) Wing it and write my own translator (aspirins and 'ho ho' time.

So the path I want to make would be in an animation is:

midi song>translator>translator renames and reassigns midi to corresponding wav file mega matrix>jscript>controller>procedural animation start object
>translate wav file to object>object plays wav file

Opinions?
Observations?
Pointing and Laughing? :)


NOTE:

This is part of the text from the MIDI to MEL transformation:
"My goal in this project was to be able to drive an animation through the note and instrument information found in a Musical Instrument Digital Interface (MIDI) file. To achieve this I wrote a program (based on the Timidity library) which would take a MIDI file as input and output a Maya Embedded Language (MEL) script. The MEL script contains function stubs (note_on() / note_off()) which the MEL prorammer must define. The note_on() / note_off() functions are passed the following information:

# Frame that the note was hit/released.
# Instrument that was played.
# How hard the note was played (velocity).
# The note played (Ex. Middle C).

With this simple interface the MEL programmer is able to easily create amazing effects."

Post by stan // Dec 20, 2007, 8:51am

stan
Total Posts: 1240
pic
jscript is a tad different that javascript..jscript is what truespace uses ..;)

Post by jamesmc // Dec 20, 2007, 9:25am

jamesmc
Total Posts: 2566
true Stan, I'm a consistent context violator of terms. :)

Post by Wigand // Dec 20, 2007, 9:40am

Wigand
Total Posts: 462
pic
I don't understand what you want to do with wave files. :confused:

Post by jamesmc // Dec 20, 2007, 10:52am

jamesmc
Total Posts: 2566
one note = one wav file

a corresponding strike of a hammer, blow on a horn, strum on a guitar, keyboard plinked will result in properly rendered sound if sync'd proplerly. :)

I've seen the music machine, but the method is tedious and subject to error.

I want to make a method to
(1) translate a midi file to jscript
(midi's can play out note by note or by instrument or chords etc.)
(2) introduce that jscript as a controller to tS
(3) subsequently make various types of matrices for different instruments that can be referenced by plugging the midi file into the jscript translator (i.e. wave file distributor per instrument) controller

Something like that anyway - a very long term project, but one that would prove extremely useful in controlling sounds and music in trueSpace.

Post by Délé // Dec 20, 2007, 11:26am

Délé
Total Posts: 1374
pic
I think I get what you're saying. So you basically want a bunch of short, one note wave files setup in tS. Then somehow translate a midi file into jscript object to trigger those wave files along with a procedurally driven animation. does that sound right?

Sounds interesting, but I'm not sure how to go about it. I know that it's possible to read and write to a .txt file (Glen proved that). I'm not sure how one would go about reading a midi file though with jscript if there is a way. It would be interesting though. If you can read the midi file the rest should be possible. The only thing is you can't play more then one wave file at a time in tS. So you would be limited to note by note midi files.

It would be very cool if the devs could write a midi listener device that could listen to live midi input too. Lot's of possibilities for midi controlled scenes.

Post by jamesmc // Dec 20, 2007, 12:13pm

jamesmc
Total Posts: 2566
I think I get what you're saying. So you basically want a bunch of short, one note wave files setup in tS. Then somehow translate a midi file into jscript object to trigger those wave files along with a procedurally driven animation. does that sound right?

Sounds interesting, but I'm not sure how to go about it. I know that it's possible to read and write to a .txt file (Glen proved that). I'm not sure how one would go about reading a midi file though with jscript if there is a way. It would be interesting though. If you can read the midi file the rest should be possible. The only thing is you can't play more then one wave file at a time in tS. So you would be limited to note by note midi files.

It would be very cool if the devs could write a midi listener device that could listen to live midi input too. Lot's of possibilities for midi controlled scenes.

Bingo! You got it right on the money Dele! :)))))

Okay, here's some more in depth examples. I'm using a utility that will transfer a midi file to a ascii format they call mtx (midi text file) which can be read with a text editor (notepad) The name of the program (com) is Midi2Mtx.

Here is some of the context Mtx translation of a Midi file.

Start of track: MTrk
End of track: TrkEnd

Note On: On <ch> <note> <vol>
Note Off: Off <ch> <note> <vol>
Poly Pressure: PoPr[PolyPr] <ch> <note> <val>
Channel Pressure: ChPr[ChanPr] <ch> <val>
Controller parameter: Par[Param] <ch> <con> <val>
Pitch bend: Pb <ch> <val>
Program change: PrCh[ProgCh] <ch> <prog>
Sysex message: SysEx <hex>
Arbutrary midi bytes: Arb <hex>
...
<ch> ch=<num>
<note> n=<noteval> [note=<noteval>]
<vol> v=<num> [vol=<num>]
<val> v=<num> [val=<num>]
<con> c=<num> [con=<num>]
<prog> p=<num> [prog=<num>]
<manor> minor or major
<noteval> either a <num> or A-G optionally followed by #,
followed by <num> without intermediate spaces.

Okay, what you do at the command prompt is to type the name of the com file (Midi2Mtx and then put in the name of the Midi file you want translated to text or the MTX file.)

The following is what the transcribed file looks like in the MTX format.

MFile 1 7 480
MTrk
0 SeqSpec 05 0f 12 00 00 7f 00 00 42 01
0 SeqSpec 05 0f 1c 32 30 30 37 2e 30 35 2e 30 31
0 Meta SeqName "Sockhop.mid"
0 Meta Text "Sockhop, Music Magic DI version"
0 SMPTE 96 0 0 0 0
0 TimeSig 4/4 24 8
0 Tempo 342857
0 Meta TrkEnd
TrkEnd
MTrk
0 Par ch=3 c=7 v=127
0 Par ch=3 c=10 v=90
0 PrCh ch=3 p=2
0 SeqSpec 05 0f 09 00 40
0 SeqSpec 05 0f 06 47 65 6e 65 72 61 6c 20 4d 49 44 49
0 Meta TrkName "Piano Right Hand"
0 Par ch=3 c=91 v=85
0 Par ch=3 c=93 v=0
0 Pb ch=3 v=8192
0 Par ch=3 c=64 v=0
1173 On ch=3 n=74 v=108
1210 On ch=3 n=75 v=108
1288 On ch=3 n=76 v=108
1292 Off ch=3 n=75 v=64
1295 Off ch=3 n=74 v=64
1438 Off ch=3 n=76 v=64
1475 On ch=3 n=79 v=108
1621 Off ch=3 n=79 v=64
1700 On ch=3 n=81 v=108
1836 Off ch=3 n=81 v=64
1927 On ch=3 n=79 v=108
1931 On ch=3 n=84 v=106
1941 On ch=3 n=83 v=108
1961 Off ch=3 n=83 v=64
2043 Off ch=3 n=79 v=64
2063 Off ch=3 n=84 v=64
2162 On ch=3 n=79 v=108
2169 On ch=3 n=84 v=101
2261 Off ch=3 n=79 v=64
2274 Off ch=3 n=84 v=64
2417 On ch=3 n=79 v=108
2420 On ch=3 n=84 v=108
2529 Off ch=3 n=79 v=64

The above are the actual on/off notes displayed.

Attached is a rar file that I used. It contains the Midi file which Windows Media Player will play despite the message. :)

Anyway, this is a start and I'm ever digging into this as it looks like a worth while project.

Post by Délé // Dec 20, 2007, 1:54pm

Délé
Total Posts: 1374
pic
If you can convert a midi file to text, you should be all set to go. It might still get a little tricky, but should be entirely possible I think.

Reading and writing to a text file wasn't all that hard from what I recall. I played with it a little bit a while back using Glens script as a reference. I also found a website that had some good ideas, but I can't seem to find it right now. If I do, I'll post it.

If you haven't already, you might want to check out Glens stuff though. It will definitely show you how to go about it in tS.
http://forums1.caligari.com/truespace/showthread.php?t=3713

I think you would have to get it to work off of a timer somehow. Perhaps if you make a simple counter that's controlled by a timer. Then generate "if" statements based on the times between notes and what notes are to be played. So it would read the text file and then create a script something like:


if (Time == 15)
CommonData.RsPlaySound(Space.CurrentScene() + '/Ckey/Sound', false);
else if (Time == 30)
CommonData.RsPlaySound(Space.CurrentScene() + '/Ekey/Sound', false);
else if (Time == 45)
CommonData.RsPlaySound(Space.CurrentScene() + '/Gkey/Sound', false);


So Time would be an input that increments each time the script is executed. So when you start the timer, it would execute the script incrementing the Time value with each iteration, and each time it executes it checks to see if the value has reached the time for the note to trigger. If so it would play the appropriate sound file. It would be tricky, but I think possible.

Whatever you come up with, I look forward to seeing it. :)

Post by Wigand // Dec 20, 2007, 6:26pm

Wigand
Total Posts: 462
pic
one note = one wav file


.



Now I understand.


As Dele wrote, it is not diffcult to write a script to read a textfile.

You also could read and write cells of an excelfile.


But what is your reason to use MIDI files?

The win api gives you some functions to do that, but I think it is not

possible directly from TS. You would need an activeX Dll.

If you need more information, I can search for you.


If you want to control your animation by synthesizer-keyboard...

I tried that already, but got some problems. It would be better if

the developers could implement MIDI-IN and Out functions.

Post by jamesmc // Dec 20, 2007, 6:43pm

jamesmc
Total Posts: 2566
Now I understand.

As Dele wrote, it is not diffcult to write a script to read a textfile.
You also could read and write cells of an excelfile.

But what is your reason to use MIDI files?
The win api gives you some functions to do that, but I think it is not
possible directly from TS. You would need an activeX Dll.
If you need more information, I can search for you.

If you want to control your animation by synthesizer-keyboard...
I tried that already, but got some problems. It would be better if
the developers could implement MIDI-IN and Out functions.

MIDI files are much much smaller than WAV files. However, my purpose is not to use MIDI files directly in trueSpace.

My goal is to build a translator for MIDI to TEXT to WAV.

For example, I have a midi file, it is translated into text.

I then extrapolate by scripting the text portion of that file that corresponds to a table of wav sequences for a specified instrument.

i.e. b# for a piano in a midi sequencer would be b# for a piano in a wav matrix.

That would be one single tasking of the project. The others would prolongation of the notes, frequency and of course beat.

This leads me back to the wav matrix. Once the wav matrix is printed out for a song, I can then take that matrix and apply it to a timeline or perhaps a controller that handles WAV Files.

Initially, I would make a generic controller that performed such things as frequency, volume, notes placement and etc.

One could use some of the script controllers already written to farm out that data to object matrix which could be, for instance, a hammer for a piano string. One then would have to correlate the hammer for the piano string with the keyboard and provide movement.

I would like to create a data matrix for wav files as it would be easier to have wav files to be 'events' rather than being treated as external files only. The events could be then be tied to the external files when compiling the animation.

Hope that was somewhat clear, still working this out in my mind.

The excel idea is a good one, I may use it as a template while experimenting.

Essentially, what I'm trying to get to is to make wav files act as a physics event. I don't see it as unreasonable as music or sound for that matter is just an interpretation of properties on an object through the use of frequency changes and the objects composition.

One can simulate properties in physics such as wood, rubber, steel and etc. Instead of movement I would want the object properties to act as events or event sounds.

Getting a bit tired, off to the showers.
Awportals.com is a privately held community resource website dedicated to Active Worlds.
Copyright (c) Mark Randall 2006 - 2024. All Rights Reserved.
Awportals.com   ·   ProLibraries Live   ·   Twitter   ·   LinkedIn