-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fast api server setup and webhook for dialogueflow
- Loading branch information
1 parent
8481463
commit 4133fd7
Showing
3 changed files
with
76 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,77 @@ | ||
import pyaudio | ||
import wave | ||
import sys | ||
import subprocess | ||
import socket | ||
import subprocess | ||
import threading | ||
import time | ||
import sys | ||
|
||
class AudioStreamClient: | ||
def __init__(self, host=socket.gethostname(), port=5000): | ||
self.host = host | ||
self.port = port | ||
self.chunk_size = 1024 | ||
self.chunk_size = 8192 | ||
self.running = False | ||
|
||
def connect(self): | ||
self.client_socket = socket.socket() | ||
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
try: | ||
self.client_socket.connect((self.host, self.port)) | ||
return True | ||
except Exception as e: | ||
print(f"Connection error: {e}") | ||
return False | ||
|
||
def stream_audio(self, filename): | ||
if not self.connect(): | ||
return | ||
|
||
try: | ||
# Start FFmpeg process to convert input file to raw PCM | ||
# Convert MP3 to raw PCM using FFmpeg | ||
ffmpeg_process = subprocess.Popen([ | ||
"ffmpeg", | ||
"-i", filename, | ||
"-i", filename, # Input file | ||
"-loglevel", "panic", | ||
"-vn", # Disable video | ||
"-f", "s16le", # Output format | ||
"-acodec", "pcm_s16le", # Audio codec | ||
"-vn", # No video | ||
"-f", "s16le", # Raw PCM output | ||
"-acodec", "pcm_s16le", | ||
"-ar", "44100", # Sample rate | ||
"-ac", "2", # Channels | ||
"-ac", "2", # Stereo channels | ||
"pipe:1" | ||
], stdout=subprocess.PIPE) | ||
], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) | ||
|
||
print("Starting audio stream...") | ||
self.running = True | ||
|
||
# Read and stream data | ||
|
||
while self.running: | ||
data = ffmpeg_process.stdout.read(self.chunk_size) | ||
if len(data) == 0: | ||
if not data: | ||
break | ||
|
||
# Send to socket | ||
self.client_socket.send(data) | ||
|
||
self.client_socket.sendall(data) | ||
|
||
print("Streaming finished") | ||
|
||
except Exception as e: | ||
print(f"Streaming error: {e}") | ||
|
||
finally: | ||
# Cleanup | ||
if 'ffmpeg_process' in locals(): | ||
ffmpeg_process.kill() | ||
ffmpeg_process.kill() | ||
self.client_socket.close() | ||
|
||
def stop(self): | ||
self.running = False | ||
|
||
|
||
def main(): | ||
if len(sys.argv) < 2: | ||
print(f"Usage: {sys.argv[0]} filename") | ||
sys.exit(-1) | ||
|
||
client = AudioStreamClient() | ||
|
||
# Start streaming in a separate thread | ||
stream_thread = threading.Thread(target=client.stream_audio, args=(sys.argv[1],)) | ||
stream_thread.start() | ||
|
||
# Wait for user input to stop | ||
|
||
input("Press Enter to stop streaming...") | ||
client.stop() | ||
stream_thread.join() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters