win32-sound 0.4.3-universal-mswin32

Sign up to get free protection for your applications and to get access to all the features.
data/.gemtest ADDED
File without changes
data/CHANGES ADDED
@@ -0,0 +1,56 @@
1
+ == 0.4.3 - 21-May-2011
2
+ * Refactored Rakefile, removing an old gem install task and adding a
3
+ default task and a clean task.
4
+ * Altered the gemspec, including a platform change to 'universal-win32', and
5
+ removed a deprecated specification method.
6
+
7
+ == 0.4.2 - 6-Aug-2009
8
+ * Changed the license to Artistic 2.0.
9
+ * Updated to use the Windows::Sound module from the windows-pr library.
10
+ * Fixed the Sound::ALIAS value. It was wrongly set to SND_ALIAS_ID.
11
+ * Several gemspec updates, including the license and description.
12
+ * The Sound.get_wave_volume method is now a true alias.
13
+ * Renamed the test file to test_win32_sound.rb.
14
+ * Renamed the example file to example_win32_sound.rb to avoid any possible
15
+ confusion with actual test files.
16
+ * Added the 'example' rake task.
17
+
18
+ == 0.4.1 - 27-Jul-2007
19
+ * Changed SoundError to Sound::Error.
20
+ * Added a Rakefile with tasks for installation and testing.
21
+ * Removed the install.rb file. Installation is now handled via a rake task.
22
+ * Documentation updates.
23
+
24
+ == 0.4.0 - 26-Feb-2006
25
+ * Now pure Ruby only.
26
+ * Documentation updates and reorganization.
27
+ * Added a gemspec.
28
+ * Minor test suite changes.
29
+
30
+ == 0.3.0 - 29-May-2005
31
+ * Changed the Sound.volume= method (which didn't work) to
32
+ Sound.set_wave_volume because it takes up to two values and only affects
33
+ the wave volume, not the master volume.
34
+ * Renamed the Sound.volume method to Sound.wave_volume.
35
+ * Fixed a bug in the Sound.wave_volume method.
36
+ * Created the Sound.get_wave_volume alias for Sound.wave_volume.
37
+ * Fixed the Sound.set_wave_volume method.
38
+ * Test adjustments and additions to reflect API changes.
39
+ * Now Unicode friendly.
40
+ * Removed the sound.rd file. The sound.txt file is now rdoc friendly.
41
+
42
+ == 0.2.1 - 1-Mar-2005
43
+ * Moved the 'examples' directory to the toplevel directory
44
+ * Made the CHANGES and README files rdoc friendly
45
+ * Some cleanup in sound.h.
46
+
47
+ == 0.2.0 - 13-Jul-2004
48
+ * Moved the SoundError class under the Win32 module namespace
49
+ * Replaced the deprecated STR2CSTR() function with the StringValuePtr()
50
+ function. This means that as of version 0.2.0, this package requires
51
+ Ruby 1.8.0 or later.
52
+ * Moved the test.rb script to doc/examples
53
+ * Added the README file (oops).
54
+
55
+ == 0.1.0 - 14-Feb-2004
56
+ * Initial release
data/MANIFEST ADDED
@@ -0,0 +1,8 @@
1
+ * MANIFEST
2
+ * CHANGES
3
+ * README
4
+ * Rakefile
5
+ * win32-sound.gempsec
6
+ * examples/sound_test.rb
7
+ * lib/win32/sound.rb
8
+ * test/tc_sound.rb
data/README ADDED
@@ -0,0 +1,63 @@
1
+ == Description
2
+ A library for playing and controlling sounds on MS Windows.
3
+
4
+ == Prerequisites
5
+ * windows-pr 1.0.6 or later
6
+
7
+ == Installation instructions
8
+ === Gem installation
9
+ gem install win32-sound
10
+
11
+ === Local installation
12
+ rake test (optional)
13
+ rake install (non-gem) or rake install_gem (gem)
14
+
15
+ == Synopsis
16
+ require 'win32/sound'
17
+ include Win32
18
+
19
+ # Play a wav file
20
+ Sound.play("somefile.wav")
21
+
22
+ # Play a system sound
23
+ Sound.play("SystemAsterisk",Sound::ALIAS)
24
+
25
+ # Get the current volume of the waveform-audio output device.
26
+ p Sound.volume.join(", ") # left channel, right channel
27
+
28
+ == Acknowledgements
29
+ API ideas derived (or not) from Perl's Win32::Sound module and Python's
30
+ winsound package.
31
+
32
+ == Known Bugs
33
+ None that I'm aware of. Please report any bugs on the Win32 Utils home
34
+ page at http://rubyforge.org/projects/win32utils.
35
+
36
+ == Questions and Comments
37
+ Please post questions and/or comments on one of the forums on the project
38
+ page at http://rubyforge.org/projects/win32utils. Click the 'Forums' tab.
39
+
40
+ == Future Plans
41
+ Add ability to retrieve information about WAV files.
42
+ Add MIDI support?
43
+
44
+ == Developer's Notes
45
+ The MessageBeep() function, which the Python "winsound" module contains,
46
+ is intentionally omitted here. I felt it was redundant, because you can
47
+ achieve the same effect with something like this:
48
+ Sound.play("SystemAsterisk", Sound::ALIAS).
49
+
50
+ == License
51
+ Artistic 2.0
52
+
53
+ == Copyright
54
+ (C) 2004-2009, Daniel J. Berger, All Rights Reserved
55
+
56
+ == Warranty
57
+ This package is provided "as is" and without any express or
58
+ implied warranties, including, without limitation, the implied
59
+ warranties of merchantability and fitness for a particular purpose.
60
+
61
+ == Author(s)
62
+ Daniel Berger
63
+ Park Heesob
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/clean'
4
+
5
+ CLEAN.include("**/*.gem", "**/*.rbc")
6
+
7
+ namespace :gem do
8
+ desc "Create the win32-sound gem"
9
+ task :create => [:clean] do
10
+ spec = eval(IO.read("win32-sound.gemspec"))
11
+ Gem::Builder.new(spec).build
12
+ end
13
+
14
+ desc "Install the win32-sound library"
15
+ task :install => [:create] do
16
+ file = Dir["*.gem"].first
17
+ sh "gem install #{file}"
18
+ end
19
+ end
20
+
21
+ desc 'Run the example program'
22
+ task :example do
23
+ ruby '-Ilib examples\example_win32_sound.rb'
24
+ end
25
+
26
+ Rake::TestTask.new do |t|
27
+ t.warning = true
28
+ t.verbose = true
29
+ end
30
+
31
+ task :default => :test
@@ -0,0 +1,48 @@
1
+ #######################################################################
2
+ # example_win32_sound.rb (win32-sound)
3
+ #
4
+ # A example script to demonstrate the win32-sound library and for
5
+ # general futzing. You can run this via the 'rake example' task.
6
+ #
7
+ # Modify as you see fit.
8
+ #######################################################################
9
+ require 'win32/sound'
10
+ include Win32
11
+
12
+ wav = "c:\\windows\\media\\chimes.wav"
13
+
14
+ puts "VERSION: " + Sound::VERSION
15
+ #puts "Devices: " + Sound.devices.join(", ")
16
+
17
+ #Sound.volume = [77,128] # my personal settings
18
+
19
+ orig_left, orig_right = Sound.wave_volume
20
+ puts "Volume was: #{orig_left}, #{orig_right}"
21
+
22
+ #Sound.volume = 140
23
+ #puts "Volume is now: " + Sound.volume.join(", ")
24
+
25
+ #Sound.volume = [orig_left,orig_right]
26
+ #puts "Volume is now: " + Sound.volume.join(", ")
27
+
28
+ puts "Playing 'SystemAsterisk' sound"
29
+ sleep 1
30
+ Sound.play("SystemAsterisk",Sound::ALIAS)
31
+
32
+ puts "Playing 'chimes' sound once"
33
+ sleep 1
34
+ Sound.play(wav)
35
+
36
+ puts "Playing 'chimes' sound in a loop for 3 seconds"
37
+ sleep 1
38
+ Sound.play(wav,Sound::ASYNC|Sound::LOOP)
39
+ sleep 3
40
+ Sound.stop
41
+
42
+ puts "Playing default sound"
43
+ sleep 1
44
+ Sound.play("Foofoo", Sound::ALIAS)
45
+
46
+ puts "Playing a beep"
47
+ sleep 1
48
+ Sound.beep(500, 10000)
@@ -0,0 +1,211 @@
1
+ require 'windows/sound'
2
+ require 'windows/error'
3
+
4
+ # The Win32 module serves as a namespace only.
5
+ module Win32
6
+
7
+ # The Sound class encapsulates various methods for playing sound as well
8
+ # as querying or configuring sound related properties.
9
+ class Sound
10
+
11
+ # The Win32::Sound::Error class is typically raised if any of the
12
+ # Win32::Sound methods fail.
13
+ class Error < StandardError; end
14
+
15
+ include Windows::Sound
16
+ include Windows::Error
17
+ extend Windows::Sound
18
+ extend Windows::Error
19
+
20
+ # The version of the win32-sound library
21
+ VERSION = '0.4.3'
22
+
23
+ LOW_FREQUENCY = 37
24
+ HIGH_FREQUENCY = 32767
25
+ MAX_VOLUME = 0xFFFF
26
+
27
+ SYNC = SND_SYNC # play synchronously (default)
28
+ ASYNC = SND_ASYNC # play asynchronously
29
+ NODEFAULT = SND_NODEFAULT # silence (!default) if sound not found
30
+ MEMORY = SND_MEMORY # pszSound points to a memory file
31
+ LOOP = SND_LOOP # loop the sound until next sndPlaySound
32
+ NOSTOP = SND_NOSTOP # don't stop any currently playing sound
33
+ NOWAIT = SND_NOWAIT # don't wait if the driver is busy
34
+ ALIAS = SND_ALIAS # name is a registry alias
35
+ ALIAS_ID = SND_ALIAS_ID # alias is a predefined ID
36
+ FILENAME = SND_FILENAME # name is file name
37
+ RESOURCE = SND_RESOURCE # name is resource name or atom
38
+ PURGE = SND_PURGE # purge non-static events for task
39
+ APPLICATION = SND_APPLICATION # look for app specific association
40
+
41
+ # Returns an array of all the available sound devices; their names contain
42
+ # the type of the device and a zero-based ID number. Possible return values
43
+ # are WAVEOUT, WAVEIN, MIDIOUT, MIDIIN, AUX or MIXER.
44
+ #
45
+ def self.devices
46
+ devs = []
47
+
48
+ begin
49
+ 0.upto(waveOutGetNumDevs()){ |i| devs << "WAVEOUT#{i}" }
50
+ 0.upto(waveInGetNumDevs()){ |i| devs << "WAVEIN#{i}" }
51
+ 0.upto(midiOutGetNumDevs()){ |i| devs << "MIDIOUT#{i}" }
52
+ 0.upto(midiInGetNumDevs()){ |i| devs << "MIDIIN#{i}" }
53
+ 0.upto(auxGetNumDevs()){ |i| devs << "AUX#{i}" }
54
+ 0.upto(mixerGetNumDevs()){ |i| devs << "MIXER#{i}" }
55
+ rescue Exception
56
+ raise Error, get_last_error
57
+ end
58
+
59
+ devs
60
+ end
61
+
62
+ # Generates simple tones on the speaker. The function is synchronous; it
63
+ # does not return control to its caller until the sound finishes.
64
+ #
65
+ # The frequency (in Hertz) must be between 37 and 32767.
66
+ # The duration is in milliseconds.
67
+ #
68
+ def self.beep(frequency, duration)
69
+ if frequency > HIGH_FREQUENCY || frequency < LOW_FREQUENCY
70
+ raise Error, 'invalid frequency'
71
+ end
72
+
73
+ if 0 == Beep(frequency, duration)
74
+ raise Error, get_last_error
75
+ end
76
+
77
+ self
78
+ end
79
+
80
+ # Stops any currently playing waveform sound. If +purge+ is set to
81
+ # true, then *all* sounds are stopped. The default is false.
82
+ #
83
+ def self.stop(purge = false)
84
+ if purge && purge != 0
85
+ flags = PURGE
86
+ else
87
+ flags = 0
88
+ end
89
+
90
+ unless PlaySound(0, 0, flags)
91
+ raise Error, get_last_error
92
+ end
93
+
94
+ self
95
+ end
96
+
97
+ # Plays the specified sound. The sound can be a wave file or a system
98
+ # sound, when used in conjunction with the ALIAS flag.
99
+ #
100
+ # Valid flags:
101
+ #
102
+ # Sound::ALIAS
103
+ # The sound parameter is a system-event alias in the registry or the
104
+ # WIN.INI file. If the registry contains no such name, it plays the
105
+ # system default sound unless the NODEFAULT value is also specified.
106
+ # Do not use with FILENAME.
107
+ #
108
+ # Sound::APPLICATION
109
+ # The sound is played using an application-specific association.
110
+ #
111
+ # Sound::ASYNC
112
+ # The sound is played asynchronously and the function returns
113
+ # immediately after beginning the sound.
114
+ #
115
+ # Sound::FILENAME
116
+ # The sound parameter is the name of a WAV file. Do not use with
117
+ # ALIAS.
118
+ #
119
+ # Sound::LOOP
120
+ # The sound plays repeatedly until Sound.stop() is called. You must
121
+ # also specify the ASYNC flag to loop sounds.
122
+ #
123
+ # Sound::MEMORY
124
+ # The sound points to an image of a waveform sound in memory.
125
+ #
126
+ # Sound::NODEFAULT
127
+ # If the sound cannot be found, the function returns silently without
128
+ # playing the default sound.
129
+ #
130
+ # Sound::NOSTOP
131
+ # If a sound is currently playing, the function immediately returns
132
+ # false without playing the requested sound.
133
+ #
134
+ # Sound::NOWAIT
135
+ # If the driver is busy, return immediately without playing the sound.
136
+ #
137
+ # Sound::PURGE
138
+ # Stop playing all instances of the specified sound.
139
+ #
140
+ # Sound::SYNC
141
+ # The sound is played synchronously and the function does not return
142
+ # until the sound ends.
143
+ #
144
+ # Examples:
145
+ #
146
+ # require 'win32/sound'
147
+ # include Win32
148
+ #
149
+ # # Play a wave file once
150
+ # Sound.play('some_file.wav')
151
+ #
152
+ # # Play a wave file in an asynchronous loop for 2 seconds
153
+ # Sound.play('some_file.wav', Sound::ASYNC | Sound::LOOP)
154
+ # sleep 2
155
+ # Sound.stop
156
+ #
157
+ def self.play(sound, flags = 0)
158
+ unless PlaySound(sound, 0, flags)
159
+ raise Error, get_last_error
160
+ end
161
+
162
+ self
163
+ end
164
+
165
+ # Sets the volume for the left and right channel. If the +right_channel+
166
+ # is omitted, the volume is set for *both* channels.
167
+ #
168
+ # You may optionally pass a single Integer rather than an Array, in which
169
+ # case it is assumed you are setting both channels to the same value.
170
+ #
171
+ def self.set_wave_volume(left_channel, right_channel = nil)
172
+ right_channel ||= left_channel
173
+
174
+ lvolume = left_channel > MAX_VOLUME ? MAX_VOLUME : left_channel
175
+ rvolume = right_channel > MAX_VOLUME ? MAX_VOLUME : right_channel
176
+
177
+ volume = lvolume | rvolume << 16
178
+
179
+ if waveOutSetVolume(-1, volume) != 0
180
+ raise Error, get_last_error
181
+ end
182
+
183
+ self
184
+ end
185
+
186
+ # Returns a 2-element array that contains the volume for the left channel
187
+ # and right channel, respectively.
188
+ def self.wave_volume
189
+ volume = [0].pack('L')
190
+ if waveOutGetVolume(-1, volume) != 0
191
+ raise Error, get_last_error
192
+ end
193
+ volume = volume.unpack('L').first
194
+ [low_word(volume), high_word(volume)]
195
+ end
196
+
197
+ class << self
198
+ alias get_wave_volume wave_volume
199
+ end
200
+
201
+ private
202
+
203
+ def self.low_word(num)
204
+ num & 0xFFFF
205
+ end
206
+
207
+ def self.high_word(num)
208
+ num >> 16
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,91 @@
1
+ ##########################################################################
2
+ # test_win32_sound.rb
3
+ #
4
+ # Test suite for the win32-sound library. You should run this test case
5
+ # via the 'rake test' task.
6
+ ##########################################################################
7
+ require 'test/unit'
8
+ require 'win32/sound'
9
+ include Win32
10
+
11
+ class TC_Win32_Sound < Test::Unit::TestCase
12
+ def setup
13
+ @wav = "c:\\windows\\media\\chimes.wav"
14
+ end
15
+
16
+ def test_version
17
+ assert_equal('0.4.3', Sound::VERSION)
18
+ end
19
+
20
+ def test_beep
21
+ assert_respond_to(Sound, :beep)
22
+ assert_nothing_raised{ Sound.beep(55, 100) }
23
+ end
24
+
25
+ def test_beep_expected_errors
26
+ assert_raises(Sound::Error){ Sound.beep(0, 100) }
27
+ assert_raises(ArgumentError){ Sound.beep }
28
+ assert_raises(ArgumentError){ Sound.beep(500) }
29
+ assert_raises(ArgumentError){ Sound.beep(500, 500, 5) }
30
+ end
31
+
32
+ def test_devices
33
+ assert_respond_to(Sound, :devices)
34
+ assert_nothing_raised{ Sound.devices }
35
+ assert_kind_of(Array,Sound.devices)
36
+ end
37
+
38
+ def test_stop
39
+ assert_respond_to(Sound, :stop)
40
+ assert_nothing_raised{ Sound.stop }
41
+ assert_nothing_raised{ Sound.stop(true) }
42
+ end
43
+
44
+ def test_get_volume_basic
45
+ assert_respond_to(Sound, :wave_volume)
46
+ assert_respond_to(Sound, :get_wave_volume)
47
+ assert_nothing_raised{ Sound.get_wave_volume }
48
+ end
49
+
50
+ def test_get_volume
51
+ assert_kind_of(Array, Sound.get_wave_volume)
52
+ assert_equal(2, Sound.get_wave_volume.length)
53
+ end
54
+
55
+ def test_set_volume
56
+ assert_respond_to(Sound, :set_wave_volume)
57
+ assert_nothing_raised{ Sound.set_wave_volume(30000) } # About half
58
+ assert_nothing_raised{ Sound.set_wave_volume(30000, 30000) }
59
+ end
60
+
61
+ def test_play
62
+ assert_respond_to(Sound, :play)
63
+ assert_nothing_raised{ Sound.play(@wav) }
64
+ end
65
+
66
+ def test_play_alias
67
+ assert_nothing_raised{ Sound.play('SystemAsterisk', Sound::ALIAS) }
68
+ end
69
+
70
+ def test_expected_errors
71
+ assert_raises(Sound::Error){ Sound.beep(-1, 1) }
72
+ end
73
+
74
+ def test_constants
75
+ assert_not_nil(Sound::ALIAS)
76
+ assert_not_nil(Sound::APPLICATION)
77
+ assert_not_nil(Sound::ASYNC)
78
+ assert_not_nil(Sound::FILENAME)
79
+ assert_not_nil(Sound::LOOP)
80
+ assert_not_nil(Sound::MEMORY)
81
+ assert_not_nil(Sound::NODEFAULT)
82
+ assert_not_nil(Sound::NOSTOP)
83
+ assert_not_nil(Sound::NOWAIT)
84
+ assert_not_nil(Sound::PURGE)
85
+ assert_not_nil(Sound::SYNC)
86
+ end
87
+
88
+ def teardown
89
+ @wav = nil
90
+ end
91
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'win32-sound'
5
+ spec.version = '0.4.3'
6
+ spec.author = 'Daniel J. Berger'
7
+ spec.license = 'Artistic 2.0'
8
+ spec.email = 'djberg96@gmail.com'
9
+ spec.homepage = 'http://www.rubyforge.org/projects/win32utils'
10
+ spec.platform = Gem::Platform.new('universal-mswin32')
11
+ spec.summary = 'A library for playing with sound on MS Windows.'
12
+ spec.test_file = 'test/test_win32_sound.rb'
13
+ spec.files = Dir['**/*'] << ".gemtest"
14
+
15
+ spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
16
+ spec.rubyforge_project = 'win32utils'
17
+
18
+ spec.add_dependency('windows-pr', '>= 1.0.6')
19
+
20
+ spec.description = <<-EOF
21
+ The win32-sound library provides an interface for playing various
22
+ sounds on MS Windows operating systems, including system sounds and
23
+ wave files, as well as querying and configuring sound related properties.
24
+ EOF
25
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: win32-sound
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 3
10
+ version: 0.4.3
11
+ platform: universal-mswin32
12
+ authors:
13
+ - Daniel J. Berger
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-21 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: windows-pr
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 27
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 6
33
+ version: 1.0.6
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: " The win32-sound library provides an interface for playing various\n sounds on MS Windows operating systems, including system sounds and\n wave files, as well as querying and configuring sound related properties.\n"
37
+ email: djberg96@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - CHANGES
44
+ - README
45
+ - MANIFEST
46
+ files:
47
+ - CHANGES
48
+ - examples/example_win32_sound.rb
49
+ - lib/win32/sound.rb
50
+ - MANIFEST
51
+ - Rakefile
52
+ - README
53
+ - test/test_win32_sound.rb
54
+ - win32-sound.gemspec
55
+ - .gemtest
56
+ homepage: http://www.rubyforge.org/projects/win32utils
57
+ licenses:
58
+ - Artistic 2.0
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_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
+ requirements: []
83
+
84
+ rubyforge_project: win32utils
85
+ rubygems_version: 1.8.2
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: A library for playing with sound on MS Windows.
89
+ test_files:
90
+ - test/test_win32_sound.rb