Skip to content

Commit

Permalink
Add mono read (short/float) calls to the ruby interface.
Browse files Browse the repository at this point in the history
Fixes #4.
  • Loading branch information
hainesr committed Sep 2, 2018
1 parent 3610997 commit 9fce474
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ffi/openmpt/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ def volume_ramping=(value)
)
end

def read_mono(frames, buffer)
openmpt_module_read_mono(@mod, @sample_rate, frames, buffer)
end

def read_float_mono(frames, buffer)
openmpt_module_read_float_mono(@mod, @sample_rate, frames, buffer)
end

def read_stereo(frames, left, right)
openmpt_module_read_stereo(@mod, @sample_rate, frames, left, right)
end
Expand Down
38 changes: 38 additions & 0 deletions test/openmpt_module_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,44 @@ def test_render_params_ramping
end
end

def test_read_mono
duration = 10
raw = ::File.read(RAW_LAST_SUN_MONO_INT16)

::FFI::OpenMPT::Module.open(MOD_LAST_SUN) do |mod|
frames = mod.sample_rate / duration
bytesize = frames * 2
buffer = ::FFI::MemoryPointer.new(:short, frames)

100.times do |i|
count = mod.read_mono(frames, buffer)
assert_equal count, frames

oracle = raw.byteslice((i * bytesize), bytesize).unpack('s*')
assert_equal buffer.read_array_of_int16(count), oracle
end
end
end

def test_read_float_mono
duration = 10
raw = ::File.read(RAW_LAST_SUN_MONO_FLOAT)

::FFI::OpenMPT::Module.open(MOD_LAST_SUN) do |mod|
frames = mod.sample_rate / duration
bytesize = frames * 4
buffer = ::FFI::MemoryPointer.new(:float, frames)

100.times do |i|
count = mod.read_float_mono(frames, buffer)
assert_equal count, frames

oracle = raw.byteslice((i * bytesize), bytesize).unpack('e*')
assert_equal buffer.read_array_of_float(count), oracle
end
end
end

def test_read_stereo
duration = 10
raw = ::File.read(RAW_LAST_SUN_INT16)
Expand Down

0 comments on commit 9fce474

Please sign in to comment.