Share Audio Clips Between Gateway and Clients

A proof of concept here.

  • Use case in this example is one sound for an andon call (need components, call team leader, etc) and one as an alarm when a barcode doesn't match.

  • Two examples given for loading wav data from URLs or from file locations. Both load into the globals as a dictionary with keys 'call' and 'scan'

  • Starting a sound will use start('call') or start('scan')

  • Stopping a sound will use stop('call') or stop('scan'). Using stop() with no parameter will stop all clips.

from java.io import ByteArrayInputStream
from java.net import URL
from javax.sound.sampled import AudioSystem
from javax.sound.sampled import AudioInputStream
from javax.sound.sampled import Clip

def loadUrls():
	''' Load a set of wav urls to the global dictionary '''
	
	urlDict = {'call': 'http://192.168.140.35/music/tv/GalaxyQuest.wav',
	           'scan': 'http://192.168.140.35/music/tv/scan_alert_short.wav'
	          }
	audioDict = {}
	for key, value in urlDict.items():
		AudioClip = AudioSystem.getClip()
		audioStream = AudioSystem.getAudioInputStream(URL(value))
		AudioClip.open(audioStream)
		audioDict[key] = AudioClip
	system.util.globals['audio'] = audioDict
	
def loadFiles():
	''' Load a set of wav files to the global dictionary '''
	
	fileDict = {'call': '/mnt/d/music/Andon music/GalaxyQuest.wav',
	            'scan': '/mnt/d/music/Andon music/scan_alert_short.wav'
	           }	
	audioDict = {}
	for key, value in fileDict.items():
		AudioClip = AudioSystem.getClip()
		inputStream = ByteArrayInputStream(system.file.readFileAsBytes(value))
		audioStream = AudioSystem.getAudioInputStream(inputStream)
		AudioClip.open(audioStream)
		audioDict[key] = AudioClip
	system.util.globals['audio'] = audioDict


def start(sound):
	system.util.globals['audio'][sound].setMicrosecondPosition(0)
	system.util.globals['audio'][sound].loop(Clip.LOOP_CONTINUOUSLY)
	
def stop(sound = None):
	if sound is not None:
		system.util.globals['audio'][sound].stop()
	else:
		for key in system.util.globals['audio'].keys():
			system.util.globals['audio'][key].stop()
2 Likes