Adds on the track a triangle wave with an ADSR envelope:
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
type(tape_recorder), | intent(inout) | :: | tape | |||
integer, | intent(in) | :: | track | |||
real(kind=wp), | intent(in) | :: | t1 | |||
real(kind=wp), | intent(in) | :: | t2 | |||
real(kind=wp), | intent(in) | :: | f | |||
real(kind=wp), | intent(in) | :: | Amp | |||
type(ADSR_envelope), | intent(in), | optional | :: | envelope |
subroutine add_triangle_wave(tape, track, t1, t2, f, Amp, envelope) type(tape_recorder), intent(inout) :: tape integer, intent(in) :: track real(wp), intent(in) :: t1, t2, f, Amp type(ADSR_envelope), optional, intent(in) :: envelope ! Period in seconds: real(wp) :: tau ! Time in seconds: real(wp) :: t real(wp) :: signal ! ADSR Envelope value: real(wp) :: env real(wp) :: a, x integer :: i, n env = 1._wp ! Default value if no envelope is passed tau = 1.0_wp / f a = (2.0_wp * Amp) / (tau/2.0_wp) do concurrent(i = nint(t1*RATE) : nint(t2*RATE)-1) t = (i - nint(t1*RATE)) * dt if (present(envelope)) env = envelope%get_level(t1+t, t1, t2) ! Number of the half-period: n = int(t / (tau/2.0_wp)) ! Is n even or odd ? if (mod(n, 2) == 0) then x = t - n*(tau/2.0_wp) ; signal = a*x - Amp else x = t - n*(tau/2.0_wp) + tau/2.0_wp ; signal = - a*x + 3.0_wp*Amp end if tape%left(track, i) = tape%left(track, i) + signal * env tape%right(track, i) = tape%right(track, i) + signal * env end do end subroutine add_triangle_wave