Collaboration diagram for C# Audio API:
Modules | |
Enumerations | |
Events related with Audio Service | |
Classes | |
class | AudioConnection |
Implementation of IAudioConnection. | |
class | AudioManager |
Implementation of IAudioManager. Start point for interacting with Deepsy Audio service More... | |
interface | IAudioManager |
Audio Manager allos to manage the audio in devices running deepsy More... | |
interface | IAudioTask |
Interface with audio task information More... | |
Detailed Description
To work with Deepsy Audio Manager service it is necessary to use AudioManager located in GMV.ITS.HAL.DEEPSY.Audio.Facade for getting the initial AudioManager object.
AudioManager audioManager = new AudioManager("");
AudioManager has an optional parameter indicating the ip address of remote service. For instance:
AudioManager audioManager = new AudioManager("172.22.198.103");
AudioManager implements IDisposable. It can hold resources (such as socket handles) until it is disposed. The Dispose method is automatically called when existing a using or try / catch block, for which the object has been declared. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur.
- * AudioManager audioManager= null;try{audioManager = new AudioManager("172.22.198.103");List<IAudioPort> ports = audioManager.GetPorts().ToList();} catch (Exception e) {throw e;}audioManager.Dispose();
Examples
-
Create and Remove ports
using System;using System.Collections.Generic;using System.Linq;using System.Text;{class CreateAndRemovePorts{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){List<IAudioPort> ports = audioManager.GetPorts().ToList();foreach (IAudioPort port in ports){Console.WriteLine($"Port {port.Client}:{port.Name}. Type {port.Type}");}Console.WriteLine("Creating new audio port");IAudioPort newPort = audioManager.CreatePort("user1", AudioPortType.Type.OUTPUT);List<IAudioPort> portList2 = audioManager.GetPorts().ToList();Console.WriteLine($"Port list after creating a port, size {portList2.Count}");foreach (IAudioPort port in portList2){Console.WriteLine($"Port {port.Client}:{port.Name}.Type {port.Type}");}Console.WriteLine("Removing recently created audio port");audioManager.RemovePort(newPort);List<IAudioPort> portList_end = audioManager.GetPorts().ToList();Console.WriteLine($"Port list after deleting port, size {portList2.Count}");foreach (IAudioPort port in portList_end){Console.WriteLine($"Port {port.Client}:{port.Name}.Type {port.Type}");}}}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}}}
-
Create and Remove Connections
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;{class CreateAndRemoveConnections{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){Console.WriteLine("Creating new audio port");IAudioPort newPort = audioManager.CreatePort("user2", AudioPortType.Type.OUTPUT);List<IAudioPort> portList = audioManager.GetPorts().ToList();Console.WriteLine($"Connection port to the port first INPUT found");IAudioConnection audioConnectionBlocked = null;IAudioConnection audioConnection = null;foreach (IAudioPort port in portList){if (port.Type == AudioPortType.Type.INPUT){Console.WriteLine($"Port {port.Client}:{port.Name}.Type {port.Type}");audioConnectionBlocked = audioManager.CreateConnection("connectionBlocked", newPort, port, 10, 80, 40);audioConnection = audioManager.CreateConnection("connection", newPort, port);break;}}//Sleep to give enough time to update connections listThread.Sleep(1000);List<IAudioConnection> connectionList = audioManager.GetConnections().ToList();Console.WriteLine("Connection list");foreach (IAudioConnection connection in connectionList){Console.WriteLine($"\tConnection ID:\t{connection.Id}");Console.WriteLine($"\tOrigin:\t\t{connection.Origin.Client}:{connection.Origin.Name}");Console.WriteLine($"\tDestination:\t{connection.Destination.Client}:{connection.Destination.Name}");Console.WriteLine($"\tPriority:\t{connection.Priority}");Console.WriteLine($"\tIsActive:\t{connection.IsActive}");Console.WriteLine($"\tVolume:\t{connection.Volume}");Console.WriteLine($"\tGain:\t{connection.Gain}");Console.WriteLine($"\tBlockedBy:\t{string.Join(", ", connection.BlockedBy)}");Console.WriteLine($"\t-----------------");}IAudioTask track = audioManager.Play(newPort, "/usr/bin/tests/stereoTest.wav");Console.WriteLine($"Track ID: {track.Id}");Console.WriteLine($"Track Port Name: {track.Port.Name}");//Back to initial status, remove connection and portConsole.WriteLine($"Disconnecting {audioConnection.Id}");audioConnection.RemoveConnection();Console.WriteLine($"Disconnecting {audioConnectionBlocked.Id}");audioConnectionBlocked.RemoveConnection();Console.WriteLine($"Removing port {newPort.Name}");audioManager.RemovePort(newPort);Console.WriteLine("SUCCESS!");}}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}}}
-
Set connection volume and gain
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;{class SetConnectionVolumeAndGain{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){Console.WriteLine("Creating new audio port");IAudioPort newPort = audioManager.CreatePort("user3", AudioPortType.Type.OUTPUT);List<IAudioPort> portList = audioManager.GetPorts().ToList();Console.WriteLine($"Connection port to the port first INPUT found");IAudioConnection audioConnection = null;foreach (IAudioPort port in portList){if (port.Type == AudioPortType.Type.INPUT){Console.WriteLine($"Port {port.Client}:{port.Name}.Type {port.Type}");audioConnection = audioManager.CreateConnection("connection", newPort, port, 10, 80, 60);break;}}List<IAudioConnection> connectionList = audioManager.GetConnections().ToList();Console.WriteLine("Connection list");foreach (IAudioConnection connection in connectionList){Console.WriteLine($"\tConnection ID:\t{connection.Id}");Console.WriteLine($"\tOrigin:\t\t{connection.Origin.Client}:{connection.Origin.Name}");Console.WriteLine($"\tDestination:\t{connection.Destination.Client}:{connection.Destination.Name}");Console.WriteLine($"\tPriority:\t{connection.Priority}");Console.WriteLine($"\tIsActive:\t{connection.IsActive}");Console.WriteLine($"\tVolume:\t{connection.Volume}");Console.WriteLine($"\tGain:\t{connection.Gain}");Console.WriteLine($"\tBlockedBy:\t{string.Join(", ", connection.BlockedBy)}");Console.WriteLine($"\t-----------------");}int newVolume = 75;int newGain = 33;Console.WriteLine($"Changing volume to {newVolume} and gain to {newGain}");audioConnection.SetConnectionVolume(newVolume);audioConnection.SetConnectionGain(newGain);//Sleep to give enough time to update connections listThread.Sleep(2000);connectionList = audioManager.GetConnections().ToList();Console.WriteLine("Connection list");foreach (IAudioConnection connection in connectionList){Console.WriteLine($"\tConnection ID:\t{connection.Id}");Console.WriteLine($"\tOrigin:\t\t{connection.Origin.Client}:{connection.Origin.Name}");Console.WriteLine($"\tDestination:\t{connection.Destination.Client}:{connection.Destination.Name}");Console.WriteLine($"\tPriority:\t{connection.Priority}");Console.WriteLine($"\tIsActive:\t{connection.IsActive}");Console.WriteLine($"\tVolume:\t{connection.Volume}");Console.WriteLine($"\tGain:\t{connection.Gain}");Console.WriteLine($"\tBlockedBy:\t{string.Join(", ", connection.BlockedBy)}");Console.WriteLine($"\t-----------------");}//Back to initial status, remove connection and portConsole.WriteLine($"Disconnecting {audioConnection.Id}");audioConnection.RemoveConnection();Console.WriteLine($"Removing port {newPort.Name}");audioManager.RemovePort(newPort);Console.WriteLine("SUCCESS!");}}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}}}
-
Subscribe to audio events
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;{class SubscribeAudioEvents{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){audioManager.PortListEvent += AudioManager_PortListEvent;audioManager.ConnectionListEvent += AudioManager_ConnectionListEvent;audioManager.PortGpioEvent += AudioManager_PortGpioEvent;List<IAudioPort> ports = audioManager.GetPorts().ToList();foreach (IAudioPort port in ports){Console.WriteLine($"Port {port.Client}:{port.Name}. Type {port.Type}");}List<IAudioConnection> connections = audioManager.GetConnections().ToList();foreach (IAudioConnection con in connections){con.ConnectionStatusEvent += AudioConnection_ConnectionStatusEvent;}Console.WriteLine("Press any key for exit");Console.ReadKey();}}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}private static void AudioManager_ConnectionListEvent(object sender, IAudioConnectionListEvent e){if (e.Type == AudioListEventType.Type.ADDED){Console.WriteLine($"ADDED Audio Connection with id {e.Connection.Id}");e.Connection.ConnectionStatusEvent += AudioConnection_ConnectionStatusEvent;}else if (e.Type == AudioListEventType.Type.REMOVED){Console.WriteLine($"REMOVED Audio Connection with id {e.Connection.Id}");}}private static void AudioConnection_ConnectionStatusEvent(object sender, IAudioConnectionStatusEvent e){if (e.Type == AudioStatusEventType.Type.DISABLED){Console.WriteLine($"DISABLED Audio Connection with id {e.Connection.Id}");}else if (e.Type == AudioStatusEventType.Type.ENABLED){Console.WriteLine($"ENABLED Audio Connection with id {e.Connection.Id}");}else if (e.Type == AudioStatusEventType.Type.UPDATED){Console.WriteLine($"UPDATED Audio Connection with id {e.Connection.Id}");}}private static void AudioManager_PortListEvent(object sender, IAudioPortListEvent e){if (e.Type == AudioListEventType.Type.ADDED){Console.WriteLine($"ADDED Audio Port {e.Port.Client}:{e.Port.Name}");}else{Console.WriteLine($"REMOVED Audio Port with id {e.Port.Client}:{e.Port.Name}");}}private static void Track_TaskStatusEvent(object sender, IAudioTaskStatusEvent e){Console.WriteLine($"Task {e.TaskId}, Status {e.Status}");}private static void AudioManager_PortGpioEvent(object sender, IAudioPortGpioEvent e){string portFullName = $"{e.PortGpio.Port.Client}:{e.PortGpio.Port.Name}";if (e.Type == AudioPortGpioEventType.Type.ASSIGNED){Console.WriteLine($"Port with name [{portFullName}] and type [{e.PortGpio.Port.Type}] has been assigned GPIO [{e.PortGpio.Gpio}] for sink.");}else if (e.Type == AudioPortGpioEventType.Type.RELEASED){Console.WriteLine($"Port with name [{portFullName}] and type [{e.PortGpio.Port.Type}] assigned GPIO has been released.");}}}}
-
Record Example
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;{class RecordExample{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){audioManager.PortListEvent += AudioManager_PortListEvent;audioManager.ConnectionListEvent += AudioManager_ConnectionListEvent;IAudioPort myPort = audioManager.CreatePort("user_port", AudioPortType.Type.INPUT);List<IAudioPort> ports = audioManager.GetPorts().ToList();IAudioPort origin = ports.FirstOrDefault(port => port.Type == AudioPortType.Type.OUTPUT && port.Client.Contains("external_microphone"));if (origin == null){Console.WriteLine("Port not found");return;}Console.WriteLine($"Port {origin.Client}:{origin.Name}.Type {origin.Type}");audioManager.CreateConnection("my_connection", origin, myPort);IAudioTask track = audioManager.Record(myPort, "/mntDAT/test1.wav");track.TaskStatusEvent += Track_TaskStatusEvent;Console.WriteLine("Recording for 10 seconds");Thread.Sleep(10000);track.Stop();}Console.WriteLine("Press any key for exit");Console.ReadKey();}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}private static void AudioManager_ConnectionListEvent(object sender, IAudioConnectionListEvent e){if (e.Type == AudioListEventType.Type.ADDED){Console.WriteLine($"ADDED Audio Connection with id {e.Connection.Id}");}else{Console.WriteLine($"REMOVED Audio Connection with id {e.Connection.Id}");}}private static void AudioManager_PortListEvent(object sender, IAudioPortListEvent e){if (e.Type == AudioListEventType.Type.ADDED){Console.WriteLine($"ADDED Audio Port {e.Port.Client}:{e.Port.Name}");}else{Console.WriteLine($"REMOVED Audio Port with id {e.Port.Client}:{e.Port.Name}");}}private static void Track_TaskStatusEvent(object sender, IAudioTaskStatusEvent e){Console.WriteLine($"Task {e.TaskId}, Status {e.Status}");}}}
-
Audio Task Operations
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;{class PlayTrackExample{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){audioManager.PortListEvent += AudioManager_PortListEvent;audioManager.ConnectionListEvent += AudioManager_ConnectionListEvent;IAudioPort myPort = audioManager.CreatePort("user_port", AudioPortType.Type.OUTPUT);List<IAudioPort> ports = audioManager.GetPorts().ToList();IAudioPort destination = ports.FirstOrDefault(port => port.Type == AudioPortType.Type.INPUT && port.Client.Contains("external_speaker_r"));if (destination == null){Console.WriteLine("Port not found");return;}Console.WriteLine($"Port {destination.Client}:{destination.Name}.Type {destination.Type}");IAudioConnection myConnection = audioManager.CreateConnection("my_connection", myPort, destination);List<IAudioConfiguration> configurations = new List<IAudioConfiguration>();configurations.Add(new DEEPSY.Audio.Dto.AudioConfiguration(){Value = 80,Configuration = AudioConfiguration.Configuration.VOLUME});IAudioTask track = audioManager.Play(myPort, "/usr/bin/test/stereoTest.wav", configurations );track.TaskStatusEvent += Track_TaskStatusEvent;Thread.Sleep(2000);Console.WriteLine($"track status: {track.GetStatus()}");Thread.Sleep(2000);Console.WriteLine($"track status: {track.GetStatus()}");Thread.Sleep(2000);Console.WriteLine($"track status: {track.GetStatus()}");Thread.Sleep(2000);Console.WriteLine($"track status: {track.GetStatus()}");Console.WriteLine("Press any key for exit");Console.ReadKey();myConnection.RemoveConnection();audioManager.RemovePort(myPort);}}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}private static void AudioManager_ConnectionListEvent(object sender, IAudioConnectionListEvent e){if (e.Type == AudioListEventType.Type.ADDED){Console.WriteLine($"ADDED Audio Connection with id {e.Connection.Id}");}else{Console.WriteLine($"REMOVED Audio Connection with id {e.Connection.Id}");}}private static void AudioManager_PortListEvent(object sender, IAudioPortListEvent e){if (e.Type == AudioListEventType.Type.ADDED){Console.WriteLine($"ADDED Audio Port {e.Port.Client}:{e.Port.Name}");}else{Console.WriteLine($"REMOVED Audio Port with id {e.Port.Client}:{e.Port.Name}");}}private static void Track_TaskStatusEvent(object sender, IAudioTaskStatusEvent e){Console.WriteLine($"Task {e.TaskId}, Status {e.Status}");}}}
-
Get Audio configurations
using System;using System.Collections.Generic;using System.Linq;using System.Text;{class GetAudioConfigurations{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){IEnumerable<IAudioConfiguration> configurations = audioManager.GetConfiguration();foreach(IAudioConfiguration configuration in configurations){Console.WriteLine($"Configuration [{configuration.Configuration}] Value [{configuration.Value}]");}}}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}}}
-
Set Audio configurations
using System;using System.Collections.Generic;using System.Linq;using System.Text;{class SetAudioConfigurations{static void Main(string[] args){try{string deepsyServerIp = args.Length == 0 ? "192.168.0.144" : args[0];using (AudioManager audioManager = new AudioManager(deepsyServerIp)){//Reading current valueIEnumerable<IAudioConfiguration> configurations = audioManager.GetConfiguration();foreach(IAudioConfiguration c in configurations){Console.WriteLine($"Configuration [{c.Configuration}] Value [{c.Value}]");}//set value to 75%IAudioConfiguration configuration = new DEEPSY.Audio.Dto.AudioConfiguration(){Value = 75,Configuration = AudioConfiguration.Configuration.VOLUME};audioManager.SetConfiguration(configuration);// Read configurationconfigurations = audioManager.GetConfiguration();foreach (IAudioConfiguration c in configurations){Console.WriteLine($"Configuration [{c.Configuration}] Value [{c.Value}]");}// Setting value to MAX value (100)configuration = new DEEPSY.Audio.Dto.AudioConfiguration(){Value = 100,Configuration = AudioConfiguration.Configuration.VOLUME};audioManager.SetConfiguration(configuration);configurations = audioManager.GetConfiguration();// Read configurationforeach (IAudioConfiguration c in configurations){Console.WriteLine($"Configuration [{c.Configuration}] Value [{c.Value}]");}}}catch (HalException deepsyEx){Console.WriteLine($"Deepsy error : {deepsyEx.ErrorCode}");}catch (Exception ex){Console.WriteLine($"Error Not Handled exception {ex.Message}");}}}}