Permalink
Browse files

1.7.4 version: ability to set/change volume and pan. No GUI yet.

  • Loading branch information...
1 parent 50d66d6 commit e76ad9153166846631166fe96ea10feb161dd114 @datacompboy committed Jul 20, 2011
Showing with 170 additions and 11 deletions.
  1. +41 −1 Player.hx
  2. +15 −5 README.txt
  3. +31 −1 WavPlayer.hx
  4. +19 −0 debug.html
  5. +22 −3 index.html
  6. +42 −1 org/xiph/system/AudioSink.hx
  7. BIN wavplayer-debug.swf
  8. BIN wavplayer.swf
View
@@ -13,6 +13,7 @@
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*/
+import flash.media.SoundTransform;
// Main player class: loads stream, process it by appropriate file decoder,
// that will initialize correct sound decoder. Decoded audio samples
@@ -32,8 +33,14 @@ class Player extends flash.events.EventDispatcher {
var trigger : Null<Float>;
var pos : Null<Float>;
+ var schtr: SoundTransform;
+ public var volume(getVolume, setVolume): Float;
+ public var pan(getPan, setPan): Float;
+ public var soundTransform(getST, setST): SoundTransform;
+
public function new(?path : String) {
super();
+ schtr = new SoundTransform();
fname = path;
asink = null;
File = null;
@@ -96,7 +103,7 @@ class Player extends flash.events.EventDispatcher {
}
function initAsink() {
try {
- asink = new org.xiph.system.AudioSink(8192, true, 44100*5, trigger==null?null:Math.round(trigger*44100));
+ asink = new org.xiph.system.AudioSink(8192, true, 44100*5, trigger==null?null:Math.round(trigger*44100), schtr);
asink.addEventListener(PlayerEvent.PLAYING, playingEvent);
asink.addEventListener(PlayerEvent.STOPPED, stoppedEvent);
} catch (error : Dynamic) {
@@ -106,6 +113,39 @@ class Player extends flash.events.EventDispatcher {
}
}
+ public function setVolume(volume: Float): Float {
+ this.schtr.volume=volume;
+ trace("setVolume("+volume+")");
+ this.soundTransform = this.soundTransform; // Apply changes
+ return volume;
+ }
+
+ public function getVolume(): Float {
+ return this.schtr.volume;
+ }
+
+ public function setPan(pan: Float): Float {
+ this.schtr.pan=pan;
+ this.soundTransform = this.soundTransform; // Apply changes
+ return this.schtr.pan;
+ }
+
+ public function getPan(): Float {
+ return this.schtr.pan;
+ }
+
+ public function setST(st: SoundTransform): SoundTransform {
+ this.schtr = st;
+ if (this.asink!=null) {
+ this.asink.soundTransform = this.schtr;
+ }
+ return this.schtr;
+ }
+
+ public function getST(): SoundTransform {
+ return this.schtr;
+ }
+
function playingEvent(event:PlayerEvent) {
dispatchEvent(new PlayerEvent(PlayerEvent.PLAYING, event.position));
}
View
@@ -50,17 +50,27 @@ Flash Interface customize:
major_tick_color: (default 0x000066) Color of major tick score
JS interface:
- doPlay([filename][, buffer])
+ doPlay([filename][, buffer]) or play([filename][, buffer])
start playback of given filename. if filename not given -- play last
buffer argument says minimum buffer length needed to start playback
- doStop()
+ doStop() or stop()
stop playback of current file
- doPause()
+ doPause() or pause()
pauses playback of current file
- doResume()
+ doResume() or resume()
resume playback of current file after pause. will throw error if not started
- doSeek(pos)
+ doSeek(pos) or seek()
seeks playback to position (in seconds)
+ setVolume(value) or volume(value)
+ set playback volume to specifed value. 1.0 by default
+ initial value get from flash parameter "value"
+ getVolume() or volume()
+ get playback volume
+ setPan(value) or pan(value)
+ set playback pan (-1.0 is 100% left, 0.0 is center(default), 1.0 is 100% right)
+ initial value get from flash parameter "pan"
+ getPan(value) or pan()
+ get playback pan
attachHandler(Event, Handler[, User]) -> handlerId
when Event occurs, Handler will be called, with optionally User info as first argument
detachHandler(Event, Handler[, User])
View
@@ -303,7 +303,7 @@ class WavPlayerGui_Full extends WavPlayerGui {
// Main user interface: play / stop buttons & ExternalInterface
class WavPlayer {
- static var Version = "1.7.3";
+ static var Version = "1.7.4";
static var player : Player;
static var state : String = PlayerEvent.STOPPED;
static var handlers : List<JsEventHandler>;
@@ -327,6 +327,11 @@ class WavPlayer {
lastNotifyProgress = 0;
lastNotifyLoad = 0;
+ var volume: Float = 1.0;
+ var pan: Float = 0.0;
+ if (fvs.volume != null) volume = Std.parseFloat(fvs.volume);
+ if (fvs.pan != null) pan = Std.parseFloat(fvs.pan);
+
var zoom:Float = Std.parseInt(fvs.h); zoom = (zoom>0?zoom:40.0) / 40.0;
var bg_color: Int = 0x303030;
var ready_color: Int = 0xA0A0A0;
@@ -368,6 +373,8 @@ class WavPlayer {
player.addEventListener(flash.events.ProgressEvent.PROGRESS, handleProgress);
player.addEventListener(flash.events.IOErrorEvent.IO_ERROR, handleError);
player.addEventListener(PlayerLoadEvent.LOAD, handleLoad);
+ player.volume = volume;
+ player.pan = pan;
if( !flash.external.ExternalInterface.available )
throw "External Interface not available";
@@ -388,6 +395,14 @@ class WavPlayer {
try flash.external.ExternalInterface.addCallback("doSeek",doSeek) catch( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("seek",doSeek) catch( e : Dynamic ) {};
+ try flash.external.ExternalInterface.addCallback("volume",doVolume) catch ( e : Dynamic ) {};
+ try flash.external.ExternalInterface.addCallback("setVolume",doVolume) catch ( e : Dynamic ) {};
+ try flash.external.ExternalInterface.addCallback("getVolume",doVolume) catch ( e : Dynamic ) {};
+
+ try flash.external.ExternalInterface.addCallback("pan",doPan) catch ( e : Dynamic ) {};
+ try flash.external.ExternalInterface.addCallback("setPan",doPan) catch ( e : Dynamic ) {};
+ try flash.external.ExternalInterface.addCallback("getPan",doPan) catch ( e : Dynamic ) {};
+
try flash.external.ExternalInterface.addCallback("attachHandler",doAttach) catch ( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("detachHandler",doDetach) catch ( e : Dynamic ) {};
try flash.external.ExternalInterface.addCallback("removeHandler",doRemove) catch ( e : Dynamic ) {};
@@ -487,6 +502,21 @@ class WavPlayer {
static function doSeek( ?pos: Float ) {
player.seek(pos);
}
+ static function doVolume( ?volume: Float ): Float {
+ if (volume != null) {
+ player.volume = volume;
+ }
+ trace("doVolume("+volume+")");
+ return player.volume;
+ }
+ static function doPan( ?pan: Float ): Float {
+ if (pan != null) {
+ player.pan = pan;
+ }
+ trace("doPan("+pan+")");
+ return player.pan;
+ }
+
static function doAttach( event: String, handler: String, ?user: String ) {
var id = handlerId++;
handlers.push(new JsEventHandler(id, event, handler, user));
View
@@ -17,6 +17,14 @@
var player = getPlayer('haxe');
player.doStop();
}
+function setVolume(v) {
+ var player = getPlayer('haxe');
+ log("setVolume("+player.setVolume(v)+")<br/>");
+}
+function setPan(p) {
+ var player = getPlayer('haxe');
+ log("setPan("+player.setPan(p)+")<br/>");
+}
function log(str) {
var log = document.getElementById('jsLog');
log.innerHTML += str+"<br/>";
@@ -41,6 +49,17 @@
<a href="javascript:doPlay('hl-78003330781-5627.au')">doPlay('hl-78003330781-5627.au')</a>
<a href="javascript:doStop()">doStop()</a><br/>
<input id='file' /><a href="javascript:doPlay(document.getElementById('file').value)">Play by filename</a>
+<br />
+<a href="javascript:setVolume(5.0)">Volume 500%</a>
+<a href="javascript:setVolume(2.0)">Volume 200%</a>
+<a href="javascript:setVolume(1.0)">Volume 100%</a>
+<a href="javascript:setVolume(0.5)">Volume 50%</a>
+<a href="javascript:setVolume(0.1)">Volume 10%</a>
+<a href="javascript:setVolume(0.0)">Volume 0% (mute)</a>
+<br />
+<a href="javascript:setPan(0.0)">Balance: center</a>
+<a href="javascript:setPan(-1.0)">Balance: left</a>
+<a href="javascript:setPan(1.0)">Balance: right</a>
<div id="jsLog" style="border:1px solid red"></div>
<br/>
View
@@ -17,6 +17,14 @@
var player = getPlayer('haxe');
player.doStop();
}
+function setVolume(v) {
+ var player = getPlayer('haxe');
+ player.setVolume(v);
+}
+function setPan(p) {
+ var player = getPlayer('haxe');
+ player.setPan(p);
+}
var SoundLen = 0;
var SoundPos = 0;
var Last = undefined;
@@ -40,7 +48,7 @@
SoundPos += interval;
Last = now;
}
- InfoState.innerHTML = State + "("+SoundPos.toFixed(2)+"/"+SoundLen.toFixed(2)+" sec ("+getPerc(SoundPos,SoundLen)+"%)";
+ InfoState.innerHTML = State + "("+SoundPos.toFixed(2)+"/"+SoundLen.toFixed(2)+") sec ("+getPerc(SoundPos,SoundLen)+"%)";
}
function SoundState(state, position) {
if (position != undefined) SoundPos = position;
@@ -77,9 +85,20 @@
<a href="javascript:doPlay()">doPlay()</a>
<a href="javascript:doPlay('test.gsm')">doPlay('test.gsm')</a>
-<a href="javascript:doPlay('test-vf-44100.au')">doPlay('test-vf-44100.au')</a>
-<a href="javascript:doPlay('hl-78003330781-5627.au')">doPlay('hl-78003330781-5627.au')</a>
+<a href="javascript:doPlay('test.au')">doPlay('test.au')</a>
+<a href="javascript:doPlay('direct-5683.au')">doPlay('direct-5683.au')</a>
<a href="javascript:doStop()">doStop()</a>
+<br />
+<a href="javascript:setVolume(5.0)">Volume 500%</a>
+<a href="javascript:setVolume(2.0)">Volume 200%</a>
+<a href="javascript:setVolume(1.0)">Volume 100%</a>
+<a href="javascript:setVolume(0.5)">Volume 50%</a>
+<a href="javascript:setVolume(0.1)">Volume 10%</a>
+<a href="javascript:setVolume(0.0)">Volume 0% (mute)</a>
+<br />
+<a href="javascript:setPan(0.0)">Balance: center</a>
+<a href="javascript:setPan(-1.0)">Balance: left</a>
+<a href="javascript:setPan(1.0)">Balance: right</a>
<br/>
<div id="InfoFile"></div>
@@ -22,6 +22,7 @@ import flash.Vector;
import flash.media.Sound;
import flash.media.SoundChannel;
+import flash.media.SoundTransform;
import flash.events.SampleDataEvent;
@@ -36,11 +37,15 @@ class AudioSink extends flash.events.EventDispatcher {
var fill : Bool;
var size : Int;
public var pos : Float;
+ public var volume(getVolume, setVolume): Float;
+ public var pan(getPan, setPan): Float;
+ public var soundTransform(getST, setST): SoundTransform;
var s : Sound;
var sch : SoundChannel;
+ var schtr : SoundTransform;
- public function new(chunk_size : Int, fill = true, bufsize = 0, ?trigger : Int) {
+ public function new(chunk_size : Int, fill = true, bufsize = 0, ?trigger : Int, ?st: SoundTransform) {
super();
size = chunk_size;
this.fill = fill;
@@ -56,9 +61,43 @@ class AudioSink extends flash.events.EventDispatcher {
pos = 0.0;
s = new Sound();
s.addEventListener("sampleData", _data_cb);
+ if (st == null) schtr = new SoundTransform();
+ else schtr = st;
sch = null;
}
+ public function setVolume(volume: Float): Float {
+ this.schtr.volume=volume;
+ this.soundTransform = this.soundTransform;
+ return volume;
+ }
+
+ public function getVolume(): Float {
+ return this.schtr.volume;
+ }
+
+ public function setPan(pan: Float): Float {
+ this.schtr.pan=pan;
+ this.soundTransform = this.soundTransform;
+ return this.schtr.pan;
+ }
+
+ public function getPan(): Float {
+ return this.schtr.pan;
+ }
+
+ public function setST(st: SoundTransform): SoundTransform {
+ this.schtr = st;
+ if (this.sch!=null) {
+ this.sch.soundTransform = this.schtr;
+ }
+ return this.schtr;
+ }
+
+ public function getST(): SoundTransform {
+ return this.schtr;
+ }
+
public function play(?position: Float) : Bool {
if (sch!=null) return false;
triggered = true;
@@ -88,6 +127,8 @@ class AudioSink extends flash.events.EventDispatcher {
trace("playing");
sch = s.play();
+ sch.soundTransform = this.schtr;
+ trace("SoundTransform volume = "+this.schtr.volume);
sch.addEventListener(flash.events.Event.SOUND_COMPLETE, soundCompleteHandler);
dispatchEvent(new PlayerEvent(PlayerEvent.PLAYING, pos));
return true;
View
Binary file not shown.
View
Binary file not shown.

0 comments on commit e76ad91

Please sign in to comment.