Skip to content

Commit

Permalink
local synth
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Sep 15, 2023
1 parent 6bcfead commit eefa09e
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions hivemind_ggwave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ def __init__(self, config=None):
self.rx = self.config.get("ggwave-rx") or \
find_executable("ggwave-rx") or \
expanduser("~/.local/bin/ggwave-rx")
self.tx = self.config.get("ggwave-to-file") or \
find_executable("ggwave-to-file") or \
expanduser("~/.local/bin/ggwave-to-file")
self.tx = self.config.get("ggwave-cli") or \
find_executable("ggwave-cli") or \
expanduser("~/.local/bin/ggwave-cli")
if not isfile(self.rx):
raise ValueError(f"ggwave-rx not found in {self.rx}, "
f"please install from https://github.com/ggerganov/ggwave")
if not isfile(self.tx):
LOG.warning(f"ggwave-to-file not found in {self.tx}, online service will be used"
f"please install from https://github.com/ggerganov/ggwave")

self.OPCODES = {
"HMPSWD:": self.handle_pswd,
"HMKEY:": self.handle_key,
"HMHOST:": self.handle_host
}
self.running = False
self.remote = self.config.get("remote", False)
if not isfile(self.tx):
LOG.warning("ggwave-cli not found, forcing remote usage")
self.remote = True

def stop(self):
self.running = False
Expand All @@ -63,16 +65,17 @@ def run(self):
handler(p)
break
else:
print(f"invalid ggwave payload: {payload}")
LOG.error(f"invalid ggwave payload: {payload}")
except pexpect.exceptions.EOF:
# exited
print("Exited ggwave-rx process")
LOG.debug("Exited ggwave-rx process")
break
except pexpect.exceptions.TIMEOUT:
# nothing happened for a while
pass
except KeyboardInterrupt:
break
child.close(True)

def handle_host(self, payload):
pass
Expand All @@ -84,10 +87,18 @@ def handle_pswd(self, payload):
pass

def emit(self, payload):
tmp = get_temp_path("ggwave")
wav = self.encode2wave(payload,
f'{tmp}/{payload.replace(":", "_").replace("/", "_")}.wav')
play_audio(wav).wait()
if self.remote:
tmp = get_temp_path("ggwave")
wav = self.encode2wave(payload,
f'{tmp}/{payload.replace(":", "_").replace("/", "_")}.wav')
play_audio(wav).wait()
else:
p = pexpect.spawn(f"{self.tx}")
p.expect("Enter text:")
p.sendline(payload)
p.expect("Enter text:")
time.sleep(5)
p.close(True)

def encode2wave(self, message: str,
wav_path: str,
Expand All @@ -96,13 +107,7 @@ def encode2wave(self, message: str,
volume: int = 50,
payloadLength: int = -1,
useDSS: int = 0):

# TODO - attempt to use binary if available
if self.tx:
pass

url = 'https://ggwave-to-file.ggerganov.com/'

params = {
'm': message, # message to encode
'p': protocolId, # transmission protocol to use
Expand All @@ -113,7 +118,6 @@ def encode2wave(self, message: str,
}

response = requests.get(url, params=params)

if response == '' or b'Usage: ggwave-to-file' in response.content:
raise SyntaxError('Request failed')

Expand Down Expand Up @@ -163,14 +167,7 @@ def add_client(self, access_key):
user = db.get_client_by_api_key(access_key)
node_id = db.get_item_id(user)

print("Credentials added to database!\n")
print("Node ID:", node_id)
print("Friendly Name:", name)
print("Access Key:", access_key)
print("Password:", self.pswd)
print("Encryption Key:", key)

print("WARNING: Encryption Key is deprecated, only use if your client does not support password")
LOG.info(f"Credentials added to database! {access_key}")

self.bus.emit(Message("hm.ggwave.client_registered",
{"key": access_key,
Expand Down Expand Up @@ -251,6 +248,6 @@ def handle_host(self, payload):
host = "ws://" + host
identity.default_master = host
identity.save()
print(f"identity saved: {identity.IDENTITY_FILE.path}")
LOG.info(f"identity saved: {identity.IDENTITY_FILE.path}")
self.bus.emit(Message("hm.ggwave.identity_updated"))
self.stop()

0 comments on commit eefa09e

Please sign in to comment.