-
Notifications
You must be signed in to change notification settings - Fork 5
created windows audio client #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import os | ||
| import sys | ||
| import struct | ||
| import numpy as np | ||
| import time | ||
| import requests | ||
| import pyaudiowpatch as pyaudio | ||
|
|
||
| def translator(): | ||
| DOCKER_IP = "http://0.0.0.0:8000/" | ||
| print("TRANSLATOR IS RUNNING") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bad practice to use print statements like this. Use the |
||
|
|
||
| # open PyAudio manager via context manager | ||
| with pyaudio.PyAudio() as p: | ||
| # open audio stream via context manager | ||
| with p.open(format=pyaudio.paInt32, channels=2, rate=48000, input=True, frames_per_buffer=1024) as stream: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. audio settings change depending on the user's setup, it would be nice to not have these be hard-coded values Also, the input device is never specified. Look at the examples in the PyAudioWPatch repo for how you are supposed to use this. |
||
| print("Recording started...") | ||
| while True: | ||
| # read a chunk of raw audio data from the stream | ||
| raw_audio_data = stream.read(1024) | ||
|
|
||
| # convert raw audio data to floating-point values | ||
| float_audio_data = np.frombuffer(raw_audio_data, dtype=np.int32) / (2 ** 31) # normalize to range [-1.0, 1.0] | ||
|
|
||
| # gather audio data in real time | ||
| data = np.abs(float_audio_data[-20:]) | ||
| avg = np.average(float_audio_data[-20:]) | ||
| peak = np.max(float_audio_data[-20:]) | ||
|
|
||
| payload = { | ||
| "avg": float(avg), | ||
| "peak": float(peak), | ||
| "data": data.tolist(), | ||
| "source": "Windows Device" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| } | ||
|
|
||
| # send audio data to Docker container | ||
| with requests.Session() as session: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having a session that is immediately closed defeats the purpose of the session. It would be better to either use |
||
| response = session.post(DOCKER_IP + "audio_in", json=payload).json() | ||
|
|
||
| def main(): | ||
| # make sure the fifo file exists (if required) | ||
| # not needed for Windows adaptation | ||
|
|
||
| try: | ||
| translator() | ||
| except Exception as e: | ||
| print(f"Error: {e}") | ||
| sys.exit(1) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a global or a parameter passed into the translator function because the IP address of the server is not static and may need to be changed by the user.
Consider allowing the user to pass in the addr/port as command line arguments.