# Lambda Match Audio / Fingerprint

## Note
The pyacrcloud library which is used for audio fingerprinting is not currently compatible with Apple Silicon/M1.
If you are using an Apple Silicon/M1 Mac, use the `make docker_*` commands to run inside a Docker container.

## What causes this lambda to run?
An execution of [sfn-match-audio-fingerprint](https://github.com/theorchard/terraform-infra/blob/master/prod/lambda-content/match-audio/sfn-content-match-audio.json) calls it with a payload shaped like:
```
{
    "bucket": "prod-orcd-mezzanine-assets",
    "filename": "09a8f806_267f_454a_b549_3c448ab5f639.flac"
}
```

## What does this lambda do?
1. Download the s3 file described by the `bucket` and `filename` payload fields.
2. Use https://pypi.org/project/pyacrcloud/ to create a fingerprint from the downloaded file.
3. Convert fingerprint bytes to a base64 encoded string and return a payload shaped like:
   ```
   {
       "audio_fingerprint": "/gAAAHBUOUhaAQAAcnFqSNQBAQD7tGVIpQIBAPyCmkjcAAQAi/spSKYDBQCOMKhIoAAJAAj1EEh3AwoAMt2wSD4CCwCLxatICAMLAJBqv0jGAQwAoul/SI0CDAAqtZZIFQEOAGD8M0jdAhAAeJe+SDwDEADRgdVIxQATADwuBUiHARMA6pOBSNYBGwCpwIFICwIbACr3kUjxAhsAEqq1SLcCHAAXUqpIRAIeAJXrjkhTAx4ALImmSL8AHwDldhRIfwMgAN7Y2UjRAyIAjuFbSEQBIwBu6WxIGAMrAEb6tki5ASwAl2GXSFkCLABExKJIggIsAO1hm0jvAC0AMPlNSKgALwBNwg5I4gIxAIzKrUgvAjIAKU6RSKgDMgDlkrlIFAEzABPSe0iLATUAyYeGSPwBNQCTqo1IYgM3ABSkv0gVAzgAOXCxSFEBOgALEGdI0AA/AMRbBkgdAUAA2bpQSOUBQACZrIVIrwBCAIBaBEgoAkMAdVmrSOcCQwCZCchIYQJEAOO0lkgyA0QAFcGvSHoDRQDVortIZQFHAG5rSkgLA0cABMTqSLEBTABsmXhI5gNMAG3yRUgpAU0ApQ9OSN4BTQDWYYlICwJOAErToEiYAlAAWUenSPsAVAANpF1IBgNVAIkfvEiXAFYAUyDcR2sBVwA33GtIswFXAJqia0hXA1cAziytSKEDVwDVpL5I"
   }
   ```

## How is this lambda deployed?
### Deploy Configuration
#### QA
https://github.com/theorchard/terraform-infra/tree/master/qa/lambda-content
#### Prod
https://github.com/theorchard/terraform-infra/tree/master/prod/lambda-content
### PR Build
https://pipeline.theorchard.io/job/lambda-content-pull-request/
### Deploy Pipeline
https://pipeline.theorchard.io/job/lambda-content-pipeline/

## Usage

### Install

To install a development environment, you can run:

```sh
make pip_dev
```

### Requirements

- python3.11

Direct dependencies are listed in `requirements-to-freeze.txt` and locked with
their own dependencies in `requirements.txt`. `requirements.txt` is imported into
`requirements-test.txt` and testing dependencies are listed. To bump versions
of dependencies, update the version in `requirements-to-freeze.txt` and run
the command below to regenerate `requirements.txt`.

```sh
make pip_lock_versions
````

### Development

```sh
# Run the function locally using a test event and print results
make dev

# Run linting
make lint

# Run tests
make test

# Build the docker image, launch it, and run linting and tests against it
make docker_test

# Rebuild your local env from scratch
make clean dev
```

## Docker Stack

The docker stack offers an environment like the one on AWS for developing against.

```sh
# Bring the function up in a docker-compose stack. The container will mount
# src dir so it will update immediately as you develop.
make docker_up

# Send a mock event to the function
make docker_event

# Bring the stack down
make docker_down
```

> Note that this requires docker to be running locally and for you to grant docker
> permission to access your local directory.
