webstaff: egAudio HTML5 audio service
authorBill Erickson <berickxx@gmail.com>
Sun, 15 May 2016 16:51:24 +0000 (12:51 -0400)
committerMike Rylander <mrylander@gmail.com>
Thu, 18 Aug 2016 19:34:21 +0000 (15:34 -0400)
egCore.audio.play('audio.event.dot.path');

Service to look up audio URL's by key name.  Supports fall-thru behavior
where 'foo.bar.baz' will fall-thru to 'foo.bar' and 'foo' depending on
whether an audio file is avaialable.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/templates/staff/base_js.tt2
Open-ILS/web/audio/notifications/error.wav [new file with mode: 0644]
Open-ILS/web/audio/notifications/info.wav [new file with mode: 0644]
Open-ILS/web/audio/notifications/success.wav [new file with mode: 0644]
Open-ILS/web/audio/notifications/warning.wav [new file with mode: 0644]
Open-ILS/web/js/ui/default/staff/Gruntfile.js
Open-ILS/web/js/ui/default/staff/services/audio.js [new file with mode: 0644]
Open-ILS/web/js/ui/default/staff/services/coresvc.js
Open-ILS/web/js/ui/default/staff/test/karma.conf.js

index 980ec5b..c1b721b 100644 (file)
@@ -33,6 +33,7 @@
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/startup.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/hatch.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/print.js"></script>
+<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/audio.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/coresvc.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/user.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/navbar.js"></script>
diff --git a/Open-ILS/web/audio/notifications/error.wav b/Open-ILS/web/audio/notifications/error.wav
new file mode 100644 (file)
index 0000000..41e23e0
Binary files /dev/null and b/Open-ILS/web/audio/notifications/error.wav differ
diff --git a/Open-ILS/web/audio/notifications/info.wav b/Open-ILS/web/audio/notifications/info.wav
new file mode 100644 (file)
index 0000000..6740d3f
Binary files /dev/null and b/Open-ILS/web/audio/notifications/info.wav differ
diff --git a/Open-ILS/web/audio/notifications/success.wav b/Open-ILS/web/audio/notifications/success.wav
new file mode 100644 (file)
index 0000000..eb83e16
Binary files /dev/null and b/Open-ILS/web/audio/notifications/success.wav differ
diff --git a/Open-ILS/web/audio/notifications/warning.wav b/Open-ILS/web/audio/notifications/warning.wav
new file mode 100644 (file)
index 0000000..76c4ecf
Binary files /dev/null and b/Open-ILS/web/audio/notifications/warning.wav differ
index 6588ca8..910d7e1 100644 (file)
@@ -141,6 +141,7 @@ module.exports = function(grunt) {
             'services/startup.js',
             'services/hatch.js',
             'services/print.js',
+            'services/audio.js',
             'services/coresvc.js',
             'services/navbar.js',
             'services/statusbar.js',
diff --git a/Open-ILS/web/js/ui/default/staff/services/audio.js b/Open-ILS/web/js/ui/default/staff/services/audio.js
new file mode 100644 (file)
index 0000000..1b88978
--- /dev/null
@@ -0,0 +1,78 @@
+/**
+ * Core Service - egAudio
+ *
+ * Plays audio files by key name.  Each sound uses a dot-path to indicate 
+ * the sound.  
+ *
+ * For example:
+ * sound => 'warning.checkout.no_item'
+ * URLs are tested in the following order until a valid audio file is found
+ * or no other paths are left to check.
+ *
+ * /audio/notifications/warning/checkout/not_found.wav
+ * /audio/notifications/warning/checkout.wav
+ * /audio/notifications/warning.wav
+ *
+ * TODO: move audio file base path settings to the template 
+ * for configurability?
+ *
+ * Files are only played when sounds are configured to play via 
+ * workstation settings.
+ */
+
+angular.module('egCoreMod')
+
+.factory('egAudio', ['$q','egHatch', function($q, egHatch) {
+
+    var service = {
+        url_cache : {}, // map key names to audio file URLs
+        base_url : '/audio/notifications/'
+    };
+
+    /** 
+     * Play the sound found at the requested string path.  'path' is a 
+     * key name which maps to an audio file URL.
+     */
+    service.play = function(path) {
+        if (!path) return;
+        service.play_url(path, path);
+    }
+
+    service.play_url = function(path, orig_path) {
+
+        var url = service.url_cache[path] || 
+            service.base_url + path.replace(/\./g, '/') + '.wav';
+
+        var player = new Audio(url);
+
+        player.onloadeddata = function() {
+            console.debug('Playing audio URL: ' + url);
+            service.url_cache[orig_path] = url;
+            player.play();
+        };
+
+        if (service.url_cache[path]) {
+            // when serving from the cache, avoid secondary URL lookups.
+            return;
+        }
+
+        player.onerror = function() {
+            // Unable to play path at the requested URL.
+            
+            if (!path.match(/\./)) {
+                // all fall-through options have been exhausted.
+                // No path to play.
+                console.warn(
+                    "No suitable URL found for path '" + orig_path + "'");
+                return;
+            }
+
+            // Fall through to the next (more generic) option
+            path = path.replace(/\.[^\.]+$/, '');
+            service.play_url(path, orig_path);
+        }
+    }
+
+    return service;
+}]);
+
index 57b8361..f754770 100644 (file)
@@ -9,9 +9,11 @@ angular.module('egCoreMod')
 
 .factory('egCore', 
        ['egIDL','egNet','egEnv','egOrg','egPCRUD','egEvent','egAuth',
-        'egPerm','egHatch','egPrint','egStartup','egStrings','egDate',
+        'egPerm','egHatch','egPrint','egStartup','egStrings','egAudio',
+        'egDate',
 function(egIDL , egNet , egEnv , egOrg , egPCRUD , egEvent , egAuth ,
-         egPerm , egHatch , egPrint , egStartup , egStrings , egDate) {
+         egPerm , egHatch , egPrint , egStartup , egStrings , egAudio , 
+         egDate) {
 
     return {
         idl     : egIDL,
@@ -26,6 +28,7 @@ function(egIDL , egNet , egEnv , egOrg , egPCRUD , egEvent , egAuth ,
         print   : egPrint,
         startup : egStartup,
         strings : egStrings,
+        audio   : egAudio,
         date    : egDate
     };
 
index 06014df..760fc3b 100644 (file)
@@ -34,14 +34,14 @@ module.exports = function(config){
       'services/org.js',
       'services/hatch.js',
       'services/print.js',
+      'services/audio.js',
       'services/coresvc.js',
       'services/user.js',
       'services/startup.js',
       'services/ui.js',
       'services/statusbar.js',
       'services/grid.js',
-      'services/navbar.js',
-      'services/date.js',
+      'services/navbar.js', 'services/date.js',
       // load app scripts
       'app.js',
       'circ/**/*.js',