Skip to content

Commit

Permalink
Add macOS recording script example (#4)
Browse files Browse the repository at this point in the history
* Add recording example MacOS, sox and pysox

* Add more documentation on full system
  • Loading branch information
joeweiss authored Sep 1, 2022
1 parent 50f6667 commit 3847dc8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
59 changes: 44 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,84 @@
### Raspberry Pi 4 - 64bit

Bring it up and run:
docker compose -f docker-compose.rpi4.yml up -d --build
`docker compose -f docker-compose.rpi4.yml up -d --build`

To run the watcher/analyzer:
docker compose -f docker-compose.rpi4.yml exec web python manage.py runscript analyze
`docker compose -f docker-compose.rpi4.yml exec web python manage.py runscript analyze`

To run the Django test cases:
docker compose -f docker-compose.rpi4.yml exec web python manage.py test
`docker compose -f docker-compose.rpi4.yml exec web python manage.py test`

To take it down:
docker compose -f docker-compose.rpi4.yml down
`docker compose -f docker-compose.rpi4.yml down`

To run bash within the docker instance:
docker compose -f docker-compose.rpi4.yml exec web bash
`docker compose -f docker-compose.rpi4.yml exec web bash`

To rebuild the image for a reason (e.g. after pip change)
docker compose -f docker-compose.rpi4.yml down; docker compose build --no-cache
`docker compose -f docker-compose.rpi4.yml down; docker compose build --no-cache`

### MacOS M1

Bring it up and run:
docker compose -f docker-compose.macm1.yml up -d --build
`docker compose -f docker-compose.macm1.yml up -d --build`

To run the watcher/analyzer:
docker compose -f docker-compose.macm1.yml exec web python manage.py runscript analyze
`docker compose -f docker-compose.macm1.yml exec web python manage.py runscript analyze`

To run the Django test cases:
docker compose -f docker-compose.macm1.yml exec web python manage.py test
`docker compose -f docker-compose.macm1.yml exec web python manage.py test`

To take it down:
docker compose -f docker-compose.macm1.yml down
`docker compose -f docker-compose.macm1.yml down`

To run bash within the docker instance:
docker compose -f docker-compose.macm1.yml exec web bash
`docker compose -f docker-compose.macm1.yml exec web bash`

To rebuild the image for a reason (e.g. after pip change)
docker compose -f docker-compose.macm1.yml down; docker compose build --no-cache
`docker compose -f docker-compose.macm1.yml down; docker compose build --no-cache`

## Recording audio

The docker container itself does not record audio to audio_inbox directory. See below for basic examples for recording 30-second long WAV files to audio_inbox.
The docker container itself does not record audio to the `audio_inbox` directory. See below for basic examples for recording 30-second properly-dated WAV files to audio_inbox.

### Raspberry Pi 4

Prerequisites: arecord

To record an audio stream to "audio_inbox", run the following.
`python script_examples/audio_recording_rpi.py`

### MacOS M1

Prerequisites: sox
Prerequisites: sox, pysox

To record an audio stream to "audio_inbox", run the following.
`python script_examples/audio_recording_macosm1.py`
`python script_examples/audio_recording_macos.py`

## Putting it all together

In order to create a self-running, restart-tolerant installation, you'll need to setup a few extra things. For example, the setup for a Raspberry Pi 4 is provided below. Other systems will require other methods of starting and restarting the processes.

Prerequisites: Raspberry Pi 4 with clean 64-bit system, `docker`, `docker-compose` and `arecord`.

### Confirm that the system works

In one terminal, run:

```
mkdir audio_inbox
mkdir audio_post_analyze
docker compose -f docker-compose.rpi4.yml up -d --build
docker compose -f docker-compose.rpi4.yml exec web python manage.py runscript analyze
```

In another terminal, start the recording:

```
python script_examples/audio_recording_macos.py
```

### Setup systemd to auto-start the analyze and recording processes on boot

<snip> More to come ...
30 changes: 30 additions & 0 deletions script_examples/audio_recording_macos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Requires sox and pysox: install with:
# brew install sox
# pip install sox

import sox
from datetime import datetime


def main():

duration_secs = 15
output_directory = "audio_inbox"

while True:
filename = datetime.now().strftime("%Y-%m-%d-%H:%M:%S.wav")
print(f"{output_directory}/{filename}")
args = [
"-d",
"-t",
"wav",
f"{output_directory}/{filename}",
"trim",
"0",
f"{duration_secs}",
]
sox.core.sox(args)


if __name__ == "__main__":
main()

0 comments on commit 3847dc8

Please sign in to comment.