Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
error python audio
#1
this is my code:
import os
import pyaudio
import openai
import soundfile as sf
import numpy as np
import base64
from pydub import AudioSegment
from pydub.utils import which, mediainfo, make_chunks, silence
from pydub.silence import detect_nonsilent

# set up OpenAI API key
openai.api_key = "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"

# set up audio stream
CHUNK = 2000
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 3000

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

# set up prompt
prompt = "Hello, how can I help you today?"

# check if ffmpeg exists in the system path or the pydub package can find it
if which("ffmpeg") is None:
    os.environ["PATH"] += os.pathsep + "/usr/local/bin/ffmpeg"

while True:
    # record audio from microphone
    frames = []
    for i in range(int(RATE / CHUNK * 5)):
        data = stream.read(CHUNK)
        frames.append(data)

    # convert audio to text using OpenAI API
    audio_data = b''.join(frames)
    audio_array = np.frombuffer(audio_data, dtype=np.int16)
    sf.write("input.wav", audio_array, RATE)

    audio = AudioSegment.from_wav("input.wav")

    try:
        audio.export("input.mp3", format="mp3", codec="libmp3lame")

        with open("input.mp3", "rb") as f:
            audio_bytes = f.read()

        # encode audio bytes as base64 string
        audio_base64 = base64.b64encode(audio_bytes).decode()

        response = openai.Completion.create(
            engine="davinci",
            prompt=prompt,
            audio=audio_base64,
            temperature=0.5,
            max_tokens=60,
            n=1,
            stop=None,
            timeout=10,
        )

        if "choices" in response and len(response["choices"]) > 0:
            response_audio_base64 = response["choices"][0]["audio"]
            response_audio = base64.b64decode(response_audio_base64)

            # detect silence in response audio and remove it
            response_audio = AudioSegment.from_file(
                response_audio, format="mp3")
            non_silent_audio = detect_nonsilent(response_audio, min_silence_len=50)[0]
            response_audio = response_audio[non_silent_audio[0]:non_silent_audio[1]]

            stream.write(response_audio.raw_data)
        else:
            print("Unexpected response format:", response)

    except Exception as e:
        print("Error:", e)

# stop audio stream
stream.stop_stream()
stream.close()
p.terminate()
Error:
error: Python 3.11.2 (v3.11.2:878ead1ac1, Feb 7 2023, 10:02:41) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license()" for more information. ========= RESTART: /Users/alfiehoward-delamare/Documents/ai hog inc.py ========= Warning (from warnings module): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pydub/utils.py", line 170 warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning) RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work Traceback (most recent call last): File "/Users/alfiehoward-delamare/Documents/ai hog inc.py", line 8, in <module> from pydub.utils import which, mediainfo, make_chunks, silence ImportError: cannot import name 'silence' from 'pydub.utils' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pydub/utils.py) ========= RESTART: /Users/alfiehoward-delamare/Documents/ai hog inc.py ========= Warning (from warnings module): File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pydub/utils.py", line 170 warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning) RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work Traceback (most recent call last): File "/Users/alfiehoward-delamare/Documents/ai hog inc.py", line 8, in <module> from pydub.utils import which, mediainfo, make_chunks, silence ImportError: cannot import name 'silence' from 'pydub.utils' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pydub/utils.py)
.py   ai hog inc.py (Size: 2.49 KB / Downloads: 92)
Larz60+ write Mar-27-2023, 07:59 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
When posting code please use the Python tags in the toolbar. Otherwise you lose all indentation and your code becomes difficult to interpret.

The errors at the bottom suggest some libraries are not available in the current environment
Reply
#3
(Mar-26-2023, 12:29 PM)jefsummers Wrote: When posting code please use the Python tags in the toolbar. Otherwise you lose all indentation and your code becomes difficult to interpret.

The errors at the bottom suggest some libraries are not available in the current environment

how do i get those libraries?
Reply
#4
Looks like the problems are with pydub.

https://pypi.org/project/pydub/

Output:
Project description Manipulate audio with an simple and easy high level interface. See the README file for details, usage info, and a list of gotchas.
Did you read the README file?

Applicable information on this page:

https://github.com/jiaaro/pydub/

Quote:Installation
Installing pydub is easy, but don't forget to install ffmpeg/avlib (the next section in this doc)

...

Dependencies
You can open and save WAV files with pure python. For opening and saving non-wav files – like mp3 – you'll need ffmpeg or libav.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python audio analysis kiyoshi7 3 1,781 Feb-22-2022, 06:09 PM
Last Post: Axel_Erfurt
  Real Time Audio Processing with Python Sound-Device not working Slartybartfast 2 4,001 Mar-14-2021, 07:20 PM
Last Post: Slartybartfast
  Handling multi-input/output audio in python bor1904 4 3,606 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  Accessing IP Cam Audio In Python Haselsmasher 1 5,477 Jul-10-2020, 02:59 AM
Last Post: kiliantics
  How to do real-time audio signal processing using python Zenolen 7 16,608 Nov-04-2019, 02:57 AM
Last Post: jefsummers
  How to get the duration of AMR audio file using python Prince_Bhatia 0 2,260 Aug-12-2019, 05:58 AM
Last Post: Prince_Bhatia
  Python Idea Assist.: BerryIMU & Raspberry Pi 3 Audio Mapping StephLeber 0 2,294 Dec-09-2018, 10:43 AM
Last Post: StephLeber
  Play Audio File w/ Python ducky_12 8 4,641 Sep-30-2018, 08:54 AM
Last Post: wavic
  Help with python and audio Burca 2 3,156 Apr-29-2018, 09:52 PM
Last Post: Burca
  Python audio library hbknjr 5 102,790 Aug-08-2017, 03:33 AM
Last Post: hbknjr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020