Merge branch 'master' of boyska/random into master

grazie araldo del __main__! ti meriti un tarallo
This commit is contained in:
encrypt 2020-04-17 12:57:35 +02:00 committed by Gogs
commit 7dedb7a0b8
3 changed files with 115 additions and 88 deletions

View file

@ -7,10 +7,7 @@ Install
-------- --------
``` ```
git clone https://github.com/azlux/pymumble # quest non sta su pypy pip3 install git+https://github.com/azlux/pymumble # quest non sta su pypy
cd pymumble
pip3 install --user -r requirements.txt
python3 setup.py install --user
``` ```
Usi Usi
@ -21,7 +18,7 @@ Usi
con pulseaudio con pulseaudio
``` ```
python3 bot.py --stream --channel "canale" | paplay --raw --rate=48000 --format=s16le --channels=2 python3 bot.py --stream --channel "canale" | paplay -n mumblebot --raw --rate=48000 --format=s16le --channels=2
``` ```
### Stream to icecast ### Stream to icecast

View file

@ -1,14 +1,18 @@
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 import argparse
import audioop
import logging
import sys
import time
from functools import partial
def message_received(message): import pymumble_py3 as pymumble
from pymumble_py3.constants import PYMUMBLE_CLBK_TEXTMESSAGERECEIVED
is_streaming = False
silence_time = 0
def message_received(mumble, message):
global is_streaming global is_streaming
global silence_time global silence_time
command = message.message command = message.message
@ -16,25 +20,43 @@ def message_received(message):
is_streaming = True is_streaming = True
silence_time = 0 silence_time = 0
mumble.my_channel().send_text_message("Diretta iniziata") mumble.my_channel().send_text_message("Diretta iniziata")
logging.info("Diretta iniziata")
mumble.users.myself.recording() mumble.users.myself.recording()
elif command == "/stop": elif command == "/stop":
is_streaming = False is_streaming = False
mumble.my_channel().send_text_message("Diretta terminata") mumble.my_channel().send_text_message("Diretta terminata")
logging.info("Diretta terminata")
mumble.users.myself.unrecording() mumble.users.myself.unrecording()
parser = argparse.ArgumentParser(description='Regia pienamente automatizzata')
parser.add_argument('--channel', help='Set channel', default="")
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, default=64738)
parser.add_argument('--stream', action='store_true', help='Ignore commands in chat and stream everything')
parser.add_argument('--auto-suspend-stream', action='store_true', help='Ignore commands in chat and stream everything')
parser.add_argument('--max-silence-time', type=int, help='max silence time in seconds', default=30)
sys.argv.pop(0) def get_parser():
args = parser.parse_args(sys.argv) parser = argparse.ArgumentParser(description="Regia pienamente automatizzata")
parser.add_argument("--channel", help="Set channel", default="")
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, default=64738)
parser.add_argument(
"--stream",
action="store_true",
help="Ignore commands in chat and stream everything",
)
parser.add_argument(
"--auto-suspend-stream",
action="store_true",
help="Ignore commands in chat and stream everything",
)
parser.add_argument(
"--max-silence-time", type=int, help="max silence time in seconds", default=30
)
return parser
def main():
global is_streaming
global silence_time
args = get_parser().parse_args()
logging.basicConfig(level=logging.DEBUG)
pwd = "" # password pwd = "" # password
server = args.server server = args.server
nick = args.name nick = args.name
@ -42,15 +64,13 @@ channel = args.channel
port = args.port port = args.port
is_streaming = False is_streaming = False
stream_always = args.stream stream_always = args.stream
auto_suspend_stream = args.auto_suspend_stream
silence_limit = 30
# Spin up a client and connect to mumble server # Spin up a client and connect to mumble server
mumble = pymumble.Mumble(server, nick, password=pwd, port=port) mumble = pymumble.Mumble(server, nick, password=pwd, port=port)
mumble.callbacks.set_callback(PYMUMBLE_CLBK_TEXTMESSAGERECEIVED, message_received) mumble.callbacks.set_callback(
PYMUMBLE_CLBK_TEXTMESSAGERECEIVED, partial(message_received, mumble)
)
mumble.set_receive_sound(1) # Enable receiving sound from mumble server mumble.set_receive_sound(1) # Enable receiving sound from mumble server
mumble.start() mumble.start()
mumble.is_ready() # Wait for client is ready mumble.is_ready() # Wait for client is ready
@ -67,10 +87,7 @@ FLOAT_RESOLUTION = float(RESOLUTION) / 1000
MONO_CHUNK_SIZE = BITRATE * 2 * RESOLUTION / 1000 MONO_CHUNK_SIZE = BITRATE * 2 * RESOLUTION / 1000
STEREO_CHUNK_SIZE = MONO_CHUNK_SIZE * 2 STEREO_CHUNK_SIZE = MONO_CHUNK_SIZE * 2
silent = b"\x00" * int(STEREO_CHUNK_SIZE) silent = b"\x00" * int(STEREO_CHUNK_SIZE)
cursor_time = None
cursor_time = time.time() - BUFFER cursor_time = time.time() - BUFFER
silence_time = 0
silence_limit_ms = silence_limit * 1000
while mumble.is_alive(): while mumble.is_alive():
if cursor_time < time.time() - BUFFER: if cursor_time < time.time() - BUFFER:
@ -82,7 +99,7 @@ while mumble.is_alive():
# available sound is to be treated now and not later # available sound is to be treated now and not later
sound = user.sound.get_sound(FLOAT_RESOLUTION) sound = user.sound.get_sound(FLOAT_RESOLUTION)
stereo_pcm = audioop.tostereo(sound.pcm, 2, 1, 1) stereo_pcm = audioop.tostereo(sound.pcm, 2, 1, 1)
if base_sound == None: if base_sound is None:
base_sound = stereo_pcm base_sound = stereo_pcm
else: else:
base_sound = audioop.add(base_sound, stereo_pcm, 2) base_sound = audioop.add(base_sound, stereo_pcm, 2)
@ -97,11 +114,23 @@ while mumble.is_alive():
silence_time += RESOLUTION silence_time += RESOLUTION
sys.stdout.buffer.write(silent) sys.stdout.buffer.write(silent)
if auto_suspend_stream and (silence_time >= silence_limit_ms) and is_streaming: if (
args.auto_suspend_stream
and (silence_time >= args.max_silence_time * 1000)
and is_streaming
):
is_streaming = False is_streaming = False
mumble.my_channel().send_text_message("Diretta terminata in automatico dopo "+str(silence_limit)+" secondi circa di silenzio") logging.info("max-silence-time reached")
mumble.my_channel().send_text_message(
"Diretta terminata in automatico dopo %d secondi circa di silenzio"
% args.max_silence_time
)
mumble.users.myself.unrecording() mumble.users.myself.unrecording()
cursor_time += FLOAT_RESOLUTION cursor_time += FLOAT_RESOLUTION
else: else:
time.sleep(FLOAT_RESOLUTION) time.sleep(FLOAT_RESOLUTION)
if __name__ == "__main__":
main()

View file

@ -0,0 +1 @@
opuslib==3.0.1