Forums

Using C# classes in Cobra

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Using C# classes in Cobra

Postby DelphiGuy » Wed May 16, 2012 6:51 am

I'm wanting to get Cobra to be able to send out MIDI msgs to a music synth. In short, I'd like to get Cobra to play an audible musical note on my Win 7 notebook. As a starting point, I've arbitrarily chosen to take a look at Leslie Sanford's MIDI-related C# project, and to borrow as much as possible from Leslie. (There are other MIDI .net choices on the web, as well, but Leslie's seems well-known so I'll start there.)

http://www.codeproject.com/Articles/622 ... OutputBase

Keeping in mind that I'm a very consistent programmer: uniformly terrible in C#, .net and Cobra, would you guys take a moment and point me in the right direction on how to think this all out? Obviously, I'll need to port his sample MIDI output program-ette and get it working on Cobra.

Code: Select all
using(OutputDevice outDevice = new OutputDevice(0))
{
    ChannelMessageBuilder builder = new ChannelMessageBuilder();

    builder.Command = ChannelCommand.NoteOn;
    builder.MidiChannel = 0;
    builder.Data1 = 60;
    builder.Data2 = 127;
    builder.Build();

    outDevice.Send(builder.Result);

    Thread.Sleep(1000);

    builder.Command = ChannelCommand.NoteOff;
    builder.Data2 = 0;
    builder.Build();

    outDevice.Send(builder.Result);
}


(This looks fairly trivial to port, though I didn't quite get the assignment in the "using" statement. May I assume this is nothing more than just another C-ish "do two or three dimly related things in only one line of code, so that the code becomes unreadable by amateurish programmers"?)

In any case, what I'm hoping for from you guys is a brief general discussion, or links pointing to such a discussion, of how best to use Leslie's C# code in a Cobra environment? Do I port his classes (Horrors!), somehow instantiate his C# classes directly from Cobra, or somehow compile his C# code and then somehow call to the compiled code from Cobra. In short, I guess I'm ready to do my first investigation of how Cobra "plays nice" with C#, and I guess more generally how .net langs in gen'l "play nice" with other .net langs. Where do I begin, please?

Thanks.

-Paul
DelphiGuy
 
Posts: 116

Re: Using C# classes in Cobra

Postby Charles » Wed May 16, 2012 12:54 pm

Cobra plays very nice with C# for numerous reasons:

-- Cobra uses C#'s same "model" or "semantics" for things like classes, properties, methods, method invocation, etc.

-- Cobra is translated to C# at a point in the compilation process.

-- Both languages are ultimately translated to .NET bytecode which is language neutral.

-- Cobra reads .NET libraries regardless of whether they were built in C#, VB, Cobra or any other CLR-oriented language.

So no, you don't port his classes!

Generally the place to start is to make sure you can reference the library. In the simple case, you have a "Foo.Bar.dll" file with the same namespace inside. Put it in the same directory as your program and write:
# MyProgram.cobra

use Foo.Bar

class Program

def main
b = Buzz() # where Buzz is a class in the Foo.Bar namespace
trace b

# run with:
# cobra -d MyProgram.cobra
# -d is for "debug"


But in some cases the namespace might differ from the library name. Let's say the name space is "A.B":
"""
My Program.cobra

run with:
cobra -d -ref:Foo.Bar MyProgram.cobra
"""

use A.B

class Program

def main
b = Buzz() # where Buzz is a class in the A.B namespace in the Foo.Bar DLL
trace b


I have not run or compiled the following, but here is the C# code converted to Cobra:
use Foo.Bar

class Program

def main
using outDevice = OutputDevice(0)
builder = ChannelMessageBuilder()

builder.command = ChannelCommand.NoteOn
builder.midiChannel = 0
builder.data1 = 60
builder.data2 = 127
builder.build

outDevice.send(builder.result)

Thread.sleep(1000)

builder.command = ChannelCommand.NoteOff
builder.data2 = 0
builder.build

outDevice.send(builder.result)


I changed it by:
-- Removing ; () { } new
-- Removing type decls -- they can be inferred easily
-- Changing the case (Cobra uses the style seen in Java, Smalltalk, Obj-C and other languages)

You can also get into things like:
# this:
builder = ChannelMessageBuilder()
builder.command = ChannelCommand.NoteOn
builder.midiChannel = 0
builder.data1 = 60
builder.data2 = 127
builder.build
# could be:
builder = ChannelMessageBuilder(
command = ChannelCommand.NoteOn,
midiChannel = 0,
data1 = 60,
data2 = 127
)
builder.build

That should get you started.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Using C# classes in Cobra

Postby hopscc » Thu May 17, 2012 1:16 am

To emphasise;
Use the existing classes if you can get the dll
Reference the classes/dll and from Cobra just instantiate and call in to the (C# created ) .Net objects

If you ( feel you must) port C# code to Cobra Its mostly a punctuation and cruft removal exercise:

Theres a list of steps on the Porting C# to Cobra Wiki Page

I keep feeling I should make a app to automate this ( its a pretty mechanical process) but some editor macros suffices so far.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Using C# classes in Cobra

Postby DelphiGuy » Thu May 17, 2012 7:02 pm

(hopscc said:) Use the existing classes if you can get the dll


the original download packet contains the C# open source code, as well as a bunch of dll's. i assume i can figure out how to recompile the code with sharpdevelop or MSVS Express, if need be. for the moment, though, i'll see if i can get the existing dll's to do anything tomorrow. the only obvious fly in the ointment is that they look to have been compiled in 2007. whether older dll's will run just fine from today's .net and development tools, i'll find out.
DelphiGuy
 
Posts: 116

Re: Using C# classes in Cobra

Postby DelphiGuy » Mon May 28, 2012 7:27 am

Well, I didn't have to port any of Leslie Sanford's C#/Windows code to Cobra. Under Win 7, I was able to finally get a midi note to play by calling out from Cobra to his C#-based .dll's. Took a good number of hours to figure out how to do it, and had to compile his C# code in MSVC 2010 Express, and step through some of it a line at a time, before I could figure out what was wrong with my Cobra command line and my Cobra "use" statements. But I finally prevailed.

The hints you guys provided were quite helpful, and just the right amount to get me to face the pain and do it myself. So thanks.

I note that most of my current problems with Cobra aren't with Cobra at all, but with .net in general. That's a good thing, because there's of course zillions of resources for any confusion about .net .

On to the next problem: figuring out how to time a midi note using multimedia timing. Leslie's got a Win/.net library for that as well. Eventually, since my long-term target is iOS/Monotouch, not Win, I'll have to re-write all the MIDI and timing stuff using the API available to me for the former. But I'm hoping that along the way I'll get a Win prototype running smoothly, and that I'll learn some better MIDI sequencing ideas from Leslie's code which I'll be able to apply to whatever platform, since he worked on it for years and went through 4 or 5 major revisions.

-Paul
DelphiGuy
 
Posts: 116

Re: Using C# classes in Cobra

Postby Charles » Mon May 28, 2012 9:45 am

Thanks for the update. I've internalized much of .NET over the years so it's hard for me to recall what needs to be spelled out, but perhaps as you are learning these things, you could record them? I have stubbed out a wiki page at:
http://cobra-language.com/trac/cobra/wiki/DotNetTips

Your login credentials are the same as the forum.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Using C# classes in Cobra

Postby DelphiGuy » Mon May 28, 2012 12:33 pm

Charles wrote: I've internalized much of .NET over the years so it's hard for me to recall what needs to be spelled out, but perhaps as you are learning these things, you could record them?


Well, that wouldn't work very well because I'm essentially a .net beginner, so if I were to memorialize what I'm learning I'd have to write two huge Cobra books: a beginning Cobra/.net book and a general reference Cobra/.net book. I already have decent C# versions of those two books, authored by others, and constantly have my nose buried in them, in addition to learning from the web.

Instead, what I really should do to add value to the Cobra project is to, a little at a time, change your list of keywords into links. So far, only a few have been done. Question: is there a way to link to some other page in the Wiki such that the link directs the user to a specific paragraph, not just a page? I haven't looked at HTML since the year 1847, but I seem to recall that there's some type of invisible label or flag that can be inserted into a web page, such that a link can direct a user directly to that label, rather than solely to the top of the particular page.

Is there some way for a wiki contributor to insert such flags/labels on other wiki pages?

It certainly isn't essential, but if there is such capability I'd like to know about it.

Sitting here listening to a random MIDI single-note symphony. Now to add rhythm.

-Paul
DelphiGuy
 
Posts: 116

Re: Using C# classes in Cobra

Postby Charles » Mon May 28, 2012 1:55 pm

I'm skeptical that there aren't useful tips that could be added to that page. For example, the books that you find useful would constitute a tip.

Re: links within a page, you can do that in HTML with anchors. See http://www.echoecho.com/htmllinks08.htm and "The name Attribute" at http://www.w3schools.com/html/html_links.asp

But we're using a wiki, so you'll need to check:
http://cobra-language.com/trac/cobra/wi ... Formatting
http://cobra-language.com/trac/cobra/wiki/TracLinks
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 11 guests