wavex v0.2.7 Wavex.FormatChunk
Reading a format chunk.
Link to this section Summary
Link to this section Types
Link to this type
t()
t() :: %Wavex.FormatChunk{ bits_per_sample: pos_integer(), block_align: pos_integer(), byte_rate: pos_integer(), channels: pos_integer(), sample_rate: pos_integer() }
Link to this section Functions
Link to this function
read(binary)
read(binary()) :: {:ok, t(), binary()} | {:error, Wavex.Error.BlockAlignMismatch.t() | Wavex.Error.ByteRateMismatch.t() | Wavex.Error.UnexpectedEOF.t() | Wavex.Error.UnexpectedFormatSize.t() | Wavex.Error.UnexpectedID.t() | Wavex.Error.UnsupportedBitsPerSample.t() | Wavex.Error.UnsupportedFormat.t() | Wavex.Error.ZeroChannels.t()}
Read a format chunk.
Examples
sapp.org, 2018-04-30, Microsoft WAVE soundfile format
iex Wavex.FormatChunk.read(<<
...> 0x66, 0x6d, 0x74, 0x20, # f m t \s
...> 0x10, 0x00, 0x00, 0x00, # 16
...> 0x01, 0x00, 0x02, 0x00, # 1 2
...> 0x22, 0x56, 0x00, 0x00, # 22050
...> 0x88, 0x58, 0x01, 0x00, # 88200
...> 0x04, 0x00, 0x10, 0x00 # 4 16
...> >>)
{:ok,
%Wavex.FormatChunk{
bits_per_sample: 16,
block_align: 4,
byte_rate: 88_200,
channels: 2,
sample_rate: 22_050
}, ""}
Validate a format chunk.
Examples
iex> Wavex.FormatChunk.validate(%Wavex.FormatChunk{
...> bits_per_sample: 16,
...> block_align: 4,
...> byte_rate: 88_200,
...> channels: 2,
...> sample_rate: 22_050
...> })
:ok
The number of channels
must be positive. The next example gives an error
because channels
is 0
.
iex> Wavex.FormatChunk.validate(%Wavex.FormatChunk{
...> bits_per_sample: 16,
...> block_align: 4,
...> byte_rate: 88_200,
...> channels: 0,
...> sample_rate: 22_050
...> })
{:error, %Wavex.Error.ZeroChannels{}}
bits_per_sample
must be equal to 8
, 16
, or 24
. The following example
gives an error because bits_per_sample
is 32
.
iex> Wavex.FormatChunk.validate(%Wavex.FormatChunk{
...> bits_per_sample: 32,
...> block_align: 4,
...> byte_rate: 88_200,
...> channels: 2,
...> sample_rate: 22_050
...> })
{:error, %Wavex.Error.UnsupportedBitsPerSample{bits_per_sample: 32}}
block_align
must be equal to channels * bits_per_sample / 8
. The
following example gives an error because block_align
is 4
instead of
2 * 8 / 8 = 2
.
iex> Wavex.FormatChunk.validate(%Wavex.FormatChunk{
...> bits_per_sample: 8,
...> block_align: 4,
...> byte_rate: 88_200,
...> channels: 2,
...> sample_rate: 22_050
...> })
{:error, %Wavex.Error.BlockAlignMismatch{expected: 2, actual: 4}}
byte_rate
must be equal to sample_rate * block_align
. The following
example gives an error because byte_rate
is 88200
instead of
22050 * 2 = 44100
.
iex> Wavex.FormatChunk.validate(%Wavex.FormatChunk{
...> bits_per_sample: 8,
...> block_align: 2,
...> byte_rate: 88_200,
...> channels: 2,
...> sample_rate: 22_050
...> })
{:error, %Wavex.Error.ByteRateMismatch{expected: 44100, actual: 88200}}