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

The audio of the HDMI/DisplayPort input, or of the USB speaker.

The LT7911EXC passes the source's audio on as I2S. This process captures it
while there are subscribers, in the `nano_kvm_go_audio` port program, as
interleaved PCM: signed 16-bit little-endian from the HDMI input, and from
the USB speaker in the format the USB gadget set it up with, at the rate
the target chose.

## Options

  * `:name` - the name to register, `NanoKVMGo.Audio` by default
  * `:source` - `:hdmi`, the HDMI/DisplayPort input (the default), or
    `:usb`, what the target plays to the NanoKVM's USB speaker (the
    `:speaker` function of the USB gadget, see `NanoKVMGo.USB`); run one
    process per source
  * `:device` - the ALSA capture device, by default the source's:
    `"hw:CARD=LT7911EXC,DEV=0"` or `"hw:CARD=UAC2Gadget,DEV=0"`
  * `:rate` - the sample rate to expect at first, 48000 by default, the
    rate most computers send at
  * `:channels` - the channels to capture where the device has them, 2 by
    default; the USB speaker has the channels it was set up with
  * `:period_ms` - the audio in each message, 1 to 1000 ms, 20 ms by
    default
  * `:video_device` - for the `:hdmi` source, the video capture device
    whose driver reports the input's sample rate, by default the board's,
    found each time the capture starts; `nil` to measure the rate instead
  * `:program` - the port program, the library's `nano_kvm_go_audio` by
    default

## Messages

Subscribers receive, with the `:source` of the process, so that one
subscriber can take both sources apart:

  * `{NanoKVMGo.Audio, {:format, source, format}}`, such as `%{rate: 48000,
    channels: 2, sample_format: :s16_le}`, when the capture starts, when
    subscribing to a running one, and when the source switches to another
    rate. The bridge passes the source's rate on and reports it to the
    video capture driver, which the port program follows. Where the driver
    does not report it, the program measures the rate the samples arrive
    at and snaps it to a standard one (44.1 kHz, 48 kHz, ...) within a few
    seconds. The USB speaker reports the rate the target chooses.
  * `{NanoKVMGo.Audio, {:pcm, source, data}}`, `:period_ms` of samples each

No samples come while the source sends no audio. A subscriber with more
than a second of audio waiting misses the next until it catches up; one
on another node gets them all, as its messages waiting cannot be counted.

# `format`

```elixir
@type format() :: %{
  rate: pos_integer(),
  channels: pos_integer(),
  sample_format: sample_format()
}
```

# `message`

```elixir
@type message() ::
  {NanoKVMGo.Audio, {:format, source(), format()} | {:pcm, source(), binary()}}
```

A message to subscribers, see the module documentation.

# `option`

```elixir
@type option() ::
  {:name, GenServer.name()}
  | {:source, source()}
  | {:device, String.t()}
  | {:rate, pos_integer()}
  | {:channels, pos_integer()}
  | {:period_ms, 1..1000}
  | {:video_device, Path.t() | nil}
  | {:program, Path.t()}
```

# `sample_format`

```elixir
@type sample_format() :: :s16_le | :s24_3le | :s24_le | :s32_le
```

Interleaved signed samples, as ALSA names them: S16_LE, S24_3LE...

# `source`

```elixir
@type source() :: :hdmi | :usb
```

# `child_spec`

```elixir
@spec child_spec([option()]) :: Supervisor.child_spec()
```

A child specification, with the `:name` as its id: one process per
source can run in the same supervisor.

# `format`

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

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

# `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 audio to the caller until it unsubscribes or exits.

# `unsubscribe`

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

Stops sending to the caller.

---

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