Created
March 10, 2012 02:55
-
-
Save urish/2009816 to your computer and use it in GitHub Desktop.
Titanium Zero Latency Sound with Multi Touch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example application for combining TiMultiTouch module with Zero Latency Sound module. | |
* | |
* # https://marketplace.appcelerator.com/apps/860 TiMultiTouch | |
* # https://marketplace.appcelerator.com/apps/891 Zero Latency Sound | |
* | |
* Created for Jose R. Castello | |
*/ | |
Titanium.UI.setBackgroundColor('#000'); | |
Titanium.UI.iPhone.hideStatusBar(); | |
require('org.urish.titanium.multitouch'); | |
var ZLSound = require('com.salsarhythmsoftware.zlsound'); | |
function createNoteController(name, fileName, pitch, loop, rect) { | |
var soundFile = Ti.Filesystem.getFile(fileName).nativePath; | |
var sample = ZLSound.createSample({ | |
media : soundFile, | |
loopIn : loop[0], | |
loopOut : loop[1], | |
pitch : pitch | |
}); | |
function play() { | |
Ti.API.info("Playing: " + this.name); | |
sample.loop(1000); | |
} | |
function stop() { | |
Ti.API.info("Stopping: " + this.name); | |
sample.stop(); | |
} | |
return { | |
name : name, | |
rect : rect, | |
play : play, | |
stop : stop, | |
}; | |
} | |
var noteDatabaseFile = Ti.Filesystem.getFile("notes.json"); | |
var noteDatabase = JSON.parse(noteDatabaseFile.read().toString()); | |
var win = Ti.UI.createWindow({ | |
orientationModes : [Ti.UI.LANDSCAPE_RIGHT, Ti.UI.LANDSCAPE_LEFT] | |
}); | |
var isiPad = (Ti.Platform.osname == "ipad"); | |
var backgroundImage = Ti.UI.createImageView({ | |
image : isiPad ? "background@2x.png" : "background.png", | |
height : isiPad ? 480 : 320, | |
width : isiPad ? 720 : 480, | |
top : isiPad ? 144 : 0, | |
left : isiPad ? 142 : 0 | |
}); | |
win.add(backgroundImage); | |
var noteButtons = []; | |
noteDatabase.notes.map(function(note) { | |
if(isiPad) { | |
note.rect = note.rect.map(function(x) { | |
return x * 1.5; | |
}); | |
} | |
var noteController = createNoteController(note.name, note.file, note.pitch, note.loop, note.rect) | |
if(note.name.indexOf("#") >= 0) { | |
// Since sharps appear on top of other notes, they have to be first | |
noteButtons.unshift(noteController); | |
} else { | |
noteButtons.push(noteController); | |
} | |
}); | |
function findNoteIndex(x, y) { | |
for(var i = 0; i < noteButtons.length; i++) { | |
var r = noteButtons[i].rect; | |
if((r[0] <= x) && (r[1] <= y) && (r[0] + r[2] >= x) && (r[1] + r[3] >= y)) { | |
return i; | |
} | |
} | |
return null; | |
} | |
var activeNotes = {}; | |
var lastTouch = null; | |
function getTouches(e) { | |
return e.points; | |
} | |
function windowTouchStart(e) { | |
var touches = getTouches(e); | |
for(var key in touches) { | |
var selectedNote = findNoteIndex(touches[key].x, touches[key].y); | |
if(selectedNote !== activeNotes[key]) { | |
if(activeNotes[key] !== undefined && activeNotes[key] !== null) { | |
noteButtons[activeNotes[key]].stop(); | |
} | |
if(selectedNote !== null) { | |
noteButtons[selectedNote].play(); | |
} | |
activeNotes[key] = selectedNote; | |
} | |
} | |
} | |
function windowTouchEnd(e) { | |
var touches = getTouches(e); | |
for(var key in touches) { | |
if(activeNotes[key] !== undefined && activeNotes[key] !== null) { | |
noteButtons[activeNotes[key]].stop(); | |
} | |
delete activeNotes[key]; | |
lastTouch = null; | |
} | |
} | |
win.addEventListener('singletap', function(ev) { | |
// DON'T REMOVE THIS LISTENER!! | |
// hack for multi touch module | |
}); | |
win.addEventListener("touchstart", windowTouchStart); | |
win.addEventListener("touchmove", windowTouchStart); | |
win.addEventListener("touchcancel", windowTouchEnd); | |
win.addEventListener("touchend", windowTouchEnd); | |
if(isiPad) { | |
var footer = Ti.UI.createLabel({ | |
text : "Copyright (c) 2011, Uri Shaked", | |
color : 'white', | |
bottom : 10, | |
left : 10, | |
height : 'auto', | |
font : { | |
fontFamily : 'Helvetica Neue', | |
fontSize : 16 | |
} | |
}); | |
win.add(footer); | |
} | |
win.open({ | |
transition : Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment