summaryrefslogtreecommitdiff
path: root/lib/readwav.f90
blob: 506bf3a96ae69353723a904fb7b06ca748d24179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
!
! readwav - open and read the header of a WAV format file
!
! On successful exit the file is left positioned at the start of the
! data.
!
! Example of usage:
!
!  use readwav
!  integer*2 sample
!  type(wav_header) wav
!  call wav%read ('file.wav')
!  write (*,*) 'Sample rate is: ', wav%audio_format%sample_rate
!  do i=0,wav%data_size
!    read (unit=wav%lun) sample
!    ! process sample
!  end do
!
module readwav
  use, intrinsic :: iso_fortran_env, only: int64
  implicit none

  type format_chunk
     integer*2 audio_format
     integer*2 num_channels
     integer sample_rate
     integer byte_rate
     integer*2 block_align
     integer*2 bits_per_sample
  end type format_chunk
  
  type, public :: wav_header
     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, status, message)
    implicit none

    type riff_descriptor
       character(len=4) :: id
       integer :: size
    end type riff_descriptor

    class(wav_header), intent(inout) :: this
    character(len=*), intent(in) :: filename
    integer, intent(out), optional :: status
    character(len=*), intent(out), optional :: message

    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
    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
       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
          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
          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 = nextpos
    end do

  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