sonify Program

Uses

  • program~~sonify~~UsesGraph program~sonify sonify module~forsynth forsynth program~sonify->module~forsynth module~sonification sonification program~sonify->module~sonification iso_fortran_env iso_fortran_env module~forsynth->iso_fortran_env module~sonification->module~forsynth module~wav_file_class wav_file_class module~sonification->module~wav_file_class module~wav_file_class->module~forsynth module~wav_file_class->iso_fortran_env module~tape_recorder_class tape_recorder_class module~wav_file_class->module~tape_recorder_class module~tape_recorder_class->module~forsynth

A command to sonify a data file. Example: $ fpm run sonify -- -i mydata.txt (supposing the file is in the main directory of ForSynth) You can get help by typing: $ fpm run sonify -- --help


Calls

program~~sonify~~CallsGraph program~sonify sonify proc~print_copyright print_copyright program~sonify->proc~print_copyright proc~print_options print_options program~sonify->proc~print_options proc~read_argument read_argument program~sonify->proc~read_argument proc~sonify_from_file sonify_from_file program~sonify->proc~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

Variables

Type Attributes Name Initial
character(len=:), allocatable :: arg
logical :: autocenter = .false.
integer :: down = 1
integer :: i
character(len=:), allocatable :: input_file
integer :: nb
logical :: output = .false.
character(len=:), allocatable :: output_file
integer :: rep = 1
integer :: status
character(len=:), allocatable :: str

Functions

function read_argument(i, status)

Returns the argument number i of the command line.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: i
integer, intent(out) :: status

Return Value character(len=:), allocatable


Subroutines

subroutine print_copyright()

Arguments

None

subroutine print_options()

Arguments

None

Source Code

program sonify
    use forsynth, only: RATE
    use sonification, only: sonify_from_file

    implicit none
    integer :: i, nb, status
    character(:), allocatable :: input_file, output_file, arg, str
    ! Default values:
    logical :: output = .false.         ! Is an output filename provided?
    logical :: autocenter = .false.
    integer :: down = 1                 ! Downsampling
    integer :: rep = 1                  ! Repetitions

    nb = command_argument_count()
    if (nb == 0) then
        call print_options()
        call print_copyright()
        stop      ! Nothing to do
    end if

    ! Scan the command line arguments:
    i = 0
    do while (i < nb)
        i = i + 1
        arg = read_argument(i, status)

        select case(trim(arg))
            case("-i", "--input")
                i = i + 1
                input_file = read_argument(i, status)
                if (status /= 0) then
                    print '(A)', "Problem with -i"
                    stop
                end if
            case("-o", "--output")
                i = i + 1
                output_file = read_argument(i, status)
                if (status /= 0) then
                    print '(A)', "Problem with -o"
                    stop
                end if
                output = .true.
            case("-c", "--center")
                autocenter = .true.
            case("-d", "--downsampling")
                i = i + 1
                str = read_argument(i, status)
                read(str, *) down
                if (status /= 0) then
                    print '(A)', "Problem with -d"
                    stop
                end if
            case("-r", "--repetitions")
                i = i + 1
                str = read_argument(i, status)
                read(str, *) rep
                if (status /= 0) then
                    print '(A)', "Problem with -r"
                    stop
                end if
            case("-h", "--help")
                call print_options()
                stop
            case("-v", "--version")
                call print_copyright()
                stop
            case default
                print '(2A)', "Unknown option ignored: ", trim(arg)
        end select
    end do

    if (output) then
        call sonify_from_file(input_file=trim(input_file), output_file=trim(output_file), &
             & autocenter=autocenter, downsampling=down, repetitions=rep)
    else
        call sonify_from_file(input_file=trim(input_file), &
             & autocenter=autocenter, downsampling=down, repetitions=rep)
    end if

contains

    !> Returns the argument number i of the command line.
    function read_argument(i, status)
        integer, intent(in)  :: i
        integer, intent(out) :: status
        character(:), allocatable :: read_argument, arg
        integer :: length

        ! We allocate the string with the needed length before reading it:
        call get_command_argument(i, length=length)
        allocate(character(length) :: arg)
        call get_command_argument(i, value=arg, status=status)

        read_argument = arg
    end function read_argument

    subroutine print_options
        print '(A)'
        print '(A)', "Usage: sonify -i FILE [OPTION]..."
        print '(A)'
        print '(A,I0,A)', "Sonify a data FILE to a WAV file using ", RATE, " samples per second."
        print '(A)'
        print '(A)', "Options :"
        print '(A)', "  -i, --input=FILE          name of the data file"
        print '(A)', "  -o, --output=FILE         name of the WAV file (default: sonification.wav)"
        print '(A)', "  -c, --center              autocenter the signal around zero"
        print '(A)', "  -d, --downsampling=NUM    downsampling factor (integer NUM>=1)"
        print '(A)', "  -r, --repetitions=NUM     repeat the data NUM times"
        print '(A)', "  -h, --help                display this help message"
        print '(A)', "  -v, --version             display copyright and license information"
        print '(A)'
        print '(A)', "Example: sonify -i itot.dat -o itot.wav -c -d 2 -r 5 "
        print '(A)'
        print '(A)', "Homepage: <https://github.com/vmagnin/forsynth>"
        print '(A)'
    end subroutine

    subroutine print_copyright
        print '(A)', "Sonify is part of the Fortran ForSynth project"
        print '(A)', "Copyright (C) 2025 Vincent Magnin"
        print '(A)', "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
        print '(A)', "This is free software: you are free to change and redistribute it."
        print '(A)', "There is NO WARRANTY, to the extent permitted by law."
    end subroutine

end program sonify