sonify_from_file Subroutine

public subroutine sonify_from_file(input_file, output_file, autocenter, downsampling, repetitions)

Read a text file containing only one column of reals, without header, and convert it to a WAV file. If your file contains several fields (columns), you must extract the field to sonify. In Unix-like systems, it can be easily done, for example: $ cut -f 4 -d " " data.txt > column.txt The file should contain several 100,000 lines as the WAV will use generally 44,100 samples per second. - If your signal is not centered around zero, you can use the autocenter option. - Downsampling allows to take only each Mth sample and can be used to make a low tone higher. - The repetitions argument allows to repeat the signal N times, which can be interesting if it is too short.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input_file
character(len=*), intent(in), optional :: output_file
logical, intent(in), optional :: autocenter
integer, intent(in), optional :: downsampling
integer, intent(in), optional :: repetitions

Calls

proc~~sonify_from_file~~CallsGraph proc~sonify_from_file sonify_from_file proc~sonify_from_array sonify_from_array proc~sonify_from_file->proc~sonify_from_array proc~close_wav_file WAV_file%close_WAV_file proc~sonify_from_array->proc~close_wav_file proc~create_wav_file WAV_file%create_WAV_file proc~sonify_from_array->proc~create_wav_file proc~get_name WAV_file%get_name proc~sonify_from_array->proc~get_name proc~mix_tracks tape_recorder%mix_tracks proc~sonify_from_array->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

Called by

proc~~sonify_from_file~~CalledByGraph proc~sonify_from_file sonify_from_file program~sonify sonify program~sonify->proc~sonify_from_file

Source Code

    subroutine sonify_from_file(input_file, output_file, autocenter, downsampling, repetitions)
        character(*), intent(in)           :: input_file
        character(*), intent(in), optional :: output_file
        logical, intent(in), optional :: autocenter
        integer, intent(in), optional :: downsampling
        integer, intent(in), optional :: repetitions    ! Including the original
        ! Autocenter, downsampling, repetitions variables:
        logical  :: ac
        integer  :: down, rep
        integer  :: i
        ! For reading the input file:
        logical  :: found
        integer  :: file_unit, ios
        integer  :: n                   ! Number of data lines
        real(wp) :: y                   ! Data to read
        real(wp), dimension(:), allocatable :: array    ! Unknown size
        ! Name of the output file:
        character(:), allocatable :: wav_file

        inquire(file=input_file, exist=found)
        if (found) then
            ! First scan: how many lines n does the file contain?
            n = 0
            open(newunit=file_unit, file=input_file, action="read")
            do
                read(file_unit, *, iostat=ios) y
                if (ios /= 0) exit      ! End of file, if no other problem

                n = n + 1
            end do
            allocate(array(1:n))

            ! Second scan: we can now read the data and put them in the array
            rewind(file_unit)
            do i = 1, n
                read(file_unit, *, iostat=ios) array(i)
                if (ios /= 0) exit
            end do
            close(file_unit)

            ac = .false.
            if (present(autocenter)) then
                if (autocenter) ac = .true.
            end if

            if (present(downsampling)) then
                down = downsampling
            else
                down = 1
            end if

            if (present(repetitions)) then
                rep = repetitions
            else
                rep = 1
            end if

            if (present(output_file)) then
                wav_file = trim(output_file)
            else
                ! Default name:
                wav_file = "sonification.wav"
            end if

            call sonify_from_array(signal=array, output_file=wav_file, &
                 & autocenter=ac, downsampling=down, repetitions=rep)
        else
            error stop "File not found!"
        end if
    end subroutine sonify_from_file