Browse Source

add auto stream stop and fixes

encrypt 4 years ago
parent
commit
194c36ee76
1 changed files with 29 additions and 8 deletions
  1. 29 8
      mumble-bot/bot.py

+ 29 - 8
mumble-bot/bot.py

@@ -10,17 +10,25 @@ import argparse
 
 def message_received(message):
     global is_streaming
-    if message == "/start":
+    global silence_time
+    command=message.message
+    if command == "/start":
         is_streaming = True
-    elif message == "/stop":
+        silence_time = 0
+        mumble.users.myself.recording()
+    elif command == "/stop":
         is_streaming = False
-
+        mumble.users.myself.unrecording()
+        
 parser = argparse.ArgumentParser(description='Regia pienamente automatizzata')
-parser.add_argument('--channel', help='Set channel')
+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)
+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)
 args = parser.parse_args(sys.argv)
 
@@ -32,19 +40,24 @@ channel = args.channel
 port = args.port
 is_streaming=False
 stream_always= args.stream
+auto_suspend_stream = args.auto_suspend_stream
+silence_limit = 30
 
 
-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.callbacks.set_callback(PYMUMBLE_CLBK_TEXTMESSAGERECEIVED, message_received)
 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()
 
+if is_streaming:
+    mumble.users.myself.recording()
+
 BUFFER = 0.1
 BITRATE = 48000
 RESOLUTION = 10 # in ms
@@ -54,6 +67,8 @@ STEREO_CHUNK_SIZE = MONO_CHUNK_SIZE * 2
 silent = b"\x00" * int(STEREO_CHUNK_SIZE)
 cursor_time = None
 cursor_time = time.time() - BUFFER
+silence_time = 0
+silence_limit_ms = silence_limit * 1000
 
 while mumble.is_alive():
     if cursor_time < time.time() - BUFFER: 
@@ -74,9 +89,15 @@ while mumble.is_alive():
 
         if is_streaming or stream_always:  
             if base_sound:
+                silence_time = 0
                 sys.stdout.buffer.write(base_sound)
             else:
-                sys.stdout.buffer.write(silent)                        
+                silence_time += RESOLUTION
+                sys.stdout.buffer.write(silent)
+
+        if auto_suspend_stream and (silence_time >= silence_limit_ms):
+            is_streaming = False
+            mumble.users.myself.unrecording()
 
         cursor_time += FLOAT_RESOLUTION
     else: