Class: Mumble::AudioRecorder

Inherits:
Object
  • Object
show all
Includes:
ThreadTools
Defined in:
lib/mumble-ruby/audio_recorder.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, sample_rate) ⇒ AudioRecorder



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mumble-ruby/audio_recorder.rb', line 8

def initialize(client, sample_rate)
  @client = client
  @wav_format = WaveFile::Format.new(:mono, :pcm_16, sample_rate)
  @pds = PacketDataStream.new
  @pds_lock = Mutex.new

  @decoders = Hash.new do |h, k|
    h[k] = Opus::Decoder.new sample_rate, sample_rate / 100, 1
  end

  @queues = Hash.new do |h, k|
    h[k] = Queue.new
  end
end

Instance Method Details

#recording?Boolean



23
24
25
# File 'lib/mumble-ruby/audio_recorder.rb', line 23

def recording?
  @recording ||= false
end

#start(file) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/mumble-ruby/audio_recorder.rb', line 27

def start(file)
  unless recording?
    @file = WaveFile::Writer.new(file, @wav_format)
    @callback = @client.on_udp_tunnel { |msg| process_udp_tunnel msg }
    spawn_thread :write_audio
    @recording = true
  end
end

#stopObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mumble-ruby/audio_recorder.rb', line 36

def stop
  if recording?
    @client.remove_callback :udp_tunnel, @callback
    kill_threads
    @decoders.values.each &:destroy
    @decoders.clear
    @queues.clear
    @file.close
    @recording = false
  end
end