hallon-fifo 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+
19
+ *.sublime-*
@@ -3,29 +3,23 @@ require "mkfifo"
3
3
  module Hallon
4
4
  class Fifo
5
5
 
6
+ attr_accessor :format, :output
7
+
6
8
  def initialize
7
9
  @buffer = Array.new
8
10
  @playing, @stopped = false
9
- @buffer_size = 22050
11
+ @buffer_size = 22050 # overridden by format=
10
12
 
11
13
  @output ||= "hallon-fifo.pcm"
12
14
  File.delete(@output) if File.exists?(@output)
13
15
  File.mkfifo(@output) # Will error if it's overwriting another file
14
16
  end
15
17
 
16
- def format
17
- @format
18
- end
19
-
20
18
  def format=(new_format)
21
19
  @format = new_format
22
20
  @buffer_size = new_format[:rate] / 2
23
21
  end
24
22
 
25
- def output
26
- @output
27
- end
28
-
29
23
  def output=(new_output)
30
24
  old_output, @output = @output, new_output
31
25
 
@@ -36,6 +30,7 @@ module Hallon
36
30
 
37
31
  def drops
38
32
  # This SHOULD return the number of times the queue "stuttered"
33
+ # However, it ain't easy to do this with only knowledge of the fifo pipe.
39
34
  0
40
35
  end
41
36
 
@@ -55,25 +50,38 @@ module Hallon
55
50
  @buffer.clear
56
51
  end
57
52
 
53
+ def reset
54
+ self.output = @output
55
+ end
56
+
58
57
  def stream # runs indefinitely
59
58
  @stream_thread = Thread.new do
60
59
  queue = File.new(@output, "wb")
61
60
 
62
61
  loop do
63
62
 
63
+ start = Time.now.to_f
64
+ complete = start + 0.5
65
+
64
66
  # Get the next block from Spotify.
65
67
  audio_data = yield(@buffer_size)
66
68
 
67
69
  if audio_data.nil? # Audio format has changed, reset buffer.
68
- @buffer.clear # reset structure
69
- else # Write to our buffer and, if playing, to the FIFO queue.
70
+ @buffer.clear
71
+ else
70
72
  @buffer += audio_data
71
-
72
- queue.syswrite packed_samples(@buffer)
73
+ begin
74
+ queue.syswrite packed_samples(@buffer)
75
+ rescue Errno::EPIPE
76
+ self.reset
77
+ end
73
78
  @buffer.clear
74
79
  end
75
80
 
76
81
  ensure_playing
82
+
83
+ finish = Time.now.to_f
84
+ sleep complete - finish if finish < complete
77
85
  end
78
86
  end
79
87
  end
@@ -1,5 +1,5 @@
1
1
  module Hallon
2
2
  class Fifo
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hallon-fifo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-02 00:00:00.000000000 Z
12
+ date: 2013-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mkfifo
@@ -69,3 +69,4 @@ signing_key:
69
69
  specification_version: 3
70
70
  summary: Stream Spotify through Hallon to a FIFO queue.
71
71
  test_files: []
72
+ has_rdoc: