# `NanoKVMGo.Microphone`
[🔗](https://github.com/cocoa-xu/nano_kvm_go/blob/v0.4.0/lib/nano_kvm_go/microphone.ex#L1)

The microphone the target computer sees, with the `:microphone` function
of the USB gadget (see `NanoKVMGo.USB`).

`play/2` sends it PCM: interleaved samples in the microphone's format, the
channels and sample size the gadget set it up with, at the rate the target
chose. `format/1` tells that format, and subscribers hear of it.
The `nano_kvm_go_playback` port program keeps the microphone's stream
running: between samples the target hears silence, and samples that come
in faster than the target takes them are dropped once more than 200 ms
wait, so the sound never lags. The program runs while there are
subscribers, and from `play/2` until `:idle_ms` without samples.

## Messages

Subscribers receive `{NanoKVMGo.Microphone, {:format, format}}`, a
`t:NanoKVMGo.Audio.format/0`, when the program starts, when subscribing to a
running one, and when the target chooses another rate. Samples sent in
the old format until then play at the wrong speed.

## Options

  * `:name` - the name to register, `NanoKVMGo.Microphone` by default
  * `:device` - the ALSA playback device, `"hw:CARD=UAC2Gadget,DEV=0"` by
    default
  * `:idle_ms` - how long to keep the stream without samples and
    subscribers, 10 s by default
  * `:program` - the port program, the library's `nano_kvm_go_playback`
    by default

# `message`

```elixir
@type message() :: {NanoKVMGo.Microphone, {:format, NanoKVMGo.Audio.format()}}
```

A message to subscribers, see the module documentation.

# `option`

```elixir
@type option() ::
  {:name, GenServer.name()}
  | {:device, String.t()}
  | {:idle_ms, pos_integer()}
  | {:program, Path.t()}
```

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `format`

```elixir
@spec format(GenServer.server()) :: NanoKVMGo.Audio.format() | nil
```

The format of the running stream, `nil` without one.

# `play`

```elixir
@spec play(GenServer.server(), binary()) :: :ok
```

Plays samples on the microphone, in its format, any number of frames.

# `start_link`

```elixir
@spec start_link([option()]) :: GenServer.on_start()
```

Starts the process. See the module documentation for the options.

# `subscribe`

```elixir
@spec subscribe(GenServer.server()) :: :ok
```

Sends the format to the caller until it unsubscribes or exits, and keeps
the stream running meanwhile.

# `unsubscribe`

```elixir
@spec unsubscribe(GenServer.server()) :: :ok
```

Stops sending to the caller.

---

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