Skip to content

Commit

Permalink
start fresh; model IO class and treat Audio Devices as such
Browse files Browse the repository at this point in the history
  • Loading branch information
domgetter committed Aug 8, 2014
1 parent 1bfdd83 commit 0826897
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 68 deletions.
Binary file added bin/sounds.wav
Binary file not shown.
26 changes: 26 additions & 0 deletions examples/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'sound'
include Sound
# Example of opening a device, writing to it, and closing it up
device = Device.open(Device::DEFAULT, "w", Format::PCM)
file = File.open("bin/sounds.wav", "r")
device.write(file)
device.close

# This will close the device on its own after the block finishes
Device.open(Device::DEFAULT, "w", Format::PCM) do |device|
device.write(File.open("bin/sounds.wav", "r"))
end

format = Format.new

Device.open(Device::DEFAULT, "w", format)

#file = File.open
#file.write
#file.close

data = ""

device = Device.open
device.write(data)
device.close
37 changes: 0 additions & 37 deletions lib/os/os.rb

This file was deleted.

12 changes: 2 additions & 10 deletions lib/sound.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
require_relative 'os/os'

if OS.windows?
require 'win32/sound'
else
warn("Sound output not yet implemented for this platform: #{OS.os}")
end

require_relative 'sound/sound'
require_relative 'sound/out'
require 'sound/device'
require 'sound/format'
40 changes: 40 additions & 0 deletions lib/sound/device.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

module Sound

WAVE_FORMAT_PCM = 1
WAVE_MAPPER = -1

class Device

def initialize(device_id)

end

DEFAULT = self.new(WAVE_MAPPER)

class << self
# Opens a sound device for reading or writing
# device_id is an id of one of several devices, usually defined by a constant
# direction is reading or writing or both
# format_id is MIDI vs PCM or others
# this method can take a block and if so closes the device after execution
def open(device = Device.new(WAVE_MAPPER), direction = "w", format = Format.new(WAVE_FORMAT_PCM), &block)
if block_given?
block.call(device)
device.close
else
device
end
end
end

def write(data)

end

def close

end
end

end
21 changes: 21 additions & 0 deletions lib/sound/format.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


module Sound

class Format
attr_accessor :channels, :sample_rate, :bps
def initialize(format_type = WAVE_FORMAT_PCM)
channels = 1
sample_rate = 44100
bps = 16
end
PCM = self.new
def block_align
(bps >> 3) * channels
end
def avg_bps
block_align * sample_rate
end
end

end
17 changes: 0 additions & 17 deletions lib/sound/out.rb

This file was deleted.

4 changes: 0 additions & 4 deletions lib/sound/sound.rb

This file was deleted.

0 comments on commit 0826897

Please sign in to comment.