2025-01-18 10:56:40 +00:00
# Written by retoor@molodetz.nl
# This source code initializes a Text-to-Speech (TTS) engine, plays text as audio using the TTS engine, and plays audio files using both the VLC media player and PyAudio.
# Libraries imported: 'pyaudio', 'wave', 'pyttsx3', 'functools', 'os', 'simpleaudio'
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import pyaudio
import functools
import os
import subprocess
import sys
import pygame
2025-01-18 14:40:31 +00:00
import time
2025-01-18 10:56:40 +00:00
2025-01-19 01:30:39 +00:00
PLAY_SOUND = True
def stop_audio ():
global PLAY_SOUND
PLAY_SOUND = False
async def stop_audio_async ():
global PLAY_SOUND
PLAY_SOUND = False
2025-01-18 10:56:40 +00:00
2025-01-18 14:42:26 +00:00
async def play_audio_async ( filename ):
2025-01-19 01:30:39 +00:00
global PLAY_SOUND
PLAY_SOUND = True
2025-01-18 14:38:02 +00:00
pygame . mixer . init ()
pygame . mixer . music . load ( filename )
2025-01-18 14:40:31 +00:00
time_start = time . time ()
2025-01-18 14:38:02 +00:00
pygame . mixer . music . play ()
2025-01-18 14:40:31 +00:00
2025-01-18 14:38:02 +00:00
while pygame . mixer . music . get_busy ():
2025-01-19 01:30:39 +00:00
if not PLAY_SOUND :
pygame . mixer . music . stop ()
break
2025-01-18 14:40:31 +00:00
duration = time . time () - time_start
yield duration
2025-01-19 01:30:39 +00:00
2025-01-18 10:56:40 +00:00
def play_audio ( filename ):
pygame . mixer . init ()
pygame . mixer . music . load ( filename )
pygame . mixer . music . play ()
while pygame . mixer . music . get_busy ():
pygame . time . Clock () . tick ( 10 )
def play_audio2 ( filename ):
ffmpeg_cmd = [
"ffmpeg" ,
"-i" , filename ,
"-f" , "s16le" ,
"-ar" , "44100" ,
"-ac" , "2" ,
"pipe:1"
]
process = subprocess . Popen ( ffmpeg_cmd , stdout = subprocess . PIPE , stderr = subprocess . PIPE , bufsize = 10 ** 6 )
p = pyaudio . PyAudio ()
stream = p . open (
format = p . get_format_from_width ( 2 ),
channels = 2 ,
rate = 44100 ,
output = True
)
chunk_size = 4096
try :
while True :
data = process . stdout . read ( chunk_size )
if not data :
break
stream . write ( data )
finally :
stream . stop_stream ()
stream . close ()
p . terminate ()
process . stdout . close ()
process . wait ()