Table of Contents

C# (WampSharp) - WAMP

Initializing the project

Note.gif

Note: This example requires Visual Studio 2017 with C# support and the NuGet package manager extension.

As of version 1.2.5.36-beta of WampSharp, you will need to add ws://127.0.0.1:8080 as an allowed origin if you are running the sample on the same computer as the Wwise Authoring application. To do so, go to Project > User Preferences... and add the address in the Allowed origins text box in the Wwise Authoring API section. Note that values are comma-separated.

To install dependencies, open the sample project <Wwise installation path>/SDK/samples/WwiseAuthoringAPI/cs/WaapiCS/WaapiCS.sln in Visual Studio 2017. Install dependencies by right-clicking on the solution and selecting Restore NuGet Packages.

To select the startup object, right-click on the WaapiCS project in the Solution Explorer and click on Properties. Then select the Application tab and under the section labeled Startup object, select WaapiCS.CallWwise.

Project Code

Locate the sample file <Wwise installation path>/SDK/samples/WwiseAuthoringAPI/cs/WaapiCS/CallWwise.cs.

This file contains the following code, which allows you to connect to the Wwise Authoring API.

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using WampSharp.Core.Serialization;
using WampSharp.V2;
using WampSharp.V2.Client;
using WampSharp.V2.Core.Contracts;
using WampSharp.V2.Rpc;

namespace WaapiCS
{
    class CallWwise
    {
        const string serverAddress = "ws://127.0.0.1:8080/waapi";

        public static void Main(string[] args)
        {
            // Wwise allows by default ws://127.0.0.1 and ws://localhost.
            // However, if you specify anything else in the server address, you will need to add 
            // it as an allowed origin in Wwise User Preferences. 
            // To do so, go to Project > User Preferences... and in the Wwise Authoring API section
            // at the bottom of the window, add the address in the Allowed origins text box. 
            // Note that values are comma-separated.       

            DefaultWampChannelFactory factory = new DefaultWampChannelFactory();
            IWampChannel channel = factory.CreateJsonChannel(serverAddress, "realm1");
            channel.Open().Wait();

            IWampRealmProxy realmProxy = channel.RealmProxy;

            CallGetInfo(realmProxy);
            CallGetSelectedObjects(realmProxy);
            
            Console.ReadLine();
        }

        public static void CallGetInfo(IWampRealmProxy realmProxy)
        {
            Console.WriteLine("Calling 'ak.wwise.core.getInfo'");

            // Arguments are passed using keywordArguments with primitive values which are serialized as Json
            IDictionary<string, object> keywordArguments = new Dictionary<string, object>();

            realmProxy.RpcCatalog.Invoke(
                new AssertCallback(),
                new CallOptions(),
                "ak.wwise.core.getInfo",
                new object[] { }, // Volontarily empty, we use only keywordArguments
                keywordArguments);
        }

        public static void CallGetSelectedObjects(IWampRealmProxy realmProxy)
        {
            Console.WriteLine("Calling 'ak.wwise.ui.getSelectedObjects'");

            // Optional arguments are passed using a subclass of CallOptions providing data members as specified by the Wwise Authoring API
            MyCallOptions options = new MyCallOptions();
            options.@return = new string[] { "id", "name", "parent" };

            realmProxy.RpcCatalog.Invoke(
                new GetSelectedObjectsCallback(),
                options,
                "ak.wwise.ui.getSelectedObjects",
                new object[] { }, // Volontarily empty, we use only keywordArguments
                new Dictionary<string, object>()); // Volontarily empty, no keywordArguments are necessary for this API call
        }
    }

    public class AssertCallback : IWampRawRpcOperationClientCallback
    {
        public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails details, TMessage[] arguments, IDictionary<string, TMessage> argumentsKeywords)
        {
            string name = argumentsKeywords["displayName"].ToString();
            string copyright = formatter.Deserialize<JToken>(argumentsKeywords["version"])["displayName"].ToString();
            Console.WriteLine("ak.wwise.core.getInfo: Hello {0} {1}", name, copyright);
        }

        // Other method overloads are never used: WAAPI always sends keyword arguments
        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error, TMessage[] arguments, TMessage argumentsKeywords) {}
        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error) {}
        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error, TMessage[] arguments) {}
        public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails details) {}
        public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails details, TMessage[] arguments) {}
    }

    public class GetSelectedObjectsCallback : IWampRawRpcOperationClientCallback
    {
        public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails details, TMessage[] arguments, IDictionary<string, TMessage> argumentsKeywords)
        {
            string prefix = "ak.wwise.ui.getSelectedObjects: ";
            Console.WriteLine(prefix + "Got selected object data!");

            IEnumerable<JToken> objects = formatter.Deserialize<IEnumerable<JToken>>(argumentsKeywords["objects"]);
            int selectedObjectsNum = objects.Count<JToken>();
            Console.WriteLine(prefix + "Got {0} object(s)!", selectedObjectsNum);

            if (selectedObjectsNum >= 1)
            {
                JToken firstObject = objects.First<JToken>();
                
                Console.WriteLine(prefix + "The first selected object is '{0}' ({1})",
                    firstObject["name"].ToString(),
                    firstObject["id"].ToString());

                JToken parent = firstObject["parent"];
                if (parent == null)
                {
                    Console.WriteLine(prefix + "It has no parent.");
                }
                else
                {
                    Console.WriteLine(prefix + "Its parent is '{0}' ({1})",
                        parent["name"].ToString(),
                        parent["id"].ToString());
                }
            }
            else
            {
                Console.WriteLine(prefix + "Select something and try again!");
            }
        }

        // Other method overloads are never used: WAAPI always sends keyword arguments
        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error, TMessage[] arguments, TMessage argumentsKeywords) { }
        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error) { }
        public void Error<TMessage>(IWampFormatter<TMessage> formatter, TMessage details, string error, TMessage[] arguments) { }
        public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails details) { }
        public void Result<TMessage>(IWampFormatter<TMessage> formatter, ResultDetails details, TMessage[] arguments) { }
    }

    public class MyCallOptions : CallOptions
    {
        // Add data members with name corresponding to the option fields (see reference documentation)
        //          vvvvvvvvvvvvvvv
        [DataMember(Name = "return")]
        public IEnumerable<string> @return { get; set; }

        // If a data member is not set, it will not be sent to WAAPI
        [DataMember(Name = "platform")]
        public string platform { get; set; }
    }
}

Running the project

Build the solution (Ctrl+B) and run the sample using Visual Studio (F5).

The sample calls two methods: one to get general information from the Wwise Authoring Application and another to get the objects currently selected in the Application.

Observe messages printed in the output console. You should see something similar to:

Calling 'ak.wwise.core.getInfo'
Calling 'ak.wwise.ui.getSelectedObjects'
ak.wwise.core.getInfo: Hello Wwise v2017.1.0
ak.wwise.ui.getSelectedObjects: Got selected object data!
ak.wwise.ui.getSelectedObjects: Got 1 object(s)!
ak.wwise.ui.getSelectedObjects: The first selected object is 'New Sequence Container' ({03E8DC21-EB2F-4607-BFCC-25BCE69DFB27})
ak.wwise.ui.getSelectedObjects: Its parent is 'ParentA' ({FBB64B3C-711C-46E6-9BBC-B49511F08244})
Note.gif
Note: The information retrieved from Wwise, such as object IDs, will vary based on the project opened in the Wwise Authoring Application. If no selection exists, the following message will be displayed: ak.wwise.ui.getSelectedObjects: Select something and try again!