Skip to main content

Open Sound Control server and client implementations in pure Python

Project description

Open Sound Control server and client implementations in pure python.

https://github.com/attwad/python-osc/actions/workflows/python-test.yml/badge.svg

Current status

This library was developed following the OpenSoundControl Specification 1.0 and is currently in a stable state.

Features

  • UDP and TCP blocking/threading/forking/asyncio server implementations

  • UDP and TCP clients, including asyncio support

  • TCP support for 1.0 and 1.1 protocol formats

  • int, int64, float, string, double, MIDI, timestamps, blob, nil OSC arguments

  • simple OSC address<->callback matching system

  • support for sending responses from callback handlers in client and server

  • extensive unit test coverage

  • basic client and server examples

Documentation

Available at https://python-osc.readthedocs.io/.

Installation

python-osc is a pure python library that has no external dependencies, to install it just use pip (prefered):

https://img.shields.io/pypi/v/python-osc.svg
$ pip install python-osc

Examples

Simple client

"""Small example OSC client

This program sends 10 random values between 0.0 and 1.0 to the /filter address,
waiting for 1 seconds between each value.
"""
import argparse
import random
import time

from pythonosc import udp_client


if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip", default="127.0.0.1",
      help="The ip of the OSC server")
  parser.add_argument("--port", type=int, default=5005,
      help="The port the OSC server is listening on")
  args = parser.parse_args()

  client = udp_client.SimpleUDPClient(args.ip, args.port)

  for x in range(10):
    client.send_message("/filter", random.random())
    time.sleep(1)

Simple server

"""Small example OSC server

This program listens to several addresses, and prints some information about
received packets.
"""
import argparse
import math

from pythonosc.dispatcher import Dispatcher
from pythonosc import osc_server

def print_volume_handler(unused_addr, args, volume):
  print("[{0}] ~ {1}".format(args[0], volume))

def print_compute_handler(unused_addr, args, volume):
  try:
    print("[{0}] ~ {1}".format(args[0], args[1](volume)))
  except ValueError: pass

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip",
      default="127.0.0.1", help="The ip to listen on")
  parser.add_argument("--port",
      type=int, default=5005, help="The port to listen on")
  args = parser.parse_args()

  dispatcher = Dispatcher()
  dispatcher.map("/filter", print)
  dispatcher.map("/volume", print_volume_handler, "Volume")
  dispatcher.map("/logvolume", print_compute_handler, "Log volume", math.log)

  server = osc_server.ThreadingOSCUDPServer(
      (args.ip, args.port), dispatcher)
  print("Serving on {}".format(server.server_address))
  server.serve_forever()

Building bundles

from pythonosc import osc_bundle_builder
from pythonosc import osc_message_builder

bundle = osc_bundle_builder.OscBundleBuilder(
    osc_bundle_builder.IMMEDIATELY)
msg = osc_message_builder.OscMessageBuilder(address="/SYNC")
msg.add_arg(4.0)
# Add 4 messages in the bundle, each with more arguments.
bundle.add_content(msg.build())
msg.add_arg(2)
bundle.add_content(msg.build())
msg.add_arg("value")
bundle.add_content(msg.build())
msg.add_arg(b"\x01\x02\x03")
bundle.add_content(msg.build())

sub_bundle = bundle.build()
# Now add the same bundle inside itself.
bundle.add_content(sub_bundle)
# The bundle has 5 elements in total now.

bundle = bundle.build()
# You can now send it via a client with the `.send()` method:
client.send(bundle)

License?

Unlicensed, do what you want with it. (http://unlicense.org)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_osc-1.9.3.tar.gz (33.3 kB view details)

Uploaded Source

Built Distribution

python_osc-1.9.3-py3-none-any.whl (43.8 kB view details)

Uploaded Python 3

File details

Details for the file python_osc-1.9.3.tar.gz.

File metadata

  • Download URL: python_osc-1.9.3.tar.gz
  • Upload date:
  • Size: 33.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for python_osc-1.9.3.tar.gz
Algorithm Hash digest
SHA256 bd0fa40def43ce509894709feb0e18f02192aca192c5e6c8fe2ba69e58f21794
MD5 d0fccb83da362954d08b8364b10285e4
BLAKE2b-256 1e6e770a91c92e4e8db6213764fb78531dd26a4982000d9b325881f3599835e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_osc-1.9.3.tar.gz:

Publisher: publish-pypi.yml on attwad/python-osc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_osc-1.9.3-py3-none-any.whl.

File metadata

  • Download URL: python_osc-1.9.3-py3-none-any.whl
  • Upload date:
  • Size: 43.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for python_osc-1.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7def2075be72f07bae5a4c1a55cc7d907b247f4a5d910f3159ed30ac2b1f17cc
MD5 ed7e80d848a8d7deac87b38b4f1dcd26
BLAKE2b-256 570401b3de6a7565cfc20f3bf4fce959b099ad30a08157865436d48cc02f6c08

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_osc-1.9.3-py3-none-any.whl:

Publisher: publish-pypi.yml on attwad/python-osc

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page