Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio being cut off #116

Open
Dalrae1 opened this issue Oct 13, 2021 · 0 comments
Open

Audio being cut off #116

Dalrae1 opened this issue Oct 13, 2021 · 0 comments

Comments

@Dalrae1
Copy link

Dalrae1 commented Oct 13, 2021

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant