-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecording_app.py
141 lines (114 loc) · 4.4 KB
/
recording_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from flask import Flask, request
from audfprint_connector import Connector
from datetime import datetime
import pytz
import logging
from recording import radiorec
import time
from multiprocessing import Process, Queue
from audfprint.audio_read import buf_to_float
import numpy as np
# import wave
# import contextlib
from gevent.pywsgi import WSGIServer
dt_format = '%Y-%m-%d %H:%M:%S'
logging.basicConfig()
class Config(object):
DEBUG = False
# Application setup
app = Flask(__name__)
app.config.from_object(Config())
app.logger.setLevel(logging.INFO) # use the native logger in flask
def consumer(task_queue):
# Create connector to audfprint
afp = Connector()
app.logger.info("Consumer started")
while True:
task, data = task_queue.get() # Blocking
array, station = data
cur_dt = datetime.now(pytz.timezone('Europe/Copenhagen')).strftime(dt_format)
# # Save to file
# file_name = 'recordings/%s_%s.wav' % (station, cur_dt.replace(':', ''))
# with contextlib.closing(wave.open(file_name, 'w')) as of:
# of.setframerate(afp.sample_rate)
# of.setnchannels(1)
# of.setsampwidth(2)
# for buf in array:
# of.writeframes(buf)
# Convert array
array = np.ascontiguousarray(np.concatenate(
[buf_to_float(buf, dtype=np.float32) for buf in array]
), dtype=np.float32)
# Ingest into table
app.logger.info("Ingesting %s" % (station + '.' + cur_dt))
afp.ingest_array(array, station + '.' + cur_dt)
print('Ingested object')
def keep_recording(queue, stations):
app.logger.info("Starting main process")
recording_processes = dict()
# Define producer processes
for n, radio_station in enumerate(stations):
recording_processes[n] = (Process(target=radiorec.record_stream,
args=(radio_station, queue),
name=radio_station.get('name', '')),
radio_station)
# Run processes
for n in recording_processes.keys():
(p, radio_station) = recording_processes[n]
app.logger.info("Starting recording: %s" % p.name)
p.start()
while True:
# If they shut down, restart
for n in recording_processes.keys():
(p, radio_station) = recording_processes[n]
if not p.is_alive():
app.logger.info("Restarting recording: %s" % p.name)
p.join(1) # Tidy up?
del recording_processes[n] # Delete from dict
# Define new process
p = Process(target=radiorec.record_stream,
args=(radio_station, queue),
name=radio_station.get('name', ''))
p.start()
recording_processes[n] = (p, radio_station)
# Wait a bit before retrying
time.sleep(1)
@app.route('/', methods=['GET'])
def hello():
return 'Hello'
if __name__ == '__main__':
# Setup radio recording
radio_stations = [
{'name': 'P1',
'url': 'http://live-icy.gss.dr.dk/A/A03H.mp3'},
{'name': 'P2',
'url': 'http://live-icy.gss.dr.dk/A/A04H.mp3'},
{'name': 'P3',
'url': 'http://live-icy.gss.dr.dk/A/A05H.mp3'},
{'name': 'P4_Kobenhavn',
'url': 'http://live-icy.gss.dr.dk/A/A08H.mp3'},
{'name': 'P5',
'url': 'http://live-icy.gss.dr.dk/A/A25H.mp3'},
{'name': 'P6',
'url': 'http://live-icy.gss.dr.dk/A/A29H.mp3'},
{'name': 'P7',
'url': 'http://live-icy.gss.dr.dk/A/A21H.mp3'},
{'name': 'P8',
'url': 'http://live-icy.gss.dr.dk/A/A22H.mp3'}]
# radio_stations = []
# Define queues
task_queue = Queue()
# Define a producer queue/process
producer_process = Process(target=keep_recording, args=(task_queue, radio_stations), name='producer')
producer_process.start()
# Define consumer process
consumer_process = Process(target=consumer, args=(task_queue,), name='consumer')
consumer_process.start()
# Dev server
# app.run(host='0.0.0.0', port=5000, use_reloader=False)
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
try:
[p.join(1) for p in [producer_process, consumer_process]]
except KeyboardInterrupt:
print('Exiting')