# `MobAudioIo.Fake`
[🔗](https://github.com/GenericJam/mob_audio_io/blob/main/lib/mob_audio_io/fake.ex#L1)

A host backend for tests: `config :mob_audio_io, backend: MobAudioIo.Fake`.

It keeps streams in an Agent and answers like a device does:

  * `open/3` sends the owner `{:mob_audio_io, :started, stream, info}` and
    `close/1` sends `:stopped`. Output, input and duplex all open; input
    and duplex answer `{:error, :permission}` after `deny_permission/0`.
  * `status/0` reports 48 kHz streams with 192-frame callbacks whose
    `callbacks` and `frames` grow with wall time, so
    `MobAudioIo.SelfTest`-style checks work; inputs report the level set
    with `set_input_level/2`.
  * `devices/1` lists a built-in mic and speaker unless `set_devices/2`
    replaced them.
  * `decode_stream/3` decodes WAV files (16-, 24- and 32-bit PCM, 32-bit
    float) in Elixir, in a process per decode that sends the owner what
    a device's decode thread sends: `:decode_started`, `:decoded` chunks
    within the window (waiting for `decode_ack/2`), the end; with the
    same options (mono, `max_ms`), errors, `decode_cancel/1` and the
    owner's exit. With a `processor:` (a name here) no chunks are sent,
    as on a device where the processor takes them. `MobAudioIo.decode/2`
    runs on it.
  * Processors are the built-ins plus whatever `register_processor/1`
    added.

To drive a screen through what the hardware would do, `inject/3` sends
the stream's owner any message (`:device_lost`, `:interrupted`, `:xrun`,
`:post`, ...), as `Mob.Test.send_message/2` would.

State is global (one Agent): use it from `async: false` tests and call
`reset/0` in `setup`.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `deny_permission`

```elixir
@spec deny_permission() :: :ok
```

Input and duplex opens answer `{:error, :permission}` until `reset/0`.

# `fail_next_open`

```elixir
@spec fail_next_open(term()) :: :ok
```

The next `open/3` answers `{:error, reason}`.

# `inject`

```elixir
@spec inject(MobAudioIo.stream(), atom(), term()) :: :ok | {:error, :not_open}
```

Send the stream's owner `{:mob_audio_io, kind, stream, payload}`.

# `register_processor`

```elixir
@spec register_processor(String.t()) :: :ok
```

Make `name` openable, as an app's `mai_register` would.

# `reset`

```elixir
@spec reset() :: :ok
```

Back to no streams or decodes, the built-in processors and devices, the permission granted, no queued failure.

# `set_devices`

```elixir
@spec set_devices(:input | :output, [map()]) :: :ok
```

What `devices(direction)` lists.

# `set_input_level`

```elixir
@spec set_input_level(MobAudioIo.stream(), float()) :: :ok
```

The input level (dBFS) an input or duplex stream's status reports.

---

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