Sublime Forum

"with sr.Microphone() as source:" doesn't work in sublime text

#1

“”"
import speech_recognition as sr
import time

r = sr.Recognizer()

time.sleep(3)
print(“Saying something and I will print it.”)

with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)

text = r.recognize_google(audio)
print(text)
“”"
This code work in Jupyter Notebook and Terminal, but not in sublime text. I think this is cause by sublime text can’t use microphone. Am I right? I hope someone can help me.

0 Likes

#2

How are you running that code? (perhaps a build system?) In what way does it “not work”?

0 Likes

#4


as the picture shows. No matter what I say, it doesn’t response when it run in sublime text. but when I run it in terminal it works, and it also works in Jupyter notebook.

0 Likes

#5

0 Likes

#6


this work.

0 Likes

#7

Does it work, if you use the following customized Python.sublime-build ?

{
	"shell_cmd": "python3 -u \"$file\"",
	"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
	"selector": "source.python",

	"env": {"PYTHONIOENCODING": "utf-8"},

	"windows": {
		"shell_cmd": "python -u \"$file\""
	},

	"variants":
	[
		{
			"name": "Syntax Check",
			"shell_cmd": "python3 -m py_compile \"$file\"",

			"windows": {
				"shell_cmd": "python -m py_compile \"$file\""
			}
		}
	]
}
0 Likes

#8

No, it still doesn’t work.

0 Likes

#9

So I wonder, whether r.recognize_google(audo) or r.listen() behave just like pythons input() function.

If audio is streamed via STDIN or something like that, the only solution was to use Terminus as build system, because the default build system is not able to handle inputs. It is designed to output compiler messages only.

See Int input function error or one of the other hundreds of threads about the input() topic.

0 Likes

#10

I know your means. I also try to run my code with SublimeREPL and Terminus, but it still doesn’t work.
I remember that when ran this code on the terminal for the first time, the terminal would request microphone permission, but Sublime never requested microphone permission. I’m not sure if this problem is related to that.

0 Likes

#11

Perhaps permissions have already been denied or the app is “quarantined”, so you won’t get a permission popup.

0 Likes

#12


But there’s no Sublime.

0 Likes

#13

You could manually add ST to that list.

0 Likes

#14

Thank you very much!
I add ST to list by

sudo sqlite3 ~/Library/Application\ Support/com.apple.TCC/TCC.db “INSERT or REPLACE INTO access VALUES(‘kTCCServiceMicrophone’,‘com.sublimetext.4’,0,0,1,1,NULL,NULL,NULL,‘UNUSED’,NULL,0,1551892126);”

and now it works.

0 Likes