note
	description: "Controller for the audio library."
	author: "Louis Marchand"
	date: "Tue, 07 Apr 2015 01:15:20 +0000"
	revision: "2.0"

class interface
	AUDIO_LIBRARY_CONTROLLER

create 
	make
			-- Initialization for Current.
			-- Activates the sound context.

feature -- Access

	capture_controller: AUDIO_CAPTURE_CONTROLLER
			-- Used to capture sound from microphone
		require
			capture_enabled: is_capture_enable

	capture_device: AUDIO_DEVICE
			-- The capture AUDIO_DEVICE presently used by Current
		require
			sound_enabled: is_capture_enable

	capture_devices: LIST [AUDIO_DEVICE]
			-- Lists all possible capture devices that Current may use
		ensure
			all_playback_device: across
					Result as la_result
				all
					la_result.item.is_capture
				end

	current_thread_id: POINTER
			-- Thread identifier of the Current thread
			-- (from THREAD_ENVIRONMENT)

	disable_capture
			-- Closes the capture sound context.

	disable_playback
			-- Closes the playback sound context.

	enable_capture (a_frequency, a_channel_count, a_bits_per_sample, a_buffer_count: INTEGER_32)
			-- Activates the capture sound context using the system default device. The capturing can be done with capture_controller.
			-- The resulting capture_controller will have use a_channel_count number of channel, a_bits_per_sample number of bits per
			-- single channel sample (8 or 16) and the internal buffer will be able to contain a_buffer_count samples (for example,
			-- a stereo 16 bits capture with a buffer count of 1024 will have the size 1024*2*2 = 5096 bytes)
		require
			is_not_already_enabled: not is_capture_enable
			channel_valid: a_channel_count >= 1 and a_channel_count <= 2
			bits_per_sample_valid: a_bits_per_sample ~ 8 or a_bits_per_sample ~ 16
			frequency_valid: a_frequency > 0

	enable_capture_with_device (a_device: AUDIO_DEVICE; a_frequency, a_channel_count, a_bits_per_sample, a_buffer_count: INTEGER_32)
			-- Activates the capture sound context using a_device. The capturing can be done with capture_controller.
			-- The resulting capture_controller will have use a_channel_count number of channel, a_bits_per_sample number of bits per
			-- single channel sample (8 or 16) and the internal buffer will be able to contain a_buffer_count samples (for example,
			-- a stereo 16 bits capture with a buffer count of 1024 will have the size 1024*2*2 = 5096 bytes)
		require
			is_not_already_enabled: not is_capture_enable
			device_valid: a_device.is_capture and capture_devices.has (a_device)
			channel_valid: a_channel_count >= 1 and a_channel_count <= 2
			bits_per_sample_valid: a_bits_per_sample ~ 8 or a_bits_per_sample ~ 16
			frequency_valid: a_frequency > 0

	enable_playback
			-- Activates the playback sound context.
		require
			is_not_already_enabled: not is_playback_enable

	enable_playback_with_device (a_device: AUDIO_DEVICE)
			-- Activates the playback sound context using a_device.
		require
			is_not_already_enabled: not is_playback_enable
			is_device_valid: a_device.is_playback and playback_devices.has (a_device)

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

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

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

	is_capture_enable: BOOLEAN
			-- True when a capture sound is active.

	is_playback_enable: BOOLEAN
			-- True when a playback sound context is active.

	last_error: READABLE_STRING_GENERAL
			-- The last error generate by the library
			-- (from AUDIO_OPENAL_ERROR_MANAGER)

	listener: AUDIO_LISTENER
			-- Gets the sound listener.
		require
			get_listener_sound_open: is_playback_enable

	playback_device: AUDIO_DEVICE
			-- The playback AUDIO_DEVICE presently used by Current
		require
			sound_enabled: is_playback_enable

	playback_devices: LIST [AUDIO_DEVICE]
			-- Lists all possible playback devices that Current may use.
		ensure
			all_playback_device: across
					Result as la_result
				all
					la_result.item.is_playback
				end

	quit_library
			-- Clears the library. Must be used before the end of the application

	terminated: BOOLEAN
			-- True if the thread has terminated.
			-- (from THREAD)

	thread_id: POINTER
			-- Thread-id of the current thread object.
			-- (from THREAD)
	
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)
		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)

	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)
		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))

	frozen is_deep_equal alias "≡≡≡" (other: AUDIO_LIBRARY_CONTROLLER): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		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)

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

	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)
		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))

	frozen standard_is_equal alias "" (other: AUDIO_LIBRARY_CONTROLLER): 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
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
	
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

	is_exit_supported: BOOLEAN
			-- Can exit be called?
			-- (from THREAD)
		ensure -- from THREAD
			is_class: class

	is_last_launch_successful: BOOLEAN
			-- Was the last call to launch or launch_with_attributes in current thread
			-- of execution successful?
			-- (from THREAD)

	is_launchable: BOOLEAN
			-- Can we launch a new thread?
			-- (from THREAD)

	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
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
	
feature -- Duplication

	copy (other: AUDIO_LIBRARY_CONTROLLER)
			-- 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)
		ensure -- from ANY
			is_equal: Current ~ other

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

	frozen deep_twin: AUDIO_LIBRARY_CONTROLLER
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)

	frozen standard_copy (other: AUDIO_LIBRARY_CONTROLLER)
			-- 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)
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)

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

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

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

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

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

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

	exit
			-- Exit calling thread. Must be called from the thread itself.
			-- (from THREAD)
		require -- from THREAD
			is_exit_supported: is_exit_supported
			self: current_thread_id = thread_id

	frozen launch_thread
			-- Initialize a new thread running execute.
			-- Set is_last_launch_successful to True if successful, False otherwise.			
			-- (from THREAD)
		require -- from THREAD
			thread_capable: {PLATFORM}.is_thread_capable
			is_launchable: is_launchable

	frozen launch_with_attributes (attr: THREAD_ATTRIBUTES)
			-- Initialize a new thread running execute, using attributes.
			-- Set is_last_launch_successful to True if successful, False otherwise.
			-- (from THREAD)
		require -- from THREAD
			thread_capable: {PLATFORM}.is_thread_capable
			is_launchable: is_launchable

	sleep (nanoseconds: INTEGER_64)
			-- Suspend thread execution for interval specified in
			-- nanoseconds (1 nanosecond = 10^(-9) second).
			-- (from THREAD)
		require -- from THREAD
			self: current_thread_id = thread_id
			non_negative_nanoseconds: nanoseconds >= 0
	
feature -- Implementation

	disable_print_on_error
			-- Desactive the print_on_error functionnality.
			-- (from GAME_ERROR_MANAGER)

	enable_print_on_error
			-- Active the print_on_error functionnality.
			-- (from GAME_ERROR_MANAGER)

	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)

	set_print_on_error (a_value: BOOLEAN)
			-- Assign to print_on_error the value of a_value
			-- (from GAME_ERROR_MANAGER)
		ensure -- from GAME_ERROR_MANAGER
			is_assign: print_on_error ~ a_value
	
feature -- Implementation Routine

	execute
			-- The thread main loop (started with launch_in_thread)
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void

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

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		ensure -- from ANY
			instance_free: class

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

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
	
feature -- Sources management

	is_thread_executing: BOOLEAN
			-- The update thread is running

	last_source_added: AUDIO_SOURCE
			-- Returns the last sound source that has been created.
		require
			sources_get_last_add_sound_open: is_playback_enable

	launch_in_thread
			-- Makes Current automatically update using another thread.
			-- You have to manually call stop_thread before closing the application
		require
			launch_in_thread_sound_open: is_playback_enable
			not_thread_running: not is_thread_executing
		ensure
			is_thread_running: is_thread_executing

	set_sound_buffer_size (a_buffer_size: INTEGER_32)
			-- Set the buffer size for the sound streaming (default is 65536). Allocating too little memory to the buffer can cause sound to stop before finishing.

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

	sources: CHAIN_INDEXABLE_ITERATOR [AUDIO_SOURCE]
			-- All AUDIO_SOURCE of the system.
		require
			sources_sound_open: is_playback_enable

	sources_add
			-- Creates a new sound source. To receive the sound source, use the source_get_last_add method.
		require
			sources_add_sound_open: is_playback_enable
		ensure
				internal_sources.count = old internal_sources.count + 1

	sources_at (a_index: INTEGER_32): AUDIO_SOURCE
			-- Returns the a_index-th sound source.
		require
			sources_get_at_sound_open: is_playback_enable
			al_controler_source_get_index_valid: a_index > 0 and then a_index < sources_count + 1

	sources_count: INTEGER_32
			-- The current number of sound sources in the sound context.
		require
			sources_count_sound_open: is_playback_enable

	sources_has (a_source: AUDIO_SOURCE): BOOLEAN
			-- Returns true if the sound source a_source is still in the sound controller.
		require
			sources_has_sound_open: is_playback_enable

	sources_prune (a_source: AUDIO_SOURCE)
			-- Removes the sound source a_source from the sound controller. A sound that has been removed from the sound
			-- Controller can continue to work on its own, but it will not be updated by the update_sound_playing routine.
		require
			sources_remove_sound_open: is_playback_enable
			al_controler_source_remove_source_valid: a_source /= Void and then sources_has (a_source)
		ensure
			al_controler_source_remove_source_removed: not internal_sources.has (a_source)

	sources_remove (a_index: INTEGER_32)
			-- Removes the a_index-th sound source.
		require
			sources_remove_at_sound_open: is_playback_enable
			al_controler_source_remove_index_valid: a_index > 0 and then a_index < sources_count + 1

	sources_wipe_out
			-- This method removes all sound sources in the sound context.
		require
			update_sound_playing_sound_open: is_playback_enable

	stop_thread
			-- Stops the thread previously called with launch_in_thread
		require
			is_thread_running: is_thread_executing
		ensure
			is_thread_not_running: not is_thread_executing

	update
			-- This method must be executed at regular interval. If it is not executed enough in a certain time lap, the sound will stop before finishing.
			-- If this happens, you can call this method more often or use bigger sound_buffer_size. You can use the method update_playing for each individual
			-- Sound sources in the project and it will do the same effect.
		require
			update_sound_playing_sound_open: is_playback_enable
	
feature -- Synchronization

	join
			-- The calling thread waits for the current child thread to terminate.
			-- (from THREAD)

	join_all
			-- The calling thread waits for all other threads to terminate.
			-- (from THREAD_CONTROL)

	join_with_timeout (a_timeout_ms: NATURAL_64): BOOLEAN
			-- The calling thread waits for the current child thread to
			-- terminate for at most a_timeout_ms milliseconds.
			-- True if wait terminates within a_timeout_ms, False otherwise.
			-- (from THREAD)

	yield
			-- The calling thread yields its execution in favor of another
			-- thread for an OS specific amount of time.
			-- (from THREAD_CONTROL)
	
invariant
	is_sound_open_context_valid: is_playback_enable implies (not {AUDIO_EXTERNAL}.alc_get_current_context.is_default_pointer)
	is_sound_open_sources_valid: (not is_playback_enable) implies (internal_sources.count = 0)
	capture_valid: (is_capture_enable implies attached internal_capture_controller) and (attached internal_capture_controller implies is_capture_enable)

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class AUDIO_LIBRARY_CONTROLLER

Generated by ISE EiffelStudio