# `TuningFork.Stage.Bed`
[🔗](https://github.com/jaman/tuning_fork/blob/v0.1.12/tuning_fork/lib/tuning_fork/stage/bed.ex#L1)

The looping layers under a `TuningFork.Stage`: named PCM buffers read from one
playhead, each at its own gain, replaceable at a bar boundary with a cross-fade.

A bed is `nil` when nothing loops. `chunk/3` produces the next chunk of the mix and the
bed to carry forward; `replace/3`, `gains/2` and `drop/2` change what plays. A layer
reads from the playhead less the frame it started at: 0 unless it was added with
`from_start: true`, which starts it at its own beginning when it begins to sound.

# `fade`

```elixir
@type fade() :: %{
  layers: %{required(term()) =&gt; layer()},
  position: non_neg_integer(),
  left: non_neg_integer(),
  total: pos_integer()
}
```

# `layer`

```elixir
@type layer() :: %{pcm: binary(), gain: float(), start: non_neg_integer() | :fresh}
```

# `t`

```elixir
@type t() :: %TuningFork.Stage.Bed{
  fade: nil | fade(),
  layers: %{required(term()) =&gt; layer()},
  pending:
    nil | {%{required(term()) =&gt; layer()}, pos_integer(), non_neg_integer()},
  position: non_neg_integer()
}
```

# `chunk`

```elixir
@spec chunk(t() | nil, pos_integer(), pos_integer()) :: {binary(), t() | nil}
```

The next `frames` frames of the mix, and the bed to carry forward.

# `drop`

```elixir
@spec drop(t() | nil, [term()]) :: t() | nil
```

The bed without the layers named in `names`; the others play on as they were.

# `gains`

```elixir
@spec gains(t() | nil, %{required(term()) =&gt; number()}) :: t() | nil
```

Set the gain of the named layers; names not playing are ignored.

# `position`

```elixir
@spec position(t() | nil) :: non_neg_integer() | nil
```

The playhead in frames, or `nil` for no bed.

# `replace`

```elixir
@spec replace(t() | nil, %{required(term()) =&gt; binary()}, keyword()) :: t()
```

Replace, or with `keep: true` extend, the playing set with `pcms`, a map of name to PCM.

`opts` are those of `TuningFork.Stage.layers/3` plus `:rate`, needed to turn `:fade_ms`
into frames.

---

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