Skip to content

Commit d5f1651

Browse files
committed
flask app done
1 parent 4a695a7 commit d5f1651

3 files changed

Lines changed: 83 additions & 14 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ camera, RODE VideoMicNTG and smartLav+ microphones
66
# Setup
77

88
Clone this repository to your local machine. Detailed instructions about
9-
cloning repositories and installing python dependencies can be found [here](https://docs.google.com/document/d/15Mj3x9Im7Yfz3sPo5f4dUjQZgabjVtIL2RBHvM2798E/edit?usp=sharing).
9+
cloning repositories and installing python dependencies can be found
10+
[here](https://docs.google.com/document/d/15Mj3x9Im7Yfz3sPo5f4dUjQZgabjVtIL2RBHvM2798E/edit?usp=sharing).
1011

1112
## Install Python (3.5 - 3.9)
1213
Do not install the latest version of Python (currently 3.10) as it is not
@@ -55,10 +56,10 @@ python vao-recorder.py config
5556

5657
Test that the chosen audio devices are working
5758
```
58-
python vao-recorder.py test-microphones
59+
python vao-recorder.py test microphone
5960
```
6061

61-
Record an experiment with the chosen devices
62+
Record an experiment with the configured devices
6263
```
6364
python vao-recorder.py record
6465
```

localhost-vao-recorder.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from recorder import Recorder, Config
2+
from recorder.io import yaml_dump
3+
4+
import socket
5+
6+
from flask import Flask
7+
8+
app = Flask(__name__)
9+
10+
app.secret_key = __name__
11+
12+
# ! Workaround: Global variable because it is not JSON serializable and can't
13+
# ! be stored in Flask.session.
14+
recorder = Recorder(Config.from_yaml(), setup_name=False)
15+
print(yaml_dump(recorder.config))
16+
17+
18+
@app.route('/setup', methods=['GET'])
19+
def setup():
20+
# TODO get recording name from request
21+
recorder.setup()
22+
return str(recorder.next_output.name)
23+
24+
25+
@app.route('/start', methods=['GET'])
26+
def start():
27+
recorder.start()
28+
return 'Recording started'
29+
30+
31+
@app.route('/stop', methods=['GET'])
32+
def stop():
33+
return str(recorder.stop().name)
34+
35+
36+
if __name__ == "__main__":
37+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
38+
s.connect(("8.8.8.8", 80))
39+
host = s.getsockname()[0]
40+
app.run(debug=True, host=host)

recorder/recorder.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,63 @@
88

99

1010
class Recorder:
11+
next_output = None
12+
devices = []
1113

1214
def __init__(
1315
self,
1416
config: Config,
1517
output_folder: Optional[Path] = DEFAULT_OUTPUT_FOLDER,
16-
name: Optional[str] = None,
18+
setup_name: Optional[str] = None,
1719
):
20+
"""Recorder object, used to record data from configured devices.
21+
22+
Args:
23+
config (Config): Configuration dictionary.
24+
25+
output_folder (Optional[Path]): Root folder for the recorded
26+
output. For each recording session, a subfolder will be created
27+
based on the `setup_name` provided. Defaults to
28+
DEFAULT_OUTPUT_FOLDER.
29+
setup_name (Optional[str]): The recorded output will be placed in a
30+
subfolder of `output_folder` with the provided name. If not
31+
provided, it will default to a name based on the current date
32+
and time. If `False` is provided instead, the recorder will not
33+
be fully initialized and an additionall call to `setup` will be
34+
required.
35+
"""
1836
self.config = config
1937
# Initialize the output folder
20-
output_folder.mkdir(parents=True, exist_ok=True)
38+
self.output_folder = output_folder
39+
self.output_folder.mkdir(parents=True, exist_ok=True)
40+
if setup_name is not False:
41+
self.setup(setup_name)
42+
43+
def setup(self, name: Optional[str] = None):
44+
# Initialize next recording output
2145
if not name:
2246
name = datetime.now().strftime('date_%Y-%m-%d;time_%H-%M-%S')
23-
self.output_folder = output_folder / name
24-
self.output_folder.mkdir(exist_ok=False)
47+
self.next_output = self.output_folder / name
48+
self.next_output.mkdir(exist_ok=False)
2549
# Initialize all devices
26-
self.devices = self.config.devices(self.output_folder)
50+
self.devices = self.config.devices(self.next_output)
2751

2852
def start(self):
29-
# Start the recording
53+
# Start the recording. Does nothing if the recording is not setup.
3054
for d in self.devices:
3155
d.start()
3256

33-
def stop(self):
57+
def stop(self) -> Path:
3458
# Stop the recording
3559
for d in self.devices:
3660
d.stop()
61+
# Cleanup devices, setup must be called to start recording again
62+
self.devices = []
63+
if not self.next_output:
64+
raise RuntimeError('Recording not setup')
65+
output = Path(str(self.next_output))
66+
self.next_output = None
67+
return output
3768

3869
def wait(self, seconds: Optional[int] = None):
3970
# Wait for user input
@@ -47,7 +78,4 @@ def wait(self, seconds: Optional[int] = None):
4778
def __call__(self, seconds: Optional[int] = None) -> Path:
4879
self.start()
4980
self.wait(seconds=seconds)
50-
self.stop()
51-
# Cleanup devices, a new instance of the recorder must be called
52-
del self.devices
53-
return self.output_folder
81+
return self.stop()

0 commit comments

Comments
 (0)