Created
June 10, 2014 03:26
-
-
Save stephenfeather/ff473cf80e029234e971 to your computer and use it in GitHub Desktop.
Ti.Media.Sound sequencer
This file contains hidden or 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
| /*jslint vars: true, plusplus: true, sloppy: true, nomen: true, maxerr: 1000 */ | |
| function Sequencer(){ | |
| var self = {}; | |
| var song, songTitle, currentNote, totalNotes, noteBatch, notes, timeouts = []; | |
| if(Ti.App.Properties.hasProperty('noteBatch')){ | |
| noteBatch = Ti.App.Properties.getInt('noteBatch'); | |
| }else{ | |
| noteBatch = 1; | |
| } | |
| var player1, player2, hitter, player = {}; | |
| hitter = 0; | |
| self.clearTimeouts = function(){ | |
| Ti.API.info('Clearing Timers'); | |
| timeouts = []; | |
| }; | |
| self.loadSounds = function(){ | |
| // We clear out any loaded sounds | |
| player = {}; | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 0'); | |
| player.a0 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A0.mp3', volume: 1}); | |
| player.b0 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B0.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 1'); | |
| player.a1 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A1.mp3', volume: 1}); | |
| player.b1 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B1.mp3', volume: 1}); | |
| player.c1 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C1.mp3', volume: 1}); | |
| player.d1 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.D1.mp3', volume: 1}); | |
| player.e1 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.E1.mp3', volume: 1}); | |
| player.f1 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.F1.mp3', volume: 1}); | |
| player.g1 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.G1.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 2'); | |
| player.a2 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A2.mp3', volume: 1}); | |
| player.b2 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B2.mp3', volume: 1}); | |
| player.c2 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C2.mp3', volume: 1}); | |
| player.d2 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.D2.mp3', volume: 1}); | |
| player.e2 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.E2.mp3', volume: 1}); | |
| player.f2 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.F2.mp3', volume: 1}); | |
| player.g2 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.G2.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 3'); | |
| player.a3 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A3.mp3', volume: 1}); | |
| player.b3 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B3.mp3', volume: 1}); | |
| player.c3 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C3.mp3', volume: 1}); | |
| player.d3 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.D3.mp3', volume: 1}); | |
| player.e3 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.E3.mp3', volume: 1}); | |
| player.f3 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.F3.mp3', volume: 1}); | |
| player.g3 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.G3.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 4'); | |
| player.a4 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A4.mp3', volume: 1}); | |
| player.b4 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B4.mp3', volume: 1}); | |
| player.c4 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C4.mp3', volume: 1}); | |
| player.d4 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.D4.mp3', volume: 1}); | |
| player.e4 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.E4.mp3', volume: 1}); | |
| player.f4 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.F4.mp3', volume: 1}); | |
| player.g4 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.G4.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 5'); | |
| player.a5 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A5.mp3', volume: 1}); | |
| player.b5 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B5.mp3', volume: 1}); | |
| player.c5 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C5.mp3', volume: 1}); | |
| player.d5 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.D5.mp3', volume: 1}); | |
| player.e5 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.E5.mp3', volume: 1}); | |
| player.f5 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.F5.mp3', volume: 1}); | |
| player.g5 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.G5.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 6'); | |
| player.a6 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A6.mp3', volume: 1}); | |
| player.b6 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B6.mp3', volume: 1}); | |
| player.c6 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C6.mp3', volume: 1}); | |
| player.d6 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.D6.mp3', volume: 1}); | |
| player.e6 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.E6.mp3', volume: 1}); | |
| player.f6 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.F6.mp3', volume: 1}); | |
| player.g6 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.G6.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 7'); | |
| player.a7 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.A7.mp3', volume: 1}); | |
| player.b7 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.B7.mp3', volume: 1}); | |
| player.c7 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C7.mp3', volume: 1}); | |
| player.d7 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.D7.mp3', volume: 1}); | |
| player.e7 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.E7.mp3', volume: 1}); | |
| player.f7 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.F7.mp3', volume: 1}); | |
| player.g7 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.G7.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| Ti.API.info('Loading Octive 8'); | |
| player.c8 = Ti.Media.createSound({url: Ti.Filesystem.resourcesDirectory+'audioLibrary/Piano.ff.C8.mp3', volume: 1}); | |
| Ti.API.info(Ti.Platform.availableMemory); | |
| }; | |
| self.incNoteBatch = function(){ | |
| noteBatch++; | |
| if(noteBatch === 6){ | |
| noteBatch = totalNotes; | |
| } | |
| if(noteBatch === (totalNotes+1)){ | |
| noteBatch = 1; | |
| } | |
| Ti.App.Properties.setInt('noteBatch',noteBatch); | |
| Ti.App.fireEvent('update_count_button'); | |
| }; | |
| self.getSongTitle = function(){ | |
| return songTitle; | |
| }; | |
| self.getCurrentNote = function(){ | |
| return currentNote; | |
| }; | |
| self.getTotalNotes = function(){ | |
| return totalNotes; | |
| }; | |
| self.buildNoteURL = function(note){ | |
| var noteURL; | |
| var path = Ti.Filesystem.resourcesDirectory+'audioLibrary/'; | |
| var filename = 'Piano.ff.'+note.step.toUpperCase()+note.octive+'.mp3'; | |
| noteURL = path+filename; | |
| return noteURL; | |
| }; | |
| self.loadSong = function(file){ | |
| songfile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'songs',file); | |
| song = JSON.parse(songfile.read()); | |
| songTitle = song.title; | |
| totalNotes = song.notation.length; | |
| notes = song.notation; | |
| currentNote = 0; | |
| noteBatch = 1; | |
| Ti.App.Properties.setString('songFile', file); | |
| Ti.App.Properties.setInt('noteBatch',noteBatch); | |
| Ti.App.fireEvent('update_song_title', {title:songTitle}); | |
| Ti.App.fireEvent('update_count_button'); | |
| }; | |
| self.nextNote = function(){ | |
| currentNote++; | |
| }; | |
| function playNote(){ | |
| Ti.API.info('Playing Note #'+currentNote+': '+notes[currentNote].step+notes[currentNote].octive); | |
| player[notes[currentNote].step+notes[currentNote].octive].stop(); | |
| player[notes[currentNote].step+notes[currentNote].octive].play(); | |
| currentNote++; | |
| if(currentNote === (totalNotes)){ | |
| currentNote = 0; | |
| } | |
| } | |
| self.play = function(){ | |
| Ti.API.info('Current Note: '+notes[currentNote].step); | |
| if(noteBatch === 1){ | |
| playNote(); | |
| }else{ | |
| playNote(); | |
| for(var x=1;x<noteBatch;x++){ | |
| timeouts.push(setTimeout(function(){ | |
| playNote(); | |
| },1000*x)); | |
| } | |
| } | |
| }; | |
| self.loadSounds(); | |
| if (Ti.App.Properties.hasProperty('songFile')){ | |
| self.loadSong(Ti.App.Properties.getString('songFile')); | |
| } else { | |
| self.loadSong('mary_had_a_little_lamb.json'); | |
| } | |
| return self; | |
| } | |
| module.exports =Sequencer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment