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

Receive an error TODO: Add 500 Error on Receiving SIP Response #274

Open
jawad097 opened this issue Sep 11, 2024 · 0 comments
Open

Receive an error TODO: Add 500 Error on Receiving SIP Response #274

jawad097 opened this issue Sep 11, 2024 · 0 comments

Comments

@jawad097
Copy link

hey here is my code
import logging
from pyVoIP.VoIP import VoIPPhone, CallState
import speech_recognition as sr
import uuid
import pywav
from pydub import AudioSegment
import os
import shutil
import time

Set up logging

logging.basicConfig(
level=logging.DEBUG, # Log all levels (DEBUG, INFO, WARNING, ERROR, CRITICAL)
format='%(asctime)s - %(levelname)s - %(message)s', # Log format
handlers=[
logging.FileHandler("voip_log.log"), # Save logs to a file
logging.StreamHandler() # Also output logs to the console
]
)

ffmpeg_path = shutil.which("ffmpeg")
if ffmpeg_path:
logging.info(f"FFmpeg found at: {ffmpeg_path}")
else:
logging.error("FFmpeg not found in PATH")

try:
# Create a simple test audio file if it doesn't exist
if not os.path.exists("test.wav"):
from pydub.generators import Sine
sound = Sine(440).to_audio_segment(duration=5000) # 1-second tone
sound.export("test.wav", format="wav")
logging.info("Test audio file 'test.wav' created.")

test_audio = AudioSegment.from_file("test.wav")
logging.info("FFmpeg is correctly configured.")

except Exception as e:
logging.error(f"Error with FFmpeg configuration: {e}")

def convert_to_wav(audio, tmpFileName):
try:
data_bytes = b"".join(audio)
wave_write = pywav.WavWrite(tmpFileName, 1, 8000, 8, 7)
wave_write.write(data_bytes)
wave_write.close()
logging.info(f"Audio converted to WAV and saved as {tmpFileName}")
return tmpFileName
except Exception as e:
logging.error(f"Error converting audio to WAV: {e}")
return None

def transcribe_to_text(audio_file):
recognizer = sr.Recognizer()
try:
# Load the .wav audio file and recognize speech using Sphinx
with sr.AudioFile(audio_file) as source:
audio = recognizer.record(source)
# Use the offline Sphinx recognizer
transcription = recognizer.recognize_sphinx(audio)
logging.info(f"Transcription successful: {transcription}")
return transcription
except sr.UnknownValueError:
logging.warning("Sphinx could not understand the audio.")
except sr.RequestError as e:
logging.error(f"Sphinx error; {e}")
return ""

def answer(call):
try:
call.answer()
logging.info("Call answered.")
buffer = []
buff_length = 0

    while call.state == CallState.ANSWERED:
        audio = call.read_audio()
        buff_length += len(audio) / 8  # Sample rate is 8000 Hz

        if buff_length <= 1000:  # Buffer until 1000ms
            buffer.append(audio)
        else:
            # Save audio to a temporary .wav file for transcription
            tmpFileName = f"./_audio_buffer_{uuid.uuid4()}.wav"
            if convert_to_wav(buffer, tmpFileName):
                transcription = transcribe_to_text(tmpFileName)
                logging.info(f"Transcription: {transcription}")
            buffer = []
            buff_length = 0

except Exception as e:
    logging.error(f"Error during call processing: {e}")
finally:
    call.hangup()
    logging.info("Call hung up.")

vp = VoIPPhone('ip', 5060, 'phone name', 'passowrd', callCallback=answer)
vp.start()

while vp._status == 'REGISTERING':
time.sleep(5)
logging.info(f"Phone is still registering... Current state: {vp._status}")

Check if the phone is successfully registered

if vp._status == 'REGISTERED':
logging.info(f"Phone successfully registered with status: {vp._status}")
else:
logging.error(f"Phone failed to register. Current status: {vp._status}")

input("Press any key to exit the VOIP phone session.")
vp.stop()
logging.info("VOIP phone session stopped.")
when i run this at show eh this error 2024-09-11 17:45:46,172 - INFO - FFmpeg found at: /usr/bin/ffmpeg
2024-09-11 17:45:46,172 - INFO - FFmpeg is correctly configured.
2024-09-11 17:45:46,313 - ERROR - Phone failed to register. Current status: PhoneStatus.REGISTERING
Press any key to exit the VOIP phone session.TODO: Add 500 Error on Receiving SIP Response

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