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 (3.5+).

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

Current status

This library was developped following the specifications at http://opensoundcontrol.org/spec-1_0 and is currently in a stable state.

Features

  • UDP blocking/threading/forking/asyncio server implementations

  • UDP client

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

  • simple OSC address<->callback matching system

  • 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

or from the raw sources for the development version:

$ python setup.py test
$ python setup.py install

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 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()
  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 as described in other examples.

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.7.7.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_osc-1.7.7-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file python-osc-1.7.7.tar.gz.

File metadata

  • Download URL: python-osc-1.7.7.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for python-osc-1.7.7.tar.gz
Algorithm Hash digest
SHA256 4c7d68a7719d9425ab2a4ee9a2b9d5a9f5b66593fb46e20e38f91e1452bea2d2
MD5 eb10f4dbc5540852b7d039a031156512
BLAKE2b-256 0765bb834db7153fc43b4ef9206f932a9d6c40a98705cf8460b94bf7244ac3c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_osc-1.7.7-py3-none-any.whl
  • Upload date:
  • Size: 31.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for python_osc-1.7.7-py3-none-any.whl
Algorithm Hash digest
SHA256 1d846e1b873fd46f3b1c3287e1429d82a1f56ab71101f85a744a5f03d60345ed
MD5 7d21a29c38f716de95e87fe3bc5d74d8
BLAKE2b-256 be8d3e70d2374b2e83cd96bb177744eaa5ab5b1264d67d0e1173558c146ba79c

See more details on using hashes here.

Supported by

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