Mercurial > hg > audiodb
changeset 735:c625f52cf9b4
* Querier -> Bridge
* Added getSound to Bridge - fetched a slice of audio
* Initial hooks for getSound tests - needs some buffering thought.
author | mas01mj |
---|---|
date | Wed, 15 Sep 2010 15:09:13 +0000 |
parents | 35bfc91b67d3 |
children | e7cf6b9c7944 |
files | bindings/as3/build/as3bridge.swf bindings/as3/src/org/omras2/audiodb/Bridge.as bindings/as3/src/org/omras2/audiodb/Querier.as bindings/as3/src/tests/AllTests.as bindings/as3/src/tests/TestBridge.as bindings/as3/src/tests/TestQuerier.as |
diffstat | 6 files changed, 243 insertions(+), 216 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bindings/as3/src/org/omras2/audiodb/Bridge.as Wed Sep 15 15:09:13 2010 +0000 @@ -0,0 +1,160 @@ +package org.omras2.audiodb +{ + import flash.net.URLVariables; + import com.adobe.serialization.json.JSON; + + import org.omras2.audiodb.model.SearchResult; + import org.omras2.audiodb.model.Track; + + import flash.events.ErrorEvent; + import flash.events.Event; + import flash.events.EventDispatcher; + import flash.events.IOErrorEvent; + import flash.media.Sound; + import flash.net.URLLoader; + import flash.net.URLRequest; + import flash.utils.Dictionary; + + /** + * @author mikej + */ + public class Bridge extends EventDispatcher + { + private var _endpoint : String; + + private var _trackCache : Dictionary; + + public function Bridge(endpoint : String) + { + this._endpoint = endpoint; + this._trackCache = new Dictionary(); + } + + /** + * Metadata handling + */ + + public function lookup(uid : String) : void + { + if(_trackCache[uid]) + { + this.dispatchEvent(new LookupEvent(LookupEvent.COMPLETE, _trackCache[uid])); + } + else + { + var url : String = _endpoint + "/track/" + uid; + var req : URLRequest = new URLRequest(url); + var loader : URLLoader = new URLLoader(); + + try + { + loader.load(req); + } + catch(error : SecurityError) + { + var err : ErrorEvent = new ErrorEvent(ErrorEvent.ERROR); + err.text = error.message; + this.dispatchEvent(err); + } + + loader.addEventListener(IOErrorEvent.IO_ERROR, this.handleQueryError); + loader.addEventListener(Event.COMPLETE, this.handleLookupComplete); + } + } + + private function handleLookupComplete(event : Event) : void + { + var response : Object = getResponse(event); + if(validResponse(response)) + { + var track : Track = Track.createFromResponse(response['data']); + _trackCache[track.uid] = track; + this.dispatchEvent(new LookupEvent(LookupEvent.COMPLETE, track)); + } + } + + /** + * Search handling + */ + + public function search(uid : String) : void + { + var url : String = _endpoint + "/search/" + uid; + var req : URLRequest = new URLRequest(url); + var loader : URLLoader = new URLLoader(); + + try + { + loader.load(req); + } + catch(error : SecurityError) + { + var err : ErrorEvent = new ErrorEvent(ErrorEvent.ERROR); + err.text = error.message; + this.dispatchEvent(err); + } + + loader.addEventListener(IOErrorEvent.IO_ERROR, this.handleQueryError); + loader.addEventListener(Event.COMPLETE, this.handleSearchComplete); + } + + private function handleSearchComplete(event : Event) : void + { + var response : Object = getResponse(event); + if(validResponse(response)) + { + var results : Array = []; + for each(var result : Array in response['data']) + { + results.push(SearchResult.createFromResponse(result)); + } + this.dispatchEvent(new SearchEvent(LookupEvent.COMPLETE, results)); + } + } + + /** + * Playback functions + */ + + public function getSound(uid : String, start : uint, length : uint) : Sound + { + var url : String = _endpoint + "/audio/" + uid; + var req : URLRequest = new URLRequest(url); + var vars : URLVariables = new URLVariables(); + vars['start'] = start; + vars['length'] = length; + req.data = vars; + var sound : Sound = new Sound(); + sound.load(req); + return sound; + } + + /** + * Reusable parsing / error functions + */ + + private function getResponse(event : Event) : Object + { + var data : String = (event.target as URLLoader).data as String; + var response : Object = (JSON.decode(data) as Object); + return response; + } + + private function validResponse(response : Object) : Boolean + { + if(response['status'] != 'ok') + { + var err : ErrorEvent = new ErrorEvent(ErrorEvent.ERROR); + err.text = response['message']; + this.dispatchEvent(err); + return false; + } + return true; + } + + private function handleQueryError(event : IOErrorEvent) : void + { + this.dispatchEvent(event); + } + } +}
--- a/bindings/as3/src/org/omras2/audiodb/Querier.as Wed Sep 15 11:59:47 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,141 +0,0 @@ -package org.omras2.audiodb -{ - import com.adobe.serialization.json.JSON; - - import org.omras2.audiodb.model.SearchResult; - import org.omras2.audiodb.model.Track; - - import flash.events.ErrorEvent; - import flash.events.Event; - import flash.events.EventDispatcher; - import flash.events.IOErrorEvent; - import flash.net.URLLoader; - import flash.net.URLRequest; - import flash.utils.Dictionary; - - /** - * @author mikej - */ - public class Querier extends EventDispatcher - { - private var _endpoint : String; - - private var _trackCache : Dictionary; - - public function Querier(endpoint : String) - { - this._endpoint = endpoint; - this._trackCache = new Dictionary(); - } - - /** - * Metadata handling - */ - - public function lookup(uid : String) : void - { - if(_trackCache[uid]) - { - this.dispatchEvent(new LookupEvent(LookupEvent.COMPLETE, _trackCache[uid])); - } - else - { - var url : String = _endpoint + "/track/" + uid; - var req : URLRequest = new URLRequest(url); - var loader : URLLoader = new URLLoader(); - - try - { - loader.load(req); - } - catch(error : SecurityError) - { - var err : ErrorEvent = new ErrorEvent(ErrorEvent.ERROR); - err.text = error.message; - this.dispatchEvent(err); - } - - loader.addEventListener(IOErrorEvent.IO_ERROR, this.handleQueryError); - loader.addEventListener(Event.COMPLETE, this.handleLookupComplete); - } - } - - private function handleLookupComplete(event : Event) : void - { - var response : Object = getResponse(event); - if(validResponse(response)) - { - var track : Track = Track.createFromResponse(response['data']); - _trackCache[track.uid] = track; - this.dispatchEvent(new LookupEvent(LookupEvent.COMPLETE, track)); - } - } - - /** - * Search handling - */ - - public function search(uid : String) : void - { - var url : String = _endpoint + "/search/" + uid; - var req : URLRequest = new URLRequest(url); - var loader : URLLoader = new URLLoader(); - - try - { - loader.load(req); - } - catch(error : SecurityError) - { - var err : ErrorEvent = new ErrorEvent(ErrorEvent.ERROR); - err.text = error.message; - this.dispatchEvent(err); - } - - loader.addEventListener(IOErrorEvent.IO_ERROR, this.handleQueryError); - loader.addEventListener(Event.COMPLETE, this.handleSearchComplete); - } - - private function handleSearchComplete(event : Event) : void - { - var response : Object = getResponse(event); - if(validResponse(response)) - { - var results : Array = []; - for each(var result : Array in response['data']) - { - results.push(SearchResult.createFromResponse(result)); - } - this.dispatchEvent(new SearchEvent(LookupEvent.COMPLETE, results)); - } - } - - /** - * Reusable parsing / error functions - */ - - private function getResponse(event : Event) : Object - { - var data : String = (event.target as URLLoader).data as String; - var response : Object = (JSON.decode(data) as Object); - return response; - } - - private function validResponse(response : Object) : Boolean - { - if(response['status'] != 'ok') - { - var err : ErrorEvent = new ErrorEvent(ErrorEvent.ERROR); - err.text = response['message']; - this.dispatchEvent(err); - return false; - } - return true; - } - - private function handleQueryError(event : IOErrorEvent) : void - { - this.dispatchEvent(event); - } - } -}
--- a/bindings/as3/src/tests/AllTests.as Wed Sep 15 11:59:47 2010 +0000 +++ b/bindings/as3/src/tests/AllTests.as Wed Sep 15 15:09:13 2010 +0000 @@ -12,9 +12,10 @@ { super(); - addTest(new TestQuerier("testLookupSuccess")); - addTest(new TestQuerier("testLookupFail")); - addTest(new TestQuerier("testSearchSuccess")); + addTest(new TestBridge("testLookupSuccess")); + addTest(new TestBridge("testLookupFail")); + addTest(new TestBridge("testSearchSuccess")); + addTest(new TestBridge("testPlay")); } } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bindings/as3/src/tests/TestBridge.as Wed Sep 15 15:09:13 2010 +0000 @@ -0,0 +1,79 @@ +package tests +{ + import asunit.framework.TestCase; + + import org.omras2.audiodb.Bridge; + import org.omras2.audiodb.LookupEvent; + import org.omras2.audiodb.SearchEvent; + import org.omras2.audiodb.model.SearchResult; + + import flash.events.ErrorEvent; + import flash.media.Sound; + + /** + * @author mikej + */ + public class TestBridge extends TestCase + { + private var _bridge : Bridge; + + public function TestBridge(testMethod : String) + { + super(testMethod); + } + + override protected function setUp() : void + { + super.setUp(); + this._bridge = new Bridge("http://harrison.doc.gold.ac.uk:8080"); + } + + public function testLookupSuccess() : void + { + var handler : Function = addAsync(handleLookupSuccessComplete, 2000); + _bridge.addEventListener(LookupEvent.COMPLETE, handler); + _bridge.lookup("AWAL1000"); + } + + public function testLookupFail() : void + { + var handler : Function = addAsync(handleLookupFailComplete, 2000); + _bridge.addEventListener(ErrorEvent.ERROR, handler); + _bridge.lookup("AWAL10000"); + } + + private function handleLookupFailComplete(event : ErrorEvent) : void + { + assertEquals(event.text, 'Invalid key'); + } + + protected function handleLookupSuccessComplete(event : LookupEvent) : void + { + assertEquals(event.track.uid, 'AWAL1000'); + assertEquals(event.track.artist, 'Moscow Drive'); + assertEquals(event.track.seconds, '221000'); + } + + public function testSearchSuccess() : void + { + var handler : Function = addAsync(handleSearchSuccessComplete, 50000); + _bridge.addEventListener(SearchEvent.COMPLETE, handler); + _bridge.search("AWAL1000"); + } + + protected function handleSearchSuccessComplete(event : SearchEvent) : void + { + assertEquals(20, event.results.length); + var firstMatch : SearchResult = (event.results[0] as SearchResult); + assertEquals("AWAL1000", firstMatch.uid); + } + + public function testPlay() : void + { + var sound : Sound = _bridge.getSound("AWAL1000", 10, 5); + trace("Length: "+sound.length); + trace(sound.bytesLoaded); + trace(sound.bytesTotal); + } + } +}
--- a/bindings/as3/src/tests/TestQuerier.as Wed Sep 15 11:59:47 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -package tests -{ - import asunit.framework.TestCase; - - import org.omras2.audiodb.LookupEvent; - import org.omras2.audiodb.Querier; - import org.omras2.audiodb.SearchEvent; - import org.omras2.audiodb.model.SearchResult; - - import flash.events.ErrorEvent; - - /** - * @author mikej - */ - public class TestQuerier extends TestCase - { - private var _querier : Querier; - - public function TestQuerier(testMethod : String) - { - super(testMethod); - } - - override protected function setUp() : void - { - super.setUp(); - this._querier = new Querier("http://127.0.0.1:8080"); - } - - public function testLookupSuccess() : void - { - var handler : Function = addAsync(handleLookupSuccessComplete, 2000); - _querier.addEventListener(LookupEvent.COMPLETE, handler); - _querier.lookup("AWAL1000"); - } - - public function testLookupFail() : void - { - var handler : Function = addAsync(handleLookupFailComplete, 2000); - _querier.addEventListener(ErrorEvent.ERROR, handler); - _querier.lookup("AWAL10000"); - } - - private function handleLookupFailComplete(event : ErrorEvent) : void - { - assertEquals(event.text, 'Invalid key'); - } - - protected function handleLookupSuccessComplete(event : LookupEvent) : void - { - assertEquals(event.track.uid, 'AWAL1000'); - assertEquals(event.track.artist, 'Moscow Drive'); - assertEquals(event.track.seconds, '221000'); - } - - public function testSearchSuccess() : void - { - var handler : Function = addAsync(handleSearchSuccessComplete, 10000); - _querier.addEventListener(SearchEvent.COMPLETE, handler); - _querier.search("AWAL1000"); - } - - protected function handleSearchSuccessComplete(event : SearchEvent) : void - { - assertEquals(20, event.results.length); - var firstMatch : SearchResult = (event.results[0] as SearchResult); - assertEquals("AWAL1000", firstMatch.uid); - assertEquals(0, firstMatch.ipos); - assertEquals(0, firstMatch.qpos); - } - } -}