azlux / pymumble

Mumble client implementation in Python
GNU General Public License v3.0
129 stars 61 forks source link

Example audio only client was only sending or receiving not both. #98

Open l33tlinuxh4x0r opened 3 years ago

l33tlinuxh4x0r commented 3 years ago

I downloaded the audio only client and had to change some of the imports to get it to work as well as separate the audio listeners to get it to work. Here is the updated script if anyone wants it. I'm on linux by the way... This might fix the issues with the RPi 3B+?

# A python script to do both listening and talking. This is the basic model
# for an audio-only mumble client.

# Usage:

# Install pyaudio (instructions: https://people.csail.mit.edu/hubert/pyaudio/#downloads)
# If `fatal error: 'portaudio.h' file not found` is encountered while installing
# pyaudio even after following the instruction, this solution might be of help:
# https://stackoverflow.com/questions/33513522/when-installing-pyaudio-pip-cannot-find-portaudio-h-in-usr-local-include
#
# Install dependencies for pymumble.
#
# Set up a mumber server. For testing purpose, you can use https://guildbit.com/
# to spin up a free server. Hard code the server details in this file.
#
# run `python3 ./listen_n_talk.py`. Now an audio-only mumble client is connected
# to the server.
#
# To test its functionality, in a separate device, use some official mumble
# client (https://www.mumble.com/mumble-download.php) to verbally communicate
# with this audio-only client.
#
# Works on MacOS. Does NOT work on RPi 3B+ (I cannot figure out why. Help will
# be much appreciated)

import pymumble_py3 as pymumble_py3
from pymumble_py3.callbacks import PYMUMBLE_CLBK_SOUNDRECEIVED as PCS
import pyaudio

# Connection details for mumble server. Hardcoded for now, will have to be
# command line arguments eventually
pwd = ""  # password
server = "127.0.0.1"  # server address
nick = "audio-only_client"
port = 64738  # port number

# pyaudio set up
CHUNK = 1024
FORMAT = pyaudio.paInt16  # pymumble soundchunk.pcm is 16 bits
CHANNELS = 1
RATE = 48000  # pymumble soundchunk.pcm is 48000Hz

p = pyaudio.PyAudio()
stream_listen = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                #input=True,  # enable both talk
                output=True,  # and listen
                frames_per_buffer=CHUNK)

stream_talk = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,  # enable both talk
                #output=True,  # and listen
                frames_per_buffer=CHUNK)

# mumble client set up
def sound_received_handler(user, soundchunk):
    """ play sound received from mumble server upon its arrival """
    stream_listen.write(soundchunk.pcm)

# Spin up a client and connect to mumble server
mumble = pymumble_py3.Mumble(server, nick, password=pwd, port=port)

# set up callback called when PCS event occurs
mumble.callbacks.set_callback(PCS, sound_received_handler)
mumble.set_receive_sound(1)  # Enable receiving sound from mumble server
mumble.start()
mumble.is_ready()  # Wait for client is ready

# constant capturing sound and sending it to mumble server
while True:
    data = stream_talk.read(CHUNK)
    mumble.sound_output.add_sound(data)

# close the stream and pyaudio instance
stream_talk.stop_stream()
stream_talk.close()
stream_listen.stop_stream()
stream_listen.close()
p.terminate()
azlux commented 3 years ago

Can I put this script into the example folder of the lib ?

l33tlinuxh4x0r commented 3 years ago

Yeah feel free... I posted it for you to update the script... Also I haven't tested it since my first post so you might want to test it again before adding it just to be sure that it works with the latest patches.

TLDR: Yes.

piezza commented 2 years ago

Thanks, this piece of code came very handy for me. But I had to add exception_on_overflow=False to the read call:

data = stream_talk.read(CHUNK, exception_on_overflow=False)

Else the script did not run long on my RPi4.