UNPKG

openfl

Version:

A fast, productive library for 2D cross-platform development.

179 lines (162 loc) 6.41 kB
// Class: openfl.media.SoundChannel var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this $global.Object.defineProperty(exports, "__esModule", {value: true}); var __map_reserved = {}; // Imports var $hxClasses = require("./../../hxClasses_stub").default; var $hxEnums = require("./../../hxEnums_stub").default; var $import = require("./../../import_stub").default; var $bind = require("./../../bind_stub").default; var $extend = require("./../../extend_stub").default; function openfl_events_EventDispatcher() {return require("./../../openfl/events/EventDispatcher");} function openfl_media_SoundMixer() {return require("./../../openfl/media/SoundMixer");} function openfl_events_SampleDataEvent() {return require("./../../openfl/events/SampleDataEvent");} function openfl_Lib() {return require("./../../openfl/Lib");} function openfl_events_Event() {return require("./../../openfl/events/Event");} function Reflect() {return require("./../../Reflect");} function Std() {return require("./../../Std");} function openfl_utils__$ByteArray_ByteArray_$Impl_$() {return require("./../../openfl/utils/_ByteArray/ByteArray_Impl_");} function _$UInt_UInt_$Impl_$() {return require("./../../_UInt/UInt_Impl_");} function openfl_media_SoundTransform() {return require("./../../openfl/media/SoundTransform");} // Constructor var SoundChannel = function(sound,audioSource,soundTransform) { this.__firstRun = true; (openfl_events_EventDispatcher().default).call(this,this); this.__sound = sound; this.leftPeak = 1; this.rightPeak = 1; if(soundTransform != null) { this.__soundTransform = soundTransform; } else { this.__soundTransform = new (openfl_media_SoundTransform().default)(); } this.__initAudioSource(audioSource); (openfl_media_SoundMixer().default).__registerSoundChannel(this); } // Meta SoundChannel.__name__ = "openfl.media.SoundChannel"; SoundChannel.__isInterface__ = false; SoundChannel.__super__ = (openfl_events_EventDispatcher().default); SoundChannel.prototype = $extend((openfl_events_EventDispatcher().default).prototype, { stop: function() { (openfl_media_SoundMixer().default).__unregisterSoundChannel(this); if(!this.__isValid) { return; } if(this.__processor != null) { this.__processor.disconnect(); this.__processor.onaudioprocess = null; this.__processor = null; } this.__audioSource.stop(); this.__dispose(); }, __dispose: function() { if(!this.__isValid) { return; } this.__audioSource.onComplete.remove($bind(this,this.audioSource_onComplete)); this.__audioSource.dispose(); this.__audioSource = null; this.__isValid = false; }, __startSampleData: function() { var _gthis = this; var webAudioContext = this.__sound.__webAudioContext; if(webAudioContext != null) { this.__sampleDataEvent = new (openfl_events_SampleDataEvent().default)("sampleData"); this.__sound.dispatchEvent(this.__sampleDataEvent); var bufferSize = this.__sampleDataEvent.getBufferSize(); if(bufferSize == 0) { (openfl_Lib().default).setTimeout(function() { _gthis.stop(); _gthis.dispatchEvent(new (openfl_events_Event().default)("soundComplete")); },1); } else { this.__processor = webAudioContext.createScriptProcessor(bufferSize,0,2); this.__processor.connect(webAudioContext.destination); this.__processor.onaudioprocess = $bind(this,this.onSample); (Reflect().default).callMethod(webAudioContext,(Reflect().default).field(webAudioContext,"resume"),[]); } } }, __updateTransform: function() { this.set_soundTransform(this.get_soundTransform()); }, __initAudioSource: function(audioSource) { this.__audioSource = audioSource; if(this.__audioSource == null) { return; } this.__audioSource.onComplete.add($bind(this,this.audioSource_onComplete)); this.__isValid = true; this.__audioSource.play(); }, get_position: function() { if(!this.__isValid) { return 0; } return this.__audioSource.get_currentTime() + this.__audioSource.offset; }, set_position: function(value) { if(!this.__isValid) { return 0; } this.__audioSource.set_currentTime((Std().default).int(value) - this.__audioSource.offset); return value; }, get_soundTransform: function() { return this.__soundTransform.clone(); }, set_soundTransform: function(value) { if(value != null) { this.__soundTransform.pan = value.pan; this.__soundTransform.volume = value.volume; var pan = (openfl_media_SoundMixer().default).__soundTransform.pan + this.__soundTransform.pan; if(pan < -1) { pan = -1; } if(pan > 1) { pan = 1; } var volume = (openfl_media_SoundMixer().default).__soundTransform.volume * this.__soundTransform.volume; if(this.__isValid) { this.__audioSource.set_gain(volume); var position = this.__audioSource.get_position(); position.x = pan; position.z = -1 * Math.sqrt(1 - Math.pow(pan,2)); this.__audioSource.set_position(position); return value; } } return value; }, audioSource_onComplete: function() { (openfl_media_SoundMixer().default).__unregisterSoundChannel(this); this.__dispose(); this.dispatchEvent(new (openfl_events_Event().default)("soundComplete")); }, onSample: function(event) { var hasSampleData = false; if(this.__firstRun) { hasSampleData = true; this.__firstRun = false; } else { (openfl_utils__$ByteArray_ByteArray_$Impl_$().default).set_length(this.__sampleDataEvent.data,0); this.__sound.dispatchEvent(this.__sampleDataEvent); hasSampleData = (_$UInt_UInt_$Impl_$().default).gt((openfl_utils__$ByteArray_ByteArray_$Impl_$().default).get_length(this.__sampleDataEvent.data),0); } if(hasSampleData) { this.__sampleDataEvent.getSamples(event); } else { this.stop(); this.dispatchEvent(new (openfl_events_Event().default)("soundComplete")); } } }); SoundChannel.prototype.__class__ = SoundChannel.prototype.constructor = $hxClasses["openfl.media.SoundChannel"] = SoundChannel; // Init Object.defineProperties(SoundChannel.prototype,{ position : { get : function () { return this.get_position (); }, set : function (v) { return this.set_position (v); }}, soundTransform : { get : function () { return this.get_soundTransform (); }, set : function (v) { return this.set_soundTransform (v); }}}); // Statics // Export exports.default = SoundChannel;