# `Membrane.WAV.Parser`
[🔗](https://github.com/membraneframework/membrane_wav_plugin/blob/v0.10.3/lib/membrane_wav/parser.ex#L1)

Element responsible for parsing WAV files.

It requires WAV file in uncompressed, PCM format on the input (otherwise error is raised) and
provides raw audio on the output. WAV header is parsed to extract metadata of the raw audio format.
Then it is dropped and only samples are sent to the next element.

The element has one option - `frames_per_buffer`. User can specify number of frames sent in
one buffer when demand unit on the output is `:buffers`. One frame contains
`bits per sample * number of channels` bits.

## WAV Header

```
   0                   4                   8                   12                  16
   _________________________________________________________________________________
0  |                   |                   |                   |                   |
   |      "RIFF"       |    file length    |      "WAVE"       |       "fmt "      |
   |                   |                   |                   |                   |
   |___________________|___________________|___________________|___________________|
16 |                   |         |         |                   |                   |
   |   format chunk    | format  |number of|      sample       | data transmission |
   |      length       |(1 - PCM)|channels |       rate        |       rate        |
   |___________________|_________|_________|___________________|___________________|
32 |  block  |  bits   |                   |                   |                   |
   |  align  |  per    |      "fact"       |     fact chunk    |    samples per    |
   |  unit   | sample  |                   |       length      |      channel      |
   |_________|_________|___________________|___________________|___________________|
48 |                   |                   |                                       |
   |      "data"       |    data length    |                 DATA                  |
   |                   |     in bytes      |                                       |
   |___________________|___________________|_______________________________________|
```
Header may contain additional bytes between `bits per sample` and `"fact"` in case of `format`
different from 1 (1 represents PCM / uncompressed format). Length of block from `format` until
`"fact"` is present in `format chunk length` (it is 16 for PCM).

Blocks from byte 36 to 48 are optional. There can be additional bytes after `samples per
channel` if `fact chunk length` contains number bigger than 4.

## Parsing

Stages of parsing:
- `:init` - Parser waits for the first 16 bytes. After getting them, it parses these bytes
  to ensure that it is a WAV file. After parsing, the stage is set to `:format`.
- `:format` - Parser waits for the next 28 bytes - Parser  then knows `format chunk length` and `format`,
  so it is able to raise an error in case of different `format` than 1 (PCM) / 3 (IEEE float) or different
  length than 16 (for PCM). Then it parses it and create `Membrane.RawAudio` struct with audio format to send it
  as stream_format to the next element. Stage is set to `:fact` or `:data` depending on last 8 bytes.
- `:fact` - Parser waits for `8 + fact chunk length` bytes. It  parses them only to check if
  the header is correct, but does not use that data in any way. After parsing, the stage is
  set to `:data`.
- `:data` - header is already fully parsed. All new data from the input is sent to the output.

## Pads

### `:input`

Accepted formats:
```
RemoteStream
```

Direction: | `:input`
Availability: | `:always`
Flow control: | `:auto`

### `:output`

Accepted formats:
```
RawAudio
```

Direction: | `:output`
Availability: | `:always`
Flow control: | `:auto`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
