note
	description: "Class that generates sound using lists of INTEGER_16"
	author: "Émilio Gonzalez"
	adaptation: "Louis Marchand"
	date: "2016-03-29"
	revision: "16w09a"
	legal: "See notice at end of class."

class interface
	AUDIO_SOUND_GENERATOR

create 
	make
			-- Initialization for Current. Sets some attributes.

feature -- Access

	arc_cosine (v: REAL_64): REAL_64
			-- Trigonometric arccosine of radian v
			-- in the range [0, pi].
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	arc_sine (v: REAL_64): REAL_64
			-- Trigonometric arcsine of radian v
			-- in the range [-pi/2, +pi/2].
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	arc_tangent (v: REAL_64): REAL_64
			-- Trigonometric arctangent of radian v
			-- in the range [-pi/2, +pi/2].
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	ceiling (v: REAL_64): REAL_64
			-- Least integral greater than or equal to v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	cosine (v: REAL_64): REAL_64
			-- Trigonometric cosine of radian v approximated
			-- in the range [-pi/4, +pi/4].
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	dabs (v: REAL_64): REAL_64
			-- Absolute of v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	Euler: REAL_64 = 2.7182818284590452353602874713526625
			-- Logarithm base
			-- (from MATH_CONST)

	exp (x: REAL_64): REAL_64
			-- Exponential of v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	floor (v: REAL_64): REAL_64
			-- Greatest integral less than or equal to v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	generating_type: TYPE [detachable AUDIO_SOUND_GENERATOR]
			-- 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

	log (v: REAL_64): REAL_64
			-- Natural logarithm of v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	log10 (v: REAL_64): REAL_64
			-- Base 10 logarithm of v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	log_2 (v: REAL_64): REAL_64
			-- Base 2 logarithm of v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	Pi: REAL_64 = 3.1415926535897932384626433832795029
			-- (from MATH_CONST)

	Pi_2: REAL_64 = 1.5707963267948966192313216916397514
			-- (from MATH_CONST)

	Pi_4: REAL_64 = 0.7853981633974483096156608458198757
			-- (from MATH_CONST)

	sine (v: REAL_64): REAL_64
			-- Trigonometric sine of radian v approximated
			-- in range [-pi/4, +pi/4].
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	sqrt (v: REAL_64): REAL_64
			-- Square root of v.
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class

	Sqrt2: REAL_64 = 1.4142135623730950488016887242096981
			-- Square root of 2
			-- (from MATH_CONST)

	tangent (v: REAL_64): REAL_64
			-- Trigonometric tangent of radian v approximated
			-- in range [-pi/4, +pi/4].
			-- (from DOUBLE_MATH)
		ensure -- from DOUBLE_MATH
			instance_free: class
	
feature --Access

	add_noise (a_sound: CHAIN [INTEGER_16]; a_amplitude: INTEGER_32)
			--adds random numbers to a_sound
			--side effect on a_sound
		require
			amplitude_valid: a_amplitude >= 0
		ensure
			result_valid: old a_sound.count = a_sound.count

	add_silence_from_samples (a_sound: CHAIN [INTEGER_16]; a_samples: INTEGER_32)
			-- Adds a silence (zeros) of a_samples samples to a_sound.
			-- Of course, it has a side effect on a_sound.
		require
			samples_valid: a_samples >= 0
		ensure
			at_least_one_zero: across
					a_sound as la_sound
				some
					la_sound.item = 0
				end

	add_silence_from_seconds (a_sound: CHAIN [INTEGER_16]; a_seconds: REAL_64)
			-- Adds a silence (zeros) of a_seconds seconds to a_sound.
			-- Of course, it has a side effect on a_sound.
		require
			seconds_valid: a_seconds >= 0.to_double
		ensure
			sound_count_valid: a_sound.count = old a_sound.count + get_number_of_samples_from_duration (a_seconds)
			at_least_one_zero: across
					a_sound as la_sound
				some
					la_sound.item = 0
				end

	amplify_wave (a_sound: CHAIN [INTEGER_16]; a_amp_value: REAL_64)
			--amplifies a_sound by multiplicating a_sound[i] with a_amp_value
			--side effect on a_sound
		require
			amp_value_valid: a_amp_value >= 0.to_double
		ensure
			result_valid: old a_sound.count = a_sound.count

	Bits_per_sample: INTEGER_32 = 16
			--number of bits per sample in the sound.

	create_sine_wave (a_amplitude: REAL_64; a_frequency: INTEGER_32): CHAIN [INTEGER_16]
			-- Method that creates a sine square wave and returns it as a list of INTEGER_16
			-- amplitude is in (relative) dB, frequency is in Hz
		require
			amplitude_too_high: a_amplitude <= Max_amplitude
			amplitude_too_low: a_amplitude >= 0.to_double
			frequency_too_high: a_frequency <= max_frequency
			frequency_too_low: a_frequency >= Min_frequency
		ensure
			result_valid: Result.count = get_wave_length_from_frequency (a_frequency)

	create_square_wave (a_amplitude: REAL_64; a_frequency: INTEGER_32): CHAIN [INTEGER_16]
			--Method that creates a single square wave and returns it as a list of INTEGER_16
			-- amplitude is in (relative) dB, frequency is in Hz
		require
			amplitude_too_high: a_amplitude <= Max_amplitude
			amplitude_too_low: a_amplitude >= 0.to_double
			frequency_too_high: a_frequency <= max_frequency
			frequency_too_low: a_frequency >= Min_frequency
		ensure
			result_valid: Result.count = get_wave_length_from_frequency (a_frequency)

	create_triangle_wave (a_amplitude: REAL_64; a_frequency: INTEGER_32): CHAIN [INTEGER_16]
			--Method that creates a triangle square wave and returns it as a list of INTEGER_16
			-- amplitude is in (relative) dB, frequency is in Hz
		require
			amplitude_too_high: a_amplitude <= Max_amplitude
			amplitude_too_low: a_amplitude >= 0.to_double
			frequency_too_high: a_frequency <= max_frequency
			frequency_too_low: a_frequency >= Min_frequency
		ensure
			result_valid: Result.count = get_wave_length_from_frequency (a_frequency)

	fade (a_sound: CHAIN [INTEGER_16]; a_begin_length_percentage: REAL_64; a_end_length_percentage: REAL_64; a_begin_volume_percentage: REAL_64; a_end_volume_percentage: REAL_64)
			-- fades (a_begin_length_percentage % to a_length_end_percentage) from (a_begin_volume_percentage % to a_end_volume_percentage %)
			-- of the sound. Fade out or fade in.
			-- side effect on a_sound.
		require
			begin_length_good: a_begin_length_percentage >= 0.to_double and a_begin_length_percentage <= 1.to_double
			end_length_good: a_end_length_percentage >= a_begin_length_percentage and a_end_length_percentage <= 1.to_double
			begin_volume_good: a_begin_volume_percentage >= 0.to_double and a_begin_volume_percentage <= 1.to_double
			end_volume_good: a_end_volume_percentage >= 0.to_double and a_end_volume_percentage <= 1.to_double
		ensure
			result_valid: old a_sound.count = a_sound.count

	Max_amplitude: REAL_64
			--maximum amplitude (in relative dB) that can be expressed using INTEGER_16.

	max_frequency: INTEGER_32
			--sample_rate // 2

	max_integer_16: INTEGER_16
			--Highest value for an INTEGER_16

	max_integer_32: INTEGER_32
			--Highest value for an INTEGER_32

	Min_frequency: INTEGER_32 = 20
			--can't really hear below this frequency

	min_integer_16: INTEGER_16
			--Lowest value for an INTEGER_16

	min_integer_32: INTEGER_32
			--Lowest value for an INTEGER_32

	mix (a_sound1: CHAIN [INTEGER_16]; a_sound2: CHAIN [INTEGER_16]; a_percentage: REAL_64)
			-- Mixes two waves by adding up a_sound2[i] to a_sound2[j] starting j at a_percentage% of the sound.
			-- if there is overflow, caps the amplitude.
			-- side effect on a_sound1
		require
			sound1_valid: a_sound1.count > 0
			sound2_valid: a_sound2.count > 0
			percentage_valid: a_percentage >= 0.to_double and a_percentage <= 1.to_double
		ensure
			result_valid: a_sound1.count >= a_sound2.count

	Number_of_channels: INTEGER_32 = 1
			--number of channels for the sound.

	repeat_wave_from_duration (a_sound: CHAIN [INTEGER_16]; a_seconds: REAL_64)
			--Repeats a_sound until it lasts a_seconds seconds. Doesnt repeat if a_seconds is lower than a_sound duration.
			--1 = no repetition
			--Side effect on a_sound
		require
			duration_valid: a_seconds >= 0.to_double
		ensure
			repetition_valid: a_sound.count >= get_number_of_samples_from_duration (a_seconds) and old a_sound.count <= a_sound.count

	repeat_wave_from_repetitions (a_sound: CHAIN [INTEGER_16]; a_repetition: INTEGER_32)
			--Appends a copy of a_sound to a_sound (a_repetition - 1) duration(s).
			--1 = no repetition
			--Side effect on a_sound
		require
			repetition_valid: a_repetition > 0
		ensure
			repetition_valid: a_sound.count = old a_sound.count * a_repetition

	Sample_rate: INTEGER_32 = 44100
			--number of samples playing per second
	
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_SOUND_GENERATOR): 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_SOUND_GENERATOR): 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_SOUND_GENERATOR): 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

	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_SOUND_GENERATOR)
			-- 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_SOUND_GENERATOR)
			-- 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_SOUND_GENERATOR
			-- 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_SOUND_GENERATOR)
			-- 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_SOUND_GENERATOR
			-- 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_SOUND_GENERATOR
			-- 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_SOUND_GENERATOR
			-- 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
	
feature -- Debug

	print_wave (a_wave: CHAIN [INTEGER_16])
			--prints the wave in the console
	
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
	
invariant
	max_frequency_valid: max_frequency = Sample_rate // 2

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

note
	license: "GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 | Copyright (c) 2016 Émilio Gonzalez and Guillaume Jean"
	source: "[url: https://www.gnu.org/licenses/gpl-3.0.html]"

end -- class AUDIO_SOUND_GENERATOR

Generated by ISE EiffelStudio