sending OSC events via Player

parent 985ad69b
......@@ -27,4 +27,5 @@ use Mix.Config
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
import_config "#{Mix.env}.exs"
use Mix.Config
config :grains_engine,
osc_client_ip: {127,0,0,1},
osc_client_port: 8001
use Mix.Config
config :grains_engine,
osc_client_ip: {127,0,0,1},
osc_client_port: 8001
......@@ -35,7 +35,7 @@ defmodule GrainsEngine.Grain do
def to_string(grain) do
grain_string =
Agent.get(grain, fn state -> state end)
|> Enum.reduce("", fn {k,v}, acc -> "#{acc}, #{k}" end )
|> Enum.reduce("", fn {k, _v}, acc -> "#{acc}, #{k}" end )
"{" <> String.replace_leading(grain_string, ", ", "") <> "}"
end
......
defmodule GrainsEngine do
use Application
@moduledoc """
Documentation for GrainsEngine.
"""
@doc """
Hello world.
## Examples
iex> GrainsEngine.hello
:world
"""
def hello do
:world
end
end
......@@ -5,13 +5,18 @@ defmodule GrainsEngine.Application do
use Application
def start(_type, _args) do
def start(_type, args) do
import Supervisor.Spec, warn: false
ip = Application.fetch_env!(:grains_engine, :osc_client_ip)
port = Application.fetch_env!(:grains_engine, :osc_client_port)
IO.puts "Supervising #{port}"
# Define workers and child supervisors to be supervised
children = [
# Starts a worker by calling: GrainsEngine.Worker.start_link(arg1, arg2, arg3)
# worker(GrainsEngine.Worker, [arg1, arg2, arg3]),
worker(GrainsEngine.OscSender, [:foo, ip, port])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
......
defmodule GrainsEngine.OscSender do
alias GrainsEngine.{OscSender, Player}
@doc """
starts a new sending process for OSC on `ip` (tuple) and `port`
"""
def start_link(name, ip, port) do
Agent.start_link(fn -> {ip, port} end, name: name)
end
@doc """
Sends an OSC `message` with `value` on a given `channel`
"""
def send(pid, channel, message) do
Agent.get(pid, fn {ip, port} ->
IO.puts "sending to #{channel}"
ExOsc.Sender.send_message(ip, port, {channel, message})
{ip, port}
end)
end
def send_player(pid, player) do
name = Player.name(player)
Player.get_values(player)
|> Enum.each(fn {k,v} ->
channel = "/#{name}/#{k}"
OscSender.send(pid, channel, {:osc_integer, v})
end)
end
end
......@@ -40,6 +40,11 @@ defmodule GrainsEngine.Player do
Agent.update(player, fn state -> Map.put(state, :name, name) end)
end
def name(player) do
Agent.get(player, fn state -> state.name end)
end
def set_grain(player, grain) do
Agent.update(player, fn state -> Map.put(state, :grain, grain) end)
end
......@@ -49,17 +54,6 @@ defmodule GrainsEngine.Player do
end
def set_params(player, names) when is_list(names) do
params = Enum.map(names, fn name ->
{:ok, param} = Parameter.start_link(name)
param
end)
Agent.update(player, fn state ->
Grain.replace_parameters(state.grain, params)
state
end)
end
def to_string(player) do
......
......@@ -15,7 +15,7 @@ defmodule GrainsEngine.Mixfile do
# Type "mix help compile.app" for more information
def application do
# Specify extra applications you'll use from Erlang/Elixir
[extra_applications: [:logger],
[extra_applications: [:logger, :ex_osc],
mod: {GrainsEngine.Application, []}]
end
......@@ -29,6 +29,7 @@ defmodule GrainsEngine.Mixfile do
#
# Type "mix help deps" for more examples and options
defp deps do
[{:mix_test_watch, "~> 0.3", only: :dev, runtime: false}]
[ {:ex_osc, git: "git://github.com/jwarwick/ex_osc.git"},
{:mix_test_watch, "~> 0.3", only: :dev, runtime: false}]
end
end
%{"fs": {:hex, :fs, "2.12.0", "ad631efacc9a5683c8eaa1b274e24fa64a1b8eb30747e9595b93bec7e492e25e", [:rebar3], []},
%{"ex_osc": {:git, "git://github.com/jwarwick/ex_osc.git", "2f695afc837670d022a90a7954a2cf53d023ad80", []},
"fs": {:hex, :fs, "2.12.0", "ad631efacc9a5683c8eaa1b274e24fa64a1b8eb30747e9595b93bec7e492e25e", [:rebar3], []},
"mix_test_watch": {:hex, :mix_test_watch, "0.3.3", "70859889a8d1d43d1b75d69d87258a301f43209a17787cdb2bd9cab42adf271d", [:mix], [{:fs, "~> 2.12", [hex: :fs, optional: false]}]}}
defmodule OscSenderTest do
use ExUnit.Case, async: true
doctest GrainsEngine.OscSender
alias GrainsEngine.{OscSender, Player}
describe "populated Player" do
setup do
{:ok, player} = Player.start_link("frank", ["foo", "bar"])
values = %{foo: 1, bar: 3}
Player.update_parameters(player, values)
end
test "sends the OSC values to channel", %{player: player} do
OscSender.send_player(:foo, player)
assert true == true
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment