Old 07-02-2022, 07:53 PM   #1
procedure
Human being with feelings
 
Join Date: Jun 2022
Posts: 11
Default String to Hex

Hi.
I'm testing OSC output from TouchOSC and hoping to convert OSC to Midi to send to my DAW. TouchOSC can send Midi directly but I'm just testing using it for now.
I'm able to capture an OSC message and multiple parameters. In all my tests, OSCii-bot will only receive OSC parameters that are strings. I can send out an Integer or Float of course but OSCii-bot will not read it.

I created a Fader for testing in TouchOSC called "/Note" with 2 string parameters. The 1st parameter outputs "60" to represent the Middle C note. The 2nd parameter outputs the fader level to represent Velocity from 0-127.

I'm able to log the OSC message using the following code:
Code:
oscmatch("/Note",$'s') ? (
	  oscparm(idx,type);
	  type=='s' ? (
		    #note = null; #volume = null; //must use #variables
		
		    oscparm(0,'s',#note);
		    oscparm(1,'s',#volume); 

		    printf(oscstr);
		    printf (" Note: ");
		    printf (#note);
		    printf (" Volume: ");
		    printf(#volume);
	  );
);
How can I convert the string output to Hex in order to create my msg1, msg2 and msg3 needed to output Midi? I have researched this forum and don't see the solution to this seemingly obvious question. FYI, you can probably tell I'm not a programmer. Just trying to get by using the examples I see. This took me a very long time to cobble together the code above ha ha.
Thank you.

Last edited by procedure; 07-07-2022 at 09:45 AM.
procedure is offline   Reply With Quote
Old 07-05-2022, 06:12 PM   #2
procedure
Human being with feelings
 
Join Date: Jun 2022
Posts: 11
Default

Is my question off-the-wall somehow or the answer so obvious...

In the C language, I do now see people creating their own functions to convert an integer into a Hex value. So I think that can be handled similarly.

I see the C language has functions that could help convert a string to a integer but I don't see that in EEL2. Can that be done? Doesn't everyone need to convert a string to Hex in order to send out Midi?
procedure is offline   Reply With Quote
Old 07-06-2022, 04:30 AM   #3
Fleeesch
Human being with feelings
 
Fleeesch's Avatar
 
Join Date: Apr 2011
Location: Germany
Posts: 177
Default

If I understand correctly, you're getting 0-127 in string format but the output format needs to be a number.

In this case try this:

Code:
match("%i",#input_value,output_value);
"%i" : output format, integer
#input : input string containing your OSC value
output_value : variable that gets to store the value for MIDI output
Fleeesch is offline   Reply With Quote
Old 07-06-2022, 12:04 PM   #4
procedure
Human being with feelings
 
Join Date: Jun 2022
Posts: 11
Default

Thanks very much for responding.
Here is the current version of my code with your code added at the end:
Code:
oscmatch("/NoteOn/",$'s') ? (
	oscparm(idx,type);
	type=='s' ? (
		#note = null; #velocity = null;
		
		oscparm(0,'s',#note);
		oscparm(1,'s',#velocity); 

		printf(oscstr);
		printf (" Note: ");
		printf (#note);
		printf (" Velocity: ");
		printf(#velocity);
		printf("\n");
		
		match("%i",#note,intNote);
		printf("this is intNote: ");
		printf(intNote);
		printf("\n");

		match("%i",#velocity,intVelocity);
		printf("this is intVelocity: ");
		printf(intVelocity);
		printf("\n");
	);
);
The OSCII-bot LOG window gets this when I move a fader two times in TouchOSC:

/NoteOn/ Note: 60 Velocity: 95.19064
this is intNote:
this is intVelocity:
/NoteOn/ Note: 60 Velocity: 35.404
this is intNote:
this is intVelocity:

So I'm not able to get a conversion with either OSC argument. I tried it with #intNote and #intVelocity as well. I don't really understand the use of the types of variables from the programming guide. Any ideas?
Thanks.

Last edited by procedure; 07-07-2022 at 09:45 AM.
procedure is offline   Reply With Quote
Old 07-06-2022, 01:27 PM   #5
Fleeesch
Human being with feelings
 
Fleeesch's Avatar
 
Join Date: Apr 2011
Location: Germany
Posts: 177
Default

Following the programming manual printf() does what sprintf() does, it's a function used to convert numbers to strings. If you just put a number into it there should be some kind of an error message.

sprintf goes like
Code:
sprintf(#string_out,"%i",input_value)
From right to left: input_value goes in, gets formatted as an integer, string goes out.

For sending MIDI you use this one:

midisend(device_index,m1,m2,m3)

Sends a simple 3 byte message. device_index is the index number of the midi device, m1 the status byte (type of message + channel), m2 the controller number, m3 the value.

So you wanna do something like sending modwheel on channel 1, max position to the first midi device in the list you can do this:

Code:
midisend(0,176,1,127);

or in hex

midisend(0,0xB0,0x01,0x7F);

same thing, just different appearance.
This table here is a good overview of MIDI messages.
Comes with all the data formats and whatnot.

Maybe I'll come up with a quick solution and leave some code that should allow you to write a simple OSC-MIDI converter. Would be in my interest, too, could use some scripts here and there. OscII Bot just won't output any gfx on my machines, sample scripts ain't working right, don't know why, have to go figure.
Fleeesch is offline   Reply With Quote
Old 07-06-2022, 04:26 PM   #6
procedure
Human being with feelings
 
Join Date: Jun 2022
Posts: 11
Default

Thank you very much.
So this is great I now get 2 arguments that are integers from the following code. (This code is completely ridiculous since I'm converting back and forth from floats and strings and integers over and over but I don't know another way...)

Now I just need to convert integers to HEX. You are a hero if you can code that. That would be amazing Thank you.

---------------------
Code:
oscmatch("/NoteOn/",$'s') ? (
  oscparm(idx,type);
  type=='s' ? (
    #note = null; #velocity = null;//must use #variables

    oscparm(0,'s',#note); //string
    oscparm(1,'s',#velocity); //string

    printf(oscstr);
    printf (" Note: ");
    printf (#note);
    printf (" Velocity: ");
    printf(#velocity);
    printf("\n");

    //OSC Arg 1 - note
    match("%i",#note,intNote); //convert string to integer
    sprintf(#string_out1,"%i",intNote); //convert int to string again! printf only prints strings
    printf("this is intNote: ");
    printf(#string_out1);
    printf("\n");

    //OSC Arg 2 - velocity
    match("%f",#velocity,floatVelocity);//convert string to float
    floatVelocity = floor(floatVelocity);//round float to nearest decimal (will be #.00000)
    sprintf(#strVelocity,"%i",floatVelocity);//converts float to string (but will look like integer without #.00000)
    match("%i",#strVelocity,intVelocity);//convert string to integer
    sprintf(#string_out2,"%i",intVelocity); //convert int to string again! printf only prints strings
    printf("this is intVelocity: ");
    printf(#string_out2);
    printf("\n");
  );
);
--------------------------
And the LOG reads:
Code:
/NoteOn/ Note: 60 Velocity: 48.527893
this is intNote: 60
this is intVelocity: 48

Last edited by procedure; 07-07-2022 at 09:48 AM.
procedure is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.