-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
89 lines (79 loc) · 2.94 KB
/
client.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
# -*- coding: utf-8 -*-
#
# Wildlife Audio Stream Sampler
# Author: Peter Ersts (ersts@amnh.org)
#
# --------------------------------------------------------------------------
#
# This file is part of Wildlife Audio Stream Sampler
#
# Wildlife Audio Stream Sampler is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Wildlife Audio Stream Sampler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.
#
# --------------------------------------------------------------------------
import os
import sys
import socket
import time
import librosa
import librosa.display
import numpy as np
import soundfile as sf
import matplotlib.pyplot as plt
from multiprocessing import Process
LENGTH = 60 # Seconds
SAMPLE_RATE = 44100
SAMPLE_LENGTH = 4 # Seconds
OVERLAP = 2 # Seconds
DELAY = 5 # Minutes
VMAX = 10 # Max dB for constant scaling across samples
MAX_HZ = 8000 # Limit the y-axis of the output image
HOST = sys.argv[1]
PORT = 8888
def process_data(socket):
timestamp = time.strftime('%Y-%m-%d_%H-%M-%S')
os.mkdir(timestamp)
socket.send(LENGTH.to_bytes(2, byteorder='big'))
buffer = bytearray()
while len(buffer) < (SAMPLE_RATE * 4) * LENGTH:
buffer += socket.recv(4096)
audio = np.frombuffer(buffer, dtype=np.int32)
# Write full audio
file_name = os.path.join(timestamp, '{}.wav'.format(timestamp))
sf.write(file_name, audio, SAMPLE_RATE)
# Create samples
iterations = (LENGTH // (SAMPLE_LENGTH - OVERLAP)) - 1
for s in range(iterations):
start = SAMPLE_RATE * (s * (SAMPLE_LENGTH - OVERLAP))
end = start + (SAMPLE_RATE * SAMPLE_LENGTH)
data = audio[start:end]
file_name = os.path.join(timestamp, 'sample_{:03d}.wav'.format(s))
sf.write(file_name, data, SAMPLE_RATE)
data, _ = librosa.load(file_name, SAMPLE_RATE)
data = np.abs(librosa.stft(data, window='hann')) ** 1.6
data = 10 * np.log10(data)
fig = plt.figure(frameon=False)
fig.set_size_inches(10,10)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
_ = fig.add_axes(ax)
librosa.display.specshow(data, ax=ax, y_axis='linear', cmap='viridis', vmin=-80, vmax=VMAX)
ax.set_ylim([0, MAX_HZ])
fig.savefig('{}jpg'.format(file_name[:-3], 100))
plt.close()
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((HOST, PORT))
while True:
p = Process(target=process_data, args=(socket,))
p.start()
time.sleep(DELAY * 60)