83 lines
2.7 KiB
Python
83 lines
2.7 KiB
Python
import pymumble_py3 as pymumble
|
|
from pymumble_py3.constants import *
|
|
import subprocess as sp
|
|
import time
|
|
import sys
|
|
import os
|
|
import fcntl
|
|
import audioop
|
|
import argparse
|
|
|
|
def message_received(message):
|
|
global is_streaming
|
|
if message == "/start":
|
|
is_streaming = True
|
|
elif message == "/stop":
|
|
is_streaming = False
|
|
|
|
parser = argparse.ArgumentParser(description='Regia pienamente automatizzata')
|
|
parser.add_argument('--channel', help='Set channel')
|
|
parser.add_argument('--name', help='Set bot nickname', default="RadioRobbot")
|
|
parser.add_argument('--server', help='Set server', default="mumble.esiliati.org")
|
|
parser.add_argument('--port', help='Set port', type=int)
|
|
parser.add_argument('--stream', action='store_true', help='Ignore commands in chat and stream everything')
|
|
sys.argv.pop(0)
|
|
args = parser.parse_args(sys.argv)
|
|
|
|
|
|
pwd = "" # password
|
|
server = args.server
|
|
nick = args.name
|
|
channel = args.channel
|
|
port = args.port
|
|
is_streaming=False
|
|
stream_always= args.stream
|
|
|
|
|
|
mumble.callbacks.set_callback(PYMUMBLE_CLBK_TEXTMESSAGERECEIVED, message_received)
|
|
|
|
# Spin up a client and connect to mumble server
|
|
mumble = pymumble.Mumble(server, nick, password=pwd, port=port)
|
|
# set up callback called when PCS event occurs
|
|
mumble.set_receive_sound(1) # Enable receiving sound from mumble server
|
|
mumble.start()
|
|
mumble.is_ready() # Wait for client is ready
|
|
mumble.channels.find_by_name(channel).move_in()
|
|
mumble.users.myself.mute()
|
|
|
|
BUFFER = 0.1
|
|
BITRATE = 48000
|
|
RESOLUTION = 10 # in ms
|
|
FLOAT_RESOLUTION = float(RESOLUTION) / 1000
|
|
MONO_CHUNK_SIZE = BITRATE * 2 * RESOLUTION / 1000
|
|
STEREO_CHUNK_SIZE = MONO_CHUNK_SIZE * 2
|
|
silent = b"\x00" * int(STEREO_CHUNK_SIZE)
|
|
cursor_time = None
|
|
cursor_time = time.time() - BUFFER
|
|
|
|
while mumble.is_alive():
|
|
if cursor_time < time.time() - BUFFER:
|
|
base_sound = None
|
|
|
|
try:
|
|
for user in mumble.users.values(): # check the audio queue of each user
|
|
if user.sound.is_sound():
|
|
# available sound is to be treated now and not later
|
|
sound = user.sound.get_sound(FLOAT_RESOLUTION)
|
|
stereo_pcm = audioop.tostereo(sound.pcm, 2, 1, 1)
|
|
if base_sound == None:
|
|
base_sound = stereo_pcm
|
|
else:
|
|
base_sound = audioop.add(base_sound, stereo_pcm, 2)
|
|
except RuntimeError:
|
|
print("ignored exception in stderr...", file=sys.stderr)
|
|
|
|
if is_streaming or stream_always:
|
|
if base_sound:
|
|
sys.stdout.buffer.write(base_sound)
|
|
else:
|
|
sys.stdout.buffer.write(silent)
|
|
|
|
cursor_time += FLOAT_RESOLUTION
|
|
else:
|
|
time.sleep(FLOAT_RESOLUTION)
|