pymumble icon indicating copy to clipboard operation
pymumble copied to clipboard

Audio being cut off

Open Dalrae1 opened this issue 3 years ago • 0 comments

Hello, I am attempting to write incoming audio data to a wave file, however the incoming audio is ALWAYS cut off about a quarter second before the audio should end. Here's the simple script:

class MumbleBot:
	def __init__(self):
		self.SpeakingUsers = {}
		self.mumble = pymumble_py3.Mumble("localhost", "Bot",64738,reconnect=True)
		self.mumble.set_loop_rate(0.001)
		self.mumble.set_application_string("Bot")
		self.mumble.set_receive_sound(True)
		self.mumble.start()
		self.mumble.is_ready()
		self.loop()
		


	def loop(self):
		while self.mumble.is_alive():
			for user in self.mumble.users.values():
				userID = user.get_property("session")
				userName = user.get_property("name")
				if user.sound.is_sound():
					if userID not in self.SpeakingUsers:
						audioFilename = os.path.join(os.getcwd(), "%s %s" % (userName, time.strftime("%m %d %Y - %I %M %S %p")))
						self.SpeakingUsers[userID] = {}
						self.SpeakingUsers[userID]["StartTime"] = time.time()
						self.SpeakingUsers[userID]["SoundFile"] = AudioFile(audioFilename)

					sound = user.sound.get_sound()
					self.SpeakingUsers[userID]["SoundFile"].write(sound.pcm)
					self.SpeakingUsers[userID]["LastSound"] = time.time()
				else:
					if userID in self.SpeakingUsers:
						print(time.time()-self.SpeakingUsers[userID]["LastSound"])
						if time.time()-self.SpeakingUsers[userID]["LastSound"] > 0.1: #Hasn't recieved a sound packet in 0.1s
							self.SpeakingUsers[userID]["SoundFile"].close()
							del self.SpeakingUsers[userID]

class AudioFile():
	"""
	Manage the audio saving, through a pipe or in a WAV file
	"""
	def __init__(self, name):
		from subprocess import Popen, PIPE
		import sys
		
		self.name = name
		self.type = None
		self.file_obj = None
		
		self.name += ".wav"
		self.file_obj = wave.open(self.name, "wb")
		self.file_obj.setparams((2, 2, BITRATE, 0, 'NONE', 'not compressed'))
		self.type = "wav"
		
	def write(self, data):
		if self.type == "pipe":
			self.file_obj.write(data)
		else:
			self.file_obj.writeframes(data)
	
	def close(self):
		self.file_obj.close()

MumbleBot()

Dalrae1 avatar Oct 13 '21 01:10 Dalrae1