Skip to content
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

feat: Add synchronization to start of audio recordings #1984

Merged
merged 34 commits into from
May 1, 2023
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f995624
Basic idea
OmLanke Mar 22, 2023
952609b
Merge branch 'Pycord-Development:master' into voice-recording
OmLanke Mar 23, 2023
93df7a3
trying to use internal clock to time packets
OmLanke Mar 23, 2023
4d62fd3
Merge branch 'Pycord-Development:master' into voice-recording
OmLanke Mar 24, 2023
ee53416
Merge branch 'voice-recording' of https://github.com/Om1609/pycord in…
OmLanke Mar 24, 2023
7eabc87
final solution and cleanup
OmLanke Mar 24, 2023
66890d8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2023
0484a0c
Update CHANGELOG.md
OmLanke Mar 24, 2023
27b2d3e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2023
653f574
Example
OmLanke Mar 24, 2023
a30c89e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 24, 2023
81b0112
Merge branch 'master' into voice-recording
OmLanke Mar 24, 2023
a06a08b
Merge branch 'master' into voice-recording
OmLanke Mar 25, 2023
c70806e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 25, 2023
74e1040
Update CHANGELOG.md
OmLanke Mar 25, 2023
36b2feb
optionally use internal clock
OmLanke Mar 29, 2023
13464ee
Update discord/voice_client.py
OmLanke Apr 2, 2023
a6f2a48
Merge branch 'master' into voice-recording
OmLanke Apr 2, 2023
b5924eb
Merge branch 'voice-recording' of https://github.com/Om1609/pycord in…
OmLanke Apr 2, 2023
36690d7
Merge branch 'Pycord-Development:master' into voice-recording
OmLanke Apr 3, 2023
9c57b8c
Merge branch 'voice-recording' of https://github.com/Om1609/pycord in…
OmLanke Apr 3, 2023
90cf800
Revert "optionally use internal clock"
OmLanke Apr 4, 2023
afdd1ea
implement receive time as fallback
OmLanke Apr 4, 2023
6185ff6
implement receive time as fallback
OmLanke Apr 4, 2023
0257254
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 4, 2023
f55eeb3
remove fmt comments
OmLanke Apr 4, 2023
f49d033
Merge branch 'master' into voice-recording
plun1331 Apr 8, 2023
d67a1c9
Merge branch 'master' into voice-recording
OmLanke Apr 17, 2023
c7c80d0
Merge branch 'master' into voice-recording
Lulalaby Apr 18, 2023
901285c
Add pydub version
OmLanke Apr 19, 2023
b8d6f4b
Merge branch 'master' into voice-recording
OmLanke Apr 27, 2023
72a312d
Merge branch 'master' into voice-recording
OmLanke Apr 29, 2023
65e7bcf
change condition
OmLanke Apr 29, 2023
ecd3177
Merge branch 'master' into voice-recording
plun1331 May 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
final solution and cleanup
  • Loading branch information
OmLanke committed Mar 24, 2023
commit 7eabc87e92c901ebdcfe9f70c3a6e6ab7e88b1f5
41 changes: 15 additions & 26 deletions discord/voice_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def unpack_audio(self, data):

self.decoder.decode(data)

def start_recording(self, sink, callback, *args):
def start_recording(self, sink, callback, *args, sync_start=False):
OmLanke marked this conversation as resolved.
Show resolved Hide resolved
"""The bot will begin recording audio from the current voice channel it is in.
This function uses a thread so the current code line will not be stopped.
Must be in a voice channel to use.
Expand All @@ -716,6 +716,9 @@ def start_recording(self, sink, callback, *args):
A function which is called after the bot has stopped recording.
*args:
Args which will be passed to the callback function.
sync_start: :class:`bool`
If True, the recordings of subsequent users will start with silence.
This is useful for recording audio just as it was heard.

Raises
------
Expand All @@ -738,6 +741,7 @@ def start_recording(self, sink, callback, *args):
self.decoder = opus.DecodeManager(self)
self.decoder.start()
self.recording = True
self.sync_start = sync_start
self.sink = sink
sink.init(self)

Expand Down Expand Up @@ -829,43 +833,28 @@ def recv_decoded_audio(self, data):
# Add silence when they were not being recorded.
if data.ssrc not in self.user_timestamps: # First packet from user

if not self.user_timestamps: # First packet from anyone
if (
not self.user_timestamps or not self.sync_start
): # First packet from anyone
self.first_packet_timestamp = data.receive_time
silence = 0

else: # Already received a packet from someone else
else: # Previously received a packet from someone else

silence = (
int((data.receive_time - self.first_packet_timestamp) * 48000)
# - 960
) # not adding insane silence

# print(silence, silence / 48000, "here", end=" ")

self.user_timestamps.update({data.ssrc: data.receive_time})

else: # Already received a packet from user
silence = max(
0,
int(
(
(data.receive_time - self.user_timestamps[data.ssrc]) * 48000
- 960
)
),
) # seems to be adding lots of silence
- 1920
)

self.user_timestamps[data.ssrc] = data.receive_time
else: # Previously received a packet from user
silence = data.timestamp - self.user_timestamps[data.ssrc] - 960

# if silence:
# print(silence, silence / 48000, len(data.decoded_data), end=" ")
self.user_timestamps.update({data.ssrc: data.timestamp})

data.decoded_data = (
struct.pack("<h", 0) * silence * opus._OpusStruct.CHANNELS
struct.pack("<h", 0) * max(0, silence) * opus._OpusStruct.CHANNELS
+ data.decoded_data
)
# if silence:
# print(len(data.decoded_data))

while data.ssrc not in self.ws.ssrc_map:
time.sleep(0.05)
Expand Down