-
Notifications
You must be signed in to change notification settings - Fork 2
/
SwarComm_v3.0.py
227 lines (187 loc) · 8.37 KB
/
SwarComm_v3.0.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import threading
import time
from scipy.io.wavfile import write
import numpy as np
import sounddevice as sd
from scipy.fftpack import fft
import essentia.standard as ess
from hamming_code import *
from tanpura import Taanpura_detector
fs = 44100 # Sample rate
duration = 5 # Duration of recording in seconds
# BASE_FREQ = 1400
# ---------TANPURA ----------
BASE_FREQ = Taanpura_detector()
BASE_FREQ = np.round(BASE_FREQ, -1) * 2
print(f"Baseline set to {BASE_FREQ}")
#-----------TEMPO-------------
# Record audio
print("Listening for Tempo...")
recording = sd.rec(int(fs * duration), samplerate=fs, channels=2)
sd.wait()
print("Processing Tempo...")
write('output.wav', fs, recording) # Save as WAV file
# Load the audio into Essentia
audio = ess.MonoLoader(filename="output.wav", sampleRate=fs)()
# Compute beat positions and BPM.
rhythm_extractor = ess.RhythmExtractor2013(method="multifeature")
bpm, beats, beats_confidence, _, beats_intervals = rhythm_extractor(audio)
print(bpm)
rec_duration = 60/np.round(bpm, -1)
send_duration = 2* rec_duration + 0.2
print(rec_duration)
print(send_duration)
#--------- TRANSMIT/RECEIVE ----------
# Function to play a note for a given duration
def play_note(frequency, duration):
samples = np.arange(44100 * duration)
note = np.sin(2 * np.pi * frequency * samples / 44100)
sd.play(note, blocking=True)
print("Playing note: ", frequency)
note_map = {
"000": "Sa",
"001": "Re1",
"011": "Ga3",
"010": "Ma1",
"110": "Pa",
"111": "Dha1",
"101": "Ni3",
"100": "HighFreq",
}
def bits_to_notes(bits):
return [note_map[bits[i: i + 3]] for i in range(0, len(bits), 3)]
map_note = {}
for key, value in note_map.items():
map_note[value] = key
print(map_note)
# Define the Carnatic notes and their frequencies
sa_frequency = BASE_FREQ # Sa frequency in Hz
carnatic_notes = {
"Sa": sa_frequency,
"Re1": sa_frequency * 256 / 243,
"Re2": sa_frequency * 9 / 8,
"Ga2": sa_frequency * 32 / 27,
"Ga3": sa_frequency * 81 / 64,
"Ma1": sa_frequency * 4 / 3,
"Ma2": sa_frequency * 729 / 512,
"Pa": sa_frequency * 3 / 2,
"Dha1": sa_frequency * 128 / 81,
"Dha2": sa_frequency * 27 / 16,
"Ni2": sa_frequency * 16 / 9,
"Ni3": sa_frequency * 243 / 128,
"HighFreq": sa_frequency * 3.9,
"Sentinel": sa_frequency * 4.5
}
play_upper = {
'Sa': False,
'Re1': False,
'Ga3': False,
'Ma1': False,
'Pa': False,
'Dha1': False,
'Ni3': False,
'HighFreq': False,
'Sentinel': False
}
def receive():
Job = input("Would you like to TRANSMIT, RECEIVE or END? ")
if Job == "TRANSMIT":
packets = get_input()
packet_ = []
for packet in packets:
packet = map(str, packet)
packet = "".join(packet)
packet_.append(packet)
for packet in packet_:
bits = packet
message_to_send = bits_to_notes(bits)
print(message_to_send)
# Play the aarohanam of the Mohanam raga
for note in message_to_send:
if play_upper[note]:
play_note(carnatic_notes[note]*2, send_duration)
play_upper[note] = False
else:
play_note(carnatic_notes[note], send_duration)
play_upper[note] = True
play_note(carnatic_notes["Sentinel"], 3)
elif Job == "RECEIVE":
# Show readiness
play_note(carnatic_notes['Ni3']*2, 3)
Done = False
rec_freq = []
received_data = []
count_nothing = 0
count = 0
max_freq = 0
while not Done:
fs = 44100
duration = rec_duration
data = sd.rec(int(fs * duration), fs, channels=1)
sd.wait() # wait for recording to finish
# take the FFT of the recorded audio
fft_out = fft(data[:, 0])
# find the index of the maximum value in the spectrum
max_index = np.argmax(np.abs(fft_out))
# calculate the frequency of the maximum value
frequency = max_index * fs / len(data)
if abs(frequency - max_freq) > 3 and frequency >= BASE_FREQ:
max_freq = frequency
count = 0
# print(frequency)
# print("Receiving:", end=" ")
if (frequency > carnatic_notes["Sa"] - 3) and (carnatic_notes["Sa"] + 3 > frequency) or (frequency > 2*carnatic_notes["Sa"] - 3) and (2*carnatic_notes["Sa"] + 3 > frequency):
# if (frequency%sa_frequency < 3 or frequency%sa_frequency > sa_frequency-3):
count_nothing = 0
print("Receiving: Sa")
received_data.append(map_note['Sa'])
elif (frequency > carnatic_notes["Ma1"] - 3) and (carnatic_notes["Ma1"] + 3 > frequency) or (frequency > 2*carnatic_notes["Ma1"] - 3) and (2*carnatic_notes["Ma1"] + 3 > frequency):
# elif(frequency%(sa_frequency*4/3) < 3 or frequency%(sa_frequency*4/3) > sa_frequency-3):
count_nothing = 0
print('Receiving: Ma1')
received_data.append(map_note['Ma1'])
elif (frequency > carnatic_notes["Re1"] - 3) and (carnatic_notes["Re1"] + 3 > frequency) or (frequency > 2*carnatic_notes["Re1"] - 3) and (2*carnatic_notes["Re1"] + 3 > frequency):
# elif(frequency%(sa_frequency*256/243) < 3 or frequency%(sa_frequency*256/243) > sa_frequency-3):
count_nothing = 0
print('Receiving: Re1')
received_data.append(map_note['Re1'])
elif (frequency > carnatic_notes["Ga3"] - 3) and (carnatic_notes["Ga3"] + 3 > frequency) or (frequency > 2*carnatic_notes["Ga3"] - 3) and (2*carnatic_notes["Ga3"] + 3 > frequency):
# elif(frequency%(sa_frequency*81/64) < 3 or frequency%(sa_frequency*81/64) > sa_frequency-3):
count_nothing = 0
print('Receiving: Ga3')
received_data.append(map_note['Ga3'])
elif (frequency > carnatic_notes["Pa"] - 3) and (carnatic_notes["Pa"] + 3 > frequency) or (frequency > 2*carnatic_notes["Pa"] - 3) and (2*carnatic_notes["Pa"] + 3 > frequency):
# elif(frequency%(sa_frequency*3/2) < 3 or frequency%(sa_frequency*3/2) > sa_frequency-3):
count_nothing = 0
print("Receiving: Pa")
received_data.append(map_note['Pa'])
elif (frequency > carnatic_notes["Dha1"] - 3) and (carnatic_notes["Dha1"] + 3 > frequency) or (frequency > 2*carnatic_notes["Dha1"] - 3) and (2*carnatic_notes["Dha1"] + 3 > frequency):
# elif(frequency%(sa_frequency*128/81) < 3 or frequency%(sa_frequency*128/81) > sa_frequency-3):
count_nothing = 0
print("Receiving: Dha1")
received_data.append(map_note['Dha1'])
elif (frequency > carnatic_notes["Ni3"] - 3) and (carnatic_notes["Ni3"] + 3 > frequency) or (frequency > 2*carnatic_notes["Ni3"] - 3) and (2*carnatic_notes["Ni3"] + 3 > frequency):
# elif(frequency%(sa_frequency*243/128) < 3 or frequency%(sa_frequency*243/128) > sa_frequency-3):
count_nothing = 0
print("Receiving: Ni3")
received_data.append(map_note['Ni3'])
elif (frequency > carnatic_notes["HighFreq"] - 3) and (carnatic_notes["HighFreq"] + 3 > frequency) or (frequency > 2*carnatic_notes["HighFreq"] - 3) and (2*carnatic_notes["HighFreq"] + 3 > frequency):
count_nothing = 0
print("Receiving: HighFreq")
received_data.append(map_note['HighFreq'])
elif (frequency > carnatic_notes["Sentinel"] - 3) and (
carnatic_notes["Sentinel"] + 3 > frequency):
break
else:
count += 1
rec_data = ''
for strn in received_data:
rec_data = rec_data + strn
packets = [rec_data[21*i:21*i+21] for i in range(len(rec_data)//21)]
packets_ = []
for i in range(len(packets)):
packets_.append([int(char) for char in packets[i]])
out = get_output(packets_)
print(f"Message received is: {out}")
if __name__ == "__main__":
receive()