A Simple Sample Player Using OSC

This simple application listens for OSC messages that contain the following commands:

Audio and OSC Setup

We create a clock object that will allow us to control playing the sample.

We also create an audio output and connect it to the first system output channel:



local clock = Transport.Clock(100)

local audioOutput = Audio.Output("out", "system:playback_1")


We create an OSC input and bind it to a local port:



local port = 3033
local oscInput = Osc.Input(port)
print("listening on port " + port + "..\n")


Handle OSC Messages

We register an event handler on the OSC input to react to messages:


oscInput.onReceive(function(message) {

Start a switch statement based on the path of the incoming message:


	switch(message.path()) {

If the message is /load the filename of the sample to load will be the first argument in the OSC message.

We load this clip and then schedule it at the very beginning position (1.0) on the audio output, to be driven by the clock defined above.

Note the BPM of the clock does not matter in this case as we are only scheduling one event to occur at the very beginning of the timeline.


	case "/load":
		local file = message.arg(0)
		local clip = Audio.Clip(file)
		audioOutput.schedule(clip, 1.0, clock)
		break;

OSC /play message: calls the clock restart() method which returns the transport to the start position and then starts the clock:


	case "/play":
		clock.restart()
		break;

OSC /stop message: stops the clock


	case "/stop":
		clock.stop()
		break;

OSC /exit message: immediately shuts down the application


	case "/exit":
		System.exit(0)

A default case handles any other unrecognized message and prints a warning:


	default:
		print("warning: unknown message path " + message.path() + "\n")
	}
})


No scheduled events were added in the main part of the script so add the System.stayAlive() method to keep the script from immediately completing:


System.stayAlive()


Testing

To test this application we can send OSC messages from a terminal using the oscsend command:

oscsend localhost 3033 /load s /path/to/file.wav
oscsend localhost 3033 /play
oscsend localhost 3033 /stop
oscsend localhost 3033 /exit



Complete Script

local clock = Transport.Clock(100)

local audioOutput = Audio.Output("out", "system:playback_1")

local port = 3033
local oscInput = Osc.Input(port)
print("listening on port " + port + "..\n")

oscInput.onReceive(function(message) {

	switch(message.path()) {

	case "/load":
		local file = message.arg(0)
		local clip = Audio.Clip(file)
		audioOutput.schedule(clip, 1.0, clock)
		break;

	case "/play":
		clock.restart()
		break;

	case "/stop":
		clock.stop()
		break;

	case "/exit":
		System.exit(0)

	default:
		print("warning: unknown message path " + message.path() + "\n")
	}
})

System.stayAlive()


Tutorial IndexList of All Examples


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.