hallon-queue-output 0.0.2
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.
- data/.gitignore +21 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/hallon-queue-output.gemspec +21 -0
- data/lib/hallon-queue-output.rb +2 -0
- data/lib/hallon-queue-output/queue-output.rb +90 -0
- data/lib/hallon-queue-output/version.rb +5 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Elliott Williams
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Hallon::QueueOutput
|
2
|
+
|
3
|
+
An audio driver for Hallon, a ruby client for libspotify. Streams audio into a ruby [Queue][1], allowing your code to work directly with audio samples out of Hallon. One sample is an arrays of signed 16-bit integers, one for each channel.
|
4
|
+
|
5
|
+
[1]: http://www.ruby-doc.org/stdlib-2.0/libdoc/thread/rdoc/Queue.html
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'hallon-queue-output'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hallon-queue-output
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Initialize a Hallon player and pass QueueOutput as the driver. Pass a `&block` as the second argument to set queue the driver should push into. (the block runs after Hallon creates the driver, where you would normally establish callbacks):
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# After loading Hallon and creating a session...
|
27
|
+
|
28
|
+
require 'thread'
|
29
|
+
require 'hallon-queue-output'
|
30
|
+
|
31
|
+
my_queue = Queue.new
|
32
|
+
|
33
|
+
player = Hallon::Player.new Hallon::QueueOutput, Proc.new do
|
34
|
+
@driver.queue = my_queue
|
35
|
+
end
|
36
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hallon-queue-output/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "hallon-queue-output"
|
8
|
+
gem.version = Hallon::QueueOutput::VERSION
|
9
|
+
gem.authors = ["Elliott Williams"]
|
10
|
+
gem.email = ["e@elliottwillia.ms"]
|
11
|
+
gem.description = <<END
|
12
|
+
Stream Spotify via Hallon to ruby Queue for playback or processing in another thread.
|
13
|
+
END
|
14
|
+
gem.summary = "Stream Spotify via Hallon to ruby Queue for playback or processing in another thread."
|
15
|
+
gem.homepage = "http://github.com/elliottwilliams/hallon-queue-output"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module Hallon
|
2
|
+
class QueueOutput
|
3
|
+
|
4
|
+
attr_accessor :queue, :format, :verbose
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@should_stream = true
|
8
|
+
@stutter = 0
|
9
|
+
@verbose = false
|
10
|
+
end
|
11
|
+
|
12
|
+
def format=(format)
|
13
|
+
@format = format
|
14
|
+
@buffer_size = (format[:rate] / 2) + 100
|
15
|
+
end
|
16
|
+
|
17
|
+
def play
|
18
|
+
ensure_streaming
|
19
|
+
print "\e[1;32m(play)\e[0m" if @verbose
|
20
|
+
end
|
21
|
+
|
22
|
+
def pause
|
23
|
+
@playing = false
|
24
|
+
stop_streaming
|
25
|
+
print "\e[1;33m(pause)\e[0m" if @verbose
|
26
|
+
end
|
27
|
+
|
28
|
+
def stop
|
29
|
+
stop_streaming
|
30
|
+
print "\e[1;31m(stop)\e[0m" if @verbose
|
31
|
+
end
|
32
|
+
|
33
|
+
def drops # Return number of queue stutters since the last call
|
34
|
+
current_stutter, @stutter = @stutter, 0
|
35
|
+
print "(reported #{current_stutter} stutters) " if current_stutter > 0 && @verbose
|
36
|
+
current_stutter
|
37
|
+
end
|
38
|
+
|
39
|
+
def stream # runs indefinitely
|
40
|
+
@stream_thread = Thread.new do
|
41
|
+
unless @queue
|
42
|
+
raise "Need a Queue to output to before streaming!"
|
43
|
+
end
|
44
|
+
play # always play initially
|
45
|
+
|
46
|
+
loop do
|
47
|
+
completion = Time.now.to_f + 0.5
|
48
|
+
|
49
|
+
unless @should_stream
|
50
|
+
Thread.stop
|
51
|
+
next
|
52
|
+
end
|
53
|
+
|
54
|
+
# Get the next block from Spotify.
|
55
|
+
audio_data = yield(@buffer_size)
|
56
|
+
|
57
|
+
@queue << audio_data unless audio_data.nil?
|
58
|
+
|
59
|
+
# sleep until it's time for the next frame
|
60
|
+
actual = Time.now.to_f
|
61
|
+
if actual > completion
|
62
|
+
process_stutter(completion, actual)
|
63
|
+
else
|
64
|
+
sleep completion - actual
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def ensure_streaming
|
73
|
+
@should_stream = true
|
74
|
+
@stream_thread.wakeup if @stream_thread.stop?
|
75
|
+
end
|
76
|
+
|
77
|
+
def stop_streaming
|
78
|
+
@should_stream = false
|
79
|
+
end
|
80
|
+
|
81
|
+
def process_stutter(projected_end, actual_end)
|
82
|
+
sec_missed = actual_end - projected_end
|
83
|
+
samples_missed = (sec_missed * @format[:rate]).to_i
|
84
|
+
print "(#{samples_missed} stutter) " if @verbose
|
85
|
+
@stutter += samples_missed
|
86
|
+
pause if @stutter > (@format[:rate] / 4)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hallon-queue-output
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Elliott Williams
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-27 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'Stream Spotify via Hallon to ruby Queue for playback or processing
|
15
|
+
in another thread.
|
16
|
+
|
17
|
+
'
|
18
|
+
email:
|
19
|
+
- e@elliottwillia.ms
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- .gitignore
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- hallon-queue-output.gemspec
|
30
|
+
- lib/hallon-queue-output.rb
|
31
|
+
- lib/hallon-queue-output/queue-output.rb
|
32
|
+
- lib/hallon-queue-output/version.rb
|
33
|
+
homepage: http://github.com/elliottwilliams/hallon-queue-output
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.25
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Stream Spotify via Hallon to ruby Queue for playback or processing in another
|
57
|
+
thread.
|
58
|
+
test_files: []
|
59
|
+
has_rdoc:
|