Class: Discordrb::Voice::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/discordrb/voice/encoder.rb

Overview

Wrapper class around opus-ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEncoder

Returns a new instance of Encoder.



9
10
11
12
13
14
15
# File 'lib/discordrb/voice/encoder.rb', line 9

def initialize
  @sample_rate = 48_000
  @frame_size = 960
  @channels = 2
  @volume = 1.0
  @opus = Opus::Encoder.new(@sample_rate, @frame_size, @channels)
end

Instance Attribute Details

#volumeObject

Returns the value of attribute volume.



7
8
9
# File 'lib/discordrb/voice/encoder.rb', line 7

def volume
  @volume
end

Instance Method Details

#destroyObject



21
22
23
# File 'lib/discordrb/voice/encoder.rb', line 21

def destroy
  @opus.destroy
end

#encode(buffer) ⇒ Object



17
18
19
# File 'lib/discordrb/voice/encoder.rb', line 17

def encode(buffer)
  @opus.encode(buffer, 1920)
end

#encode_file(file) ⇒ Object



25
26
27
28
# File 'lib/discordrb/voice/encoder.rb', line 25

def encode_file(file)
  command = "ffmpeg -loglevel 0 -i #{file.path} -f s16le -ar 48000 -ac 2 -af volume=#{@volume} pipe:1"
  IO.popen(command)
end

#encode_io(io) ⇒ Object



30
31
32
33
34
35
# File 'lib/discordrb/voice/encoder.rb', line 30

def encode_io(io)
  ret_io, writer = IO.pipe
  command = "ffmpeg -loglevel 0 -i - -f s16le -ar 48000 -ac 2 -af volume=#{@volume} pipe:1"
  spawn(command, in: io, out: writer)
  ret_io
end