Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Speech To Speech
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
huggingface
Speech To Speech
Commits
9e156c85
Commit
9e156c85
authored
6 months ago
by
Andres Marafioti
Browse files
Options
Downloads
Patches
Plain Diff
revert changes to listen and play
parent
e5b1c0f3
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
listen_and_play.py
+11
-27
11 additions, 27 deletions
listen_and_play.py
with
11 additions
and
27 deletions
listen_and_play.py
+
11
−
27
View file @
9e156c85
...
...
@@ -3,8 +3,8 @@ import threading
from
queue
import
Queue
from
dataclasses
import
dataclass
,
field
import
sounddevice
as
sd
import
argp
arse
import
numpy
as
np
from
transformers
import
HfArgumentP
arse
r
@dataclass
class
ListenAndPlayArguments
:
...
...
@@ -29,6 +29,7 @@ class ListenAndPlayArguments:
metadata
=
{
"
help
"
:
"
The network port for receiving data. Default is 12346.
"
},
)
def
listen_and_play
(
send_rate
=
16000
,
recv_rate
=
44100
,
...
...
@@ -52,14 +53,8 @@ def listen_and_play(
def
callback_recv
(
outdata
,
frames
,
time
,
status
):
if
not
recv_queue
.
empty
():
data
=
recv_queue
.
get
()
# Convert bytes to numpy array
audio_array
=
np
.
frombuffer
(
data
,
dtype
=
np
.
int16
)
# Reduce volume to 30%
audio_array
=
(
audio_array
*
0.3
).
astype
(
np
.
int16
)
# Convert back to bytes
reduced_data
=
audio_array
.
tobytes
()
outdata
[:
len
(
reduced_data
)]
=
reduced_data
outdata
[
len
(
reduced_data
)
:]
=
b
"
\x00
"
*
(
len
(
outdata
)
-
len
(
reduced_data
))
outdata
[:
len
(
data
)]
=
data
outdata
[
len
(
data
)
:]
=
b
"
\x00
"
*
(
len
(
outdata
)
-
len
(
data
))
else
:
outdata
[:]
=
b
"
\x00
"
*
len
(
outdata
)
...
...
@@ -79,7 +74,7 @@ def listen_and_play(
while
len
(
data
)
<
chunk_size
:
packet
=
conn
.
recv
(
chunk_size
-
len
(
data
))
if
not
packet
:
return
None
return
None
# Connection has been closed
data
+=
packet
return
data
...
...
@@ -96,16 +91,13 @@ def listen_and_play(
blocksize
=
list_play_chunk_size
,
callback
=
callback_send
,
)
recv_stream
=
sd
.
RawOutputStream
(
samplerate
=
recv_rate
,
channels
=
1
,
dtype
=
"
int16
"
,
blocksize
=
list_play_chunk_size
,
callback
=
callback_recv
,
device
=
0
,
)
threading
.
Thread
(
target
=
send_stream
.
start
).
start
()
threading
.
Thread
(
target
=
recv_stream
.
start
).
start
()
...
...
@@ -117,9 +109,7 @@ def listen_and_play(
input
(
"
Press Enter to stop...
"
)
except
KeyboardInterrupt
:
print
(
"
\n
Program interrupted by user. Exiting...
"
)
except
Exception
as
e
:
print
(
f
"
An error occurred:
{
e
}
"
)
print
(
"
Finished streaming.
"
)
finally
:
stop_event
.
set
()
...
...
@@ -131,14 +121,8 @@ def listen_and_play(
recv_socket
.
close
()
print
(
"
Connection closed.
"
)
if
__name__
==
"
__main__
"
:
parser
=
argparse
.
ArgumentParser
(
description
=
"
Listen and Play Audio
"
)
parser
.
add_argument
(
"
--send_rate
"
,
type
=
int
,
default
=
16000
,
help
=
"
In Hz. Default is 16000.
"
)
parser
.
add_argument
(
"
--recv_rate
"
,
type
=
int
,
default
=
16000
,
help
=
"
In Hz. Default is 16000.
"
)
parser
.
add_argument
(
"
--list_play_chunk_size
"
,
type
=
int
,
default
=
1024
,
help
=
"
The size of data chunks (in bytes). Default is 1024.
"
)
parser
.
add_argument
(
"
--host
"
,
type
=
str
,
default
=
"
localhost
"
,
help
=
"
The hostname or IP address for listening and playing. Default is
'
localhost
'
.
"
)
parser
.
add_argument
(
"
--send_port
"
,
type
=
int
,
default
=
12345
,
help
=
"
The network port for sending data. Default is 12345.
"
)
parser
.
add_argument
(
"
--recv_port
"
,
type
=
int
,
default
=
12346
,
help
=
"
The network port for receiving data. Default is 12346.
"
)
args
=
parser
.
parse_args
()
listen_and_play
(
**
vars
(
args
))
parser
=
HfArgumentParser
((
ListenAndPlayArguments
,))
(
listen_and_play_kwargs
,)
=
parser
.
parse_args_into_dataclasses
()
listen_and_play
(
**
vars
(
listen_and_play_kwargs
))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment