Class: Audio

Inherits:
Object
  • Object
show all
Defined in:
lib/vimamsa/audio.rb

Overview

following the example gstreamer-4.2.0/sample/helloworld_e.rb

Constant Summary collapse

@@playbin =
nil

Class Method Summary collapse

Class Method Details

.play(fn) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vimamsa/audio.rb', line 17

def self.play(fn)
  playbin = @@playbin
  if playbin.nil?
    playbin = Gst::ElementFactory.make("playbin")
    if playbin.nil?
      puts "'playbin' gstreamer plugin missing"
      return
    end
  else
    if playbin.current_state == "playing"
      playbin.stop # Stop previous play
    end
  end

  # playbin.seek(10.0)

  # playbin.volume
  # playbin.volume=1.0
  # playbin.stream_time
  # playbin.current_state

  # take the commandline argument and ensure that it is a uri
  if Gst.valid_uri?(fn)
    uri = fn
  else
    uri = Gst.filename_to_uri(fn)
  end
  playbin.uri = uri
  @@playbin = playbin
  $pb = playbin

  bus = playbin.bus
  bus.add_watch do |bus, message|
    case message.type
    when Gst::MessageType::EOS
      puts "End-of-stream"
    when Gst::MessageType::ERROR
      error, debug = message.parse_error
      puts "Debugging info: #{debug || "none"}"
      puts "Error: #{error.message}"
    end
    true
  end

  message("Start playing audio: #{fn}")

  # start play back and listed to events
  playbin.play
  playbin.seek_simple(Gst::Format::TIME, Gst::SeekFlags::NONE, 10.0)
end

.seek_forward(secs = 5.0) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vimamsa/audio.rb', line 68

def self.seek_forward(secs = 5.0)
  return if @@playbin.nil?
  if @@playbin.current_state == "playing"
    duration = @@playbin.query_duration(Gst::Format::TIME)[1]
    curpos = @@playbin.query_position(Gst::Format::TIME)[1]
    newpos = curpos + secs * 1.0e9
    newpos = 0.0 if newpos < 0
    return if newpos > duration
    @@playbin.seek_simple(Gst::Format::TIME, Gst::SeekFlags::FLUSH, newpos)
    message("New audio pos: #{(newpos / 1.0e9).round(1)}/#{(duration / 1.0e9).round(1)}")
    # $pb.query_position(Gst::Format::TIME)[1]/1.0e9
  end
end

.stopObject



13
14
15
# File 'lib/vimamsa/audio.rb', line 13

def self.stop
  @@playbin.stop if !@@playbin.nil?
end