chords_and_melody Program

Uses

  • program~~chords_and_melody~~UsesGraph program~chords_and_melody chords_and_melody module~audio_effects audio_effects program~chords_and_melody->module~audio_effects module~envelopes envelopes program~chords_and_melody->module~envelopes module~forsynth forsynth program~chords_and_melody->module~forsynth module~music music program~chords_and_melody->module~music module~music_common music_common program~chords_and_melody->module~music_common module~signals signals program~chords_and_melody->module~signals module~wav_file_class wav_file_class program~chords_and_melody->module~wav_file_class module~audio_effects->module~forsynth module~tape_recorder_class tape_recorder_class module~audio_effects->module~tape_recorder_class module~envelopes->module~forsynth module~envelopes->module~tape_recorder_class iso_fortran_env iso_fortran_env module~forsynth->iso_fortran_env module~music->module~envelopes module~music->module~forsynth module~music->module~music_common module~music->module~signals module~music->module~tape_recorder_class module~signals->module~envelopes module~signals->module~forsynth module~signals->module~tape_recorder_class module~wav_file_class->module~forsynth module~wav_file_class->iso_fortran_env module~wav_file_class->module~tape_recorder_class module~tape_recorder_class->module~forsynth

A sequence of synth chords is repeated, and the corresponding notes are played randomly by plucked strings.


Calls

program~~chords_and_melody~~CallsGraph program~chords_and_melody chords_and_melody proc~add_chord add_chord program~chords_and_melody->proc~add_chord proc~add_karplus_strong add_karplus_strong program~chords_and_melody->proc~add_karplus_strong proc~adsr_new ADSR_envelope%ADSR_new program~chords_and_melody->proc~adsr_new proc~apply_delay_effect apply_delay_effect program~chords_and_melody->proc~apply_delay_effect proc~close_wav_file WAV_file%close_WAV_file program~chords_and_melody->proc~close_wav_file proc~copy_section tape_recorder%copy_section program~chords_and_melody->proc~copy_section proc~create_wav_file WAV_file%create_WAV_file program~chords_and_melody->proc~create_wav_file proc~fr fr program~chords_and_melody->proc~fr proc~get_name WAV_file%get_name program~chords_and_melody->proc~get_name proc~mix_tracks tape_recorder%mix_tracks program~chords_and_melody->proc~mix_tracks proc~add_note add_note proc~add_chord->proc~add_note proc~finalize tape_recorder%finalize proc~close_wav_file->proc~finalize proc~write_normalized_data WAV_file%write_normalized_data proc~close_wav_file->proc~write_normalized_data proc~new tape_recorder%new proc~create_wav_file->proc~new proc~write_header WAV_file%write_header proc~create_wav_file->proc~write_header proc~add_sine_wave add_sine_wave proc~add_note->proc~add_sine_wave proc~clear_tracks tape_recorder%clear_tracks proc~new->proc~clear_tracks proc~adsr_level ADSR_envelope%ADSR_level proc~add_sine_wave->proc~adsr_level

Variables

Type Attributes Name Initial
real(kind=wp) :: chosen_note(0:3)
type(WAV_file) :: demo
real(kind=wp) :: dnote
type(ADSR_envelope) :: env
integer :: i
real(kind=wp) :: r
real(kind=wp) :: t

Source Code

program chords_and_melody
    use forsynth, only: wp
    use wav_file_class, only: WAV_file
    use signals, only: add_karplus_strong
    use music_common, only: MINOR_CHORD, MAJOR_CHORD
    use music, only: add_chord, fr
    use audio_effects, only: apply_delay_effect
    use envelopes, only: ADSR_envelope

    implicit none
    type(WAV_file) :: demo
    type(ADSR_envelope) :: env
    integer  :: i
    real(wp) :: t, dnote, r
    real(wp) :: chosen_note(0:3)

    print *, "**** Demo chords and melody ****"
    ! We create a new WAV file, and define the number of tracks and its duration:
    call demo%create_WAV_file('chords_and_melody.wav', tracks=2, duration=120._wp)
    ! We create an ADSR envelope that will be passed to signals (add_chord):
    call env%new(A=15._wp, D=40._wp, S=80._wp, R=15._wp)

    ! Notes duration in seconds:
    dnote = 3.0_wp

    associate(tape => demo%tape_recorder)

    print *, "Track 1: repeating Am C G Dm chords..."
    t = 0.0_wp
    call add_chord(tape, track=1, t1=t,      t2=t+dnote,   f=fr("A3"), Amp=1.0_wp, chord=MINOR_CHORD, envelope=env)
    call add_chord(tape, track=1, t1=t+dnote,   t2=t+2*dnote, f=fr("C3"), Amp=1.0_wp, chord=MAJOR_CHORD, envelope=env)
    call add_chord(tape, track=1, t1=t+2*dnote, t2=t+3*dnote, f=fr("G3"), Amp=1.0_wp, chord=MAJOR_CHORD, envelope=env)
    call add_chord(tape, track=1, t1=t+3*dnote, t2=t+4*dnote, f=fr("D3"), Amp=1.0_wp, chord=MINOR_CHORD, envelope=env)
    ! Repeat those four chords until the end of the track:
    do i = 1, 9
        call demo%copy_section(from_track=1, to_track=1, t1=t, t2=t+4*dnote, t3=4*dnote*i)
    end do

    print *, "Track 2: playing random A C G D notes using plucked strings..."
    dnote = dnote / 4
    ! An array of notes that can be played:
    chosen_note(0) = fr("A3")
    chosen_note(1) = fr("C3")
    chosen_note(2) = fr("G3")
    chosen_note(3) = fr("D3")

    do i = 0, 9*16
        t = dnote * i
        call random_number(r)
        call add_karplus_strong(tape, track=2, t1=t, t2=t+dnote, f=chosen_note(int(r*4)), Amp=1._wp)
    end do

    ! A double delay inspired by The Edge.
    ! Dotted quavers delay:
    call apply_delay_effect(tape, track=2, t1=0.0_wp, t2=demo%duration, delay=dnote*0.75_wp, Amp=0.45_wp)
    ! Plus a quavers delay:
    call apply_delay_effect(tape, track=2, t1=0.0_wp, t2=demo%duration, delay=dnote*0.50_wp, Amp=0.30_wp)

    end associate

    print *, "Final mix..."
    ! In the mix, chords are rather on the left
    ! and plucked strings on the right (and their level is lowered):
    call demo%mix_tracks(levels=[1._wp, 0.75_wp], pan=[-0.5_wp, +0.5_wp])
    call demo%close_WAV_file()

    print *,"You can now play the file ", demo%get_name()
end program chords_and_melody