0

Hello guys ı am trying to do desktop voice assistant for ubuntu.In my program ıt was working with

os.system("mpg123 audio.mp3")

How can ı use pydub module instead of this line?

but ı don't want to use system to play audio file for talk with me.I think it's slower. I want faster program and today ı was trying pydub module.

There is my program;

from gtts import gTTS
import speech_recognition as sr
import os
import webbrowser
import datetime
import time
import sys
import random
from pydub import AudioSegment
from pydub.playback import play

#sound = AudioSegment.from_mp3('hello.mp3')
#play(sound)


def talkToMe(audio):
    print(audio)
    tts = gTTS(text=audio, lang= "en")
    tts.save("audio.mp3")
    play("audio.mp3")  #os.system("mpg123 audio.mp3")


def OurCommands():
    r = sr.Recognizer()

    with sr.Microphone() as source:
        os("clear")
        print("Ready for next command")
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration = 1)
        audio = r.listen(source)

    try:
        command = r.recognize_google(audio)
        print("You said: " + command + "\n")

    except sr.UnknownValueError:
        print("Your last command not understand")
        command = str(input("Command: "))
    return command

#if statements..

def asistan(command):
    if "open web browser" in command:
        talkToMe("İt\'s opening")
        webbrowser.open("www.google.com.tr")



    elif "play music" in command:
        mixer.init()
        mixer.music.load('/path/to/music/')
        mixer.music.play()
    elif "stop music" in command:
        mixer.music.stop()


    elif "thank you" in  command:
        talkToMe("You're welcome")

    elif "shutdown my computer" in command:
        talkToMe("I will close after five second")
        time.sleep(5)
        os.system("shutdown now -h")

    elif "open youtube" in command:
        webbrowser.open("youtube.com")


hour = int(datetime.datetime.now().hour)
if hour >= 1 and hour <12:
    talkToMe("Goodmorning")
elif hour >= 12 and hour < 16:
    talkToMe("Good afternoon")
else:
    talkToMe("Good night")


while True:
    asistan(OurCommands())

but when ı want to use pydub module ı get an error like this;

AttributeError: 'str' object has no attribute 'raw_data'

I tried in my computer this module working for me and played mp3 files. So now how can ı use pydub module in my program for computer to talk with me. I think ı need audiosegment but how can ı use in my program do ı have to use another module? or also ı can work with pydub module in my program? Thanks in advance:)

Can İlgu
  • 137
  • 3
  • 12

1 Answers1

2

From the docs:

from pydub import AudioSegment
from pydub.playback import play

sound = AudioSegment.from_file("mysound.wav", format="wav")
play(sound)

Play doesn't expect a filename, it expects input like this. You want

sound = AudioSegment.from_file("audio.mp3", format="mp3")
play(sound)
clubby789
  • 2,543
  • 4
  • 16
  • 32
  • 1
    Ups.. it was really easy to fix sorry about my easy question but ı didin't fixed when ı try to fix the error. thank you it's working perfectly. – Can İlgu Jan 24 '20 at 12:10