OSC (OpenSoundControl) and PB

Just starting out? Need help? Post your questions and find answers here.
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

OSC (OpenSoundControl) and PB

Post by HotteHC »

Hi there,
I have a little with sending the correct OSC data to the Sequencer-Prog "Reaper" via UDP.
Here's the Code :

Code: Select all

EnableExplicit

Macro CALC_ALIGNMENT(x) 
  (x+4) & $fffc 
EndMacro  

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf


Declare.i BuildMessage(adr.s,typeTag.s,val.f) 

Enumeration
  #MAINWIN
  
  #BUTTON_SEND
EndEnumeration

#REAPER_IP = "192.168.178.21"; ->Change to your'e IP
#REAPER_PORT = 8000          ; ->Change to your'e PORT 


Global winEvent.i = 0
Global ReaperID   = 0

Global *message   = 0

ReaperID = OpenNetworkConnection(#REAPER_IP, #REAPER_PORT, #PB_Network_UDP)
If ReaperID = 0
  Debug "Unable to connect to Reaper"
  End
EndIf


If OpenWindow(#MAINWIN,0,0,100,25,"OSC Test") = #False
  Debug "Couldn't create Main Window!"
  End
EndIf  

ButtonGadget(#BUTTON_SEND,0,0,100,25,"Send to Reaper")

Repeat
  winEvent = WindowEvent()
  Select winEvent
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #BUTTON_SEND
          If *message = 0
            *message = BuildMessage("/track/1/fx/1/fxparam/1/value",",f",8.01)
          EndIf
          If *message <> 0  
            SendNetworkData(ReaperID, *message,1024)
            FreeMemory(*message)
            *message = 0
          EndIf  
      EndSelect
  EndSelect    
      
  
  
  Delay(1)
Until winEvent = #PB_Event_CloseWindow

CloseNetworkConnection(ReaperID)



End

  
Procedure.i BuildMessage(adr.s,typeTag.s,val.f)
 Protected *message = AllocateMemory(256)
 
 Protected adrAlignment.l = CALC_ALIGNMENT(Len(adr))
 
 CopyMemory(@adr,*message,StringByteLength(adr))
 
 CopyMemory(@typeTag,*message+adrAlignment,2)
  
 ;Make BigEndian Float
 Protected tmpFloat.f = 0
 PokeB(@tmpFloat+0,PeekB(@val+3))
 PokeB(@tmpFloat+1,PeekB(@val+2))
 PokeB(@tmpFloat+2,PeekB(@val+1))
 PokeB(@tmpFloat+3,PeekB(@val+0))
 
 CopyMemory(@tmpFloat,*message+adrAlignment+SizeOf(long),4)
 
 ProcedureReturn *message
EndProcedure
Open Sound Control (OSC) is a content format for messaging among computers, sound synthesizers, and other multimedia devices that are optimized for modern networking technology.
Here is a better Description about what OSC is http://en.wikipedia.org/wiki/Open_Sound_Control

My Problem is, that the Message that I'm sending to Reaper should look like
"/track/1/fx/1/fxparam/1/value ,f 8.01"
but Reaper recieves
"/track/1/fx/1/fxparam/1/value [f] 8.01"
.
I know it's a very specific question but hopefully anyone has an anwser for my Question.

mfg Christian
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: OSC (OpenSoundControl) and PB

Post by jesperbrannmark »

Hi Christian.
You beat me to it. I was asking few things with OSC and PB about a week ago. ( i am using GlovePie to send kinect/wii balanceboard/xbox controller stuff to PB). I have a basic library that i need to work on a bit and will put my contribution here in a few days.
Great you are working on it as well. I cant answer your question on top of my head, but I will put some sources in the forum and hopefully that can make things clear. I think OSC can be great to combine with PB.
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Re: OSC (OpenSoundControl) and PB

Post by HotteHC »

Hi Jesper,

thanks for replying. I'm looking forward to see something of your work .
I also think it would be really nice to see OSC working with PB. There are so many more Options where you could use OSC. It's not only helpful when dealing with music/video/dmx but also with a sort of message-system between different apps and so on.
And if you need some help with your'e project(OSC sided) then let me know :wink: .

mfg Christian
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OSC (OpenSoundControl) and PB

Post by infratec »

Hi HotteHC,

in my opinion your packet is ok.
It fullfills the OSC rules.

One thing: you send 1024 bytes, but your buffer is only 256 bytes.
This can result in a crash.
And your complete message is a lot shorter.
I think you have to send only the bytes of your build packet and not more.

Btw. do you know an 'OSC dummy server' where it is possible to test messages?

Bernd
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OSC (OpenSoundControl) and PB

Post by infratec »

Hi,

please try this:

Code: Select all

EnableExplicit

Macro CALC_ALIGNMENT(x)
  (x+4) & $fffc
EndMacro 

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf


Declare.i BuildMessage(*message, adr.s, typeTag.s, val.f)

Enumeration
  #MAINWIN
 
  #BUTTON_SEND
EndEnumeration

#REAPER_IP = "192.168.0.1"; ->Change to your'e IP
#REAPER_PORT = 8000          ; ->Change to your'e PORT


Global winEvent.i = 0
Global ReaperID   = 0

Define *message
Define Size.i

ReaperID = OpenNetworkConnection(#REAPER_IP, #REAPER_PORT, #PB_Network_UDP)
If ReaperID = 0
  Debug "Unable to connect to Reaper"
  End
EndIf


If OpenWindow(#MAINWIN,0,0,100,25,"OSC Test") = #False
  Debug "Couldn't create Main Window!"
  End
EndIf 

ButtonGadget(#BUTTON_SEND,0,0,100,25,"Send to Reaper")

Repeat
  winEvent = WindowEvent()
  Select winEvent
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #BUTTON_SEND
          *message = AllocateMemory(1024)
          If *message
            Size = BuildMessage(*message, "/track/1/fx/1/fxparam/1/value",",f",8.01)
            If Size
              SendNetworkData(ReaperID, *message, Size)
            EndIf
            FreeMemory(*message)
            *message = 0
          EndIf 
      EndSelect
  EndSelect   
  
  Delay(1)
Until winEvent = #PB_Event_CloseWindow

CloseNetworkConnection(ReaperID)



End

 
Procedure.i BuildMessage(*Buffer, addr.s, typeTag.s, val.f)
  
  Protected Ptr.i = 0
  
  If *Buffer
    
    PokeS(*Buffer + Ptr, addr)
    Ptr + Len(addr) + 1
    While Ptr % 4 : Ptr + 1: Wend
    
    PokeS(*Buffer + Ptr, typeTag)
    Ptr + Len(typeTag) + 1
    While Ptr % 4 : Ptr + 1: Wend
    
    ;Make BigEndian Float    
    PokeB(*Buffer + Ptr, PeekB(@val+3))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+2))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+1))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+0))
    Ptr + 1
    
  EndIf
 
  ProcedureReturn Ptr
  
EndProcedure
Bernd
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Re: OSC (OpenSoundControl) and PB

Post by HotteHC »

Hello Infratec,

I tried youre example, but the error remains the same. As you said, the OSC-Package-Allignment seems to be ok because Reaper reacts for the message I send to. But if you listen to reaper's port, the ',' isn't right interpreted. I tried many things for now but it's always the same :?: . Could it be a Bug in PB's Network lib?
I'm working an a little Dummy-Server for the OSC-Message-System but I'm using Reaper for now to see, if the messages I'm sending are correct. You could also use PD(PureData) as a Dummy-Server.(PD is Freeware).

Ok...
Then let's try more ;)
mfg Christian

PS : @Infratec : Grüße aus dem schönen Bayer :wink:
HotteHC
User
User
Posts: 19
Joined: Fri Feb 29, 2008 7:44 am

Re: OSC (OpenSoundControl) and PB

Post by HotteHC »

Hi there,
it seems to be a bug in Reaper itself.
When using OSC-Monitor (http://www.frieder-weiss.de/OSC/index.html - Freeware) the message I've sended is recieved correctly.

Edit :

For the one's that are interessted in.
Reaper only accepts float values from 0.0 - 1.0 .
The value's shown in the GUI are represented as Strings.

So there is no Bug in Reaper. It's only a different thing, it interprets the Messages I'd sended to it.

mfg Christian
riri
New User
New User
Posts: 1
Joined: Sun Mar 09, 2014 4:36 pm

Re: OSC (OpenSoundControl) and PB

Post by riri »

hi there,
try this code
the probleme is just to send a ascii code

Code: Select all

EnableExplicit

Macro CALC_ALIGNMENT(x)
  (x+4) & $fffc
EndMacro 



If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf


Declare.i BuildMessage(*message, adr.s, typeTag.s, val.f)

Enumeration
  #MAINWIN
 
  #BUTTON_SEND
EndEnumeration

Define *ipbuf
Define Result
Define String$
Define ss
Define resultt.s
Define myip$
Define Connection

#REAPER_PORT = 9000          ; ->Change to your'e PORT

; auto log IP and send global udp adress

CreateNetworkServer(0, #REAPER_PORT, #PB_Network_UDP)
*ipbuf = AllocateMemory(1000)
ExamineIPAddresses() 
Repeat
  Result = NextIPAddress()
  If Not result
    Break
  EndIf
  Debug  IPString(result)
  String$ + IPString(result)+";"
ForEver
PokeS(*ipbuf,string$,Len(string$))

  
    For ss=1 To CountString(string$,";")
      resultt.s=StringField(string$,ss,";")
      myip$ =StringField(resultt.s,1,".")+"."+StringField(resultt.s,2,".")+"."+StringField(resultt.s,3,".")+".255"    
      Connection = OpenNetworkConnection(myip$, #REAPER_PORT, #PB_Network_UDP) ;change to my IP broadcast

    Next



Global winEvent.i = 0
Global ReaperID   = 0

Define *message
Define Size.i

ReaperID = OpenNetworkConnection(myip$, #REAPER_PORT, #PB_Network_UDP)
If ReaperID = 0
  Debug "Unable to connect to Reaper"
  End
EndIf


If OpenWindow(#MAINWIN,0,0,100,25,"OSC Test") = #False
  Debug "Couldn't create Main Window!"
  End
EndIf 

ButtonGadget(#BUTTON_SEND,0,0,100,25,"Send to Reaper")

Repeat
  winEvent = WindowEvent()
  Select winEvent
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #BUTTON_SEND
          *message = AllocateMemory(2048)
          If *message
            Size = BuildMessage(*message, "/7/fader23",",f",1)
            If Size
              SendNetworkData(ReaperID, *message, Size)
                           
              
            EndIf
            FreeMemory(*message)
            *message = 0
          EndIf 
      EndSelect
  EndSelect   
  
  Delay(1)
Until winEvent = #PB_Event_CloseWindow

CloseNetworkConnection(ReaperID)



End

 
Procedure.i BuildMessage(*Buffer, adr.s, typeTag.s, val.f)
  
  Protected Ptr.i = 0
  Shared *message
  
  
  If *Buffer
       
    PokeS(*Buffer + Ptr, adr,-1,#PB_Ascii)
    Ptr + Len(adr) + 1
    While Ptr % 4 : Ptr + 1 : Wend
    
    PokeS(*Buffer + Ptr, typeTag,-1,#PB_Ascii)
    Ptr + Len(typeTag) + 1
    While Ptr % 4 : Ptr + 1 : Wend
    
    ;Make BigEndian Float 
    
        
    PokeB(*Buffer + Ptr, PeekB(@val+3))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+2))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+1))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+0))
    Ptr + 1
    
  EndIf
 
  ProcedureReturn Ptr
  
EndProcedure[/b]
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: OSC (OpenSoundControl) and PB

Post by Simo_na »

Hi, instead of the FLOAT I would like to send a INT (1 to 256) using this same code, I've tried but without success :?
can you help me please?
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: OSC (OpenSoundControl) and PB

Post by Simo_na »

Simo_na wrote:Hi, instead of the FLOAT I would like to send a INT (1 to 256) using this same code, I've tried but without success :?
can you help me please?
!up!
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OSC (OpenSoundControl) and PB

Post by infratec »

What is so complicated to change val.f to val.i :?:

Or use a Structure with Union

Code: Select all

Procedure.i BuildMessage(*Buffer, adr.s, typeTag.s, val.i)
 
  Protected Ptr.i = 0
  Shared *message
 
 
  If *Buffer
       
    PokeS(*Buffer + Ptr, adr,-1,#PB_Ascii)
    Ptr + Len(adr) + 1
    While Ptr % 4 : Ptr + 1 : Wend
   
    PokeS(*Buffer + Ptr, typeTag,-1,#PB_Ascii)
    Ptr + Len(typeTag) + 1
    While Ptr % 4 : Ptr + 1 : Wend
   
    ;Make BigEndian Float
   
       
    PokeB(*Buffer + Ptr, PeekB(@val+3))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+2))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+1))
    Ptr + 1
    PokeB(*Buffer + Ptr, PeekB(@val+0))
    Ptr + 1
   
  EndIf
 
  ProcedureReturn Ptr
 
EndProcedure
You need ",i" as type.

Code: Select all

Size = BuildMessage(*message, "/7/fader23",",i",1234)
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: OSC (OpenSoundControl) and PB

Post by Simo_na »

Thank you, Bernd, now work

i've changed this:

Code: Select all

Declare.i BuildMessage(*message, adr.s, typeTag.s, val.f)
to this

Code: Select all

Declare.i BuildMessage(*message, adr.s, typeTag.s, val.i)
:oops:

if i want to get the reply code on the UPD it's much complicated ?

how i can read answer from the UDP ?

Thank you
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OSC (OpenSoundControl) and PB

Post by infratec »

Use somethig like this after your SendNetwork stuff:

Code: Select all

Timeout = 100
*Buffer = AllocateMemory(2048, #PB_Memory_NoClear)
If *Buffer
  Repeat
    
    NetEv = NetworkClientEvent(Con)
    Select NetEv
      Case #PB_NetworkEvent_None
        Delay(10)
        Timeout - 1
      Case #PB_NetworkEvent_Data
        RcvLen = ReceiveNetworkData(Con, *Buffer, MemorySize(*Buffer))
        If RcvLen
          ShowMemoryViewer(*Buffer, RcvLen)
          Break
        EndIf
    EndSelect
    
  Until Timeout = 0
  FreeMemory(*Buffer)
EndIf
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: OSC (OpenSoundControl) and PB

Post by Simo_na »

infratec wrote:Use somethig like this after your SendNetwork stuff:

[/code]

00000000048D1B00 2F 63 68 2F 30 31 2F 6D 69 78 2F 66 61 64 65 72 /ch/01/mix/fader
00000000048D1B10 00 00 00 00 2C 66 00 00 3F 6E 7B 9F ....,f..?n{Ÿ

8)
Thank you
Simo_na
Enthusiast
Enthusiast
Posts: 177
Joined: Sun Mar 03, 2013 9:01 am

Re: OSC (OpenSoundControl) and PB

Post by Simo_na »

Please Help me :|

Code: Select all

0000000004B41B00  2F 63 68 2F 30 31 2F 6D 69 78 2F 66 61 64 65 72  /ch/01/mix/fader
0000000004B41B10  00 00 00 00 2C 66 00 00 3D B4 2D 0B              ....,f..=´-.
i have this 4 byte big endian in reply and converted to float as
3D B4 2D 0B = 0.08797654
(I converted this online)

how do i convert from big endian to float in Purebasic ?

Thank you
Post Reply