Packages
wavex
0.4.3
0.14.1
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.4
0.9.3
0.9.1
0.9.0
0.8.1
0.8.0
0.7.3
0.7.2
0.7.1
0.5.5
0.5.4
0.5.3
0.5.2
0.5.0
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.0
0.3.0
0.2.16
0.2.13
0.2.11
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.2
0.2.1
0.2.0
0.1.8
0.1.0
Read WAVE LPCM data
Current section
Files
Jump to
Current section
Files
lib/wavex/data_chunk.ex
defmodule Wavex.DataChunk do
@moduledoc """
Reading a data chunk.
A data chunk normally contains:
- a `"data"` FourCC,
- the size of the actual audio data,
- the actual audio data.
"""
alias Wavex.Error.{UnexpectedEOF, UnexpectedFourCC}
alias Wavex.Utils
defstruct [:size, :data]
@type t :: %__MODULE__{size: non_neg_integer, data: binary}
@expected_data_four_cc "data"
@doc ~S"""
Read a data chunk.
## Examples
iex> Wavex.DataChunk.read(<<
...> 0x64, 0x61, 0x74, 0x61, # d a t a
...> 0x08, 0x00, 0x00, 0x00, # 8
...> 0x00, 0x00, 0x00, 0x00, # 0
...> 0x00, 0x00, 0x00, 0x00 # 0
...> >>)
{:ok, %Wavex.DataChunk{size: 8, data: <<0, 0, 0, 0, 0, 0, 0, 0>>}}
## Caveat
### "data" FourCC
Bytes 1-4 must read `"data"` to indicate a data chunk. A different value
results in an error.
iex> Wavex.DataChunk.read(<<"dat ", 2, 0, 0>>)
{:error, %Wavex.Error.UnexpectedFourCC{expected: "data", actual: "dat "}}
"""
@spec read(binary) :: {:ok, t} | {:error, UnexpectedEOF.t() | UnexpectedFourCC.t()}
def read(binary) when is_binary(binary) do
with {:ok, etc} <- Utils.read_fourCC(binary, @expected_data_four_cc),
<<size::32-little, etc::binary>> <- etc,
<<data::binary-size(size), _::binary>> <- etc do
{:ok, %__MODULE__{size: size, data: data}}
else
etc when is_binary(etc) -> {:error, %UnexpectedEOF{}}
error -> error
end
end
end