-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundwebapp.js
104 lines (87 loc) · 2.92 KB
/
soundwebapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
soundState = new Mongo.Collection("soundState");
if (Meteor.isServer)
{
// This code only runs on the server
Meteor.startup(function() {
// on startup, if the db is empty, fill it with initial values
if (soundState.find().count() == 0)
{
soundState.insert({name:"Cow", file:"cow.mp3", checked:false, session:0});
soundState.insert({name:"Dog", file:"dog.mp3", checked:false, session:0});
soundState.insert({name:"Sheep", file:"sheep.mp3", checked:false, session:0});
soundState.insert({name:"Eagle", file:"eagle.mp3", checked:false, session:0});
}
});
// Track changes in our db
soundState.find().observeChanges({changed: function(id){
sound = soundState.findOne(id);
if (sound.checked)
playServerSound(sound.name);
}});
}
if (Meteor.isClient)
{
// This code only runs on the client
Template.body.helpers({
soundOptions: function () {
// get all the sounds to place the option buttons
return soundState.find();
}
});
// Track changes in our db
soundState.find().observeChanges({changed: function(id){
sound = soundState.findOne(id);
// if a sound was selected by another session, then react playing the sound locally
if (sound.checked && sound.session != Meteor.default_connection._lastSessionId)
playSound(sound.name);
}});
// Attach the change event on the radio buttons
Template.body.events({
"change .soundOption": function(event){
// on a change, update db with new states of the radio buttons
updateSoundState(this._id);
}
});
}
// Update the soundState with current selection of radio buttons
function updateSoundState(selectedId)
{
// uncheck the last selected one
last = soundState.findOne({checked: true});
if (last) soundState.update(last._id, {$set: {checked:false}});
// check the newly selected one
soundState.update(selectedId, {$set: {checked:true, session:Meteor.default_connection._lastSessionId}});
}
// Play a sound from the sound database
function playSound(soundName)
{
// find filename associated with this sound type
element = soundState.findOne({name: soundName});
console.log("Playing sound: " + soundName);
s = new buzz.sound(element.file);
s.play();
}
// Play a sound on the server side using mplayer.
function playServerSound(soundName)
{
// find filename associated with this sound type
element = soundState.findOne({name: soundName});
path = getPath() + "/public/";
// command to execute
cmd = "mplayer -quiet " + path + element.file;
exec = Npm.require('child_process').exec;
child = exec(cmd, function(error, stdout, stderr) {
//console.log('stdout: ' + stdout);
//console.log('stderr: ' + stderr);
if(error !== null)
{
//console.log('exec error: ' + error);
}
});
}
// Get the path to the application root
function getPath()
{
path = Npm.require('fs').realpathSync( process.cwd() + '/../../../../../' );
return path;
}