audio-playback 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 315481daaba2617f0d73152c97a05f6aacd7ac82
4
- data.tar.gz: a6b72af924a20066a5ef290ee98fc7d3e4487c85
3
+ metadata.gz: eecccbe6c5faee30c7857d77f3d89e230b435f2a
4
+ data.tar.gz: 4621a617d40eaa1b923642b6caec93595e7fd40d
5
5
  SHA512:
6
- metadata.gz: 229fc708dee47f9a559780defc1819cf6d133bfb1c34018aaf2c02103bbbb59670a47e833ab258d06c4bc8c509626c278b0df796ef6cb29eadef9f46524b9302
7
- data.tar.gz: f2d343c8f9e7d88f3ba5415debdc32ab9f024d0dc949d757b2e8d108b535bff7322b2e3a30d8e20993a249b5ea7789d79249af8e2fd385332b49d30f19925552
6
+ metadata.gz: 582c662be9f91bb72c4958e0978b852beef42a0cdcf2c9a552452a3a870f8832165db47075bc0ccd3def87cefbcc901af26b52555b26e8b3da3bd5febddefcc3
7
+ data.tar.gz: 8f7c7cb70b298584fa091806139453a0e6a634263fbbbe7a463f69ec6e4450c5b3aa427b89782567c867d321e794d41f76a13f34f5bd300acd2d7dedc9075127
data/README.md CHANGED
@@ -87,7 +87,8 @@ More Ruby code examples:
87
87
  * [Select a file and play](https://github.com/arirusso/audio-playback/blob/master/examples/select_and_play.rb)
88
88
  * [Play multiple files in one stream simultaneously](https://github.com/arirusso/audio-playback/blob/master/examples/play_multiple_simultaneous.rb)
89
89
  * [Play multiple files in one stream sequentially](https://github.com/arirusso/audio-playback/blob/master/examples/play_multiple_sequential.rb)
90
- * [Play multiple files in multiple streams](https://github.com/arirusso/audio-playback/blob/master/examples/play_multiple.rb)
90
+ * [Play multiple files in multiple streams](https://github.com/arirusso/audio-playback/blob/master/examples/play_multiple_streams.rb)
91
+ * [Looping playback](https://github.com/arirusso/audio-playback/blob/master/examples/loop.rb)
91
92
 
92
93
  ## License
93
94
 
@@ -24,7 +24,7 @@ require "audio-playback/sound"
24
24
  # Play audio files
25
25
  module AudioPlayback
26
26
 
27
- VERSION = "0.0.4"
27
+ VERSION = "0.0.5"
28
28
 
29
29
  # Convenience method to play an audio file
30
30
  # @param [Array<::File>, Array<String>, ::File, String] file_paths
@@ -4,6 +4,9 @@ module AudioPlayback
4
4
 
5
5
  class Stream < FFI::PortAudio::Stream
6
6
 
7
+ attr_reader :is_playing
8
+ alias_method :playing?, :is_playing
9
+
7
10
  # Keep track of all streams
8
11
  # @return [Array<Stream>]
9
12
  def self.streams
@@ -14,6 +17,7 @@ module AudioPlayback
14
17
  # @param [Hash] options
15
18
  # @option options [IO] logger
16
19
  def initialize(output, options = {})
20
+ @is_playing = false
17
21
  @is_muted = false
18
22
  @gain = 1.0
19
23
  @input = nil
@@ -28,8 +32,13 @@ module AudioPlayback
28
32
  # @option options [IO] logger
29
33
  # @return [Stream]
30
34
  def play(playback, options = {})
35
+ @is_playing = true
31
36
  report(playback, options[:logger]) if options[:logger]
32
- open_playback(playback)
37
+ if @stream.nil?
38
+ open_playback(playback)
39
+ else
40
+ continue(playback)
41
+ end
33
42
  start
34
43
  self
35
44
  end
@@ -52,13 +61,25 @@ module AudioPlayback
52
61
  end
53
62
  rescue SystemExit, Interrupt
54
63
  # Control-C
55
- ensure
56
- true
64
+ @is_playing = false
65
+ exit
57
66
  end
67
+ @is_playing = false
68
+ true
58
69
  end
59
70
 
60
71
  private
61
72
 
73
+ def open_stream(playback)
74
+ @userdata = playback.data.to_pointer
75
+ FFI::PortAudio::API.Pa_OpenStream(@stream, @input, @output, @freq, @frames, @flags, @method, @userdata)
76
+ end
77
+
78
+ def continue(playback)
79
+ playback.reset
80
+ open_stream(playback)
81
+ end
82
+
62
83
  # Initialize the callback that's fired when the stream exits
63
84
  # @return [Stream]
64
85
  def initialize_exit_callback(options = {})
@@ -75,6 +96,7 @@ module AudioPlayback
75
96
  #close
76
97
  FFI::PortAudio::API.Pa_Terminate
77
98
  end
99
+ @is_playing = false
78
100
  true
79
101
  end
80
102
 
@@ -82,7 +104,12 @@ module AudioPlayback
82
104
  # @param [Playback] playback
83
105
  # @return [Boolean]
84
106
  def open_playback(playback)
85
- open(@input, @output, playback.sample_rate.to_i, playback.buffer_size, FFI::PortAudio::API::NoFlag, playback.data)
107
+ @freq = playback.sample_rate.to_i
108
+ @frames = playback.buffer_size
109
+ @flags = FFI::PortAudio::API::NoFlag
110
+ @stream = FFI::Buffer.new(:pointer)
111
+ @method = method(:process)
112
+ open_stream(playback)
86
113
  true
87
114
  end
88
115
 
@@ -22,7 +22,8 @@ module AudioPlayback
22
22
 
23
23
  attr_reader :buffer_size, :channels, :data, :output, :num_channels, :sounds, :stream
24
24
  def_delegators :@sounds, :audio_files
25
-
25
+ def_delegators :@data, :reset
26
+
26
27
  # @param [Array<Sound>, Sound] sounds
27
28
  # @param [Output] output
28
29
  # @param [Hash] options
@@ -135,7 +136,7 @@ module AudioPlayback
135
136
  # @return [Playback::Action]
136
137
  def populate(options = {})
137
138
  populate_channels(options)
138
- @data = StreamData.to_pointer(self)
139
+ @data = StreamData.new(self)
139
140
  self
140
141
  end
141
142
 
@@ -7,7 +7,7 @@ module AudioPlayback
7
7
 
8
8
  extend Forwardable
9
9
 
10
- def_delegators :@data, :flatten, :slice, :to_ary, :unshift
10
+ def_delegators :@data, :[], :[]=, :flatten, :slice, :to_ary, :unshift
11
11
 
12
12
  # @param [Playback::Action] playback
13
13
  def initialize(playback)
@@ -19,6 +19,15 @@ module AudioPlayback
19
19
  populate
20
20
  end
21
21
 
22
+ def reset
23
+ indexes = [
24
+ Playback::METADATA.index(:pointer),
25
+ Playback::METADATA.index(:is_eof)
26
+ ]
27
+ indexes.each { |index| @data[index] = 0.0 }
28
+ true
29
+ end
30
+
22
31
  # A C pointer version of the audio data
23
32
  # @return [FFI::Pointer]
24
33
  def to_pointer
@@ -35,6 +35,7 @@ class AudioPlayback::Device::StreamTest < Minitest::Test
35
35
  @stream.expects(:report).once
36
36
  @stream.expects(:open_playback).once
37
37
  @stream.expects(:start).once
38
+ @result = @stream.play(@playback)
38
39
  end
39
40
 
40
41
  teardown do
@@ -44,7 +45,11 @@ class AudioPlayback::Device::StreamTest < Minitest::Test
44
45
  end
45
46
 
46
47
  should "return self" do
47
- assert_equal @stream, @stream.play(@playback)
48
+ assert_equal @stream, @result
49
+ end
50
+
51
+ should "flag as playing" do
52
+ assert @stream.playing?
48
53
  end
49
54
 
50
55
  end
@@ -15,9 +15,9 @@ class AudioPlayback::Playback::StreamDataTest < Minitest::Test
15
15
  @data = @playback.data
16
16
  end
17
17
 
18
- should "return a pointer" do
18
+ should "return a stream data object" do
19
19
  refute_nil @data
20
- assert_kind_of FFI::Pointer, @data
20
+ assert_kind_of AudioPlayback::Playback::StreamData, @data
21
21
  end
22
22
 
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: audio-playback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Russo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-14 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest