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.
Type | Intent | Optional | 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 |
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