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 AUDIO_SOUND_GENERATOR create make feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end feature {NONE} --Initialization make -- Initialization for Current. Sets some attributes. do max_frequency := Sample_rate // 2 max_integer_16 := max_integer_16.Max_value min_integer_16 := max_integer_16.Min_value max_integer_32 := max_integer_32.Max_value min_integer_32 := max_integer_32.Min_value end feature -- Access arc_cosine (v: REAL_64): REAL_64 -- Trigonometric arccosine of radian v -- in the range [0, pi]. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" alias "acos" ensure -- from DOUBLE_MATH instance_free: class end arc_sine (v: REAL_64): REAL_64 -- Trigonometric arcsine of radian v -- in the range [-pi/2, +pi/2]. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" alias "asin" ensure -- from DOUBLE_MATH instance_free: class end arc_tangent (v: REAL_64): REAL_64 -- Trigonometric arctangent of radian v -- in the range [-pi/2, +pi/2]. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" alias "atan" ensure -- from DOUBLE_MATH instance_free: class end ceiling (v: REAL_64): REAL_64 -- Least integral greater than or equal to v. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" alias "ceil" ensure -- from DOUBLE_MATH instance_free: class end cosine (v: REAL_64): REAL_64 -- Trigonometric cosine of radian v approximated -- in the range [-pi/4, +pi/4]. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" alias "cos" ensure -- from DOUBLE_MATH instance_free: class end dabs (v: REAL_64): REAL_64 -- Absolute of v. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" alias "fabs" ensure -- from DOUBLE_MATH instance_free: class end Euler: REAL_64 = 2.7182818284590452353602874713526625 -- Logarithm base -- (from MATH_CONST) exp (x: REAL_64): REAL_64 -- Exponential of v. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" ensure -- from DOUBLE_MATH instance_free: class end floor (v: REAL_64): REAL_64 -- Greatest integral less than or equal to v. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" ensure -- from DOUBLE_MATH instance_free: class end generating_type: TYPE [detachable AUDIO_SOUND_GENERATOR] -- 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 log (v: REAL_64): REAL_64 -- Natural logarithm of v. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" ensure -- from DOUBLE_MATH instance_free: class end log10 (v: REAL_64): REAL_64 -- Base 10 logarithm of v. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" ensure -- from DOUBLE_MATH instance_free: class end log_2 (v: REAL_64): REAL_64 -- Base 2 logarithm of v. -- (from DOUBLE_MATH) do Result := log (v) / log ({REAL_64} 2.0) ensure -- from DOUBLE_MATH instance_free: class end 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) external "C signature (double): double use <math.h>" alias "sin" ensure -- from DOUBLE_MATH instance_free: class end sqrt (v: REAL_64): REAL_64 -- Square root of v. -- (from DOUBLE_MATH) external "C signature (double): double use <math.h>" ensure -- from DOUBLE_MATH instance_free: class end 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) external "C signature (double): double use <math.h>" alias "tan" ensure -- from DOUBLE_MATH instance_free: class end 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 local l_max_number: INTEGER_16 l_random_number: RANDOM i: INTEGER_32 do l_max_number := get_max_number_from_amplitude (a_amplitude.to_double) create l_random_number.make from i := 1 until i > a_sound.count loop a_sound [i] := add_up (a_sound [i], ((l_random_number.double_item - 0.5) * l_max_number.to_double).rounded.to_integer_16); l_random_number.forth i := i + 1 end ensure result_valid: old a_sound.count = a_sound.count end 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 local i: INTEGER_32 do from i := 1 until i > a_samples loop a_sound.extend (0) i := i + 1 end ensure at_least_one_zero: across a_sound as la_sound some la_sound.item = 0 end 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 local l_number_of_silence_samples: INTEGER_32 do l_number_of_silence_samples := get_number_of_samples_from_duration (a_seconds) add_silence_from_samples (a_sound, l_number_of_silence_samples) 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 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 local l_sound_length: INTEGER_32 i: INTEGER_32 do from i := 1 until i > l_sound_length loop a_sound [i] := amplify_sample (a_sound [i], a_amp_value) end ensure result_valid: old a_sound.count = a_sound.count end 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 local l_length: INTEGER_32 l_highest_number: INTEGER_16 l_wave: ARRAYED_LIST [INTEGER_16] i: INTEGER_32 do l_length := (Sample_rate // a_frequency) * Number_of_channels l_highest_number := get_max_number_from_amplitude (a_amplitude) create l_wave.make (l_length) from i := 0 until i >= (l_length) loop l_wave.extend ((sine ((i / l_length) * 2.to_double * Pi) * l_highest_number.to_double).rounded.to_integer_16) i := i + 1 end Result := l_wave ensure result_valid: Result.count = get_wave_length_from_frequency (a_frequency) end 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 local l_wave: ARRAYED_LIST [INTEGER_16] l_highest_number: INTEGER_16 l_length: INTEGER_32 i: INTEGER_32 do l_length := get_wave_length_from_frequency (a_frequency) create l_wave.make (l_length) l_highest_number := get_max_number_from_amplitude (a_amplitude) from i := 0 until i >= (l_length) // 2 loop l_wave.extend (l_highest_number) i := i + 1 end from i := i until i >= l_length loop l_wave.extend (- l_highest_number) i := i + 1 end Result := l_wave ensure result_valid: Result.count = get_wave_length_from_frequency (a_frequency) end 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 local l_length: INTEGER_32 l_half_length: INTEGER_32 l_highest_number: INTEGER_16 l_max_range: INTEGER_32 l_wave: ARRAYED_LIST [INTEGER_16] i: INTEGER_32 do l_length := (Sample_rate // a_frequency) * Number_of_channels l_half_length := l_length // 2 l_highest_number := get_max_number_from_amplitude (a_amplitude) l_max_range := l_highest_number * 2.to_integer_32 create l_wave.make (l_length) from i := 0 until i >= l_half_length loop l_wave.extend (- l_highest_number + ((i / l_half_length) * l_max_range.to_double).rounded.to_integer_16) i := i + 1 end from i := i until i >= l_length loop l_wave.extend (l_highest_number - (((i - l_half_length) / l_half_length) * l_max_range.to_double).rounded.to_integer_16) i := i + 1 end Result := l_wave ensure result_valid: Result.count = get_wave_length_from_frequency (a_frequency) end 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 local l_sample_index_begin: INTEGER_32 l_sample_index_end: INTEGER_32 l_number_of_fade_samples: INTEGER_32 l_logarithmic_percentage_list: LIST [REAL_64] i: INTEGER_32 do l_sample_index_begin := get_sample_index_from_percentage (a_sound.count, a_begin_length_percentage) l_sample_index_end := get_sample_index_from_percentage (a_sound.count, a_end_length_percentage) l_number_of_fade_samples := l_sample_index_end - l_sample_index_begin + 1 l_logarithmic_percentage_list := get_logarithmic_percentage_list_from_linear_percentage_range (a_begin_volume_percentage, a_end_volume_percentage, l_number_of_fade_samples) from i := l_sample_index_begin until i > l_sample_index_end loop a_sound [i] := amplify_sample (a_sound [i], l_logarithmic_percentage_list [i - l_sample_index_begin + 1]) i := i + 1 end ensure result_valid: old a_sound.count = a_sound.count end Max_amplitude: REAL_64 --maximum amplitude (in relative dB) that can be expressed using INTEGER_16. once Result := 20.to_double * log10 (2 ^ (Bits_per_sample - 1).to_double - 1.to_double) end 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 local i: INTEGER_32 j: INTEGER_32 l_length_difference: INTEGER_32 l_first_sample_number: INTEGER_32 do l_first_sample_number := get_sample_index_from_percentage (a_sound1.count, a_percentage) l_length_difference := l_first_sample_number + a_sound2.count - a_sound1.count if (l_length_difference > 0) then add_silence_from_samples (a_sound1, l_length_difference) end from i := l_first_sample_number j := 1 until i >= a_sound2.count loop a_sound1 [i] := add_up (a_sound1 [i], a_sound2 [j]) i := i + 1 j := j + 1 end ensure result_valid: a_sound1.count >= a_sound2.count end 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 local i: INTEGER_32 l_initial_sound_length: INTEGER_32 l_number_of_silence_samples: INTEGER_32 do l_initial_sound_length := a_sound.count l_number_of_silence_samples := get_number_of_samples_from_duration (a_seconds) from i := l_initial_sound_length until i >= l_number_of_silence_samples loop a_sound.extend (a_sound [(i \\ l_initial_sound_length) + 1]) i := i + 1 end ensure repetition_valid: a_sound.count >= get_number_of_samples_from_duration (a_seconds) and old a_sound.count <= a_sound.count end 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 local i: INTEGER_32 l_list: CHAIN [INTEGER_16] do l_list := a_sound.twin from i := 1 until i >= a_repetition loop a_sound.append (l_list) i := i + 1 end ensure repetition_valid: a_sound.count = old a_sound.count * a_repetition end 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) 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_SOUND_GENERATOR): 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_SOUND_GENERATOR): 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_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 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 -- 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_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) 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_SOUND_GENERATOR) -- 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_SOUND_GENERATOR -- 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_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) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: AUDIO_SOUND_GENERATOR -- 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_SOUND_GENERATOR -- 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_SOUND_GENERATOR 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_SOUND_GENERATOR -- 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} add_up (a_sample1: INTEGER_16; a_sample2: INTEGER_16): INTEGER_16 --makes the sum of two samples and caps if necessary, then returns the result. local l_mix_result: INTEGER_32 do l_mix_result := a_sample1 + a_sample2.to_integer_32 if (l_mix_result > max_integer_16.to_integer_32) then l_mix_result := max_integer_16.to_integer_32 elseif (l_mix_result < min_integer_16.to_integer_32) then l_mix_result := min_integer_16.to_integer_32 end Result := l_mix_result.to_integer_16 ensure result_valid: Result <= max_integer_16 and Result >= min_integer_16 end amplify_sample (a_sample: INTEGER_16; a_amp_value: REAL_64): INTEGER_16 --Multiplies a_sample by a_amp_value and returns the capped (if necessary) result. require amp_value_valid: a_amp_value >= 0.to_double local l_amp_result: REAL_64 do l_amp_result := a_sample.to_double * a_amp_value if (l_amp_result > max_integer_16.to_double) then l_amp_result := max_integer_16.to_double elseif (l_amp_result < min_integer_16.to_double) then l_amp_result := min_integer_16.to_double end Result := l_amp_result.rounded.to_integer_16 ensure result_valid: Result <= max_integer_16 and Result >= min_integer_16 end get_duration_from_number_of_samples (a_samples: INTEGER_32): REAL_64 -- calculates the duration a_samples samples takes to play. require samples_valid: a_samples >= 0 do Result := a_samples / Sample_rate ensure result_valid: Result >= 0.to_double end get_logarithmic_percentage_from_linear_percentage (a_linear_percentage: REAL_64): REAL_64 --returns the logarithmic percentage for perception of sound from a linear percentage. require linear_percentage_valid: a_linear_percentage <= 1.to_double and a_linear_percentage >= 0.to_double do Result := 0.001 * exp (6.908 * a_linear_percentage) if (Result > 1.to_double) then Result := 1.to_double end end get_logarithmic_percentage_list_from_linear_percentage_range (a_begin_volume_percentage: REAL_64; a_end_volume_percentage: REAL_64; a_number_of_samples: INTEGER_32): LIST [REAL_64] --Calculates the logarithmic value for each linear percentage value of 1 to a_number_of_sample --using the linear scaling from a_begin_volume_percentage to a_end_volume_percentage... yeah. require begin_volume_percentage_valid: a_begin_volume_percentage >= 0.to_double and a_begin_volume_percentage <= 1.to_double end_volume_percentage_valid: a_end_volume_percentage >= 0.to_double and a_end_volume_percentage <= 1.to_double number_of_samples_valid: a_number_of_samples >= 0 local l_logarithmic_percentage_list: LIST [REAL_64] l_percentage_iteration: REAL_64 i: INTEGER_32 do l_percentage_iteration := (a_end_volume_percentage - a_begin_volume_percentage) / a_number_of_samples.to_double create {ARRAYED_LIST [REAL_64]} l_logarithmic_percentage_list.make (a_number_of_samples) from i := 1 until i > a_number_of_samples loop l_logarithmic_percentage_list.extend (get_logarithmic_percentage_from_linear_percentage (a_begin_volume_percentage + (i.to_double * l_percentage_iteration))) i := i + 1 end Result := l_logarithmic_percentage_list ensure result_valid: Result.count = a_number_of_samples end get_max_number_from_amplitude (a_amplitude: REAL_64): INTEGER_16 --calculates the highest possible number for a given amplitude. require amplitude_valid: a_amplitude <= Max_amplitude and a_amplitude >= 0.to_double do Result := (10 ^ (a_amplitude / 20.to_double)).rounded.to_integer_16 end get_number_of_samples_from_duration (a_seconds: REAL_64): INTEGER_32 -- calculates the number of samples during a_seconds seconds.s require seconds_valid: a_seconds >= 0.to_double do Result := (a_seconds * Sample_rate.to_double).rounded ensure result_valid: Result >= 0 end get_sample_index_from_percentage (a_number_of_samples: INTEGER_32; a_percentage: REAL_64): INTEGER_32 -- calculates the index of the sample at a_percentages of a_number_of_samples. require percentage_valid: a_percentage >= 0.to_double do Result := (a_percentage * (a_number_of_samples - 1).to_double).rounded + 1 end get_wave_length_from_frequency (a_frequency: INTEGER_32): INTEGER_32 --calculates the wave's length (in samples) for a given frequency. require frequency_valid: a_frequency <= max_frequency and a_frequency >= Min_frequency do Result := (Sample_rate // a_frequency) * Number_of_channels end feature -- Debug print_wave (a_wave: CHAIN [INTEGER_16]) --prints the wave in the console local i: INTEGER_32 j: INTEGER_32 format_integer: FORMAT_INTEGER do create format_integer.make (6) print ("%N-------------------------------------------%N") print (a_wave.count) print (" samples vvvvvvvvvvvvvvvvvvvvv") print ("%N-------------------------------------------%N") from i := 1 until i > a_wave.count loop from j := 1 until j >= 8 loop if (i <= a_wave.count) then format_integer.left_justify print ("[" + format_integer.formatted (a_wave [i].to_integer_32) + "] ") i := i + 1 end j := j + 1 end; Io.put_new_line end print ("%N-------------------------------------------%N") print (a_wave.count) print (" samples ^^^^^^^^^^^^^^^^^^^^") print ("%N-------------------------------------------%N") end 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 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