# Seengreat DA7212 Audio Codec HAT Test Utility v34

This package installs the `sg-audio` and `sg-audio-test` utilities for the Seengreat DA7212 Audio Codec HAT.

It is designed for two use cases:

1. **Factory-programmed EEPROM boards**: the HAT EEPROM loads the audio overlay automatically. Install this package only for the test tools.
2. **Blank EEPROM prototypes**: install this package with `--with-overlay` to add `dtoverlay=iqaudio-codec` to `/boot/firmware/config.txt`.

## Install for production boards with EEPROM

```bash
unzip da7212-v34-dac100.zip
cd da7212-v34
sudo ./install.sh
sudo reboot
```

This mode does **not** edit `/boot/firmware/config.txt`.

## Install for blank EEPROM prototypes

```bash
sudo ./install.sh --with-overlay
sudo reboot
```

This adds a fallback block:

```text
dtparam=i2c_arm=on
dtparam=i2s=on
dtoverlay=iqaudio-codec
```

Remove the fallback block:

```bash
sudo ./install.sh --remove-overlay
sudo reboot
```

## Main tests

```bash
sg-audio status
sg-audio-test hp
sg-audio-test aux
sg-audio-test headset-mic
sg-audio-test onboard-mic
sg-audio-test onboard-mic-p
sg-audio-test onboard-mic-n
sg-audio-test onboard-mic-compare
sg-audio-test mp3 your_file.mp3
sg-audio-test leds
sg-audio-test button 10
```

## Default sound card

Set DA7212 as the default ALSA playback device:

```bash
sudo sg-audio set-default
```

Undo this setting:

```bash
sudo sg-audio unset-default
```

## V32 recording behavior

- `sg-audio-test` does **not** trim, cut, or discard the beginning of recordings.
- The old `SG_AUDIO_DISCARD_SECONDS` trimming behavior has been removed from the test path.
- Start speaking after the `Recording starts now` prompt.
- Default recording format is `48000 Hz / S16_LE / 2 channels`.

You can change recording duration or channel count:

```bash
SG_AUDIO_RECORD_SECONDS=15 sg-audio-test onboard-mic-p
SG_AUDIO_CHANNELS=1 sg-audio-test onboard-mic-p
```

## Onboard MIC tests

On the V1.1 schematic, the two onboard MEMS MICs are routed separately to DA7212 MIC2:

- `onboard-mic-p`: U1 -> MIC2_P
- `onboard-mic-n`: U2 -> MIC2_N
- `onboard-mic`: records the two onboard MICs together using MIC2 differential mode, U1/MIC2_P + U2/MIC2_N
- `onboard-mic-p`: records U1/MIC2_P only
- `onboard-mic-n`: records U2/MIC2_N only
- `onboard-mic-compare`: records U1/MIC2_P first, then U2/MIC2_N
- `onboard-mic-diff`: alias of `onboard-mic`, two-MIC combined mode


Note: U1 and U2 are connected to MIC2_P and MIC2_N of the same DA7212 MIC2 input. The combined mode is one differential/combined capture path, not two independent left/right microphone channels.

Generated files:

```text
/tmp/sg_onboard_mic_two_mics_diff_48k_s16.wav
/tmp/sg_onboard_mic_u1_mic2_p_48k_s16.wav
/tmp/sg_onboard_mic_u2_mic2_n_48k_s16.wav
```

## Notes

The audio overlay used by the product is `iqaudio-codec`. Depending on EEPROM contents and Raspberry Pi OS behavior, the ALSA card may appear as `Zero`, `IQaudIOCODEC`, or `HAT [SG DA7212 HAT]`; the utility scripts support these names.


## V33 additions: SPK playback and volume control

This package is based on the V32 two-MIC default build and adds two SPK-related commands:

```bash
sg-audio volume spk <0-63>
```

This adjusts the DA7212 `Lineout Volume`, which is used by the DA7212 direct SPK / Lineout output. It is independent from the external APA2068 amplifier volume control.

```bash
sg-audio-test spk xxx.wav
```

This switches the profile to `spk` and plays the specified WAV file through the DA7212 SPK / Lineout output. If no file is specified, it plays the bundled test WAV file.

Recommended test example:

```bash
sg-audio spk
sg-audio volume dac 110
sg-audio volume spk 55
sg-audio-test spk /path/to/test.wav
```


## V34 addition: DAC playback volume cap

V34 caps DA7212 DAC playback volume at 100 to reduce high-volume noise and input overdrive when feeding the external speaker amplifier.

```bash
sg-audio volume dac 100
sg-audio volume dac 127   # automatically capped to 100
```

Playback profiles such as `hp`, `spk`, and `speakers` also force the DAC playback volume to 100 after the profile is applied.

Recommended speaker test:

```bash
sg-audio speakers
sg-audio volume dac 100
sg-audio-test spk /path/to/test.wav
```
