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 feature --Access Sample_rate: INTEGER_32 = 44100 --number of samples playing per second Number_of_channels: INTEGER_32 = 1 --number of channels for the sound. Bits_per_sample: INTEGER_32 = 16 --number of bits per sample in the sound. Max_amplitude: REAL_64 --maximum amplitude (in relative dB) that can be expressed using INTEGER_16. max_frequency: INTEGER_32 --sample_rate // 2 Min_frequency: INTEGER_32 = 20 --can't really hear below this frequency max_integer_16: INTEGER_16 --Highest value for an INTEGER_16 min_integer_16: INTEGER_16 --Lowest value for an INTEGER_16 max_integer_32: INTEGER_32 --Highest value for an INTEGER_32 min_integer_32: INTEGER_32 --Lowest value for an INTEGER_32 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_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_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) 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 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 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 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 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 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_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 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 feature -- Debug print_wave (a_wave: CHAIN [INTEGER_16]) --prints the wave in the console invariant max_frequency_valid: max_frequency = Sample_rate // 2 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