note
	description: "[
							A sound source in a 3d environment.
							
							If a stereo sound is queued in the source, the 3d environment
							is not applied.
							
							16 bit buffers are signed and 0 is the silent position
							8 bit buffers are unsigned and 128 is the silent position
							for stereo formats the left channel comes first
		
	]"
	author: "Louis Marchand"
	date: "Tue, 07 Apr 2015 01:15:20 +0000"
	revision: "2.0"

class 
	AUDIO_SOURCE

create {AUDIO_LIBRARY_CONTROLLER}
	make

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	initialize_buffers
			-- Create the internal sound_al_buffer
		local
			l_are_buffers: BOOLEAN
			i: INTEGER_32
			l_sound_al_buffer_c: ANY
		do
			l_sound_al_buffer_c := sound_al_buffer.to_c
			{AUDIO_EXTERNAL}.al_gen_buffers (Buffers_count, $l_sound_al_buffer_c.to_pointer)
			from
				l_are_buffers := True
				i := sound_al_buffer.lower
			until
				i > sound_al_buffer.upper or not l_are_buffers
			loop
				l_are_buffers := l_are_buffers and {AUDIO_EXTERNAL}.al_is_buffer (sound_al_buffer.at (i))
				i := i + 1
			end
			if not l_are_buffers then
				read_al_error ("Cannot generate buffer for audio source.")
			end
		end

	make (a_buffer_size: INTEGER_32)
			-- Initialization for Current using `a_buffer_size as the size of an audio buffer
			-- Note: Shorter a_buffer_size takes less memory but there is more
			-- possibility that the buffer empty itself before the application have te time to fill it.
		require
			make_source_sound_enable: Audio_library.is_playback_enable
		local
			l_sources: ARRAY [NATURAL_32]
			l_source_c: ANY
		do
			has_been_stop := True
			create l_sources.make_filled (0, 1, 1)
			create g_mutex.make
			create internal_sound_queued.make
			create sound_al_buffer.make_filled (0, 0, Buffers_count - 1)
			l_source_c := l_sources.to_c
			clear_error
			{AUDIO_EXTERNAL}.al_gen_sources (1, $l_source_c.to_pointer)
			index := l_sources.at (1)
			if {AUDIO_EXTERNAL}.al_is_source (index) then
				buffer_size := a_buffer_size
				buffer_tail := 0
				buffer_head := 0
				initialize_buffers
				if not has_error then
					is_open := True
					set_position (0.0, 0.0, 0.0)
					if not has_error then
						set_direction (0.0, 0.0, 0.0)
						if not has_error then
							set_direction (0.0, 0.0, 0.0)
						end
					end
					is_open := not has_error
					temp_buffer := temp_buffer.memory_alloc (buffer_size)
					is_thread_safe := False
				end
			else
				read_al_error ("Cannot generate a new source")
			end
		ensure
			is_open: not has_error implies is_open
		end
	
feature -- Access

	Audio_library: AUDIO_LIBRARY_CONTROLLER
			-- Access to the audio library internal fonctionnality
			-- (from AUDIO_LIBRARY_SHARED)
		once ("PROCESS")
			create Result.make
			if attached internal_audio_library as la_audio_library then
				Result := la_audio_library
			else
				create Result.make
			end
		end

	buffer_size: INTEGER_32
			-- The buffer size for the sound streaming (default is 65536). Allocate too little memory to buffer can cause sound to stop before finishing.

	direction: TUPLE [x: REAL_32; y: REAL_32; z: REAL_32]
			-- Get the source direction. Meaning where the source object is looking at in the 3D environment.
		require
			source_is_open: is_open
		do
			Result := params_3_float ({AUDIO_EXTERNAL}.al_direction)
		end

	gain: REAL_32 assign set_gain
			-- The gain (volume) of Current. The gain will always be a REAL between 0 and 1.
			-- If the gain is set at 0, Current is mute. If the gain is set at 1, it is at it's max volume.
		require
			source_is_open: is_open
		do
			Result := param_float_c ({AUDIO_EXTERNAL}.al_gain)
		end

	generating_type: TYPE [detachable AUDIO_SOURCE]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end

	has_error: BOOLEAN
			-- Is the library has generate an error
			-- (from GAME_ERROR_MANAGER)

	is_initial: BOOLEAN
			-- True if the sound source has been rewinded.
		require
			source_is_open: is_open
		do
			Result := (param_int_c ({AUDIO_EXTERNAL}.al_source_state) = {AUDIO_EXTERNAL}.al_initial)
		end

	is_open: BOOLEAN
			-- Is Current open and ready to be used (without initialization error)

	is_pause: BOOLEAN
			-- True if the sound source is currently on pause.
		require
			source_is_open: is_open
		do
			Result := (param_int_c ({AUDIO_EXTERNAL}.al_source_state) = {AUDIO_EXTERNAL}.al_paused)
		end

	is_playing: BOOLEAN
			-- True if the sound source is currently playing.
		require
			source_is_open: is_open
		do
			Result := (param_int_c ({AUDIO_EXTERNAL}.al_source_state) = {AUDIO_EXTERNAL}.al_playing) or (not has_been_stop and is_al_stop)
		end

	is_stop: BOOLEAN
			-- True if the sound source has been stoped.
		require
			source_is_open: is_open
		do
			Result := has_been_stop and is_al_stop
		end

	last_error: READABLE_STRING_GENERAL
			-- The last error generate by the library
			-- (from AUDIO_OPENAL_ERROR_MANAGER)
		do
			if is_manual_error then
				Result := Precursor {GAME_ERROR_MANAGER}
			elseif last_al_error_code /= {AUDIO_EXTERNAL}.al_no_error then
				if last_al_error_code = {AUDIO_EXTERNAL}.al_invalid_enum then
					Result := "An invalid enum value was passed to an OpenAL function."
				elseif last_al_error_code = {AUDIO_EXTERNAL}.al_invalid_value then
					Result := "An invalid value was passed to an OpenAL function."
				elseif last_al_error_code = {AUDIO_EXTERNAL}.al_invalid_operation then
					Result := "The requested operation is not valid."
				elseif last_al_error_code = {AUDIO_EXTERNAL}.al_invalid_name then
					Result := "A bad name (ID) was passed to an OpenAL function."
				elseif last_al_error_code = {AUDIO_EXTERNAL}.al_out_of_memory then
					Result := "The requested operation resulted in OpenAL running out of memory."
				else
					Result := "Unmanaged OpenAL error."
				end
			elseif last_alc_error_code /= {AUDIO_EXTERNAL}.alc_no_error then
				if last_alc_error_code = {AUDIO_EXTERNAL}.alc_invalid_value then
					Result := "An invalid value was passed to an OpenAL context function."
				elseif last_alc_error_code = {AUDIO_EXTERNAL}.alc_invalid_device then
					Result := "The device is not valid."
				elseif last_alc_error_code = {AUDIO_EXTERNAL}.alc_invalid_context then
					Result := "The context is not valid."
				elseif last_alc_error_code = {AUDIO_EXTERNAL}.alc_invalid_enum then
					Result := "Invalid enum parameter passed to an ALC call."
				elseif last_alc_error_code = {AUDIO_EXTERNAL}.alc_out_of_memory then
					Result := "Out of memory."
				else
					Result := "Unmanaged OpenAL context error."
				end
			else
				Result := "No error."
			end
		end

	pause
			-- Put the streaming in pause.
		require
			source_is_open: is_open
		do
			has_been_stop := False
			clear_error
			{AUDIO_EXTERNAL}.al_source_pause (index)
			if not is_pause then
				read_al_error ("Cannot pause audio source")
			end
		end

	play
			-- Start the sound streaming.
		require
			source_is_open: is_open
		do
			has_been_stop := False
			clear_error
			update_playing
			if not has_error then
				{AUDIO_EXTERNAL}.al_source_play (index)
				if not is_playing then
					read_al_error ("Cannot play audio source.")
				end
			end
		end

	position: TUPLE [x: REAL_32; y: REAL_32; z: REAL_32]
			-- Get the object position in a 3D environment.
			-- (from AUDIO_3D_OBJECT)
		do
			Result := params_3_float ({AUDIO_EXTERNAL}.al_position)
		end

	queue_sound (a_sound: AUDIO_SOUND)
			-- Add a a_sound to the playing queue.
			-- Don't loop the sound at all (only one playing).
		require
			source_is_open: is_open
			queud_sound_is_open: a_sound.is_open
		do
			queue_sound_loop (a_sound, 0)
		end

	queue_sound_infinite_loop (a_sound: AUDIO_SOUND)
			-- Add a a_sound to the playing queue.
			-- Loop the a_sound until the source is stopped.
		require
			source_is_open: is_open
			queud_sound_is_open: a_sound.is_open
		do
			queue_sound_loop (a_sound, -1)
		end

	queue_sound_loop (a_sound: AUDIO_SOUND; a_nb_loop: INTEGER_32)
			-- Add a a_sound to the playing queue.
			-- Put a_nb_loop to 0 for no loop and to -1 for infinite loop
		require
			source_is_open: is_open
			queud_sound_is_open: a_sound.is_open
		local
			l_sound_tuple: TUPLE [sound: AUDIO_SOUND; nb_loop: INTEGER_32]
		do
			create l_sound_tuple
			l_sound_tuple.sound := a_sound
			l_sound_tuple.nb_loop := a_nb_loop;
			internal_sound_queued.put (l_sound_tuple)
		end

	set_direction (a_x, a_y, a_z: REAL_32)
			-- Set the source direction. Meaning where the source object is looking at in the 3D environment.
		require
			source_is_open: is_open
		do
			set_params_3_float ({AUDIO_EXTERNAL}.al_direction, a_x, a_y, a_z)
		end

	set_gain (a_value: REAL_32)
			-- Set the Current gain (volume) to a_value. The a_value must always be a REAL between 0 and 1.
			-- If the a_value is set at 0, the source is mute. If the a_value is set at 1, it is at it's max volume.
		require
			source_is_open: is_open
			source_set_gain_valid_value: a_value >= 0.0 and then a_value <= 1.0
		do
			set_param_float_c ({AUDIO_EXTERNAL}.al_gain, a_value)
		ensure
			source_set_gain_is_set: not has_error implies gain = a_value
		end

	set_position (a_x, a_y, a_z: REAL_32)
			-- Set the object position in a 3D environment.
			-- (from AUDIO_3D_OBJECT)
		require -- from  GAME_3D_OBJECT
			True
		do
			set_params_3_float ({AUDIO_EXTERNAL}.al_position, a_x, a_y, a_z)
		ensure -- from GAME_3D_OBJECT
			is_assign: attached position as la_position implies (la_position.x ~ a_x and la_position.y ~ a_y and la_position.z ~ a_z)
		ensure then -- from AUDIO_3D_OBJECT
			is_assign: not has_error implies (attached position as la_position implies (la_position.x ~ a_x and la_position.y ~ a_y and la_position.z ~ a_z))
		end

	set_velocity (a_x, a_y, a_z: REAL_32)
			-- Set the object velocity (deplacement) in a 3D environment.
			-- (from AUDIO_3D_OBJECT)
		do
			set_params_3_float ({AUDIO_EXTERNAL}.al_velocity, a_x, a_y, a_z)
		ensure then -- from AUDIO_3D_OBJECT
			is_assign: not has_error implies (attached velocity as la_velocity implies (la_velocity.x ~ a_x and la_velocity.y ~ a_y and la_velocity.z ~ a_z))
		end

	set_x (a_x: REAL_32)
			-- Assign the x coordinate of the position
			-- (from GAME_3D_OBJECT)
		do
			set_position (a_x, y, z)
		ensure -- from GAME_3D_OBJECT
			is_assign: x ~ a_x
		end

	set_y (a_y: REAL_32)
			-- Assign the y coordinate of the position
			-- (from GAME_3D_OBJECT)
		do
			set_position (x, a_y, z)
		ensure -- from GAME_3D_OBJECT
			is_assign: y ~ a_y
		end

	set_z (a_z: REAL_32)
			-- Assign the z coordinate of the position
			-- (from GAME_3D_OBJECT)
		do
			set_position (x, y, a_z)
		ensure -- from GAME_3D_OBJECT
			is_assign: z ~ a_z
		end

	sound_queued: CHAIN_INDEXABLE_ITERATOR [TUPLE [sound: AUDIO_SOUND; nb_loop: INTEGER_32]]
			-- The queue containing every sound to play
		do
			create Result.make (internal_sound_queued)
		end

	stop
			-- Stop the streaming. All queud sound will be remove from the queue.
		require
			source_is_open: is_open
		do
			has_been_stop := True
			clear_error
			{AUDIO_EXTERNAL}.al_source_stop (index)
			if not is_stop then
				read_al_error ("Cannot stop audio source.")
			end
			if not has_error then
				internal_sound_queued.wipe_out
				update_playing
				{AUDIO_EXTERNAL}.al_source_rewind (index)
				if not is_initial then
					read_al_error ("Cannot rewind audio source.")
				end
			end
		end

	update_playing
			-- This methode must be execute at regular interval. If it is not execute enough in a certain time lap, the sounds will stop before finishing.
			-- If this append, you can call this methode more often or use bigger buffer_size. You can use the routine update on the AUDIO_LIBRARY_CONTROLLER
			-- and it will do the same effect.
		require
			source_is_open: is_open
		local
			last_position: INTEGER_32
		do
			if is_thread_safe then
				g_mutex.lock
			end
			from
			until
				processed_buffers_count < 1
			loop
				unqueue_buffer
			end
			from
				last_position := buffer_head - 1
			until
				buffer_tail = (buffer_head + 1) \\ Buffers_count or internal_sound_queued.is_empty or last_position = buffer_head
			loop
				last_position := buffer_head
				queue_next_buffer
			end
			if is_al_stop and not has_been_stop then
				{AUDIO_EXTERNAL}.al_source_play (index)
			end
			if is_thread_safe then
				g_mutex.unlock
			end
		end

	velocity: TUPLE [x: REAL_32; y: REAL_32; z: REAL_32]
			-- Get the object velocity (deplacement) in a 3D environment.
			-- (from AUDIO_3D_OBJECT)
		do
			Result := params_3_float ({AUDIO_EXTERNAL}.al_velocity)
		end

	x: REAL_32 assign set_x
			-- X coordinate of the position
			-- (from GAME_3D_OBJECT)
		do
			Result := position.x
		end

	y: REAL_32 assign set_y
			-- Y coordinate of the position
			-- (from GAME_3D_OBJECT)
		do
			Result := position.y
		end

	z: REAL_32 assign set_z
			-- Z coordinate of the position
			-- (from GAME_3D_OBJECT)
		do
			Result := position.z
		end
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal alias "≡≡≡" (other: AUDIO_SOURCE): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_equal (other: AUDIO_SOURCE): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal alias "≜" (other: AUDIO_SOURCE): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end
	
feature {NONE} -- Status report

	is_in_final_collect: BOOLEAN
			-- Is GC currently performing final collection
			-- after execution of current program?
			-- Safe to use in dispose.
			-- (from DISPOSABLE)
		external
			"C inline use %"eif_memory.h%""
		alias
			"return eif_is_in_final_collect;"
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: AUDIO_SOURCE)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: AUDIO_SOURCE)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: AUDIO_SOURCE
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: AUDIO_SOURCE)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: AUDIO_SOURCE
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: AUDIO_SOURCE
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached AUDIO_SOURCE
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable AUDIO_SOURCE
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature {NONE} -- Implementation

	disable_print_on_error
			-- Desactive the print_on_error functionnality.
			-- (from GAME_ERROR_MANAGER)
		do
			Print_on_error_internal.put (False)
		end

	enable_print_on_error
			-- Active the print_on_error functionnality.
			-- (from GAME_ERROR_MANAGER)
		do
			Print_on_error_internal.put (True)
		end

	internal_audio_library: detachable AUDIO_LIBRARY_CONTROLLER
			-- Assign to this attribute prior to use Audio_library to inject a specific AUDIO_LIBRARY_CONTROLLER singleton.
			-- (from AUDIO_LIBRARY_SHARED)

	manual_error: detachable READABLE_STRING_GENERAL
			-- The specific message for the last error
			-- (from GAME_ERROR_MANAGER)

	params_3_float (a_id: INTEGER_32): TUPLE [x: REAL_32; y: REAL_32; z: REAL_32]
			-- (from AUDIO_3D_OBJECT)
		local
			l_params_vector: ARRAY [REAL_32]
			l_params_c: ANY
		do
			create l_params_vector.make_filled (0.0, 1, 3)
			l_params_c := l_params_vector.to_c
			clear_error
			assign_params_float_pointer_c (a_id, $l_params_c.to_pointer)
			read_al_error ("Cannot get audio floats parameters (3)")
			Result := [l_params_vector.at (1), l_params_vector.at (2), l_params_vector.at (3)]
		end

	print_on_error: BOOLEAN
			-- When an error occured, the library will print
			-- informations about the error on the error console
			-- output (default is True).
			-- (from GAME_ERROR_MANAGER)
		do
			Result := Print_on_error_internal.item
		end

	Print_on_error_internal: CELL [BOOLEAN]
			-- True when an error occured,
			-- The library will print it right away.
			-- (from GAME_ERROR_MANAGER)
		once ("PROCESS")
			create Result.put (True)
		end

	set_params_3_float (a_id: INTEGER_32; a_x, a_y, a_z: REAL_32)
			-- (from AUDIO_3D_OBJECT)
		local
			l_params_vector: ARRAY [REAL_32]
			l_params_c: ANY
		do
			create l_params_vector.make_filled (0.0, 1, 3)
			l_params_vector.at (1) := a_x
			l_params_vector.at (2) := a_y
			l_params_vector.at (3) := a_z
			l_params_c := l_params_vector.to_c
			clear_error
			set_params_float_pointer_c (a_id, $l_params_c.to_pointer)
			read_al_error ("Cannot set audio float parameters (3).")
		ensure -- from AUDIO_3D_OBJECT
			is_assign: not has_error implies (attached params_3_float (a_id) as la_params implies (la_params.x ~ a_x and la_params.y ~ a_y and la_params.z ~ a_z))
		end

	set_print_on_error (a_value: BOOLEAN)
			-- Assign to print_on_error the value of a_value
			-- (from GAME_ERROR_MANAGER)
		do
			if a_value then
				enable_print_on_error
			else
				disable_print_on_error
			end
		ensure -- from GAME_ERROR_MANAGER
			is_assign: print_on_error ~ a_value
		end
	
feature {AUDIO_LIBRARY_CONTROLLER} 

	close
			-- Close Current so that it's feature cannot be used anymore.
		do
			stop
			is_open := False
		end

	set_thread_safe
			-- Enable thread facility inside Current
		do
			is_thread_safe := True
		end
	
feature {NONE} 

	clear_alc_error (a_device: POINTER)
			-- (from AUDIO_OPENAL_ERROR_MANAGER)
		do
			clear_error
			last_alc_error_code := {AUDIO_EXTERNAL}.alc_get_error (a_device)
		end

	clear_error
			-- Remove error pending in Current
			-- (from AUDIO_OPENAL_ERROR_MANAGER)
		do
			last_alc_error_code := {AUDIO_EXTERNAL}.alc_get_error (create {POINTER})
			last_al_error_code := {AUDIO_EXTERNAL}.al_get_error
			Precursor {GAME_ERROR_MANAGER}
		ensure -- from GAME_ERROR_MANAGER
			no_error: not has_error
		end

	is_error_managable: BOOLEAN
			-- Can the present error can be manage
			-- (from AUDIO_OPENAL_ERROR_MANAGER)
		do
			Result := last_al_error_code = {AUDIO_EXTERNAL}.al_out_of_memory or last_al_error_code = {AUDIO_EXTERNAL}.al_no_error
		end

	is_manual_error: BOOLEAN
			-- Is the present error a manual error
			-- (from AUDIO_OPENAL_ERROR_MANAGER)

	last_al_error_code: INTEGER_32
			-- The last error index return by the OpenAL library.
			-- (from AUDIO_OPENAL_ERROR_MANAGER)

	last_alc_error_code: INTEGER_32
			-- The last error index return by the OpenAL context library.
			-- (from AUDIO_OPENAL_ERROR_MANAGER)

	put_manual_error (a_general_message, a_specific_error: READABLE_STRING_GENERAL)
			-- Create a manual error using a_general_error for the debug information
			-- and a_specific_error for the lasting information
			-- (from AUDIO_OPENAL_ERROR_MANAGER)
		do
			last_al_error_code := {AUDIO_EXTERNAL}.al_no_error
			is_manual_error := True
			Precursor {GAME_ERROR_MANAGER} (a_general_message, a_specific_error)
		ensure -- from GAME_ERROR_MANAGER
				has_error
		end

	read_al_error (a_message: READABLE_STRING_GENERAL)
			-- Read the next OpenAL error
			-- (from AUDIO_OPENAL_ERROR_MANAGER)
		do
			last_alc_error_code := {AUDIO_EXTERNAL}.alc_no_error
			last_al_error_code := {AUDIO_EXTERNAL}.al_get_error
			has_error := last_al_error_code /= {AUDIO_EXTERNAL}.al_no_error
			if has_error and Print_on_error_internal.item then
				Io.Error.put_string (a_message.to_string_8 + "%N");
				Io.Error.put_string (last_error.to_string_8 + "%N")
			end
		end

	read_alc_error (a_device: POINTER; a_message: READABLE_STRING_GENERAL)
			-- Read the next OpenAL error
			-- (from AUDIO_OPENAL_ERROR_MANAGER)
		do
			last_al_error_code := {AUDIO_EXTERNAL}.al_no_error
			last_alc_error_code := {AUDIO_EXTERNAL}.alc_get_error (a_device)
			has_error := last_alc_error_code /= {AUDIO_EXTERNAL}.alc_no_error
			if has_error and Print_on_error_internal.item then
				Io.Error.put_string (a_message.to_string_8 + "%N");
				Io.Error.put_string (last_error.to_string_8 + "%N")
			end
		end
	
feature {NONE} -- Implementation - Routines

	assign_params_float_pointer_c (a_id: INTEGER_32; a_ptr: POINTER)
			-- Retreive the internal float C param at index a_id and put it in a_ptr
		do
			{AUDIO_EXTERNAL}.al_get_source_fv (index, a_id, a_ptr)
		end

	param_float_c (a_id: INTEGER_32): REAL_32
			-- Retreive the internal float C param at index a_id
		local
			l_value: REAL_32
		do
			{AUDIO_EXTERNAL}.al_get_source_f (index, a_id, $l_value.to_pointer)
			Result := l_value
		end

	param_int_c (a_id: INTEGER_32): INTEGER_32
			-- Retreive the internal integer C param at index a_id
		local
			l_value: INTEGER_32
		do
			{AUDIO_EXTERNAL}.al_get_source_i (index, a_id, $l_value.to_pointer)
			Result := l_value
		end

	processed_buffers_count: INTEGER_32
			-- The number of buffer in the internal queue that has been processed
		do
			Result := param_int_c ({AUDIO_EXTERNAL}.al_buffers_processed)
		end

	processed_buffers_number: INTEGER_32
			-- The number of buffer in the internal queue that has been processed
		do
			Result := param_int_c ({AUDIO_EXTERNAL}.al_buffers_processed)
		end

	queuded_buffers_count: INTEGER_32
			-- The number of buffer in the internal queue that has not been processed yet.
		do
			Result := param_int_c ({AUDIO_EXTERNAL}.al_buffers_queued)
		end

	queuded_buffers_number: INTEGER_32
		obsolete "Use `queuded_buffers_count' instead"
			-- The number of buffer in the internal queue that has not been processed yet.
		do
			Result := param_int_c ({AUDIO_EXTERNAL}.al_buffers_queued)
		end

	queue_buffer (a_buffer: POINTER; a_length, a_channel, a_bits_resolution, a_frequency: INTEGER_32)
			-- Queue a_buffer in the internal C queue. a_buffer has a size of a_lenght
			-- has a_channel to play at a_frequency hz using a_bit_resolution.
		require
			has_place: buffer_tail /= (buffer_head + 1) \\ Buffers_count
		local
			l_buffer_name: ARRAY [NATURAL_32]
			l_buffer_name_c: ANY
			l_format: INTEGER_32
		do
			if a_channel = 1 then
				if a_bits_resolution = 8 then
					l_format := {AUDIO_EXTERNAL}.al_format_mono8
				else
					l_format := {AUDIO_EXTERNAL}.al_format_mono16
				end
			else
				if a_bits_resolution = 8 then
					l_format := {AUDIO_EXTERNAL}.al_format_stereo8
				else
					l_format := {AUDIO_EXTERNAL}.al_format_stereo16
				end
			end
			clear_error
			{AUDIO_EXTERNAL}.al_buffer_data (sound_al_buffer.at (buffer_head), l_format, a_buffer, a_length, a_frequency)
			read_al_error ("Cannot read buffer audio data.")
			if not has_error then
				create l_buffer_name.make_filled (sound_al_buffer.at (buffer_head), 1, 1)
				buffer_head := (buffer_head + 1) \\ Buffers_count
				l_buffer_name_c := l_buffer_name.to_c
				clear_error
				{AUDIO_EXTERNAL}.al_source_queue_buffers (index, 1, $l_buffer_name_c.to_pointer)
				read_al_error ("Cannot queud audio buffer.")
			end
		end

	queue_next_buffer
			-- Queue the next C buffer returned from the next sound of the internal_sound_queued
		local
			l_last_fill_buffer_size, l_channel, bits_resolution, l_freq, l_byte_per_buffer_sample: INTEGER_32
			l_infinite_loop_restarted, l_streaming_sound_null: BOOLEAN
		do
			from
				l_last_fill_buffer_size := 0
				l_infinite_loop_restarted := False
				l_streaming_sound_null := False
			until
				l_last_fill_buffer_size /= 0 or internal_sound_queued.is_empty or l_streaming_sound_null
			loop
				internal_sound_queued.item.sound.fill_buffer (temp_buffer, buffer_size)
				l_last_fill_buffer_size := internal_sound_queued.item.sound.last_buffer_size
				l_channel := internal_sound_queued.item.sound.channel_count
				bits_resolution := internal_sound_queued.item.sound.bits_per_sample
				l_freq := internal_sound_queued.item.sound.frequency
				l_byte_per_buffer_sample := internal_sound_queued.item.sound.byte_per_buffer_sample
				if internal_sound_queued.item.sound.is_finished then
					internal_sound_queued.item.sound.restart
					if internal_sound_queued.item.nb_loop = 0 then
						internal_sound_queued.remove
						if internal_sound_queued.is_empty then
							has_been_stop := True
						end
					else
						if internal_sound_queued.item.nb_loop > 0 then
							internal_sound_queued.item.nb_loop := internal_sound_queued.item.nb_loop - 1
						else
							l_infinite_loop_restarted := True
						end
					end
				else
					l_streaming_sound_null := l_last_fill_buffer_size = 0
					l_infinite_loop_restarted := False
				end
			end
			if l_last_fill_buffer_size > 0 then
				queue_buffer (temp_buffer, l_last_fill_buffer_size, l_channel, bits_resolution, l_freq)
			end
		end

	set_param_float_c (a_id: INTEGER_32; a_value: REAL_32)
			-- Store the internal float a_value C param at index a_id
		do
			{AUDIO_EXTERNAL}.al_set_source_f (index, a_id, a_value)
		end

	set_params_float_pointer_c (a_id: INTEGER_32; a_ptr: POINTER)
			-- Store the internal float C param pointed by a_ptr at index a_id
		do
			{AUDIO_EXTERNAL}.al_set_source_fv (index, a_id, a_ptr)
		end

	unqueue_buffer
			-- Remove the top buffer in the C internal queue
		require
			unqueue_buffer_nb_processed_valid: processed_buffers_count > 0
		local
			l_buffer_name: ARRAY [NATURAL_32]
			l_buffer_name_c: ANY
		do
			check
					buffer_tail /= buffer_head
			end
			create l_buffer_name.make_filled (sound_al_buffer.at (buffer_tail), 1, 1)
			buffer_tail := (buffer_tail + 1) \\ Buffers_count
			l_buffer_name_c := l_buffer_name.to_c
			clear_error
			{AUDIO_EXTERNAL}.al_source_unqueue_buffers (index, 1, $l_buffer_name_c.to_pointer)
			read_al_error ("Cannot unqueue audio buffer.")
		end
	
feature -- Implementation - Routines

	dispose
			-- Action to be executed just before garbage collection
			-- reclaims an object.
			-- Effect it in descendants to perform specific dispose
			-- actions. Those actions should only take care of freeing
			-- external resources; they should not perform remote calls
			-- on other objects since these may also be dead and reclaimed.
		local
			sources: ARRAY [NATURAL_32]
			sources_c: ANY
			l_sound_al_buffer_c: ANY
		do
			stop;
			temp_buffer.memory_free
			l_sound_al_buffer_c := sound_al_buffer.to_c
			{AUDIO_EXTERNAL}.al_delete_buffers (Buffers_count, $l_sound_al_buffer_c.to_pointer)
			create sources.make_filled (index, 1, 1)
			sources_c := sources.to_c
			{AUDIO_EXTERNAL}.al_delete_sources (1, $sources_c.to_pointer)
		end
	
feature {NONE} -- Implementation - Variables

	buffer_head: INTEGER_32
			-- The first element in the circular buffer

	buffer_tail: INTEGER_32
			-- The last element in the circular buffer

	Buffers_count: INTEGER_32
			-- The number of internal buffer in Current
		once ("PROCESS")
			Result := 4
		end

	g_mutex: MUTEX
			-- The mutex used to manage thread synchronisation inside Current

	has_been_stop: BOOLEAN
			-- True if Current has been stoped by the Eiffel library (not the OpenAL library)

	index: NATURAL_32
			-- The internal identifier of Current

	internal_sound_queued: LINKED_QUEUE [TUPLE [sound: AUDIO_SOUND; nb_loop: INTEGER_32]]
			-- The queue containing every sound to play

	is_al_stop: BOOLEAN
			-- True if the sound source has been stoped.
		require
			source_is_open: is_open
		do
			Result := (param_int_c ({AUDIO_EXTERNAL}.al_source_state) = {AUDIO_EXTERNAL}.al_stopped)
		end

	is_thread_safe: BOOLEAN
			-- Is tread facility enabled in Current

	Nb_buffer: INTEGER_32
		obsolete "Use `buffers_count' instead"
			-- The number of internal buffer in Current
		once ("PROCESS")
			Result := 4
		end

	sound_al_buffer: ARRAY [NATURAL_32]
			-- Internal C buffer queue

	temp_buffer: POINTER
			-- The temporary buffer used to transfert data in the real buffer
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result;
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		local
			s: READABLE_STRING_8
		do
			if attached o then
				s := o.out
				if attached {READABLE_STRING_32} s as s32 then
					Io.put_string_32 (s32)
				elseif attached {READABLE_STRING_8} s as s8 then
					Io.put_string (s8)
				else
					Io.put_string_32 (s.as_string_32)
				end
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_32
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ".as_string_32)
				create l_exc;
				l_msg.append (generating_type.name_32);
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
invariant
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class AUDIO_SOURCE

Generated by ISE EiffelStudio