77 lines
2.4 KiB
Python
Raw Normal View History

2025-01-18 07:58:13 +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
2025-01-18 09:02:13 +00:00
import pygame
2025-01-18 07:58:13 +00:00
def play_audio(filename):
2025-01-18 09:02:13 +00:00
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):
2025-01-18 07:58:13 +00:00
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)
2025-01-18 09:02:13 +00:00
p = pyaudio.PyAudio()
stream = p.open(
format=p.get_format_from_width(2),
2025-01-18 07:58:13 +00:00
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()
2025-01-18 09:02:13 +00:00
p.terminate()
2025-01-18 07:58:13 +00:00
process.stdout.close()
2025-01-18 09:02:13 +00:00
process.wait()