<commit_before>import WaveAudio				= require("awayjs-core/lib/data/WaveAudio");
import IAsset					= require("awayjs-core/lib/library/IAsset");
import URLLoaderDataFormat		= require("awayjs-core/lib/net/URLLoaderDataFormat");
import ParserBase				= require("awayjs-core/lib/parsers/ParserBase");
import ParserUtils				= require("awayjs-core/lib/parsers/ParserUtils");
import ByteArray				= require("awayjs-core/lib/utils/ByteArray");

class WaveAudioParser extends ParserBase
{
	private _loadingImage:boolean;
	private _htmlAudioElement:HTMLAudioElement;

	constructor()
	{
		super(URLLoaderDataFormat.BLOB);
	}

	public static supportsType(extension:string):boolean
	{

		extension = extension.toLowerCase();
		return extension == "wav" || extension == "mp3" || extension == "ogg";//|| extension == "bmp";//|| extension == "atf";

	}

	public static supportsData(data:any):boolean
	{

		if (data instanceof HTMLAudioElement)
			return true;

		if (!(data instanceof ByteArray))
			return false;

		var ba:ByteArray = <ByteArray> data;
		var filetype : string = WaveAudioParser.parseFileType(ba);
		return filetype? true : false;

	}

	public _pProceedParsing():boolean
	{

		var asset:WaveAudio;

		if (this._loadingImage) {
			return ParserBase.MORE_TO_PARSE;
		}
		else if (this._htmlAudioElement) {
			asset = ParserUtils.audioToWaveAudio(this._htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);
		}
		else if (this.data instanceof HTMLAudioElement) {// Parse HTMLImageElement
			var htmlAudioElement:HTMLAudioElement = <HTMLAudioElement> this.data;
			asset = ParserUtils.audioToWaveAudio(htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);
		}
		else if (this.data instanceof ByteArray) { // Parse a ByteArray
			var ba:ByteArray = this.data;
			var filetype = WaveAudioParser.parseFileType(ba);
			var htmlAudioElement:HTMLAudioElement = ParserUtils.byteArrayToAudio(ba, filetype);
			asset = ParserUtils.audioToWaveAudio(htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);
		}
		else if (this.data instanceof ArrayBuffer) {// Parse an ArrayBuffer
			var filetype = WaveAudioParser.parseFileType(this.data.arraybytes);
			this._htmlAudioElement = ParserUtils.arrayBufferToAudio(this.data, filetype);

			asset = ParserUtils.audioToWaveAudio(this._htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);

		}
		else if (this.data instanceof Blob) { // Parse a Blob

			this._htmlAudioElement = ParserUtils.blobToAudio(this.data);

			this._htmlAudioElement.onload = (event) => this.onLoadComplete(event);
			this._loadingImage = true;

			return ParserBase.MORE_TO_PARSE;
		}

		this._pContent = asset;

		return ParserBase.PARSING_DONE;

	}

	public onLoadComplete(event)
	{
		this._loadingImage = false;
	}

	private static parseFileType(ba:ByteArray):string
	{
		ba.position = 0;

		ba.position = 0;
		if (ba.readUnsignedShort() & 0xFFE0) {
			return 'mp3'; // test for MP3 syncword
		}

		ba.position = 0;
		if (ba.readUTFBytes(4) == 'RIFF')
			return 'wav';

		ba.position = 0;
		if (ba.readUTFBytes(4) == 'OggS')
			return 'ogg';

		ba.position = 0;

		return null;
	}
}<commit_msg>fix for hanging wave parser and for when error is encountered loading<commit_after>import WaveAudio				= require("awayjs-core/lib/data/WaveAudio");
import IAsset					= require("awayjs-core/lib/library/IAsset");
import URLLoaderDataFormat		= require("awayjs-core/lib/net/URLLoaderDataFormat");
import ParserBase				= require("awayjs-core/lib/parsers/ParserBase");
import ParserUtils				= require("awayjs-core/lib/parsers/ParserUtils");
import ByteArray				= require("awayjs-core/lib/utils/ByteArray");

class WaveAudioParser extends ParserBase
{
	private _loadingImage:boolean;
	private _htmlAudioElement:HTMLAudioElement;

	constructor()
	{
		super(URLLoaderDataFormat.BLOB);
	}

	public static supportsType(extension:string):boolean
	{

		extension = extension.toLowerCase();
		return extension == "wav" || extension == "mp3" || extension == "ogg";

	}

	public static supportsData(data:any):boolean
	{

		if (data instanceof HTMLAudioElement)
			return true;

		if (!(data instanceof ByteArray))
			return false;

		var ba:ByteArray = <ByteArray> data;
		var filetype : string = WaveAudioParser.parseFileType(ba);
		return filetype? true : false;

	}

	public _pProceedParsing():boolean
	{

		var asset:WaveAudio;

		if (this._loadingImage) {
			return ParserBase.MORE_TO_PARSE;
		}
		else if (this._htmlAudioElement) {
			asset = ParserUtils.audioToWaveAudio(this._htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);
		}
		else if (this.data instanceof HTMLAudioElement) {// Parse HTMLImageElement
			var htmlAudioElement:HTMLAudioElement = <HTMLAudioElement> this.data;
			asset = ParserUtils.audioToWaveAudio(htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);
		}
		else if (this.data instanceof ByteArray) { // Parse a ByteArray
			var ba:ByteArray = this.data;
			var filetype = WaveAudioParser.parseFileType(ba);
			var htmlAudioElement:HTMLAudioElement = ParserUtils.byteArrayToAudio(ba, filetype);
			asset = ParserUtils.audioToWaveAudio(htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);
		}
		else if (this.data instanceof ArrayBuffer) {// Parse an ArrayBuffer
			var filetype = WaveAudioParser.parseFileType(this.data.arraybytes);
			this._htmlAudioElement = ParserUtils.arrayBufferToAudio(this.data, filetype);

			asset = ParserUtils.audioToWaveAudio(this._htmlAudioElement);
			this._pFinalizeAsset(<IAsset> asset, this._iFileName);

		}
		else if (this.data instanceof Blob) { // Parse a Blob

			this._htmlAudioElement = ParserUtils.blobToAudio(this.data);

			this._htmlAudioElement.onloadeddata = (event) => this.onLoadComplete(event);
			this._htmlAudioElement.onerror = (event) => this.onError(event);
			this._loadingImage = true;

			return ParserBase.MORE_TO_PARSE;
		}

		this._pContent = asset;

		return ParserBase.PARSING_DONE;

	}

	public onLoadComplete(event)
	{
		this._loadingImage = false;
	}

	public onError(event)
	{
		console.log(event.target.error);
		this._loadingImage = false;
	}

	private static parseFileType(ba:ByteArray):string
	{
		ba.position = 0;

		ba.position = 0;
		if (ba.readUnsignedShort() & 0xFFE0) {
			return 'mp3'; // test for MP3 syncword
		}

		ba.position = 0;
		if (ba.readUTFBytes(4) == 'RIFF')
			return 'wav';

		ba.position = 0;
		if (ba.readUTFBytes(4) == 'OggS')
			return 'ogg';

		ba.position = 0;

		return null;
	}
}
{'fraction_non_alphanumeric': 0.11804178740277566, 'fraction_numerical': 0.0036602104621015707, 'mean_word_length': 4.57179269328802, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2244031', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2128, 'n_words': 423}