Skip to content

Instantly share code, notes, and snippets.

@Scarygami
Created May 31, 2012 08:56
Show Gist options
  • Save Scarygami/2842012 to your computer and use it in GitHub Desktop.
Save Scarygami/2842012 to your computer and use it in GitHub Desktop.
Hangout Band basic structure (see http://www.allmyplus.com/hangout-band/)
(function (window) {
"use strict";
var audio, hangout_band, sounds;
sounds = {
// define various parameters for each note that can be played
};
function WebAudio() {
// all web audio API stuff handled here
this.AUDIO_SUPPORT = false;
if (window.AudioContext || window.webkitAudioContext) {
this.AUDIO_SUPPORT = true;
}
this.load_sound = function(nr, callback) {
//...
}
this.play_sound = function(nr) {
//...
}
}
function HangoutBand() {
this.sending = true;
this.receiving = true;
// define several more necessary variables here...
// waiting for API to be ready...
window.gapi.hangout.onApiReady.add(this.onApiReady.bind(this));
}
HangoutBand.prototype.onApiReady = function (event) {
if (event.isApiReady) {
this.initialize_app();
}
};
HangoutBand.prototype.initialize_app = function () {
audio = new WebAudio();
if (audio.AUDIO_SUPPORT) {
for (nr in sounds) {
this.initialize_sound(nr);
}
// to all sorts of stuff to make the UI work
// add callback when others send a "sound"
window.gapi.hangout.data.onMessageReceived.add(this.onMessageReceived.bind(this));
} else {
// sorry no web audio api...
}
};
HangoutBand.prototype.initialize_sound = function (nr) {
audio.load_sound(nr, function () {
// sound finished loading
window.document.getElementById(sounds[nr].div).onclick = function () {
this.play_sound("", nr);
}.bind(this);
});
};
HangoutBand.prototype.onMessageReceived = function (event) {
this.play_sound(event.senderId, event.message);
};
HangoutBand.prototype.play_sound = function (id, nr) {
var chk_play = false;
if (id && id != window.gapi.getParticipantId()) {
// sound by someone else
if (this.receiving) {
chk_play = true;
}
} else {
// your own sound
chk_play = true;
if (this.sending) {
// send message about played sound to others
window.gapi.hangout.data.sendMessage(nr);
}
}
if (chk_play) {
audio.play_sound(nr);
// do all kinds of stuff related to recording the score and handling animations and stuff...
}
};
hangout_band = new HangoutBand();
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment