diff options
| author | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
|---|---|---|
| committer | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
| commit | c25cbdf6fb6d946175f4335fab77bfea601b56db (patch) | |
| tree | 6f7a94b7b81f999e434f61b73d47e337dfd26bf2 /lib/readwav.f90 | |
| parent | 71f5689252c9f13e70e1c24856904475b2307809 (diff) | |
| parent | 0685fcada9f95fe90f70b387cd7d757da8209624 (diff) | |
Merge tag 'debian/3.2.0_rc1+dfsg-2' into debian/trixie-backportsdebian/trixie-backports
tagging wsjtx 3.2.0~rc1+dfsg-2
Diffstat (limited to 'lib/readwav.f90')
| -rw-r--r-- | lib/readwav.f90 | 142 |
1 files changed, 133 insertions, 9 deletions
diff --git a/lib/readwav.f90 b/lib/readwav.f90 index 25369049..506bf3a9 100644 --- a/lib/readwav.f90 +++ b/lib/readwav.f90 @@ -17,6 +17,7 @@ ! end do ! module readwav + use, intrinsic :: iso_fortran_env, only: int64 implicit none type format_chunk @@ -32,13 +33,15 @@ module readwav integer :: lun type(format_chunk) :: audio_format integer :: data_size + integer :: data_bytes_remaining contains procedure :: read + procedure :: read_samples end type wav_header private contains - subroutine read (this, filename) + subroutine read (this, filename, status, message) implicit none type riff_descriptor @@ -48,26 +51,147 @@ contains class(wav_header), intent(inout) :: this character(len=*), intent(in) :: filename + integer, intent(out), optional :: status + character(len=*), intent(out), optional :: message - integer :: filepos + integer :: ios + integer(int64) :: filepos, filesize, data_end, nextpos, padded_size type(riff_descriptor) :: desc character(len=4) :: riff_type + logical :: format_found this%lun=26 - open (unit=this%lun, file=filename, access='stream',status='old') - read (unit=this%lun) desc,riff_type + this%data_size = 0 + this%data_bytes_remaining = 0 + this%audio_format = format_chunk(0, 0, 0, 0, 0, 0) + format_found = .false. + call set_result(0, '') + + open (unit=this%lun, file=filename, access='stream', form='unformatted', & + status='old', action='read', iostat=ios) + if (ios /= 0) then + call fail('cannot open file') + return + end if + inquire (unit=this%lun, size=filesize) + if (filesize < 12) then + call fail('file is too short for a RIFF/WAVE header') + return + end if + + read (unit=this%lun, iostat=ios) desc, riff_type + if (ios /= 0) then + call fail('cannot read RIFF/WAVE header') + return + end if + if (desc%id /= 'RIFF' .or. riff_type /= 'WAVE') then + call fail('not a RIFF/WAVE file') + return + end if inquire (unit=this%lun, pos=filepos) do - read (unit=this%lun, pos=filepos) desc + if (filepos + 8_int64 > filesize + 1_int64) then + call fail('missing data chunk') + return + end if + read (unit=this%lun, pos=filepos, iostat=ios) desc + if (ios /= 0) then + call fail('cannot read WAV chunk header') + return + end if inquire (unit=this%lun, pos=filepos) + if (desc%size < 0) then + call fail('WAV chunk has an invalid size') + return + end if + data_end = filepos + int(desc%size, int64) + if (data_end > filesize + 1_int64) then + call fail('WAV chunk extends beyond end of file') + return + end if + padded_size = int(desc%size, int64) + if (mod(padded_size, 2_int64) /= 0) padded_size = padded_size + 1_int64 + nextpos = filepos + padded_size if (desc%id .eq. 'fmt ') then - read (unit=this%lun) this%audio_format + if (desc%size < 16) then + call fail('fmt chunk is too short') + return + end if + read (unit=this%lun, iostat=ios) this%audio_format + if (ios /= 0) then + call fail('cannot read fmt chunk') + return + end if + if (this%audio_format%audio_format /= 1 .or. & + this%audio_format%num_channels /= 1 .or. & + this%audio_format%bits_per_sample /= 16) then + call fail('unsupported WAV format; expected mono 16-bit PCM') + return + end if + if (this%audio_format%sample_rate /= 11025 .and. & + this%audio_format%sample_rate /= 12000) then + call fail('unsupported WAV sample rate') + return + end if + format_found = .true. else if (desc%id .eq. 'data') then + if (.not. format_found) then + call fail('data chunk appears before fmt chunk') + return + end if this%data_size = desc%size - exit + this%data_bytes_remaining = desc%size + call set_result(0, '') + return + end if + if (nextpos > filesize + 1_int64) then + call fail('WAV chunk extends beyond end of file') + return end if - filepos = filepos + (desc%size + 1) / 2 * 2 ! pad to even alignment + filepos = nextpos end do - return + + contains + + subroutine fail(text) + character(len=*), intent(in) :: text + + close(unit=this%lun, iostat=ios) + this%lun = -1 + call set_result(1, text) + end subroutine fail + + subroutine set_result(code, text) + integer, intent(in) :: code + character(len=*), intent(in) :: text + + if (present(status)) status = code + if (present(message)) message = text + end subroutine set_result end subroutine read + + subroutine read_samples(this, samples, samples_read, status, message) + class(wav_header), intent(inout) :: this + integer*2, intent(out) :: samples(:) + integer, intent(out) :: samples_read + integer, intent(out), optional :: status + character(len=*), intent(out), optional :: message + + integer :: ios + + samples = 0 + samples_read = min(size(samples), this%data_bytes_remaining / 2) + if (present(status)) status = 0 + if (present(message)) message = '' + if (samples_read == 0) return + + read(unit=this%lun, iostat=ios) samples(:samples_read) + if (ios /= 0) then + samples_read = 0 + if (present(status)) status = 1 + if (present(message)) message = 'cannot read WAV sample data' + return + end if + this%data_bytes_remaining = this%data_bytes_remaining - 2 * samples_read + end subroutine read_samples end module readwav |
