write_header Subroutine

public subroutine write_header(self)

Creates the 44 bytes WAV header and prints some information:

Type Bound

WAV_file

Arguments

Type IntentOptional Attributes Name
class(WAV_file), intent(inout) :: self

WAV parameters:


Number of channels: 1 for mono, 2 for stereo, etc.


Called by

proc~~write_header~~CalledByGraph proc~write_header WAV_file%write_header proc~create_wav_file WAV_file%create_WAV_file proc~create_wav_file->proc~write_header program~all_signals all_signals program~all_signals->proc~create_wav_file program~arpeggios arpeggios program~arpeggios->proc~create_wav_file program~blues blues program~blues->proc~create_wav_file program~chords_and_melody chords_and_melody program~chords_and_melody->proc~create_wav_file program~demo_effects demo_effects program~demo_effects->proc~create_wav_file program~doppler_effect doppler_effect program~doppler_effect->proc~create_wav_file program~drone_music drone_music program~drone_music->proc~create_wav_file program~drum_machine drum_machine program~drum_machine->proc~create_wav_file program~misc_sounds misc_sounds program~misc_sounds->proc~create_wav_file program~shepard_risset_glissando shepard_risset_glissando program~shepard_risset_glissando->proc~create_wav_file program~shepard_scale shepard_scale program~shepard_scale->proc~create_wav_file

Source Code

    subroutine write_header(self)
        class(WAV_file), intent(inout)  :: self
        !****************
        ! WAV parameters:
        !****************
        ! Number of channels: 1 for mono, 2 for stereo, etc.
        integer(INT16), parameter :: CHANNELS = 2
        integer(INT16), parameter :: BITS_PER_SAMPLE = 16
        integer(INT64) :: DATA_BYTES
        integer(INT32) :: file_size, bytes_per_second, data_size
        integer(INT16) :: bytes_per_sample

        print *, "Nb of tracks, excluding track 0:", self%tracks

        DATA_BYTES = (BITS_PER_SAMPLE / 8) * CHANNELS * self%samples
        print *, "Used RAM:   ", DATA_BYTES * self%tracks, "bytes"
        print *, "File size ~ ", DATA_BYTES, "bytes"

        associate(u => self%fileunit)
            ! RIFF format:
            write(u, iostat=status) "RIFF"
            ! Remaining bytes after this data:
            file_size = 36 + DATA_BYTES
            write(u, iostat=status) file_size

            write(u, iostat=status) "WAVE"

            ! ***** First sub-chunk *****
            ! Don't remove the final space in the string!
            write(u, iostat=status) "fmt "
            ! Remaining bytes in this sub-chunk, 16 for PCM (32 bits integer):
            write(u, iostat=status) 16_INT32
            ! Encoding is 1 for PCM (16 bits integer):
            write(u, iostat=status) 1_INT16

            write(u, iostat=status) int(CHANNELS, kind=INT16)
            ! Sampling frequency:
            write(u, iostat=status) int(RATE, kind=INT32)

            bytes_per_second = RATE * CHANNELS * (BITS_PER_SAMPLE / 8)
            write(u, iostat=status) bytes_per_second

            bytes_per_sample = CHANNELS * (BITS_PER_SAMPLE / 8)
            write(u, iostat=status) bytes_per_sample

            write(u, iostat=status) BITS_PER_SAMPLE

            ! ***** Second sub-chunk *****
            write(u, iostat=status) "data"

            data_size = self%samples * CHANNELS * (BITS_PER_SAMPLE / 8)
            write(u, iostat=status) data_size
        end associate
    end subroutine write_header