<commit_before>package lime.audio;


import haxe.io.Bytes;
import lime.audio.openal.AL;
import lime.system.System;
import lime.utils.ByteArray;
import lime.utils.Float32Array;

#if (js && html5)
import js.html.Audio;
#elseif flash
import flash.media.Sound;
#elseif lime_console
import lime.audio.fmod.Sound;
#end


class AudioBuffer {
	
	
	public var bitsPerSample:Int;
	public var channels:Int;
	public var data:ByteArray;
	public var id:UInt;
	public var sampleRate:Int;
	
	#if (js && html5)
	public var src:Audio;
	#elseif flash
	public var src:Sound;
	#elseif lime_console
	public var src:Sound;
	#else
	public var src:Dynamic;
	#end
	
	
	public function new () {
		
		id = 0;
		
	}
	
	
	public function dispose ():Void {
		
		#if lime_console

			src.release ();
		
		#else

			// TODO

		#end
		
	}
	
	
	public static function fromBytes (bytes:ByteArray):AudioBuffer {
		
		#if lime_console

			trace ("not implemented");
/*
			var sound:Sound = Sound.fromBytes (bytes);

			if (sound.valid) {

				var audioBuffer = new AudioBuffer ();
				audioBuffer.bitsPerSample = 0;
				audioBuffer.channels = 0;
				audioBuffer.data = null;
				audioBuffer.sampleRate = 0;
				audioBuffer.src = sound;
				return audioBuffer;

			}
*/
	
		#elseif (cpp || neko || nodejs)
			
			var data = lime_audio_load (bytes);
			
			if (data != null) {
				
				var audioBuffer = new AudioBuffer ();
				audioBuffer.bitsPerSample = data.bitsPerSample;
				audioBuffer.channels = data.channels;
				audioBuffer.data = ByteArray.fromBytes (@:privateAccess new Bytes (data.data.length, data.data.b));
				audioBuffer.sampleRate = data.sampleRate;
				return audioBuffer;
				
			}
			
		#end
		
		return null;
		
	}
	
	
	public static function fromFile (path:String):AudioBuffer {
		
		#if lime_console

			var sound:Sound = Sound.fromFile (path);

			if (sound.valid) {
	
				var audioBuffer = new AudioBuffer ();
				audioBuffer.bitsPerSample = 0;
				audioBuffer.channels = 0;
				audioBuffer.data = null;
				audioBuffer.sampleRate = 0;
				audioBuffer.src = sound;
				return audioBuffer;
	
			}	

		#elseif (cpp || neko || nodejs)
			
			var data = lime_audio_load (path);
			
			if (data != null) {
				
				var audioBuffer = new AudioBuffer ();
				audioBuffer.bitsPerSample = data.bitsPerSample;
				audioBuffer.channels = data.channels;
				audioBuffer.data = ByteArray.fromBytes (@:privateAccess new Bytes (data.data.length, data.data.b));
				audioBuffer.sampleRate = data.sampleRate;
				return audioBuffer;
				
			}
			
		#end
		
		return null;
		
	}
	
	
	public static function fromURL (url:String, handler:AudioBuffer->Void):Void {
		
		// TODO
		
	}
	
	
	#if (cpp || neko || nodejs)
	private static var lime_audio_load:Dynamic = System.load ("lime", "lime_audio_load", 1);
	#end
	
	
}<commit_msg>Implement AudioBuffer.fromURL<commit_after>package lime.audio;


import haxe.io.Bytes;
import lime.audio.openal.AL;
import lime.net.URLLoader;
import lime.net.URLRequest;
import lime.system.System;
import lime.utils.ByteArray;
import lime.utils.Float32Array;

#if (js && html5)
import js.html.Audio;
#elseif flash
import flash.media.Sound;
#elseif lime_console
import lime.audio.fmod.Sound;
#end


class AudioBuffer {
	
	
	public var bitsPerSample:Int;
	public var channels:Int;
	public var data:ByteArray;
	public var id:UInt;
	public var sampleRate:Int;
	
	#if (js && html5)
	public var src:Audio;
	#elseif flash
	public var src:Sound;
	#elseif lime_console
	public var src:Sound;
	#else
	public var src:Dynamic;
	#end
	
	
	public function new () {
		
		id = 0;
		
	}
	
	
	public function dispose ():Void {
		
		#if lime_console
		src.release ();
		#end
		
	}
	
	
	public static function fromBytes (bytes:ByteArray):AudioBuffer {
		
		#if lime_console
		
		openfl.Lib.notImplemented ("Sound.fromBytes");
		
		/*
		var sound:Sound = Sound.fromBytes (bytes);
		
		if (sound.valid) {
			
			var audioBuffer = new AudioBuffer ();
			audioBuffer.bitsPerSample = 0;
			audioBuffer.channels = 0;
			audioBuffer.data = null;
			audioBuffer.sampleRate = 0;
			audioBuffer.src = sound;
			return audioBuffer;
			
		}
		*/
		
		#elseif (cpp || neko || nodejs)
		
		var data = lime_audio_load (bytes);
		
		if (data != null) {
			
			var audioBuffer = new AudioBuffer ();
			audioBuffer.bitsPerSample = data.bitsPerSample;
			audioBuffer.channels = data.channels;
			audioBuffer.data = ByteArray.fromBytes (@:privateAccess new Bytes (data.data.length, data.data.b));
			audioBuffer.sampleRate = data.sampleRate;
			return audioBuffer;
			
		}
		
		#end
		
		return null;
		
	}
	
	
	public static function fromFile (path:String):AudioBuffer {
		
		#if lime_console
		
		var sound:Sound = Sound.fromFile (path);
		
		if (sound.valid) {
			
			var audioBuffer = new AudioBuffer ();
			audioBuffer.bitsPerSample = 0;
			audioBuffer.channels = 0;
			audioBuffer.data = null;
			audioBuffer.sampleRate = 0;
			audioBuffer.src = sound;
			return audioBuffer;
			
		}
		
		#elseif (cpp || neko || nodejs)
		
		var data = lime_audio_load (path);
		
		if (data != null) {
			
			var audioBuffer = new AudioBuffer ();
			audioBuffer.bitsPerSample = data.bitsPerSample;
			audioBuffer.channels = data.channels;
			audioBuffer.data = ByteArray.fromBytes (@:privateAccess new Bytes (data.data.length, data.data.b));
			audioBuffer.sampleRate = data.sampleRate;
			return audioBuffer;
			
		}
		
		#end
		
		return null;
		
	}
	
	
	public static function fromURL (url:String, handler:AudioBuffer->Void):Void {
		
		if (url != null && url.indexOf ("http://") == -1 && url.indexOf ("https://") == -1) {
			
			handler (AudioBuffer.fromFile (url));
			
		} else {
			
			// TODO: Support streaming sound
			
			var loader = new URLLoader ();
			
			loader.onComplete.add (function (_) {
				
				var bytes = Bytes.ofString (loader.data);
				handler (AudioBuffer.fromBytes (ByteArray.fromBytes (bytes)));
				
			});
			
			loader.onIOError.add (function (_, msg) {
				
				handler (null);
				
			});
			
			loader.load (new URLRequest (url));
			
		}
		
	}
	
	
	#if (cpp || neko || nodejs)
	private static var lime_audio_load:Dynamic = System.load ("lime", "lime_audio_load", 1);
	#end
	
	
}
{'fraction_non_alphanumeric': 0.11507872098685279, 'fraction_numerical': 0.004220094140561597, 'mean_word_length': 2.99870214146658, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6168689', 'n_tokens_mistral': 2514, 'n_tokens_neox': 1986, 'n_words': 493}