discordrb 3.6.1 → 3.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,9 @@
2
2
 
3
3
  require 'discordrb/voice/encoder'
4
4
  require 'discordrb/voice/network'
5
+ require 'discordrb/voice/timer'
5
6
  require 'discordrb/logger'
7
+ require 'ffi'
6
8
 
7
9
  # Voice support
8
10
  module Discordrb::Voice
@@ -304,20 +306,19 @@ module Discordrb::Voice
304
306
  def play_internal
305
307
  count = 0
306
308
  @playing = true
309
+ self.speaking = true
307
310
 
308
- # Default play length (ms), will be adjusted later
309
- @length = IDEAL_LENGTH
311
+ last_sent = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
310
312
 
311
- self.speaking = true
312
313
  loop do
313
- # Starting from the tenth packet, perform length adjustment every 100 packets (2 seconds)
314
- should_adjust_this_packet = (count % @adjust_interval == @adjust_offset)
315
-
316
- # If we should adjust, start now
317
- @length_adjust = Time.now.nsec if should_adjust_this_packet
314
+ # If paused, wait
315
+ sleep 0.1 while @paused
318
316
 
319
317
  break unless @playing
320
318
 
319
+ # Get timestamp before encoding
320
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
321
+
321
322
  # If we should skip, get some data, discard it and go to the next iteration
322
323
  if @skips.positive?
323
324
  @skips -= 1
@@ -339,45 +340,22 @@ module Discordrb::Voice
339
340
  next unless buf
340
341
 
341
342
  # Track intermediate adjustment so we can measure how much encoding contributes to the total time
342
- @intermediate_adjust = Time.now.nsec if should_adjust_this_packet
343
+ intermediate_adjust = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
343
344
 
344
- # Send the packet
345
- @udp.send_audio(buf, @sequence, @time)
345
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
346
346
 
347
- # Set the stream time (for tracking how long we've been playing)
348
- @stream_time = count * @length / 1000
349
-
350
- if @length_override # Don't do adjustment because the user has manually specified an override value
351
- @length = @length_override
352
- elsif @length_adjust # Perform length adjustment
353
- # Define the time once so it doesn't get inaccurate
354
- now = Time.now.nsec
355
-
356
- # Difference between length_adjust and now in ms
357
- ms_diff = (now - @length_adjust) / 1_000_000.0
358
- if ms_diff >= 0
359
- @length = if @adjust_average
360
- (IDEAL_LENGTH - ms_diff + @length) / 2.0
361
- else
362
- IDEAL_LENGTH - ms_diff
363
- end
364
-
365
- # Track the time it took to encode
366
- encode_ms = (@intermediate_adjust - @length_adjust) / 1_000_000.0
367
- @bot.debug("Length adjustment: new length #{@length} (measured #{ms_diff}, #{(100 * encode_ms) / ms_diff}% encoding)") if @adjust_debug
368
- end
369
- @length_adjust = nil
347
+ if (last_sent + IDEAL_LENGTH) > Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
348
+ sleep_duration = (last_sent + IDEAL_LENGTH - Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)) / 1000.0
349
+ @bot.debug("Waiting for next frame: #{sleep_duration * 1000}ms (encoding #{intermediate_adjust - start_time}ms)") if @adjust_debug
350
+ sleep sleep_duration if sleep_duration.positive?
370
351
  end
371
352
 
372
- # If paused, wait
373
- sleep 0.1 while @paused
353
+ # Send the packet
354
+ @udp.send_audio(buf, @sequence, @time)
374
355
 
375
- if @length.positive?
376
- # Wait `length` ms, then send the next packet
377
- sleep @length / 1000.0
378
- else
379
- Discordrb::LOGGER.warn('Audio encoding and sending together took longer than Discord expects one packet to be (20 ms)! This may be indicative of network problems.')
380
- end
356
+ # Set the stream time (for tracking how long we've been playing)
357
+ @stream_time = count * IDEAL_LENGTH / 1000
358
+ last_sent = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
381
359
  end
382
360
 
383
361
  @bot.debug('Sending five silent frames to clear out buffers')
data/lib/discordrb.rb CHANGED
@@ -65,7 +65,9 @@ module Discordrb
65
65
  long_date: 'D', # 20 April 2021
66
66
  short_datetime: 'f', # 20 April 2021 16:20
67
67
  long_datetime: 'F', # Tuesday, 20 April 2021 16:20
68
- relative: 'R' # 2 months ago
68
+ relative: 'R', # 2 months ago
69
+ simple_datetime: 's', # 20/04/2021, 16:20
70
+ medium_datetime: 'S' # 20/04/2021, 16:20:30
69
71
  }.freeze
70
72
 
71
73
  # Splits a message into chunks of 2000 characters. Attempts to split by lines if possible.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discordrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.1
4
+ version: 3.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - meew0
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.2.0
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.2.0
26
+ version: '0.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ffi
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 3.6.1
89
+ version: 3.7.0
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 3.6.1
96
+ version: 3.7.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: bundler
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -330,6 +330,7 @@ files:
330
330
  - lib/discordrb/data/reaction.rb
331
331
  - lib/discordrb/data/recipient.rb
332
332
  - lib/discordrb/data/role.rb
333
+ - lib/discordrb/data/role_subscription.rb
333
334
  - lib/discordrb/data/server.rb
334
335
  - lib/discordrb/data/server_preview.rb
335
336
  - lib/discordrb/data/snapshot.rb
@@ -369,7 +370,9 @@ files:
369
370
  - lib/discordrb/version.rb
370
371
  - lib/discordrb/voice/encoder.rb
371
372
  - lib/discordrb/voice/network.rb
373
+ - lib/discordrb/voice/opcodes.rb
372
374
  - lib/discordrb/voice/sodium.rb
375
+ - lib/discordrb/voice/timer.rb
373
376
  - lib/discordrb/voice/voice_bot.rb
374
377
  - lib/discordrb/webhooks.rb
375
378
  - lib/discordrb/websocket.rb