switched to sound device
This commit is contained in:
@@ -6,13 +6,15 @@
|
|||||||
|
|
||||||
##Windows
|
##Windows
|
||||||
|
|
||||||
pip install pyaudio
|
pip install numpy
|
||||||
|
pip install sounddevice
|
||||||
pip install python-dotenv
|
pip install python-dotenv
|
||||||
|
|
||||||
|
|
||||||
##Mac
|
##Mac
|
||||||
|
|
||||||
brew install portaudio
|
pip install numpy
|
||||||
|
pip install sounddevice
|
||||||
pip install python-dotenv
|
pip install python-dotenv
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import openai
|
import openai
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
import pyaudio
|
import numpy as np
|
||||||
|
import sounddevice as sd
|
||||||
|
import soundfile as sf
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@@ -11,15 +13,11 @@ load_dotenv()
|
|||||||
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
|
||||||
|
|
||||||
def list_audio_devices():
|
def list_audio_devices():
|
||||||
p = pyaudio.PyAudio()
|
|
||||||
print("Available audio devices:")
|
print("Available audio devices:")
|
||||||
info = p.get_host_api_info_by_index(0)
|
devices = sd.query_devices()
|
||||||
num_devices = info.get('deviceCount')
|
for index, device in enumerate(devices):
|
||||||
# List all available devices, and mark output devices
|
if device['max_output_channels'] > 0:
|
||||||
for i in range(0, num_devices):
|
print(f"Device index {index}: {device['name']}")
|
||||||
if p.get_device_info_by_index(i).get('maxOutputChannels') > 0:
|
|
||||||
print(f"Device index {i}: {p.get_device_info_by_index(i).get('name')}")
|
|
||||||
p.terminate()
|
|
||||||
|
|
||||||
def stream_audio_to_virtual_mic(text, voice="fable", device_index=None):
|
def stream_audio_to_virtual_mic(text, voice="fable", device_index=None):
|
||||||
|
|
||||||
@@ -36,49 +34,22 @@ def stream_audio_to_virtual_mic(text, voice="fable", device_index=None):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Set up PyAudio
|
|
||||||
p = pyaudio.PyAudio()
|
|
||||||
if device_index is None:
|
if device_index is None:
|
||||||
device_index = int(input("Enter the device index: "))
|
device_index = int(input("Enter the device index: "))
|
||||||
|
|
||||||
stream = p.open(format=pyaudio.paInt16,
|
# Load the binary audio content into a numpy array
|
||||||
channels=1,
|
audio_data = np.frombuffer(response.content, dtype=np.int16)
|
||||||
rate=22050, # Adjust the rate to match the audio format
|
|
||||||
output=True,
|
|
||||||
output_device_index=device_index)
|
|
||||||
|
|
||||||
# Stream audio chunks to virtual microphone
|
# Set the samplerate assumed from OpenAI's API (check documentation for exact rate)
|
||||||
try:
|
sample_rate = 22050 # or 44100, depending on the API's output format
|
||||||
for chunk in response.iter_lines():
|
|
||||||
stream.write(chunk)
|
|
||||||
|
|
||||||
##write to text file for testing
|
# Play audio
|
||||||
with open("test_output.wav", "wb") as f:
|
sd.play(audio_data, sample_rate, device=device_index)
|
||||||
f.write(response.content)
|
sd.wait() # Wait until the audio has finished playing
|
||||||
|
|
||||||
finally:
|
sf.write('captured_audio.wav', audio_data, sample_rate)
|
||||||
# Ensure resources are cleaned up
|
|
||||||
stream.stop_stream()
|
|
||||||
stream.close()
|
|
||||||
p.terminate()
|
|
||||||
|
|
||||||
"""
|
|
||||||
# Stream audio chunks to virtual microphone
|
|
||||||
try:
|
|
||||||
# Ensure you are handling the response's binary content correctly
|
|
||||||
# Adjust the method to access binary data as per the latest library version or response object
|
|
||||||
audio_data = response.content # This might need adjustment based on actual response object methods
|
|
||||||
stream.write(audio_data)
|
|
||||||
|
|
||||||
##write to text file for testing
|
|
||||||
with open("test_output.wav", "wb") as f:
|
|
||||||
f.write(response.content)
|
|
||||||
|
|
||||||
finally:
|
|
||||||
stream.stop_stream()
|
|
||||||
stream.close()
|
|
||||||
p.terminate()
|
|
||||||
"""
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
|
|||||||
Reference in New Issue
Block a user