Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2cbdb8b364 | ||
|
|
549a87bed0 | ||
|
|
098dfc9a10 | ||
|
|
fc6c114473 | ||
|
|
91e8e90998 | ||
|
|
792e6d7a55 | ||
|
|
de763858b6 | ||
|
|
e84adaa9c4 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,4 +2,5 @@
|
|||||||
*__pycache__/
|
*__pycache__/
|
||||||
*.html
|
*.html
|
||||||
*.exe
|
*.exe
|
||||||
LICENSE
|
LICENSE
|
||||||
|
*.log
|
||||||
@@ -6,10 +6,12 @@ import time
|
|||||||
import pyaudio
|
import pyaudio
|
||||||
import discord
|
import discord
|
||||||
import numpy
|
import numpy
|
||||||
|
from debugger import setup_logger
|
||||||
|
|
||||||
voice_connection = None
|
voice_connection = None
|
||||||
|
|
||||||
LOGGER = logging.getLogger("Discord_Radio_Bot.NoiseGateV2")
|
# Configure logging
|
||||||
|
logger = setup_logger('NoiseGatev2')
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
@@ -30,19 +32,19 @@ class AudioStream:
|
|||||||
if _input:
|
if _input:
|
||||||
self.paInstance_kwargs['input_device_index'] = _input_device_index
|
self.paInstance_kwargs['input_device_index'] = _input_device_index
|
||||||
else:
|
else:
|
||||||
LOGGER.warning(f"[AudioStream.__init__]:\tInput was not enabled."
|
logger.warning(f"[AudioStream.__init__]:\tInput was not enabled."
|
||||||
f" Reinitialize with '_input=True'")
|
f" Reinitialize with '_input=True'")
|
||||||
|
|
||||||
if _output_device_index:
|
if _output_device_index:
|
||||||
if _output:
|
if _output:
|
||||||
self.paInstance_kwargs['output_device_index'] = _output_device_index
|
self.paInstance_kwargs['output_device_index'] = _output_device_index
|
||||||
else:
|
else:
|
||||||
LOGGER.warning(f"[AudioStream.__init__]:\tOutput was not enabled."
|
logger.warning(f"[AudioStream.__init__]:\tOutput was not enabled."
|
||||||
f" Reinitialize with '_output=True'")
|
f" Reinitialize with '_output=True'")
|
||||||
|
|
||||||
if _init_on_startup:
|
if _init_on_startup:
|
||||||
# Init PyAudio instance
|
# Init PyAudio instance
|
||||||
LOGGER.info("Creating PyAudio instance")
|
logger.info("Creating PyAudio instance")
|
||||||
self.paInstance = pyaudio.PyAudio()
|
self.paInstance = pyaudio.PyAudio()
|
||||||
|
|
||||||
# Define and initialize stream object if we have been passed a device ID (pyaudio.open)
|
# Define and initialize stream object if we have been passed a device ID (pyaudio.open)
|
||||||
@@ -50,27 +52,30 @@ class AudioStream:
|
|||||||
|
|
||||||
if _output_device_index or _input_device_index:
|
if _output_device_index or _input_device_index:
|
||||||
if _init_on_startup:
|
if _init_on_startup:
|
||||||
LOGGER.info("Init stream")
|
logger.info("Init stream")
|
||||||
self.init_stream()
|
self.init_stream()
|
||||||
|
|
||||||
def init_stream(self, _new_output_device_index: int = None, _new_input_device_index: int = None):
|
def init_stream(self, _new_output_device_index: int = None, _new_input_device_index: int = None):
|
||||||
|
logger.info("Checking what device has been set if any")
|
||||||
# Check what device was asked to be changed (or set)
|
# Check what device was asked to be changed (or set)
|
||||||
if _new_input_device_index:
|
if _new_input_device_index:
|
||||||
if self.paInstance_kwargs['input']:
|
if self.paInstance_kwargs['input']:
|
||||||
self.paInstance_kwargs['input_device_index'] = _new_input_device_index
|
self.paInstance_kwargs['input_device_index'] = _new_input_device_index
|
||||||
else:
|
else:
|
||||||
LOGGER.warning(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
logger.warning(f"[AudioStream.init_stream]:\tInput was not enabled when initialized."
|
||||||
f" Reinitialize with '_input=True'")
|
f" Reinitialize with '_input=True'")
|
||||||
|
|
||||||
if _new_output_device_index:
|
if _new_output_device_index:
|
||||||
if self.paInstance_kwargs['output']:
|
if self.paInstance_kwargs['output']:
|
||||||
self.paInstance_kwargs['output_device_index'] = _new_output_device_index
|
self.paInstance_kwargs['output_device_index'] = _new_output_device_index
|
||||||
else:
|
else:
|
||||||
LOGGER.warning(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
logger.warning(f"[AudioStream.init_stream]:\tOutput was not enabled when initialized."
|
||||||
f" Reinitialize with '_output=True'")
|
f" Reinitialize with '_output=True'")
|
||||||
|
|
||||||
|
logger.info("Close the stream if it's open")
|
||||||
self.close_if_open()
|
self.close_if_open()
|
||||||
|
|
||||||
|
logger.info("Open the audio stream")
|
||||||
# Open the stream
|
# Open the stream
|
||||||
self.stream = self.paInstance.open(**self.paInstance_kwargs)
|
self.stream = self.paInstance.open(**self.paInstance_kwargs)
|
||||||
|
|
||||||
@@ -80,10 +85,10 @@ class AudioStream:
|
|||||||
if self.stream.is_active():
|
if self.stream.is_active():
|
||||||
self.stream.stop_stream()
|
self.stream.stop_stream()
|
||||||
self.stream.close()
|
self.stream.close()
|
||||||
LOGGER.debug(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
logger.debug(f"[ReopenStream.close_if_open]:\t Stream was open; It was closed.")
|
||||||
|
|
||||||
def list_devices(self, _display_input_devices: bool = True, _display_output_devices: bool = True):
|
def list_devices(self, _display_input_devices: bool = True, _display_output_devices: bool = True):
|
||||||
LOGGER.info('Getting a list of the devices connected')
|
logger.info('Getting a list of the devices connected')
|
||||||
info = self.paInstance.get_host_api_info_by_index(0)
|
info = self.paInstance.get_host_api_info_by_index(0)
|
||||||
numdevices = info.get('deviceCount')
|
numdevices = info.get('deviceCount')
|
||||||
|
|
||||||
@@ -96,13 +101,13 @@ class AudioStream:
|
|||||||
input_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
input_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
||||||
devices['Input'][i] = input_device
|
devices['Input'][i] = input_device
|
||||||
if _display_input_devices:
|
if _display_input_devices:
|
||||||
LOGGER.debug(f"Input Device id {i} - {input_device}")
|
logger.debug(f"Input Device id {i} - {input_device}")
|
||||||
|
|
||||||
if (self.paInstance.get_device_info_by_host_api_device_index(0, i).get('maxOutputChannels')) > 0:
|
if (self.paInstance.get_device_info_by_host_api_device_index(0, i).get('maxOutputChannels')) > 0:
|
||||||
output_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
output_device = self.paInstance.get_device_info_by_host_api_device_index(0, i).get('name')
|
||||||
devices['Output'][i] = output_device
|
devices['Output'][i] = output_device
|
||||||
if _display_output_devices:
|
if _display_output_devices:
|
||||||
LOGGER.debug(f"Output Device id {i} - {output_device}")
|
logger.debug(f"Output Device id {i} - {output_device}")
|
||||||
|
|
||||||
return devices
|
return devices
|
||||||
|
|
||||||
@@ -126,28 +131,31 @@ class NoiseGate(AudioStream):
|
|||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
global voice_connection
|
global voice_connection
|
||||||
# Start the audio stream
|
# Start the audio stream
|
||||||
LOGGER.debug(f"Starting stream")
|
logger.debug(f"Starting stream")
|
||||||
self.stream.start_stream()
|
try:
|
||||||
# Start the stream to discord
|
self.stream.start_stream()
|
||||||
self.core()
|
# Start the stream to discord
|
||||||
|
self.core()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(err)
|
||||||
|
|
||||||
def core(self, error=None):
|
def core(self, error=None):
|
||||||
if error:
|
if error:
|
||||||
LOGGER.warning(error)
|
logger.warning(error)
|
||||||
|
|
||||||
while not voice_connection.is_connected():
|
while not voice_connection.is_connected():
|
||||||
time.sleep(.2)
|
time.sleep(.2)
|
||||||
|
|
||||||
if not voice_connection.is_playing():
|
if not voice_connection.is_playing():
|
||||||
LOGGER.debug(f"Playing stream to discord")
|
logger.debug(f"Playing stream to discord")
|
||||||
voice_connection.play(self.NGStream, after=self.core)
|
voice_connection.play(self.NGStream, after=self.core)
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
LOGGER.debug(f"Closing")
|
logger.debug(f"Closing")
|
||||||
await voice_connection.disconnect()
|
await voice_connection.disconnect()
|
||||||
if self.stream.is_active:
|
if self.stream.is_active:
|
||||||
self.stream.stop_stream()
|
self.stream.stop_stream()
|
||||||
LOGGER.debug(f"Stopping stream")
|
logger.debug(f"Stopping stream")
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyUnresolvedReferences
|
# noinspection PyUnresolvedReferences
|
||||||
@@ -169,9 +177,9 @@ class NoiseGateStream(discord.AudioSource):
|
|||||||
|
|
||||||
if self.process_set_count % 10 == 0:
|
if self.process_set_count % 10 == 0:
|
||||||
if buffer_decibel >= self.stream.THRESHOLD:
|
if buffer_decibel >= self.stream.THRESHOLD:
|
||||||
LOGGER.debug(f"[Noisegate Open] {buffer_decibel} db")
|
logger.debug(f"[Noisegate Open] {buffer_decibel} db")
|
||||||
else:
|
else:
|
||||||
LOGGER.debug(f"[Noisegate Closed] {buffer_decibel} db")
|
logger.debug(f"[Noisegate Closed] {buffer_decibel} db")
|
||||||
|
|
||||||
if buffer_decibel >= self.stream.THRESHOLD:
|
if buffer_decibel >= self.stream.THRESHOLD:
|
||||||
self.NG_fadeout_count = self.NG_fadeout
|
self.NG_fadeout_count = self.NG_fadeout
|
||||||
@@ -182,13 +190,13 @@ class NoiseGateStream(discord.AudioSource):
|
|||||||
else:
|
else:
|
||||||
if self.NG_fadeout_count > 0:
|
if self.NG_fadeout_count > 0:
|
||||||
self.NG_fadeout_count -= 1
|
self.NG_fadeout_count -= 1
|
||||||
LOGGER.debug(f"Frames in fadeout remaining: {self.NG_fadeout_count}")
|
logger.debug(f"Frames in fadeout remaining: {self.NG_fadeout_count}")
|
||||||
self.process_set_count += 1
|
self.process_set_count += 1
|
||||||
if curr_buffer:
|
if curr_buffer:
|
||||||
return bytes(curr_buffer)
|
return bytes(curr_buffer)
|
||||||
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
LOGGER.warning(e)
|
logger.warning(e)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def audio_datalist_set_volume(self, datalist, volume):
|
def audio_datalist_set_volume(self, datalist, volume):
|
||||||
|
|||||||
28
debugger.py
Normal file
28
debugger.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
running_dir = os.path.dirname(__file__)
|
||||||
|
# Ensure the directory exists
|
||||||
|
log_dir = f"{running_dir}/logs"
|
||||||
|
os.makedirs(log_dir, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logger(namespace):
|
||||||
|
# Create the file if it doesn't exist
|
||||||
|
log_file = f"{running_dir}/logs/pdab.log"
|
||||||
|
open(log_file, 'a').close()
|
||||||
|
|
||||||
|
# Configure logging
|
||||||
|
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-16.16s] [%(levelname)-7.7s] - %(message)s", "%Y-%m-%d %H:%M:%S")
|
||||||
|
logger = logging.getLogger(namespace)
|
||||||
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
fileHandler = logging.FileHandler(log_file)
|
||||||
|
fileHandler.setFormatter(logFormatter)
|
||||||
|
logger.addHandler(fileHandler)
|
||||||
|
|
||||||
|
consoleHandler = logging.StreamHandler()
|
||||||
|
consoleHandler.setFormatter(logFormatter)
|
||||||
|
logger.addHandler(consoleHandler)
|
||||||
|
|
||||||
|
return logger
|
||||||
@@ -3,6 +3,7 @@ from NoiseGatev2 import AudioStream
|
|||||||
print('Getting a list of devices')
|
print('Getting a list of devices')
|
||||||
list_of_devices = AudioStream().list_devices()
|
list_of_devices = AudioStream().list_devices()
|
||||||
print("----- INPUT DEVICES -----")
|
print("----- INPUT DEVICES -----")
|
||||||
|
print("----- *You will likely want to pick from one of these devices* -----")
|
||||||
for inputDevice in list_of_devices['Input']:
|
for inputDevice in list_of_devices['Input']:
|
||||||
print(f"{inputDevice}\t-\t{list_of_devices['Input'][inputDevice]}")
|
print(f"{inputDevice}\t-\t{list_of_devices['Input'][inputDevice]}")
|
||||||
|
|
||||||
|
|||||||
491
main.py
491
main.py
@@ -1,301 +1,246 @@
|
|||||||
import argparse, platform, os
|
# Python client file (client.py)
|
||||||
from discord import Intents, Client, Member, opus
|
import argparse
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
import socketio
|
||||||
|
import asyncio
|
||||||
|
from discord import Intents, opus, Activity, ActivityType
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from NoiseGatev2 import NoiseGate
|
from NoiseGatev2 import NoiseGate
|
||||||
|
from debugger import setup_logger, running_dir
|
||||||
|
import faulthandler
|
||||||
|
faulthandler.enable()
|
||||||
|
|
||||||
# Load the proper OPUS library for the device being used
|
logger = setup_logger('main')
|
||||||
async def load_opus():
|
|
||||||
# Check the system type and load the correct library
|
# Example usage
|
||||||
# Linux ARM AARCH64 running 32bit OS
|
logger.info("Logging initialized successfully.")
|
||||||
|
|
||||||
|
sio = socketio.AsyncClient()
|
||||||
|
client = None
|
||||||
|
device_id = None
|
||||||
|
ng_threshold = None
|
||||||
|
|
||||||
|
|
||||||
|
### Core functions
|
||||||
|
def load_opus():
|
||||||
|
logger.info(f"Running dir: '{running_dir}'")
|
||||||
processor = platform.machine()
|
processor = platform.machine()
|
||||||
print("Processor: ", processor)
|
logger.info(f"Processor: {processor}")
|
||||||
if os.name == 'nt':
|
logger.info(f'OS: {os.name}')
|
||||||
if processor == "AMD64":
|
|
||||||
print(f"Loaded OPUS library for AMD64")
|
if os.name == 'nt':
|
||||||
opus.load_opus('./opus/libopus_amd64.dll')
|
opus_path = f'{running_dir}/opus/libopus_amd64.dll' if processor == "AMD64" else None
|
||||||
return "AMD64"
|
|
||||||
else:
|
else:
|
||||||
if processor == "aarch64":
|
opus_path = f'{running_dir}/opus/libopus_aarcch64.so' if processor == "aarch64" else f'{running_dir}/opus/libopus_armv7l.so'
|
||||||
print(f"Loaded OPUS library for aarch64")
|
|
||||||
opus.load_opus('./opus/libopus_aarcch64.so')
|
logger.debug(f"Opus path: '{opus_path}'")
|
||||||
return "aarch64"
|
logger.info(f"Opus path: '{opus_path}'")
|
||||||
elif processor == "armv7l":
|
|
||||||
print(f"Loaded OPUS library for armv7l")
|
if opus_path:
|
||||||
opus.load_opus('./opus/libopus_armv7l.so')
|
opus.load_opus(opus_path)
|
||||||
return "armv7l"
|
logger.info(f"Loaded OPUS library from {opus_path}")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logger.error("Unsupported architecture or OS.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def main(clientId='OTQzNzQyMDQwMjU1MTE1MzA0.Yg3eRA.ZxEbRr55xahjfaUmPY8pmS-RHTY', channelId=367396189529833476, NGThreshold=50, deviceId=1):
|
async def set_discord_presense(presense):
|
||||||
intents = Intents.default()
|
# Set the presence of the bot (what it's listening to)
|
||||||
|
await client.change_presence(activity=Activity(type=ActivityType.listening, name=presense))
|
||||||
|
|
||||||
client = commands.Bot(command_prefix='!', intents=intents)
|
|
||||||
|
|
||||||
@client.event
|
async def join_voice_channel(channel_id):
|
||||||
async def on_ready():
|
global device_id, ng_threshold
|
||||||
print(f'We have logged in as {client.user}')
|
channel = client.get_channel(int(channel_id))
|
||||||
|
logger.info(f"Joining voice channel {channel}")
|
||||||
channelIdToJoin = client.get_channel(channelId)
|
try:
|
||||||
print("Channel", channelIdToJoin)
|
voice_connection = await channel.connect(timeout=60.0, reconnect=True)
|
||||||
|
|
||||||
print("Loading opus")
|
|
||||||
await load_opus()
|
|
||||||
|
|
||||||
if opus.is_loaded():
|
if opus.is_loaded():
|
||||||
print("Joining voice")
|
logger.info("OPUS library loaded successfully")
|
||||||
channelConnection = await channelIdToJoin.connect(timeout=60.0, reconnect=True)
|
logger.info(f"Input index: {device_id}, Voice Connection: {voice_connection}, Channel: {channel}")
|
||||||
print("Voice Connected")
|
stream_handler = NoiseGate(
|
||||||
streamHandler = NoiseGate(
|
_input_device_index=device_id,
|
||||||
_input_device_index=deviceId,
|
_voice_connection=voice_connection,
|
||||||
_voice_connection=channelConnection,
|
_noise_gate_threshold=ng_threshold
|
||||||
_noise_gate_threshold=NGThreshold)
|
)
|
||||||
# Start the audio stream
|
stream_handler.run()
|
||||||
streamHandler.run()
|
logger.info("Audio stream started")
|
||||||
print("stream running")
|
return True
|
||||||
|
else:
|
||||||
|
logger.error("Failed to load OPUS library")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(e)
|
||||||
|
logging.info('error encountered')
|
||||||
|
|
||||||
|
|
||||||
client.run(clientId)
|
async def leave_voice_channel(guild_id):
|
||||||
|
guild = await client.fetch_guild(guild_id)
|
||||||
|
logger.info(f"Leaving voice channel in guild {guild}")
|
||||||
|
voice_client = guild.voice_client
|
||||||
|
if voice_client:
|
||||||
|
await voice_client.disconnect()
|
||||||
|
logger.info("Disconnected from voice channel")
|
||||||
|
else:
|
||||||
|
logger.info("Not connected to any voice channel in the guild")
|
||||||
|
|
||||||
|
# Check if the client needs to open
|
||||||
|
if len(check_for_open_vc_connections()) <= 0:
|
||||||
|
# Tell the server the client is going to close
|
||||||
|
return False
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
return True
|
||||||
parser.add_argument("deviceId", type=int, help="The ID of the audio device to use")
|
|
||||||
parser.add_argument("channelId", type=int, help="The ID of the voice channel to use")
|
|
||||||
parser.add_argument("clientId", type=str, help="The discord client ID")
|
|
||||||
parser.add_argument("-n", "--NGThreshold", type=int, help="Change the noisegate threshold. This defaults to 50")
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
if (not args.NGThreshold):
|
|
||||||
args.NGThreshold = 50
|
|
||||||
|
|
||||||
print("Arguments:", args)
|
|
||||||
|
|
||||||
main(
|
|
||||||
clientId=args.clientId,
|
|
||||||
channelId=args.channelId,
|
|
||||||
NGThreshold=args.NGThreshold,
|
|
||||||
deviceId=args.deviceId
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
def check_for_open_vc_connections():
|
||||||
|
return client.voice_clients
|
||||||
|
|
||||||
|
|
||||||
|
def check_if_discord_vc_connected(guild_id):
|
||||||
|
if client:
|
||||||
|
return any(int(vc.guild.id) == int(guild_id) for vc in client.voice_clients)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def get_discord_username(guild_id):
|
||||||
|
try:
|
||||||
|
guild = await client.fetch_guild(guild_id)
|
||||||
|
member = await guild.fetch_member(get_discord_id())
|
||||||
|
|
||||||
|
if member.nick:
|
||||||
|
print(f"Username: {member.nick}")
|
||||||
|
return member.nick
|
||||||
|
|
||||||
|
print(f"Username: {client.user.name if client.user else None}")
|
||||||
|
return client.user.name if client.user else None
|
||||||
|
except Exception as e:
|
||||||
|
logging.warning(e)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def check_if_client_is_open():
|
||||||
|
if client:
|
||||||
|
return client.is_ready()
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_discord_id():
|
||||||
|
print(f"ID: {client.user.id if client.user else None}")
|
||||||
|
return int(client.user.id) if client.user else None
|
||||||
|
|
||||||
|
|
||||||
|
async def on_connect():
|
||||||
|
logger.info("Connected to WebSocket server")
|
||||||
|
|
||||||
|
|
||||||
#import asyncio
|
async def on_disconnect():
|
||||||
#import functools
|
logger.info("Disconnected from WebSocket server")
|
||||||
#import itertools
|
|
||||||
#import math
|
|
||||||
#import random
|
### Socket Events
|
||||||
#
|
@sio.event
|
||||||
#import discord
|
async def connect_error():
|
||||||
#from async_timeout import timeout
|
logger.error("Connection to WebSocket server failed")
|
||||||
#from discord.ext import commands
|
|
||||||
#
|
|
||||||
#
|
@sio.event
|
||||||
#class VoiceError(Exception):
|
async def set_system(data):
|
||||||
# pass
|
logger.info(f"Received command to set system name (presense): {data['system']}")
|
||||||
#
|
return await set_discord_presense(data['system'])
|
||||||
#
|
|
||||||
#class VoiceState:
|
|
||||||
# def __init__(self, bot: commands.Bot, ctx: commands.Context):
|
@sio.event
|
||||||
# self.bot = bot
|
async def join_server(data):
|
||||||
# self._ctx = ctx
|
logger.info(f"Received command to join server: {data['channelId']}")
|
||||||
#
|
return await join_voice_channel(data['channelId'])
|
||||||
# self.current = None
|
|
||||||
# self.voice = None
|
|
||||||
# self.next = asyncio.Event()
|
@sio.event
|
||||||
# self.songs = SongQueue()
|
async def leave_server(data):
|
||||||
#
|
logger.info(f"Received command to leave server: {data['guildId']}")
|
||||||
# self._loop = False
|
return await leave_voice_channel(data['guildId'])
|
||||||
# self._volume = 0.5
|
|
||||||
# self.skip_votes = set()
|
|
||||||
#
|
@sio.event
|
||||||
# self.audio_player = bot.loop.create_task(self.audio_player_task())
|
async def check_discord_vc_connected(data):
|
||||||
#
|
return check_if_discord_vc_connected(data['guildId'])
|
||||||
# def __del__(self):
|
|
||||||
# self.audio_player.cancel()
|
|
||||||
#
|
@sio.event
|
||||||
# @property
|
async def request_discord_username(data):
|
||||||
# def loop(self):
|
return await get_discord_username(data['guildId'])
|
||||||
# return self._loop
|
|
||||||
#
|
|
||||||
# @loop.setter
|
@sio.event
|
||||||
# def loop(self, value: bool):
|
async def check_client_is_open():
|
||||||
# self._loop = value
|
return check_if_client_is_open()
|
||||||
#
|
|
||||||
# @property
|
|
||||||
# def volume(self):
|
@sio.event
|
||||||
# return self._volume
|
async def request_discord_id():
|
||||||
#
|
return get_discord_id()
|
||||||
# @volume.setter
|
|
||||||
# def volume(self, value: float):
|
@sio.event
|
||||||
# self._volume = value
|
async def request_client_close():
|
||||||
#
|
exit()
|
||||||
# @property
|
|
||||||
# def is_playing(self):
|
|
||||||
# return self.voice and self.current
|
async def on_ready():
|
||||||
#
|
logger.info(f"We have logged in as {client.user}")
|
||||||
# async def audio_player_task(self):
|
logger.info("Loading OPUS library")
|
||||||
# while True:
|
load_opus()
|
||||||
# self.next.clear()
|
|
||||||
#
|
logger.info(opus.is_loaded())
|
||||||
# if not self.loop:
|
|
||||||
# # Try to get the next song within 3 minutes.
|
if opus.is_loaded():
|
||||||
# # If no song will be added to the queue in time,
|
logger.info('Emitting to the server')
|
||||||
# # the player will disconnect due to performance
|
await sio.emit('discord_ready', {'message': 'Discord bot is ready'})
|
||||||
# # reasons.
|
|
||||||
# try:
|
|
||||||
# async with timeout(180): # 3 minutes
|
async def start_bot(args):
|
||||||
# self.current = await self.songs.get()
|
global client, device_id, ng_threshold
|
||||||
# except asyncio.TimeoutError:
|
|
||||||
# self.bot.loop.create_task(self.stop())
|
# Connect to the WebSocket server
|
||||||
# return
|
await sio.connect('http://127.0.0.1:{}'.format(args.websocket_port), namespaces=['/'])
|
||||||
#
|
logger.info("Connecting to WebSocket server...")
|
||||||
# self.current.source.volume = self._volume
|
|
||||||
# self.voice.play(self.current.source, after=self.play_next_song)
|
intents = Intents.default()
|
||||||
# await self.current.source.channel.send(embed=self.current.create_embed())
|
client = commands.Bot(command_prefix='!', intents=intents)
|
||||||
#
|
|
||||||
# await self.next.wait()
|
device_id = args.device_id
|
||||||
#
|
ng_threshold = args.ng_threshold
|
||||||
# def play_next_song(self, error=None):
|
|
||||||
# if error:
|
client.add_listener(on_connect)
|
||||||
# raise VoiceError(str(error))
|
client.add_listener(on_disconnect)
|
||||||
#
|
client.add_listener(on_ready)
|
||||||
# self.next.set()
|
|
||||||
#
|
await client.start(args.client_id)
|
||||||
# def skip(self):
|
|
||||||
# self.skip_votes.clear()
|
|
||||||
#
|
def parse_arguments():
|
||||||
# if self.is_playing:
|
parser = argparse.ArgumentParser()
|
||||||
# self.voice.stop()
|
parser.add_argument("device_id", type=int, help="The ID of the audio device to use")
|
||||||
#
|
parser.add_argument("client_id", type=str, help="The Discord client ID")
|
||||||
# async def stop(self):
|
parser.add_argument("websocket_port", type=int, help="The port of the WebSocket server")
|
||||||
# self.songs.clear()
|
parser.add_argument("-n", "--ng_threshold", type=int, default=50,
|
||||||
#
|
help="Change the noise gate threshold. Defaults to 50")
|
||||||
# if self.voice:
|
return parser.parse_args()
|
||||||
# await self.voice.disconnect()
|
|
||||||
# self.voice = None
|
|
||||||
#
|
if __name__ == "__main__":
|
||||||
#
|
try:
|
||||||
#class Music(commands.Cog):
|
args = parse_arguments()
|
||||||
# def __init__(self, bot: commands.Bot):
|
logger.info("Arguments: %s", args)
|
||||||
# self.bot = bot
|
# Create an event loop
|
||||||
# self.voice_states = {}
|
loop = asyncio.get_event_loop()
|
||||||
#
|
# Run the start_bot function within the event loop
|
||||||
# def get_voice_state(self, ctx: commands.Context):
|
loop.run_until_complete(start_bot(args))
|
||||||
# state = self.voice_states.get(ctx.guild.id)
|
except Exception as e:
|
||||||
# if not state:
|
logger.error(e)
|
||||||
# state = VoiceState(self.bot, ctx)
|
logger.warning("Exiting now...")
|
||||||
# self.voice_states[ctx.guild.id] = state
|
|
||||||
#
|
|
||||||
# return state
|
|
||||||
#
|
|
||||||
# def cog_unload(self):
|
|
||||||
# for state in self.voice_states.values():
|
|
||||||
# self.bot.loop.create_task(state.stop())
|
|
||||||
#
|
|
||||||
# def cog_check(self, ctx: commands.Context):
|
|
||||||
# if not ctx.guild:
|
|
||||||
# raise commands.NoPrivateMessage('This command can\'t be used in DM channels.')
|
|
||||||
#
|
|
||||||
# return True
|
|
||||||
#
|
|
||||||
# async def cog_before_invoke(self, ctx: commands.Context):
|
|
||||||
# ctx.voice_state = self.get_voice_state(ctx)
|
|
||||||
#
|
|
||||||
# async def cog_command_error(self, ctx: commands.Context, error: commands.CommandError):
|
|
||||||
# await ctx.send('An error occurred: {}'.format(str(error)))
|
|
||||||
#
|
|
||||||
# @commands.command(name='join', invoke_without_subcommand=True)
|
|
||||||
# async def _join(self, ctx: commands.Context):
|
|
||||||
# """Joins a voice channel."""
|
|
||||||
#
|
|
||||||
# destination = ctx.author.voice.channel
|
|
||||||
# if ctx.voice_state.voice:
|
|
||||||
# await ctx.voice_state.voice.move_to(destination)
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# ctx.voice_state.voice = await destination.connect()
|
|
||||||
#
|
|
||||||
# @commands.command(name='summon')
|
|
||||||
# @commands.has_permissions(manage_guild=True)
|
|
||||||
# async def _summon(self, ctx: commands.Context, *, channel: discord.VoiceChannel = None):
|
|
||||||
# """Summons the bot to a voice channel.
|
|
||||||
#
|
|
||||||
# If no channel was specified, it joins your channel.
|
|
||||||
# """
|
|
||||||
#
|
|
||||||
# if not channel and not ctx.author.voice:
|
|
||||||
# raise VoiceError('You are neither connected to a voice channel nor specified a channel to join.')
|
|
||||||
#
|
|
||||||
# destination = channel or ctx.author.voice.channel
|
|
||||||
# if ctx.voice_state.voice:
|
|
||||||
# await ctx.voice_state.voice.move_to(destination)
|
|
||||||
# return
|
|
||||||
#
|
|
||||||
# ctx.voice_state.voice = await destination.connect()
|
|
||||||
#
|
|
||||||
# @commands.command(name='leave', aliases=['disconnect'])
|
|
||||||
# @commands.has_permissions(manage_guild=True)
|
|
||||||
# async def _leave(self, ctx: commands.Context):
|
|
||||||
# """Clears the queue and leaves the voice channel."""
|
|
||||||
#
|
|
||||||
# if not ctx.voice_state.voice:
|
|
||||||
# return await ctx.send('Not connected to any voice channel.')
|
|
||||||
#
|
|
||||||
# await ctx.voice_state.stop()
|
|
||||||
# del self.voice_states[ctx.guild.id]
|
|
||||||
#
|
|
||||||
# @commands.command(name='play')
|
|
||||||
# async def _play(self, ctx: commands.Context, *, search: str):
|
|
||||||
# """Plays a song.
|
|
||||||
#
|
|
||||||
# If there are songs in the queue, this will be queued until the
|
|
||||||
# other songs finished playing.
|
|
||||||
#
|
|
||||||
# This command automatically searches from various sites if no URL is provided.
|
|
||||||
# A list of these sites can be found here: https://rg3.github.io/youtube-dl/supportedsites.html
|
|
||||||
# """
|
|
||||||
#
|
|
||||||
# if not ctx.voice_state.voice:
|
|
||||||
# await ctx.invoke(self._join)
|
|
||||||
#
|
|
||||||
# async with ctx.typing():
|
|
||||||
# try:
|
|
||||||
# source = await YTDLSource.create_source(ctx, search, loop=self.bot.loop)
|
|
||||||
# except YTDLError as e:
|
|
||||||
# await ctx.send('An error occurred while processing this request: {}'.format(str(e)))
|
|
||||||
# else:
|
|
||||||
# song = Song(source)
|
|
||||||
#
|
|
||||||
# await ctx.voice_state.songs.put(song)
|
|
||||||
# await ctx.send('Enqueued {}'.format(str(source)))
|
|
||||||
#
|
|
||||||
# @_join.before_invoke
|
|
||||||
# @_play.before_invoke
|
|
||||||
# async def ensure_voice_state(self, ctx: commands.Context):
|
|
||||||
# if not ctx.author.voice or not ctx.author.voice.channel:
|
|
||||||
# raise commands.CommandError('You are not connected to any voice channel.')
|
|
||||||
#
|
|
||||||
# if ctx.voice_client:
|
|
||||||
# if ctx.voice_client.channel != ctx.author.voice.channel:
|
|
||||||
# raise commands.CommandError('Bot is already in a voice channel.')
|
|
||||||
#
|
|
||||||
#intents = discord.Intents.default()
|
|
||||||
#intents.message_content = True
|
|
||||||
#
|
|
||||||
#bot = commands.Bot('music.', description="Brent's Revenge", intents=intents)
|
|
||||||
#bot.add_cog(Music(bot))
|
|
||||||
#
|
|
||||||
#
|
|
||||||
#@bot.event
|
|
||||||
#async def on_ready():
|
|
||||||
# print('Logged in as:\n{0.user.name}\n{0.user.id}'.format(bot))
|
|
||||||
#
|
|
||||||
#bot.run('OTQzNzQyMDQwMjU1MTE1MzA0.Yg3eRA.ZxEbRr55xahjfaUmPY8pmS-RHTY')
|
|
||||||
|
|||||||
108
pdab-recorder.py
Normal file
108
pdab-recorder.py
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
import os
|
||||||
|
import time
|
||||||
|
import argparse
|
||||||
|
import wave
|
||||||
|
import pyaudio
|
||||||
|
from pydub import AudioSegment
|
||||||
|
from datetime import datetime
|
||||||
|
from NoiseGatev2 import NoiseGateStream, AudioStream, math, audioop
|
||||||
|
from debugger import setup_logger
|
||||||
|
|
||||||
|
# Configure logging
|
||||||
|
log = setup_logger('NoiseGatev2')
|
||||||
|
|
||||||
|
# Parameters for the audio stream
|
||||||
|
NOISE_GATE_THRESHOLD = -50 # Adjust the threshold as needed
|
||||||
|
INPUT_DEVICE_INDEX = None # Set your input device index here
|
||||||
|
|
||||||
|
# Create the recordings directory if it doesn't exist
|
||||||
|
RECORDINGS_DIR = 'recordings'
|
||||||
|
if not os.path.exists(RECORDINGS_DIR):
|
||||||
|
os.makedirs(RECORDINGS_DIR)
|
||||||
|
|
||||||
|
# Create an instance of NoiseGate
|
||||||
|
class NoiseGate(AudioStream):
|
||||||
|
def __init__(self, _noise_gate_threshold: int, _input_device_index: int = None, **kwargs):
|
||||||
|
super(NoiseGate, self).__init__(_input_device_index=_input_device_index, **kwargs)
|
||||||
|
self.THRESHOLD = _noise_gate_threshold
|
||||||
|
self.stream.start_stream()
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
try:
|
||||||
|
curr_buffer = self.stream.read(960, exception_on_overflow=False) # Read audio data from the stream
|
||||||
|
buffer_rms = audioop.rms(curr_buffer, 2) # Calculate RMS of the audio data
|
||||||
|
buffer_decibel = 20 * math.log10(buffer_rms) if buffer_rms > 0 else -float('inf')
|
||||||
|
|
||||||
|
if buffer_decibel >= self.THRESHOLD:
|
||||||
|
log.debug(f"[Noisegate Open] {buffer_decibel} dB")
|
||||||
|
return bytes(curr_buffer) # Return audio data if above threshold
|
||||||
|
|
||||||
|
log.debug(f"[Noisegate Closed] {buffer_decibel} dB")
|
||||||
|
return None # Return None if below threshold
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
log.warning(e)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
log.debug(f"Closing")
|
||||||
|
self.close_if_open()
|
||||||
|
self.paInstance.terminate()
|
||||||
|
|
||||||
|
|
||||||
|
def get_filename():
|
||||||
|
"""Generate a filename based on the current date and time."""
|
||||||
|
return os.path.join(RECORDINGS_DIR, datetime.now().strftime("%Y-%m-%d_%H-%M-%S.wav"))
|
||||||
|
|
||||||
|
|
||||||
|
def save_wave_file(frames, filename, ng):
|
||||||
|
audio_segment = AudioSegment(b''.join(frames), sample_width=ng.paInstance.get_sample_size(pyaudio.paInt16), frame_rate=48000, channels=2)
|
||||||
|
"""Save the given audio segment to a WAV file in the recordings directory."""
|
||||||
|
audio_segment.export(filename, format="mp3")
|
||||||
|
print(f"Saved recording: {filename}")
|
||||||
|
|
||||||
|
|
||||||
|
def record_transmissions():
|
||||||
|
noise_gate = NoiseGate(_noise_gate_threshold=-50, _input_device_index=INPUT_DEVICE_INDEX)
|
||||||
|
frames = []
|
||||||
|
recording = False
|
||||||
|
|
||||||
|
print("Listening for transmissions...")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
audio_data = noise_gate.read() # Get audio data from the NoiseGateStream
|
||||||
|
|
||||||
|
if audio_data: # If there's audio data (not silent)
|
||||||
|
if not recording:
|
||||||
|
print("Transmission detected, starting recording...")
|
||||||
|
recording = True
|
||||||
|
frames = [] # Reset frames for the new recording
|
||||||
|
|
||||||
|
frames.append(audio_data) # Collect audio data
|
||||||
|
|
||||||
|
elif recording:
|
||||||
|
# Silence detected, stop the recording
|
||||||
|
print("Silence detected, stopping recording...")
|
||||||
|
filename = get_filename()
|
||||||
|
save_wave_file(frames, filename, noise_gate)
|
||||||
|
recording = False # Reset recording state
|
||||||
|
frames.clear() # Clear frames for the next transmission
|
||||||
|
print("Recording stopped, waiting for the next transmission...")
|
||||||
|
|
||||||
|
time.sleep(0.1) # Short delay to avoid busy waiting
|
||||||
|
|
||||||
|
|
||||||
|
def parse_arguments():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("device_id", type=int, help="The ID of the audio device to use")
|
||||||
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
args = parse_arguments()
|
||||||
|
print("Arguments: %s", args)
|
||||||
|
INPUT_DEVICE_INDEX = args.device_id
|
||||||
|
record_transmissions()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Stopping...")
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
discord^=2.2.3
|
discord>=2.3.2
|
||||||
PyNaCl^=1.5.0
|
PyNaCl>=1.5.0
|
||||||
pyaudio^=0.2.13
|
pyaudio>=0.2.14
|
||||||
numpy^=1.24.3
|
numpy>=1.26.4
|
||||||
argparse
|
argparse
|
||||||
|
python-socketio[client]
|
||||||
Reference in New Issue
Block a user