Packages
wavex
0.7.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/chunk/data.ex
defmodule Wavex.Chunk.Data do
@moduledoc """
Read a data chunk.
"""
alias Wavex.Error.{UnexpectedEOF, UnexpectedFourCC}
alias Wavex.Utils
@enforce_keys [
:size,
:data
]
defstruct [
:size,
:data
]
@type t :: %__MODULE__{size: non_neg_integer, data: binary}
@doc ~S"""
Read a data chunk.
## Examples
iex> Wavex.Chunk.Data.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.Chunk.Data{size: 8, data: <<0, 0, 0, 0, 0, 0, 0, 0>>}}
## Caveat
### Chunk ID
Bytes 1-4 must read `"data"` to indicate a data chunk. A different value
results in an error.
iex> Wavex.Chunk.Data.read(<<
...> 0x64, 0x61, 0x74, 0x20, # d a t \s
...> 0x08, 0x00, 0x00, 0x00, # 8
...> 0x00, 0x00, 0x00, 0x00, # 0
...> 0x00, 0x00, 0x00, 0x00 # 0
...> >>)
{:error, %Wavex.Error.UnexpectedFourCC{expected: "data", actual: "dat "}}
"""
@spec read(binary) :: {:ok, t} | {:error, UnexpectedEOF.t() | UnexpectedFourCC.t()}
def read(<<
data_id::binary-size(4),
size::32-little,
data::binary-size(size),
_::binary
>>) do
with :ok <- Utils.verify_four_cc(data_id, "data") do
{:ok, %__MODULE__{size: size, data: data}}
end
end
def read(binary) when is_binary(binary), do: {:error, %UnexpectedEOF{}}
end