Dan's Update

- New classes for noisegate & audio stream
- Ability to manipulate raw data in real-time
This commit is contained in:
Logan Cusano
2022-03-27 00:50:42 -04:00
parent 0329ff7a81
commit 24adc47109
4 changed files with 134 additions and 163 deletions

44
bot.py
View File

@@ -3,8 +3,8 @@ import os
import platform
import discord
import BotResources
import sound
import configparser
import NoiseGatev2
from discord.ext import commands
@@ -33,7 +33,9 @@ class Bot(commands.Bot):
self.Bot_Connected = False
# Init the audio devices list
self.Devices_List = sound.query_devices().items()
#self.Devices_List = sound.query_devices().items()
self.Devices_List = NoiseGatev2.AudioStream().list_devices(_display_input_devices=False,
_display_output_devices=False)
# Init radio parameters
self.profile_name = None
@@ -79,6 +81,7 @@ class Bot(commands.Bot):
brief="Joins the voice channel that the caller is in")
async def join(ctx, *, member: discord.Member = None):
member = member or ctx.author.display_name
print(f"Join requested by {member}")
if not self.Bot_Connected:
# Wait for the bot to be ready to connect
@@ -89,45 +92,52 @@ class Bot(commands.Bot):
if discord.opus.is_loaded():
channel = ctx.author.voice.channel
print("Sending hello")
await ctx.send(f"Ok {str(member).capitalize()}, I'm joining {channel}")
# Join the voice channel with the audio stream
print('Joining')
voice_connection = await channel.connect()
# Create an audio stream from selected device
self.streamHandler = sound.PCMStream(self.DEVICE_ID, voice_connection)
print("Starting noisegate/stream handler")
self.streamHandler = NoiseGatev2.NoiseGate(_input_device_index=self.DEVICE_ID,
_voice_connection=voice_connection)
# Start the audio stream
await self.streamHandler.play()
self.streamHandler.run()
# Start the SDR and begin playing to the audio stream
print("Starting SDR")
self.start_sdr()
# Change the activity to the channel and band-type being used
print("Changing presence")
await self.set_activity()
# 'Lock' the bot from connecting
print("Locking the bot")
self.Bot_Connected = True
else:
# Return that the opus library would not load
await ctx.send("Opus won't load")
print("OPUS didn't load properly")
else:
await ctx.send(f"{str(member).capitalize()}, I'm already connected")
print("Bot is already in a channel")
@self.command(help="Use this command to have the bot leave your channel",
brief="Leaves the current voice channel")
async def leave(ctx, member: discord.Member = None):
member = member or ctx.author.display_name
if self.Bot_Connected:
print("Cleaning up")
# Stop the sound handlers
await self.streamHandler.pause()
print(f"Leave requested by {member}")
print("Disconnecting")
if self.Bot_Connected:
# Stop the sound handlers
# Disconnect the client from the voice channel
await ctx.voice_client.disconnect()
print("Disconnecting")
await self.streamHandler.close()
print("Changing presence")
# Change the presence to away and '@ me'
@@ -316,26 +326,29 @@ class Bot(commands.Bot):
# Load the proper OPUS library for the device being used
def load_opus(self):
# Check the system type and load the correct library
# Linux ARM AARCH64 running 32bit OS
if self.system_os_type == 'Linux_ARMv7l':
print(f"Loaded OPUS library for {self.system_os_type}")
discord.opus.load_opus('./opus/libopus_armv7l.so')
# Linux ARM AARCH64 running 64bit OS
if self.system_os_type == 'Linux_AARCH64':
print(f"Loaded OPUS library for {self.system_os_type}")
discord.opus.load_opus('./opus/libopus_aarcch64.so')
# Windows 64bit
if self.system_os_type == 'Windows_x64':
print(f"Loaded OPUS library for {self.system_os_type}")
discord.opus.load_opus('./opus/libopus_amd64.dll')
# Check to make sure the selected device is still available and has not changed its index
def check_device(self, _override):
# Check to see if an override has been passed
if not _override:
for device, index in self.Devices_List:
print(f"Device list {self.Devices_List}")
for device, index in self.Devices_List['Input']:
if int(index) == self.DEVICE_ID and str(device) == self.DEVICE_NAME:
return True
for device, index in self.Devices_List:
for device, index in self.Devices_List['Input']:
if str(device) == self.DEVICE_NAME:
self.DEVICE_ID = int(index)
return True
@@ -376,11 +389,14 @@ class Bot(commands.Bot):
if os.name == 'nt':
if processor == "AMD64":
self.system_os_type = 'Windows_x64'
print(f"OS/Arch is {self.system_os_type}")
else:
if processor == "aarch64":
self.system_os_type = 'Linux_AARCH64'
print(f"OS/Arch is {self.system_os_type}")
elif processor == "armv7l":
self.system_os_type = 'Linux_ARMv7l'
print(f"OS/Arch is {self.system_os_type}")
# Check to see if there is only one frequency
def start_sdr(self):