Package com.gmv.its.deepsy.hal.audio

Classes

interface  AudioConfiguration
 Interface that represents a audio configuration. More...
 
interface  AudioConnection
 AudioConnection. More...
 
interface  AudioConnectionEvent
 
interface  AudioConnectionListEvent
 Audio connection list event. More...
 
class  AudioConnectionObserver
 Audio connection observer. More...
 
interface  AudioConnectionStatusEvent
 Audio connection status event. More...
 
interface  AudioEvent
 
class  AudioLowLevelException
 Audio low level exception. More...
 
interface  AudioManager
 Audio Manager. More...
 
class  AudioNotAvailableException
 Audio not available exception. More...
 
class  AudioObserver
 Audio observer. More...
 
interface  AudioPort
 
interface  AudioPortListEvent
 Audio port list event. More...
 
interface  AudioTask
 Interface that represents a audio task. More...
 
interface  AudioTaskEvent
 Audio task event. More...
 
interface  TaskObserver
 Task observer. More...
 

Detailed Description

To work with Deepsy audio service it is necessary to use DeepsyAudioManager located in com.gmv.its.deepsy.audio for getting the initial AudioManager object.

AudioManager audioManager = DeepsyAudioManager.getInstance();


DeepsyAudioManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:

AudioManager audioManager = DeepsyAudioManager.getInstance("192.168.1.10");


AudioManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain an Audio object:

try (AudioManager audioManager = DeepsyAudioManager.getInstance()){
Audio audio = audioManager.getAudio();
} catch (AudioNotAvailableException e) {
e.printStackTrace();
} catch (AudioLowLevelException e) {
e.printStackTrace();
}


If this object is not inside a try with resources block, it is recommended to call to audioManager.close() in order to finalize properly;

audioManager.close();

Service availability

To detect service availability the DeepsyAudioManager implements Manager interface which provides methods for getting availability and subscribing to availability events.

class MyManagerObserver implements ManagerObserver{
public void notify(ManagerConnectionEvent event) {
System.out.println(event.getManager().getName() + " in "+ event.getManager().getIp() + " "+ event.getAction().toString());
}
}
public class App {
public static void main(String[] args) {
ManagerObserver observer = new MyManagerObserver();
try (AudioManager audioManager = DeepsyAudioManager.getInstance()){
audioManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}


Examples


Create and remove ports

package com.gmv.its.dpyjavaexamples.audio;
import java.time.Instant;
import java.util.List;
import com.gmv.its.deepsy.audio.DeepsyAudioManager;
public class CreateAndRemovePorts {
public static void main(String[] args) {
try (AudioManager audioManager = DeepsyAudioManager.getInstance("192.168.0.122")) {
List<AudioPort> portList = audioManager.getAudioPorts();
System.out.println("Initial Port list, size " + portList.size() );
for (AudioPort port : portList) {
System.out.println("\tPort " + port.getClient() + ":" + port.getName() + ".Type " + port.getType());
}
System.out.println("Creating new audio port");
AudioPort newPort = audioManager.createUserAudioPort("user1",AudioPortType.OUTPUT);
List<AudioPort> portList2 = audioManager.getAudioPorts();
System.out.println("Port list after creating a port, size " + portList2.size() );
for (AudioPort port : portList2) {
System.out.println("\tPort " + port.getClient() + ":" + port.getName() + ".Type " + port.getType());
}
System.out.println("Removing recently created audio port");
audioManager.removeUserAudioPort(newPort);
List<AudioPort> portList_end = audioManager.getAudioPorts();
System.out.println("Port list after deleting initial port, size " + portList_end.size() );
for (AudioPort port : portList_end) {
System.out.println("\tPort " + port.getClient() + ":" + port.getName() + ".Type " + port.getType());
}
} catch (AudioNotAvailableException e) {
e.printStackTrace();
} catch (AudioLowLevelException e) {
e.printStackTrace();
}
}
}


Create and remove connections

package com.gmv.its.dpyjavaexamples.audio;
import java.time.Instant;
import java.util.List;
import com.gmv.its.deepsy.audio.DeepsyAudioManager;
public class CreateAndRemoveConnections {
public static void main(String[] args) {
try (AudioManager audioManager = DeepsyAudioManager.getInstance("172.22.198.103")) {
System.out.println("Creating audio port to be used for connection");
AudioPort myPort = audioManager.createUserAudioPort("user2",AudioPort.AudioPortType.OUTPUT);
List<AudioPort> portList = audioManager.getAudioPorts();
System.out.println("Connecting " + myPort.getFullName() + " to the port first INPUT found");
for (AudioPort port : portList) {
if (port.getType() == AudioPort.AudioPortType.INPUT){
System.out.println("\tPort " + port.getClient() + ":" + port.getName() + ".Type " + port.getType());
myPort.connect(port, "my_connection_blocked", 10, 80, 40);
myPort.connect(port, "my_connection");
break; //Can only create 1 connection with the same ID
}
}
//Sleep to give enough time to update connections list
Thread.sleep(1000);
List<AudioConnection> connectionList = audioManager.getAudioConnections();
System.out.println("Connection list");
for (AudioConnection connection : connectionList) {
System.out.println("\tConnection ID:" + connection.getConnectionId());
System.out.println("\t\tOrigin: " + connection.getOrigin().getClient() + ":" + connection.getOrigin().getName());
System.out.println("\t\tDestination: " + connection.getDestination().getClient() + ":" + connection.getDestination().getName());
System.out.println("\t\tPriority: " + connection.getPriority());
System.out.println("\t\tIsActive: " + connection.getIsActive());
System.out.println("\t\tVolume: " + connection.getVolume());
System.out.println("\t\tGain: " + connection.getGain());
System.out.println("\t\tBlockedBy: " + String.join(" ", connection.getBlockedBy()));
}
AudioTask track = myPort.play("/usr/bin/tests/stereoTest.wav");
System.out.println("Track ID: " + track.getId());
System.out.println("Track Port Name: " + track.getPort().getName());
//Back to initial status, remove connection and port
System.out.println("Disconnecting my_connection");
myPort.disconnect("my_connection");
System.out.println("Disconnecting my_connection_blocked");
myPort.disconnect("my_connection_blocked");
System.out.println("Removing port " + myPort.getFullName());
audioManager.removeUserAudioPort(myPort);
System.out.println("SUCCESS!");
} catch (AudioNotAvailableException e) {
e.printStackTrace();
} catch (AudioLowLevelException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


Record sound

package com.gmv.its.dpyjavaexamples.audio;
import java.time.Instant;
import java.util.List;
import java.util.ArrayList;
import com.gmv.its.deepsy.audio.DeepsyAudioManager;
import com.gmv.its.deepsy.audio.*;
class MyAudioObserver3 implements AudioObserver {
public void notify(AudioEvent event) {
if (event instanceof AudioPortListEvent) {
notifyNewPortListEvent((AudioPortListEvent) event);
} else if (event instanceof AudioConnectionListEvent) {
notifyNewConnectionListEvent((AudioConnectionListEvent) event);
}
}
void notifyNewPortListEvent(AudioPortListEvent event) {
if (event.getAction() == AudioPortListEvent.AudioPortListAction.ADDED) {
System.out.println("ADDED Audio Port " + event.getAudioPort().getFullName() + "\n");
} else if (event.getAction() == AudioPortListEvent.AudioPortListAction.REMOVED ){
System.out.println("REMOVED Audio Port " + event.getAudioPort().getFullName() + "\n");
}
}
void notifyNewConnectionListEvent(AudioConnectionListEvent event) {
if (event.getAction() == AudioConnectionListEvent.AudioConnectionListAction.ADDED) {
System.out.println("ADDED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
} else if (event.getAction() == AudioConnectionListEvent.AudioConnectionListAction.REMOVED ){
System.out.println("REMOVED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
}
}
}
class MyAudioTaskObserver2 implements TaskObserver {
public void notify(AudioTaskEvent event) {
System.out.println("Task: " + event.getTask().getId() + ", Status: " + event.getStatus());
}
}
public class RecordExample {
public static void main(String[] args) {
try (AudioManager audioManager = DeepsyAudioManager.getInstance("192.168.0.122")) {
MyAudioObserver3 myAudioObserver = new MyAudioObserver3();
System.out.println("Recording ");
audioManager.subscribe(myAudioObserver);
MyAudioTaskObserver2 myAudioTaskObserver = new MyAudioTaskObserver2();
AudioPort myPort = audioManager.createUserAudioPort("my_port",AudioPort.AudioPortType.INPUT);
List<AudioPort> portList = audioManager.getAudioPorts();
System.out.println("Connecting " + myPort.getFullName());
for (AudioPort port : portList) {
if (port.getType() == AudioPort.AudioPortType.OUTPUT && port.getClient().contains("external_microphone") ){
System.out.println("\tPort " + port.getClient() + ":" + port.getName() + ".Type " + port.getType());
port.connect(myPort, "my_connection");
break; //Can only create 1 connection with the same ID
}
}
List<AudioConfiguration> configuration = new ArrayList<AudioConfiguration>();
configuration.add(new AudioConfigurationImpl(50,AudioConfiguration.ConfigurationType.VOLUME));
AudioTask track = myPort.record("/mntDAT/test1.wav",configuration);
track.subscribe(myAudioTaskObserver);
System.out.println("Recording for 10 seconds");
Thread.sleep(10000);
track.stop();
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (AudioNotAvailableException e) {
e.printStackTrace();
} catch (AudioLowLevelException e) {
System.out.println("Error: " + e.toString());
}
}
}


Subscribe to audio event

package com.gmv.its.dpyjavaexamples.audio;
import java.time.Instant;
import java.util.List;
import com.gmv.its.deepsy.audio.DeepsyAudioManager;
class MyAudioObserver implements AudioObserver {
public void notify(AudioEvent event) {
if (event instanceof AudioPortListEvent) {
notifyNewPortListEvent((AudioPortListEvent) event);
} else if (event instanceof AudioConnectionListEvent) {
notifyNewConnectionListEvent((AudioConnectionListEvent) event);
}
}
void notifyNewPortListEvent(AudioPortListEvent event) {
if (event.getAction() == AudioPortListEvent.AudioPortListAction.ADDED) {
System.out.println("ADDED Audio Port " + event.getAudioPort().getFullName() + "\n");
} else if (event.getAction() == AudioPortListEvent.AudioPortListAction.REMOVED ){
System.out.println("REMOVED Audio Port " + event.getAudioPort().getFullName() + "\n");
}
}
void notifyNewConnectionListEvent(AudioConnectionListEvent event) {
if (event.getAction() == AudioConnectionListEvent.AudioConnectionListAction.ADDED) {
System.out.println("ADDED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
try {
event.getAudioConnection().subscribe(new MyAudioConnectionObserver());
System.out.println("Subscribed to " + event.getAudioConnection().getConnectionId() + " connection events");
} catch (CannotSubscribeException e) {
System.out.println(e.getMessage());
}
} else if (event.getAction() == AudioConnectionListEvent.AudioConnectionListAction.REMOVED ){
System.out.println("REMOVED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
}
}
}
class MyAudioConnectionObserver implements AudioConnectionObserver {
public void notify(AudioConnectionEvent event) {
if (event instanceof AudioConnectionStatusEvent) {
notifyNewConnectionStatus((AudioConnectionStatusEvent) event);
}
}
void notifyNewConnectionStatus(AudioConnectionStatusEvent event) {
if (event.getAction() == AudioConnectionStatusEvent.AudioConnectionStatusAction.ENABLED) {
System.out.println("ENABLED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
} else if (event.getAction() == AudioConnectionStatusEvent.AudioConnectionStatusAction.DISABLED) {
System.out.println("DISABLED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
} else if (event.getAction() == AudioConnectionStatusEvent.AudioConnectionStatusAction.UPDATED) {
System.out.println("UPDATED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
}
}
}
public class SubscribeAudioEvents {
public static void main(String[] args) {
try (AudioManager audioManager = DeepsyAudioManager.getInstance("172.22.198.103")) {
MyAudioObserver myAudioObserver = new MyAudioObserver();
MyAudioConnectionObserver myAudioConnectionObserver = new MyAudioConnectionObserver();
System.out.println("Subscribed to AudioPort List Events");
audioManager.subscribe(myAudioObserver);
List<AudioPort> portList = audioManager.getAudioPorts();
System.out.println("Port list");
for (AudioPort port : portList) {
System.out.println("\tPort " + port.getClient() + ":" + port.getName() + ".Type " + port.getType());
}
List<AudioConnection> connectionList = audioManager.getAudioConnections();
System.out.println("Connection list");
for (AudioConnection connection : connectionList) {
System.out.println("\tConnection " + connection.getConnectionId() + "\n\t\tOrigin ["+ connection.getOrigin().getFullName() + "] --> Destination [" + connection.getDestination().getFullName() + "]");
}
for (AudioConnection c : connectionList) {
c.subscribe(myAudioConnectionObserver);
System.out.println("Subscribed to " + c.getConnectionId() + " connection events");
}
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
}
}
}


Audio task operations (start and stop tasks)

package com.gmv.its.dpyjavaexamples.audio;
import java.time.Instant;
import java.util.List;
import com.gmv.its.deepsy.audio.DeepsyAudioManager;
class MyAudioObserver2 implements AudioObserver {
public void notify(AudioEvent event) {
if (event instanceof AudioPortListEvent) {
notifyNewPortListEvent((AudioPortListEvent) event);
} else if (event instanceof AudioConnectionListEvent) {
notifyNewConnectionListEvent((AudioConnectionListEvent) event);
}
}
void notifyNewPortListEvent(AudioPortListEvent event) {
if (event.getAction() == AudioPortListEvent.AudioPortListAction.ADDED) {
System.out.println("ADDED Audio Port " + event.getAudioPort().getFullName() + "\n");
} else if (event.getAction() == AudioPortListEvent.AudioPortListAction.REMOVED ){
System.out.println("REMOVED Audio Port " + event.getAudioPort().getFullName() + "\n");
}
}
void notifyNewConnectionListEvent(AudioConnectionListEvent event) {
if (event.getAction() == AudioConnectionListEvent.AudioConnectionListAction.ADDED) {
System.out.println("ADDED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
} else if (event.getAction() == AudioConnectionListEvent.AudioConnectionListAction.REMOVED ){
System.out.println("REMOVED Audio Connection with id " + event.getAudioConnection().getConnectionId() + "\n");
}
}
}
class MyAudioTaskObserver implements TaskObserver {
public void notify(AudioTaskEvent event) {
System.out.println("Task: " + event.getTask().getId() + ", Status: " + event.getStatus());
}
}
public class TaskOperations {
public static void main(String[] args) {
try (AudioManager audioManager = DeepsyAudioManager.getInstance("192.168.0.122")) {
MyAudioObserver2 myAudioObserver = new MyAudioObserver2();
System.out.println("Subscribed to AudioPort List Events");
audioManager.subscribe(myAudioObserver);
MyAudioTaskObserver myAudioTaskObserver = new MyAudioTaskObserver();
AudioPort myPort = audioManager.createUserAudioPort("my_port",AudioPort.AudioPortType.OUTPUT);
List<AudioPort> portList = audioManager.getAudioPorts();
System.out.println("Connecting " + myPort.getFullName());
for (AudioPort port : portList) {
if (port.getType() == AudioPort.AudioPortType.INPUT && port.getClient().contains("external_speaker_r") ){
System.out.println("\tPort " + port.getClient() + ":" + port.getName() + ".Type " + port.getType());
myPort.connect(port, "my_connection");
break; //Can only create 1 connection with the same ID
}
}
AudioTask track = myPort.play("/usr/bin/tests/stereoTest.wav");
track.subscribe(myAudioTaskObserver);
Thread.sleep(2000);
System.out.println("track status: "+track.getTaskStatus());
Thread.sleep(2000);
System.out.println("track status: "+track.getTaskStatus());
Thread.sleep(2000);
System.out.println("track status: "+track.getTaskStatus());
Thread.sleep(2000);
System.out.println("track status: "+track.getTaskStatus());
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (AudioNotAvailableException e) {
e.printStackTrace();
} catch (AudioLowLevelException e) {
System.out.println("Error: " + e.toString());
}
}
}

Audio set configuration

package com.gmv.its.dpyjavaexamples.audio;
import java.time.Instant;
import java.util.List;
import com.gmv.its.deepsy.audio.DeepsyAudioManager;
import com.gmv.its.deepsy.audio.AudioConfigurationImpl;
public class SetAudioConfiguration {
public static void main(String[] args) {
try (AudioManager audioManager = DeepsyAudioManager.getInstance("192.168.0.122")) {
List<AudioConfiguration> configurationList = audioManager.getAudioConfiguration();
System.out.println("Default audio configuration: ");
for (AudioConfiguration configuration : configurationList) {
System.out.println("\nConfiguration " + configuration.getConfiguration() + ":" + configuration.value());
}
audioManager.setAudioConfiguration(50,AudioConfiguration.ConfigurationType.VOLUME);
configurationList = audioManager.getAudioConfiguration();
System.out.println("Default audio configuration: ");
for (AudioConfiguration configuration : configurationList) {
System.out.println("\nConfiguration " + configuration.getConfiguration() + ":" + configuration.value());
}
} catch (AudioNotAvailableException e) {
e.printStackTrace();
} catch (AudioLowLevelException e) {
e.printStackTrace();
}
}
}

Audio get configuration

package com.gmv.its.dpyjavaexamples.audio;
import java.time.Instant;
import java.util.List;
import com.gmv.its.deepsy.audio.DeepsyAudioManager;
public class GetAudioConfiguration {
public static void main(String[] args) {
try (AudioManager audioManager = DeepsyAudioManager.getInstance("192.168.0.122")) {
List<AudioConfiguration> configurationList = audioManager.getAudioConfiguration();
System.out.println("Default audio configuration: ");
for (AudioConfiguration configuration : configurationList) {
System.out.println("\nConfiguration " + configuration.getConfiguration() + ":" + configuration.value());
}
} catch (AudioNotAvailableException e) {
e.printStackTrace();
} catch (AudioLowLevelException e) {
e.printStackTrace();
}
}
}