diff --git a/.gitignore b/.gitignore index b512c09..a345416 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,90 @@ -node_modules \ No newline at end of file +# Created by https://www.gitignore.io + +### OSX ### +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +nbactions.xml +nb-configuration.xml +.nb-gradle/ + + +### Windows ### +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +### Node ### +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules diff --git a/applescripts/ApplicationLib.applescript b/applescripts/ApplicationLib.applescript deleted file mode 100644 index 26389ba..0000000 --- a/applescripts/ApplicationLib.applescript +++ /dev/null @@ -1,4 +0,0 @@ -on ApplicationIsRunning(appName) - tell application "System Events" to set appNameIsRunning to exists (processes where name is appName) - return appNameIsRunning -end ApplicationIsRunning \ No newline at end of file diff --git a/applescripts/ApplicationLib.scpt b/applescripts/ApplicationLib.scpt deleted file mode 100644 index 0727bfb..0000000 Binary files a/applescripts/ApplicationLib.scpt and /dev/null differ diff --git a/applescripts/ITunesTransport.applescript b/applescripts/ITunesTransport.applescript index 27baaa8..b5bcdab 100644 --- a/applescripts/ITunesTransport.applescript +++ b/applescripts/ITunesTransport.applescript @@ -1,142 +1,154 @@ -property ApplicationLib : load script POSIX file "ApplicationLib.scpt" +on ApplicationIsRunning(appName) + tell application "System Events" to set appNameIsRunning to exists (processes where name is appName) + return appNameIsRunning +end ApplicationIsRunning on IsRunning() - tell ApplicationLib - return ApplicationIsRunning("iTunes") - end tell + return ApplicationIsRunning("iTunes") end IsRunning on IsPlaying() - tell ApplicationLib - if ApplicationIsRunning("iTunes") then - tell application "iTunes" - return player state is playing - end tell - else - return false - end if - end tell + if ApplicationIsRunning("iTunes") then + tell application "iTunes" + return player state is playing + end tell + else + return false + end if end IsPlaying +on GetCurrentArt() + if IsPlaying() then + tell application "iTunes" + if not (exists current track) then return null + return (get raw data of artwork 1 of current track) + end tell + else + return "null" + end if +end GetCurrentArt + on GetCurrentTrack() - if IsPlaying() then - tell application "iTunes" - if not (exists current track) then return null - set trackName to (get name of current track) - set trackArtist to (get artist of current track) - set trackAlbum to (get album of current track) - return "{\"name\":\"" & trackName & "\",\"artist\":\"" & trackArtist & "\",\"album\":\"" & trackAlbum & "\"}" - end tell - else - return "null" - end if + if IsPlaying() then + tell application "iTunes" + if not (exists current track) then return null + set trackName to (get name of current track) + set trackArtist to (get artist of current track) + set trackAlbum to (get album of current track) + return "{\"name\":\"" & trackName & "\",\"artist\":\"" & trackArtist & "\",\"album\":\"" & trackAlbum & "\"}" + end tell + else + return "null" + end if end GetCurrentTrack on PausePlaying() - if IsRunning() then - tell application "iTunes" - pause - end tell - end if - return "{\"ok\":true}" + if IsRunning() then + tell application "iTunes" + pause + end tell + end if + return "{\"ok\":true}" end PausePlaying on StartPlaying() - tell application "iTunes" - launch - play - end tell - return GetCurrentTrack() + tell application "iTunes" + launch + play + end tell + return GetCurrentTrack() end StartPlaying on StopPlaying() - if IsRunning() then - tell application "iTunes" to stop - end if - return "{\"ok\":true}" + if IsRunning() then + tell application "iTunes" to stop + end if + return "{\"ok\":true}" end StopPlaying on PlayNextTrack() - if not IsRunning() then - tell application "iTunes" - activate - end tell - end if - tell application "iTunes" - next track - end tell - StartPlaying() + if not IsRunning() then + tell application "iTunes" + activate + end tell + end if + tell application "iTunes" + next track + end tell + StartPlaying() end PlayNextTrack on PlayPreviousTrack() - if not IsRunning() then - tell application "iTunes" - activate - end tell - end if - tell application "iTunes" - previous track - end tell - StartPlaying() + if not IsRunning() then + tell application "iTunes" + activate + end tell + end if + tell application "iTunes" + previous track + end tell + StartPlaying() end PlayPreviousTrack on FadeOut() - if IsRunning() and IsPlaying() - tell application "iTunes" - set originalVol to sound volume - set currentVol to sound volume - repeat with currentVol from sound volume to 0 by -1 - set sound volume to currentVol - delay 0.02 - end repeat - end tell - StopPlaying() - tell application "iTunes" - set sound volume to originalVol - end tell - end if + if IsRunning() and IsPlaying() then + tell application "iTunes" + set originalVol to sound volume + set currentVol to sound volume + repeat with currentVol from sound volume to 0 by -1 + set sound volume to currentVol + delay 0.02 + end repeat + end tell + StopPlaying() + tell application "iTunes" + set sound volume to originalVol + end tell + end if end FadeOut on FadeIn() - if not IsRunning() then - tell application "iTunes" - activate - end tell - end if - if not IsPlaying() then - tell application "iTunes" - set originalVol to sound volume - set currentVol to 0 - set sound volume to 0 - play - repeat with currentVol from 0 to originalVol by 1 - set sound volume to currentVol - delay 0.02 - end repeat - end tell - return GetCurrentTrack() - end if + if not IsRunning() then + tell application "iTunes" + activate + end tell + end if + if not IsPlaying() then + tell application "iTunes" + set originalVol to sound volume + set currentVol to 0 + set sound volume to 0 + play + repeat with currentVol from 0 to originalVol by 1 + set sound volume to currentVol + delay 0.02 + end repeat + end tell + return GetCurrentTrack() + end if end FadeIn on run argv - set command to item 1 of argv - if command is "currenttrack" then - return GetCurrentTrack() - else if command is "play" - StartPlaying() - else if command is "pause" - PausePlaying() - else if command is "stop" - StopPlaying() - else if command is "next" - PlayNextTrack() - else if command is "previous" - PlayPreviousTrack() - else if command is "fadeout" - FadeOut() - else if command is "fadein" - FadeIn() - else - return "{\"error\":\"Unsupported command\"}" - end if + set command to item 1 of argv + if command is "currenttrack" then + return GetCurrentTrack() + else if command is "play" then + StartPlaying() + else if command is "pause" then + PausePlaying() + else if command is "stop" then + StopPlaying() + else if command is "next" then + PlayNextTrack() + else if command is "previous" then + PlayPreviousTrack() + else if command is "fadeout" then + FadeOut() + else if command is "fadein" then + FadeIn() + else if command is "art" then + return GetCurrentArt() + else + return "{\"error\":\"Unsupported command\"}" + end if end run \ No newline at end of file diff --git a/applescripts/ITunesTransport.scpt b/applescripts/ITunesTransport.scpt index 138dcac..e1cab2e 100644 Binary files a/applescripts/ITunesTransport.scpt and b/applescripts/ITunesTransport.scpt differ diff --git a/demo.js b/demo.js index a22a2fb..ed09ba9 100644 --- a/demo.js +++ b/demo.js @@ -1,4 +1,4 @@ -var t = require('./'); +var t = require('./')(); t.on('playing', function(data){ console.dir(data);} ); t.on('paused', function(data){ console.log('paused');} ); -t.play(); \ No newline at end of file +t.play(); diff --git a/index.js b/index.js index a44806e..bbec2df 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,11 @@ var events = require('events'); var util = require('util'); var path = require('path'); -var Playback = function() { +var Playback = function(opts) { + opts = opts||{}; + if(typeof opts.fetchArtwork !== 'boolean'){ + opts.fetchArtwork = false; + } var that = this; events.EventEmitter.call(this); @@ -39,14 +43,39 @@ var Playback = function() { } else if (command === 'pause') { that.emit('paused', result); } else if (command === 'currenttrack') { - that.emit('playing', result); + playing(result); } } }); }; - that.playing = null; + function playing(track){ + + //if we dont want the artwork, just send the track data immediately + if(!opts.fetchArtwork){ + that.emit('playing', track); + return; + } + + //otherwise receive chunks of the file and send the whole image + //once we have all the parts as a field called 'art' that is base64 + var file = ""; + function emitFile(){ + var f = file.substring(10,file.length-2) + track.art = 'data:image/jpeg;base64,'+ new Buffer(f,"hex").toString('base64'); + that.emit('playing', track); + } + + that.runTransportScript('art', function(data) { + var d = data.toString(); + file+=d; + if(d.indexOf("ยป") !== -1){ + emitFile(); + } + }); + } + // Poll for changes to the current track setInterval(function() { that.runTransportScript('currenttrack', function(data) { @@ -62,12 +91,12 @@ var Playback = function() { track.album !== that.playing.album || track.name !== that.playing.name ) { that.playing = track; - that.emit('playing', track); + playing(track); } } else if (that.playing !== track) { that.playing = track; if (track) { - that.emit('playing', track); + playing(track); } else { that.emit('paused', track); } @@ -116,4 +145,6 @@ Playback.prototype.setVolume = function(volume, callback) { this.runTransportScript('setvolume ' + volume, callback); }; -module.exports = new Playback(); +module.exports = function(opts){ + return new Playback(opts); +}; diff --git a/package.json b/package.json index 7abd933..0c5e05f 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,28 @@ "email": "jwalgran@azavea.com", "url": "http://azavea.com" }, - "license": "MIT" + "license": "MIT", + "bugs": { + "url": "https://github.com/jwalgran/playback/issues" + }, + "_id": "playback@0.2.0", + "dist": { + "shasum": "c4e3c67612bb6105b52ae054541fc03ff6c9bb17", + "tarball": "http://registry.npmjs.org/playback/-/playback-0.2.0.tgz" + }, + "_from": "playback@*", + "_npmVersion": "1.3.11", + "_npmUser": { + "name": "jwalgran", + "email": "justin@walgran.com" + }, + "maintainers": [ + { + "name": "jwalgran", + "email": "justin@walgran.com" + } + ], + "directories": {}, + "_shasum": "c4e3c67612bb6105b52ae054541fc03ff6c9bb17", + "_resolved": "https://registry.npmjs.org/playback/-/playback-0.2.0.tgz" } diff --git a/readme.md b/readme.md index d691a30..1d16b40 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ Control the iTunes transport on OS X and Windows and receive track change events If you are in public, plug in some headphones or lower your volume, then: - var itunes = require('playback'); + var itunes = require('playback')({fetchArtwork:true}); itunes.on('playing', function(data){ console.dir(data);} ); itunes.on('paused', function(data){ console.log('paused');} ); itunes.play(); @@ -15,7 +15,9 @@ Pause, play, and change tracks in iTunes and watch the Node console. # methods - var itunes = require('playback') + var itunes = require('playback')(opts) + +* opts.fetchArtwork (boolean) - If true, will also fetch artwork as base64 and add the field called 'art' to the current track ## itunes.play([callback]) @@ -97,6 +99,10 @@ With [npm](https://npmjs.org) do: # release notes +## 0.3.0 + +Add ``art`` property to current track (Mac only) + ## 0.2.0 Add ``setVolume`` command (Windows only)