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

feature -- Access

	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

	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

	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)

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

	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

	disable_playback
			-- Closes the playback sound context.

	disable_capture
			-- Closes the capture sound context.

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

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

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

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

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

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

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

	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.

	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.

	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

	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

	is_thread_executing: BOOLEAN
			-- The update thread is running

	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

	sources_count: INTEGER_32
			-- The current number of sound sources in the sound context.
		require
			sources_count_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

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

	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_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_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_wipe_out
			-- This method removes all sound sources in the sound context.
		require
			update_sound_playing_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: CHAIN_INDEXABLE_ITERATOR [AUDIO_SOURCE]
			-- All AUDIO_SOURCE of the system.
		require
			sources_sound_open: is_playback_enable
	
feature -- Implementation Routine

	execute
			-- The thread main loop (started with launch_in_thread)
	
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)

end -- class AUDIO_LIBRARY_CONTROLLER

Generated by ISE EiffelStudio