hallon-fifo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hallon-fifo.gemspec
4
+ gemspec
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,38 @@
1
+ # Hallon::FIFO
2
+
3
+ An audio driver for Hallon, a ruby client for the official Spotify API. Streams audio into a raw PCM-formatted FIFO queue, ideal for input into other applications.
4
+
5
+ Among other things, FIFO queues can be piped into unix utilities, like SoX, or read as a stream (which is exactly why I wrote this).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'hallon-fifo'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hallon-fifo
20
+
21
+ ## Usage
22
+
23
+ Initialize a Hallon player with the FIFO driver. Pass a `&block` as the second argument to set the FIFO queue's path and customize the audio format, if you want:
24
+
25
+ ```ruby
26
+ # After loading Hallon and creating a session...
27
+
28
+ player = Hallon::Player.new Hallon::FIFO, Proc.new do
29
+ @driver.output = "hallon-fifo.pcm"
30
+ @driver.format = {:rate => 44100, :channels => 2, :type => :int16}
31
+ end
32
+ ```
33
+
34
+ Play the FIFO queue elsewhere, for example, with SoX:
35
+
36
+ ```bash
37
+ $ play -r 44100 -c 2 -t s16 hallon-fifo.pcm
38
+ ```
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-fifo/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "hallon-fifo"
8
+ gem.version = Hallon::Fifo::VERSION
9
+ gem.authors = ["Elliott Williams"]
10
+ gem.email = ["e@elliottwillia.ms"]
11
+ gem.description = "An audio driver for Hallon, a ruby client for the official " +
12
+ "Spotify API. Streams audio into a raw PCM-formatted FIFO queue, ideal for " +
13
+ "input into other applications."
14
+ gem.summary = "Stream Spotify through Hallon to a FIFO queue."
15
+ gem.homepage = "http://github.com/elliottwilliams/hallon-fifo"
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 Fifo
3
+
4
+ def initialize
5
+ @buffer = Array.new
6
+ @playing, @stopped = false
7
+ @buffer_size = 22050
8
+
9
+ @output ||= "hallon-fifo.pcm"
10
+ File.delete(@output) if File.exists?(@output)
11
+ File.mkfifo(@output) # Will error if it's overwriting another file
12
+ end
13
+
14
+ def format
15
+ @format
16
+ end
17
+
18
+ def format=(new_format)
19
+ @format = new_format
20
+ @buffer_size = new_format[:rate] / 2
21
+ end
22
+
23
+ def output
24
+ @output
25
+ end
26
+
27
+ def output=(new_output)
28
+ old_output, @output = @output, new_output
29
+
30
+ File.delete(old_output)
31
+ File.delete(new_output) if File.exists?(new_output)
32
+ File.mkfifo(new_output)
33
+ end
34
+
35
+ def drops
36
+ # This SHOULD return the number of times the queue "stuttered"
37
+ 0
38
+ end
39
+
40
+ def pause
41
+ @playing = false
42
+ end
43
+
44
+ def play
45
+ @playing = true
46
+ @stopped = false
47
+ end
48
+
49
+ def stop
50
+ @stopped = true
51
+ @stream_thread.exit if @stream_thread
52
+
53
+ @buffer.clear
54
+ end
55
+
56
+ def stream # runs indefinitely
57
+ @stream_thread = Thread.new do
58
+ queue = File.new(@output, "wb")
59
+
60
+ loop do
61
+
62
+ # Get the next block from Spotify.
63
+ audio_data = yield(@buffer_size)
64
+
65
+ if audio_data.nil? # Audio format has changed, reset buffer.
66
+ @buffer.clear # reset structure
67
+ else # Write to our buffer and, if playing, to the FIFO queue.
68
+ @buffer += audio_data
69
+
70
+ queue.syswrite packed_samples(@buffer)
71
+ @buffer.clear
72
+ end
73
+
74
+ ensure_playing
75
+ end
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def packed_samples(frames)
82
+ frames.flatten.map { |i| [i].pack("s_") }.join
83
+ end
84
+
85
+ def ensure_playing
86
+ play unless @playing
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,5 @@
1
+ module Hallon
2
+ module Fifo
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "hallon-fifo/version"
2
+ require "hallon-fifo/fifo"
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hallon-fifo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Elliott Williams
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: An audio driver for Hallon, a ruby client for the official Spotify API.
15
+ Streams audio into a raw PCM-formatted FIFO queue, ideal for input into other applications.
16
+ email:
17
+ - e@elliottwillia.ms
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - hallon-fifo.gemspec
28
+ - lib/hallon-fifo.rb
29
+ - lib/hallon-fifo/fifo.rb
30
+ - lib/hallon-fifo/version.rb
31
+ homepage: http://github.com/elliottwilliams/hallon-fifo
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.25
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Stream Spotify through Hallon to a FIFO queue.
55
+ test_files: []
56
+ has_rdoc: