1

I need your help because at the moment I use "engine.say()" from "pyttsx3" so my program "speaks" with me. That's already working but now I want an audio visualizer for this voice how can I do that?

Example

import pyttsx3

engine = pyttsx3.init()

engine.say("Hello World")
engine.runAndWait()

And I want something like this.

What I have

import pyaudio
import struct
import matplotlib.pyplot as plt
import numpy as np

mic = pyaudio.PyAudio()
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 5000
CHUNK = 3000#int(RATE/20)
stream = mic.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK)

fig, ax = plt.subplots(figsize=(14,6))
x = np.arange(0, 2 * CHUNK, 2)
ax.set_ylim(-500, 500)
ax.set_xlim(0, CHUNK)
line, = ax.plot(x, np.random.rand(CHUNK))


while True:
    data = stream.read(CHUNK)
    data = np.frombuffer(data, np.int16)
    line.set_ydata(data)
    fig.canvas.draw()
    fig.canvas.flush_events()
    plt.pause(0.01)

This already visualizes my microphone audio but how can I make the voice the source? Hope you can help me Thank you very much!

Frederick
  • 450
  • 4
  • 22

1 Answers1

1

I think what you need is - Realtime_PyAudio_FFT

One of the benefits of this is that

Starts a stream_reader that pulls live audio data from any source using PyAudio (soundcard, microphone, ...)

Since you'll be playing the audio using pyttsx3 it can extract the audio from the soundcard and would show a live visualization. This is a far better option for your case instead of extracting it via the microphone.

Also, you may need to enable threading or multiprocessing to visualize the audio if it's being executed by the same thread. Here's a good guide on that - threading or multiprocessing

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • This is really cool Thank you. But this also just takes my microphone not the voice do you know why? – Frederick Oct 31 '20 at 21:41
  • What exactly do you mean? Can you give an example? If you want the voice to be configured without the use of a mic then use sound-card option to do this. Look into the documentation as how this should be implemented. – AzyCrw4282 Oct 31 '20 at 21:44
  • Im sorry but I dont find the documentation on the github? Or the option for the soundcard – Frederick Oct 31 '20 at 21:52
  • Having had another look — it seems as though the instructions are only a pipeline, i.e. soundcard input can be enabled in many other ways. One example is [here](https://stackoverflow.com/questions/36894315/how-to-select-a-specific-input-device-with-pyaudio). You can of-course obtain and do in any way as you wish – AzyCrw4282 Oct 31 '20 at 21:56
  • There I just get my headset and Microsoft Soundmapper(input) but nothing for the voice? – Frederick Oct 31 '20 at 22:04
  • yes, because the voice will be retrieved/inputted from the soundcard. Consider that simply as a controller that handles input/output of audio. If you want voice then enable a microphone option instead of a soundcard. – AzyCrw4282 Oct 31 '20 at 22:06
  • But when I enable this soundcard option in devices it still just gets my mic audio and not the other – Frederick Oct 31 '20 at 22:10
  • Play around and research and you will probably figure out the error. If the error still persists, post it as another question and @me here to get my attention. If this answer helped with your initial question — feel free to accept it. – AzyCrw4282 Oct 31 '20 at 22:12