drum_machine Program

Uses

  • program~~drum_machine~~UsesGraph program~drum_machine drum_machine module~forsynth forsynth program~drum_machine->module~forsynth module~signals signals program~drum_machine->module~signals module~wav_file_class wav_file_class program~drum_machine->module~wav_file_class iso_fortran_env iso_fortran_env module~forsynth->iso_fortran_env module~signals->module~forsynth module~envelopes envelopes module~signals->module~envelopes module~tape_recorder_class tape_recorder_class 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~envelopes->module~forsynth module~envelopes->module~tape_recorder_class module~tape_recorder_class->module~forsynth

A rhythm following a pattern stored in an array.


Calls

program~~drum_machine~~CallsGraph program~drum_machine drum_machine proc~add_karplus_strong_drum add_karplus_strong_drum program~drum_machine->proc~add_karplus_strong_drum proc~add_karplus_strong_drum_stretched add_karplus_strong_drum_stretched program~drum_machine->proc~add_karplus_strong_drum_stretched proc~close_wav_file WAV_file%close_WAV_file program~drum_machine->proc~close_wav_file proc~create_wav_file WAV_file%create_WAV_file program~drum_machine->proc~create_wav_file proc~get_name WAV_file%get_name program~drum_machine->proc~get_name proc~mix_tracks tape_recorder%mix_tracks program~drum_machine->proc~mix_tracks 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~clear_tracks tape_recorder%clear_tracks proc~new->proc~clear_tracks

Variables

Type Attributes Name Initial
type(WAV_file) :: demo
integer :: i
integer :: j
integer, dimension(3, 16) :: pattern = reshape([1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], shape(pattern), order=[2, 1])
real(kind=wp) :: step = 0.25_wp
real(kind=wp) :: t

Source Code

program drum_machine
    use forsynth, only: wp
    use wav_file_class, only: WAV_file
    use signals, only: add_karplus_strong_drum, add_karplus_strong_drum_stretched

    implicit none
    type(WAV_file) :: demo
    integer  :: i, j
    real(wp) :: t
    real(wp) :: step = 0.25_wp
    ! Each line is a different drum:
    integer, dimension(3, 16) :: pattern = reshape( [ &
        1,0,0,0, 1,0,0,1, 1,0,0,0, 1,0,0,1,   &
        0,1,0,0, 0,1,1,1, 0,1,0,0, 0,1,0,0,   &
        1,0,1,0, 1,0,1,0, 1,0,1,0, 1,0,1,0 ], &
        shape(pattern), order = [2, 1] )

    print *, "**** Demo Drum Machine****"
    ! We create a new WAV file, and define the number of tracks and its duration:
    call demo%create_WAV_file('drum_machine.wav', tracks=3, duration=33._wp)

    associate(tape => demo%tape_recorder)

    ! A rhythm following the above pattern:
    t = 0._wp
    do i = 1, 8
        do j = 1, 16
            ! We use one track for each kind of drum:
            if (pattern(1, j) == 1) then
                call add_karplus_strong_drum(          tape, track=1, t1=t, t2=t+2*step, P=150, Amp=1._wp)
            end if
            if (pattern(2, j) == 1) then
                call add_karplus_strong_drum(          tape, track=2, t1=t, t2=t+2*step, P=400, Amp=1._wp)
            end if
            if (pattern(3, j) == 1) then
                call add_karplus_strong_drum_stretched(tape, track=3, t1=t, t2=t+2*step, P=150, Amp=0.5_wp)
            end if
            t = t + step
        end do
    end do

    end associate

    print *, "Final mix..."
    ! The three drums are positionned on the left, the center and the right:
    call demo%mix_tracks(pan=[-0.5_wp, 0._wp, +0.5_wp])
    call demo%close_WAV_file()

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