ruby-audio 1.5.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,66 @@
1
+ module RubyAudio
2
+ # Class <code>SoundInfo</code> provides information about open sound files'
3
+ # format, length, samplerate, channels, and other things.
4
+ #
5
+ # Example:
6
+ # snd = RubyAudio::Sound.open("snd.wav")
7
+ # puts snd.info.channels #=> 2
8
+ # puts snd.info.samplerate #=> 48000
9
+ # snd.close
10
+ #
11
+ # In addition it can be used to specify the format of new sound files:
12
+ #
13
+ # info = RubyAudio::SoundInfo.new :channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
14
+ # snd = RubyAudio::Sound.open("new.wav", 'w', info)
15
+ class SoundInfo < CSoundInfo
16
+ # Creates a new SoundInfo object and populates it using the given data
17
+ #
18
+ # Example:
19
+ # info = RubyAudio::SoundInfo.new :channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
20
+ def initialize options={}
21
+ # Populate from options if given
22
+ unless options.empty?
23
+ options.each {|key,value| send("#{key}=", value)}
24
+ end
25
+ end
26
+
27
+ # Returns a new <code>SoundInfo</code> object that is has the same channel
28
+ # count, sample rate, and format. This is useful in creating a new sound with
29
+ # the same format as an already existing sound.
30
+ #
31
+ # Example:
32
+ # snd1 = RubyAudio::Sound.open("snd.wav")
33
+ # snd2 = RubyAudio::Sound.open("snd2.wav", 'w', snd1.info.clone)
34
+ def clone
35
+ SoundInfo.new(:channels => channels, :samplerate => samplerate, :format => format)
36
+ end
37
+
38
+ alias_method :seekable?, :seekable
39
+
40
+ def main_format
41
+ calculate_format if @main_format.nil?
42
+ @main_format
43
+ end
44
+
45
+ def sub_format
46
+ calculate_format if @sub_format.nil?
47
+ @sub_format
48
+ end
49
+
50
+ private
51
+ def calculate_format
52
+ RubyAudio.constants.grep(/FORMAT_/).map(&:to_s).each do |f|
53
+ next if f.include?('MASK') # Skip mask constants
54
+
55
+ val = RubyAudio.const_get(f)
56
+ if val > RubyAudio::FORMAT_SUBMASK
57
+ # Main format
58
+ @main_format = f if format & RubyAudio::FORMAT_TYPEMASK == val
59
+ else
60
+ # Sub format
61
+ @sub_format = f if format & RubyAudio::FORMAT_SUBMASK == val
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'ruby-audio'
5
+ s.version = '1.5.0'
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ['Stephen Augenstein']
8
+ s.email = ['perl.programmer@gmail.com']
9
+ s.homepage = 'http://github.com/warhammerkid/ruby-audio'
10
+ s.summary = 'libsndfile wrapper for ruby'
11
+ s.description = 'ruby-audio wraps around libsndfile to provide simplified sound reading and writing support to ruby programs'
12
+
13
+ s.files = Dir['ruby-audio.gemspec', 'README.rdoc', 'LICENSE', 'Rakefile', 'lib/**/*.rb', 'spec/**/*.{rb,opts,wav,mp3}', 'ext/**/*.{c,h,rb}']
14
+ s.test_files = Dir['spec/**/*_spec.rb']
15
+ s.extensions = Dir["ext/**/extconf.rb"]
16
+
17
+ s.requirements << 'libsndfile (http://www.mega-nerd.com/libsndfile/)'
18
+
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = Dir['README.rdoc', 'ext/**/*.c']
21
+ s.rdoc_options = ['--line-numbers', '--main', 'README.rdoc']
22
+ end
@@ -0,0 +1,88 @@
1
+ require "spec_helper.rb"
2
+
3
+ describe RubyAudio::Buffer do
4
+ it "should initialize properly" do
5
+ buf = RubyAudio::Buffer.new('float', 100, 2)
6
+ buf.channels.should == 2
7
+ buf.size.should == 100
8
+ buf.real_size.should == 0
9
+ buf.type.should == :float
10
+ end
11
+
12
+ it "should support pretty typed constructors" do
13
+ lambda {
14
+ [:short, :int, :float, :double].each do |type|
15
+ buf = RubyAudio::Buffer.send(type, 100)
16
+ end
17
+ }.should_not raise_error
18
+ end
19
+
20
+ it "should allow [] access on single channel buffers" do
21
+ buf = RubyAudio::Buffer.int(100, 1)
22
+ buf[0] = 1.3
23
+ buf[0].should == 1
24
+ end
25
+
26
+ it "should allow [] access on multi-channel buffers" do
27
+ buf = RubyAudio::Buffer.double(100, 2)
28
+ buf[0] = [0.5, 0.3]
29
+ buf[0].should == [0.5, 0.3]
30
+ end
31
+
32
+ it "should raise exception if channel count of set frame does not match buffer's" do
33
+ lambda {
34
+ buf = RubyAudio::Buffer.double(100, 2)
35
+ buf[0] = [0.4, 0.8, 0.8]
36
+ }.should raise_error(RubyAudio::Error, "array length must match channel count")
37
+ end
38
+
39
+ it "should return nil on out-of-bounds [] access" do
40
+ buf = RubyAudio::Buffer.float(100)
41
+ buf[101].should == nil
42
+ buf[-1].should == nil
43
+ end
44
+
45
+ it "should truncate invalid real size" do
46
+ buf = RubyAudio::Buffer.float(100)
47
+ buf.real_size = 101
48
+ buf.real_size.should == 100
49
+ end
50
+
51
+ it "should support cloning/duping" do
52
+ buf = RubyAudio::Buffer.int(100)
53
+ buf[4] = 100
54
+
55
+ buf2 = buf.dup
56
+ buf2.size.should == buf.size
57
+ buf2[4].should == 100
58
+
59
+ buf[4] = 140
60
+ buf2[4].should == 100
61
+ end
62
+
63
+ context ".each" do
64
+ before(:each) do
65
+ @buf = RubyAudio::Buffer.int(10, 2)
66
+ 10.times do |i|
67
+ @buf[i] = [i, i]
68
+ end
69
+ end
70
+
71
+ it "should yield the elements in order" do
72
+ i = 0
73
+ @buf.each do |left, right|
74
+ left.should == i
75
+ right.should == i
76
+ i += 1
77
+ end
78
+ end
79
+
80
+ it "shouldn't do anything on an empty buffer" do
81
+ buf = RubyAudio::Buffer.int(0, 2)
82
+
83
+ buf.each do
84
+ fail "This shouldn't be executed"
85
+ end
86
+ end
87
+ end
88
+ end
Binary file
Binary file
Binary file
@@ -0,0 +1,49 @@
1
+ require "spec_helper.rb"
2
+
3
+ describe RubyAudio::SoundInfo do
4
+ it "should initialize with default properties" do
5
+ info = RubyAudio::SoundInfo.new
6
+ info.channels.should == 0
7
+ info.samplerate.should == 0
8
+ info.format.should == 0
9
+ end
10
+
11
+ it "should allow setting properties on initialize" do
12
+ info = RubyAudio::SoundInfo.new(:channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16)
13
+ info.channels.should == 1
14
+ info.samplerate.should == 48000
15
+ info.format.should == RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
16
+ end
17
+
18
+ it "should allow setting properties after initialize" do
19
+ info = RubyAudio::SoundInfo.new
20
+ info.channels = 3
21
+ info.channels.should == 3
22
+ end
23
+
24
+ it "should not be valid if properties invalid" do
25
+ info = RubyAudio::SoundInfo.new(:channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV)
26
+ info.valid?.should == false
27
+ info.format = RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
28
+ info.valid?.should == true
29
+ end
30
+
31
+ it "should allow cloning" do
32
+ info = RubyAudio::SoundInfo.new(:channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16)
33
+ info2 = info.clone
34
+ info.object_id.should_not == info2.object_id
35
+ info.channels.should == info2.channels
36
+ info.samplerate.should == info2.samplerate
37
+ info.format.should == info2.format
38
+ end
39
+
40
+ it "should calculate main and sub format from format" do
41
+ info = RubyAudio::SoundInfo.new(:format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16)
42
+ info.main_format.should == "FORMAT_WAV"
43
+ info.sub_format.should == "FORMAT_PCM_16"
44
+
45
+ info = RubyAudio::SoundInfo.new(:format => RubyAudio::FORMAT_WAVEX|RubyAudio::FORMAT_MS_ADPCM)
46
+ info.main_format.should == "FORMAT_WAVEX"
47
+ info.sub_format.should == "FORMAT_MS_ADPCM"
48
+ end
49
+ end
@@ -0,0 +1,182 @@
1
+ require "spec_helper.rb"
2
+
3
+ describe RubyAudio::Sound do
4
+ MONO_TEST_WAV = File.dirname(__FILE__)+'/data/what.wav'
5
+ STEREO_TEST_WAV = File.dirname(__FILE__)+'/data/what2.wav'
6
+ TEST_MP3 = File.dirname(__FILE__)+'/data/what.mp3'
7
+ OUT_WAV = File.dirname(__FILE__)+'/data/temp.wav'
8
+
9
+ after :each do
10
+ File.delete(OUT_WAV) if File.exists?(OUT_WAV)
11
+ end
12
+
13
+ it "should open a standard wav without issues" do
14
+ lambda {
15
+ RubyAudio::Sound.open(MONO_TEST_WAV)
16
+ }.should_not raise_error
17
+ end
18
+
19
+ it "should raise an exception if the mode is invalid" do
20
+ lambda {
21
+ RubyAudio::Sound.open(MONO_TEST_WAV, 'q')
22
+ }.should raise_error(ArgumentError, 'invalid access mode q')
23
+ end
24
+
25
+ it "should close the sound on block exit" do
26
+ s = nil
27
+ RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
28
+ snd.closed?.should be_false
29
+ s = snd
30
+ end
31
+ s.closed?.should be_true
32
+ end
33
+
34
+ it "should raise an exception for an unsupported file" do
35
+ lambda {
36
+ RubyAudio::Sound.open(TEST_MP3)
37
+ }.should raise_error(RubyAudio::Error, "File contains data in an unknown format.")
38
+ end
39
+
40
+ it "should raise an exception if file does not exist" do
41
+ lambda {
42
+ RubyAudio::Sound.open(TEST_MP3+'.not')
43
+ }.should raise_error(RubyAudio::Error, "System error : No such file or directory.")
44
+ end
45
+
46
+ it "should have the proper sound info" do
47
+ RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
48
+ snd.info.channels.should == 1
49
+ snd.info.samplerate.should == 16000
50
+ snd.info.format.should == RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
51
+ end
52
+ end
53
+
54
+ it "should allow seeking" do
55
+ lambda {
56
+ RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
57
+ snd.seek(100)
58
+ buf = snd.read(:float, 100)
59
+ buf[0].should > 0
60
+ end
61
+ }.should_not raise_error
62
+ end
63
+
64
+ it "should raise exceptions for invalid seeks" do
65
+ lambda {
66
+ RubyAudio::Sound.open(MONO_TEST_WAV) {|snd| snd.seek(-1)}
67
+ }.should raise_error(RubyAudio::Error, "invalid seek")
68
+ lambda {
69
+ RubyAudio::Sound.open(MONO_TEST_WAV) {|snd| snd.seek(1000000)}
70
+ }.should raise_error(RubyAudio::Error, "invalid seek")
71
+ end
72
+
73
+ it "should allow reading samples from the sound" do
74
+ RubyAudio::Sound.open(STEREO_TEST_WAV) do |snd|
75
+ buf = snd.read(:float, 1000)
76
+ buf.size.should == 1000
77
+ buf.real_size.should == 1000
78
+ buf[999].length.should == 2
79
+ end
80
+ end
81
+
82
+ it "should allow reading into an existing buffer" do
83
+ buf = RubyAudio::Buffer.float(1000)
84
+ buf.real_size.should == 0
85
+ RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
86
+ snd.read(buf)
87
+ end
88
+ buf.real_size.should == 1000
89
+ buf[99].should > 0
90
+ end
91
+
92
+ it "should allow reading into an existing buffer partially" do
93
+ buf = RubyAudio::Buffer.float(1000)
94
+ buf.real_size.should == 0
95
+ RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
96
+ snd.read(buf, 100)
97
+ end
98
+ buf.real_size.should == 100
99
+ buf[99].should > 0
100
+ buf[100].should == nil
101
+ end
102
+
103
+ it "should allow downmixing to mono on read" do
104
+ buf = RubyAudio::Buffer.int(100, 1)
105
+ buf2 = RubyAudio::Buffer.int(100, 2)
106
+ RubyAudio::Sound.open(STEREO_TEST_WAV, 'r') do |snd|
107
+ snd.read(buf)
108
+ snd.seek(0)
109
+ snd.read(buf2)
110
+
111
+ f = buf2[99]
112
+ buf[99].should == (f[0] + f[1]) / 2
113
+ end
114
+ end
115
+
116
+ it "should allow upmixing from mono on read" do
117
+ buf = RubyAudio::Buffer.int(100, 1)
118
+ buf2 = RubyAudio::Buffer.int(100, 2)
119
+ RubyAudio::Sound.open(STEREO_TEST_WAV, 'r') do |snd|
120
+ snd.read(buf2)
121
+ snd.seek(0)
122
+ snd.read(buf)
123
+
124
+ buf2[99].should == [buf[99], buf[99]]
125
+ end
126
+ end
127
+
128
+ it "should fail read on unsupported up/downmixing" do
129
+ buf = RubyAudio::Buffer.float(100, 5)
130
+ lambda {
131
+ RubyAudio::Sound.open(STEREO_TEST_WAV) do |snd|
132
+ snd.read(buf)
133
+ end
134
+ }.should raise_error(RubyAudio::Error, "unsupported mix from 5 to 2")
135
+ end
136
+
137
+ it "should allow writing to a new sound" do
138
+ in_buf = RubyAudio::Buffer.float(100)
139
+ out_buf = RubyAudio::Buffer.float(100)
140
+ out_info = nil
141
+ RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
142
+ snd.read(in_buf)
143
+ out_info = snd.info.clone
144
+ end
145
+
146
+ RubyAudio::Sound.open(OUT_WAV, 'rw', out_info) do |snd|
147
+ snd.write(in_buf)
148
+ snd.seek(0)
149
+ snd.read(out_buf)
150
+ end
151
+
152
+ out_buf[50].should == in_buf[50]
153
+ end
154
+
155
+ it "should allow writing to a new sound using <<" do
156
+ in_buf = RubyAudio::Buffer.float(100)
157
+ out_buf = RubyAudio::Buffer.float(100)
158
+ out_info = nil
159
+ RubyAudio::Sound.open(MONO_TEST_WAV) do |snd|
160
+ snd.read(in_buf)
161
+ out_info = snd.info.clone
162
+ end
163
+
164
+ RubyAudio::Sound.open(OUT_WAV, 'rw', out_info) do |snd|
165
+ snd << in_buf
166
+ snd.seek(0)
167
+ snd.read(out_buf)
168
+ end
169
+
170
+ out_buf[50].should == in_buf[50]
171
+ end
172
+
173
+ it "should fail write on channel mismatch" do
174
+ buf = RubyAudio::Buffer.float(100, 5)
175
+ info = RubyAudio::SoundInfo.new :channels => 2, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16
176
+ lambda {
177
+ RubyAudio::Sound.open(OUT_WAV, 'w', info) do |snd|
178
+ snd.write(buf)
179
+ end
180
+ }.should raise_error(RubyAudio::Error, "channel count mismatch: 5 vs 2")
181
+ end
182
+ end
@@ -0,0 +1,2 @@
1
+ --colour
2
+ -f nested
@@ -0,0 +1,11 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+ require 'spec/autorun'
9
+
10
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ require 'ruby-audio'
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-audio
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 5
9
+ - 0
10
+ version: 1.5.0
11
+ platform: x86-mingw32
12
+ authors:
13
+ - Stephen Augenstein
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-06 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: ruby-audio wraps around libsndfile to provide simplified sound reading and writing support to ruby programs
23
+ email:
24
+ - perl.programmer@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files:
30
+ - README.rdoc
31
+ - ext/rubyaudio_ext/ra_buffer.c
32
+ - ext/rubyaudio_ext/ra_sound.c
33
+ - ext/rubyaudio_ext/ra_soundinfo.c
34
+ - ext/rubyaudio_ext/rubyaudio_ext.c
35
+ files:
36
+ - ruby-audio.gemspec
37
+ - README.rdoc
38
+ - LICENSE
39
+ - Rakefile
40
+ - lib/ruby-audio/buffer.rb
41
+ - lib/ruby-audio/sound.rb
42
+ - lib/ruby-audio/sound_info.rb
43
+ - lib/ruby-audio.rb
44
+ - spec/buffer_spec.rb
45
+ - spec/sound_info_spec.rb
46
+ - spec/sound_spec.rb
47
+ - spec/spec_helper.rb
48
+ - spec/spec.opts
49
+ - spec/data/what.wav
50
+ - spec/data/what2.wav
51
+ - spec/data/what.mp3
52
+ - ext/rubyaudio_ext/ra_buffer.c
53
+ - ext/rubyaudio_ext/ra_sound.c
54
+ - ext/rubyaudio_ext/ra_soundinfo.c
55
+ - ext/rubyaudio_ext/rubyaudio_ext.c
56
+ - ext/rubyaudio_ext/ra_buffer.h
57
+ - ext/rubyaudio_ext/ra_sound.h
58
+ - ext/rubyaudio_ext/ra_soundinfo.h
59
+ - ext/rubyaudio_ext/extconf.rb
60
+ - lib/1.8/rubyaudio_ext.so
61
+ - lib/1.9/rubyaudio_ext.so
62
+ has_rdoc: true
63
+ homepage: http://github.com/warhammerkid/ruby-audio
64
+ licenses: []
65
+
66
+ post_install_message: You installed the binary version of this gem!
67
+ rdoc_options:
68
+ - --line-numbers
69
+ - --main
70
+ - README.rdoc
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ requirements:
92
+ - libsndfile (http://www.mega-nerd.com/libsndfile/)
93
+ rubyforge_project:
94
+ rubygems_version: 1.3.7
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: libsndfile wrapper for ruby
98
+ test_files:
99
+ - spec/buffer_spec.rb
100
+ - spec/sound_info_spec.rb
101
+ - spec/sound_spec.rb