SwiftOSC

repository·main·Indexed 41 days ago

https://github.com/orchetect/swift-osc

An Open Sound Control (OSC) toolkit written in Swift for Apple platforms, Linux, and Android. It features address pattern matching via OSCAddressSpace, strongly-typed OSC message handling using the masked() method, and modular network I/O supporting UDP and TCP.

Tokens
1.4K
Snippets
1
Records
15
Agent score
99%

What's inside SwiftOSC

  1. Communicate bidirectionally with OSCUDPSocket

    main

    The OSCUDPSocket class provides a unified interface for both sending and receiving OSC messages over UDP. Unlike using OSCUDPServer and OSCUDPClient separately, OSCUDPSocket combines them into a single entity sharing the same local UDP port without requiring port reuse.

    This is particularly useful for communicating with hardware devices (like the Behringer X32 & M32) that respond to incoming messages using the same source port from which the message was sent.

  2. Use Method Blocks for OSC pattern matching and validation

    main

    Method Blocks allow you to handle incoming OSC messages using OSCAddressSpace pattern matching via closure blocks. This pattern enables strong-typing and value validation for OSC message contents using the masked() method.

    Key capabilities demonstrated in this pattern:

    • Pattern Matching: Using OSCAddressSpace to match specific address patterns.
    • Strong Typing: Using masked() to validate and cast OSC message values to specific types.

    Note: While this example uses the UDP network protocol, the same Method Block logic applies to TCP connections.

  3. Conform a custom type to OSC codable

    main

    You can conform a custom Swift type to become an OSC message value by implementing the same protocolized interface used by SwiftOSCCore for its standard types.

    Warning: This is an advanced feature. The OSC specification defines standard data types that are universally recognized by 3rd-party hardware and software. Custom types are only understood by software that contains your specific encoding and decoding implementation logic. Additionally, ensure custom types remain compact to avoid overflowing UDP packet size limits.

    For advanced implementation details, refer to the SwiftOSCCore/OSCValue source code in the swift-osc-core repository.

  4. Configure macOS Sandbox entitlements for SwiftOSC TCP

    main

    When using SwiftOSC with the TCP protocol in a macOS project that has the Sandbox entitlement enabled, you must explicitly enable network connection options in your project's entitlements file. Without these, the app will be unable to establish TCP connections. Ensure that the following network options are enabled:

    • Incoming Connections (Server): Required if your app acts as a TCP server.
    • Outgoing Connections (Client): Required if your app acts as a TCP client.
  5. Run SwiftOSC I/O Examples

    main
    The example projects in this repository demonstrate how to perform I/O operations using SwiftOSC on Apple platforms. These examples are compatible with macOS, iOS, tvOS, and visionOS, and can be executed on physical devices or device simulators.
  6. Configure macOS Sandbox entitlements for UDP networking

    main

    If you are using SwiftOSC in a macOS project with the App Sandbox entitlement enabled, you must explicitly enable network connections in your project's entitlements. To allow the UDP client and server to communicate over the network, ensure the following network options are enabled in your .entitlements file:

    • Incoming Connections (Server): Required if your app acts as an OSC server receiving messages.
    • Outgoing Connections (Client): Required if your app acts as an OSC client sending messages.

    These settings are pre-configured in the SwiftOSC UDP example project.