diff --git a/spec/soundfile_spec.cr b/spec/soundfile_spec.cr index 43a4164..1dfdc5e 100644 --- a/spec/soundfile_spec.cr +++ b/spec/soundfile_spec.cr @@ -1,5 +1,4 @@ require "./spec_helper" -require "tempfile" require "./../../soundfile/src/soundfile.cr" include SoundFile @@ -11,6 +10,13 @@ describe SFile do int = Array(Int32).new float = Array(Float32).new double = Array(Float64).new + + + it "Test File should open" do + File.open(test_wav, "r") do |f| + f.fd.should_not eq(-1) + end + end it "is a Class" do SFile.is_a?(Class).should eq(true) @@ -33,7 +39,7 @@ describe SFile do b.info.should eq(a.info) b.close end - + describe "#open" do it "returns false when opening a non-existing file for read" do a.open("invalid.wav", :read).should eq false @@ -71,13 +77,16 @@ describe SFile do end describe "#open_fd" do - File.open(test_wav, "r") do |f| - it "can open a file given a file descriptor" do + it "can open a file given a file descriptor" do + File.open(test_wav, "r") do |f| + f.fd.should_not eq(-1) a.open_fd(f.fd, :read).should be_true a.close end + end - it "can be invoked with a block" do + it "can be invoked with a block" do + File.open(test_wav, "r") do |f| a.open_fd(f.fd, :read) do |sf| sf.class.should eq SFile sf.info.class.should eq SFile.info.class @@ -87,15 +96,16 @@ describe SFile do end describe "#open_file" do - File.open(test_wav, "r") do |f| - it "can open a file given a file object" do + it "can open a file given a file object" do + File.open(test_wav, "r") do |f| a.open_file(f, :read).should be_true a.close end end - File.open(test_wav, "r") do |f| - it "can be invoked with a block" do + it "can be invoked with a block" do + File.open(test_wav, "r") do |f| + a.open_file(f, :read) do |sf| sf.class.should eq SFile sf.info.class.should eq SFile.info.class diff --git a/src/soundfile.cr b/src/soundfile.cr index d2eede5..ebbbe01 100644 --- a/src/soundfile.cr +++ b/src/soundfile.cr @@ -22,115 +22,115 @@ lib LibSndFile end enum Mode : CInt - READ = 0x10, - WRITE = 0x20, + READ = 0x10 + WRITE = 0x20 RDWR = 0x30 end enum StringType - TITLE = 1, - COPYRIGHT, - SOFTWARE, - ARTIST, - COMMENT, - DATE, - ALBUM, - LICENSE, - TRACKNUMBER, + TITLE = 1 + COPYRIGHT + SOFTWARE + ARTIST + COMMENT + DATE + ALBUM + LICENSE + TRACKNUMBER GENRE end enum Formats - WAV = 0x010000, - AIFF = 0x020000, - AU = 0x030000, - RAW = 0x040000, - PAF = 0x050000, - SVX = 0x060000, - NIST = 0x070000, - VOC = 0x080000, - IRCAM = 0x0A0000, - W64 = 0x0B0000, - MAT4 = 0x0C0000, - MAT5 = 0x0D0000, - PVF = 0x0E0000, - XI = 0x0F0000, - HTK = 0x100000, - SDS = 0x110000, - AVR = 0x120000, - WAVEX = 0x130000, - SD2 = 0x160000, - FLAC = 0x170000, - CAF = 0x180000, - WVE = 0x190000, - OGG = 0x200000, - MPC2K = 0x210000, + WAV = 0x010000 + AIFF = 0x020000 + AU = 0x030000 + RAW = 0x040000 + PAF = 0x050000 + SVX = 0x060000 + NIST = 0x070000 + VOC = 0x080000 + IRCAM = 0x0A0000 + W64 = 0x0B0000 + MAT4 = 0x0C0000 + MAT5 = 0x0D0000 + PVF = 0x0E0000 + XI = 0x0F0000 + HTK = 0x100000 + SDS = 0x110000 + AVR = 0x120000 + WAVEX = 0x130000 + SD2 = 0x160000 + FLAC = 0x170000 + CAF = 0x180000 + WVE = 0x190000 + OGG = 0x200000 + MPC2K = 0x210000 RF64 = 0x220000 end enum Subtypes - PCM_S8 = 0x0001, - PCM_16 = 0x0002, - PCM_24 = 0x0003, - PCM_32 = 0x0004, - PCM_U8 = 0x0005, - FLOAT = 0x0006, - DOUBLE = 0x0007, - ULAW = 0x0010, - ALAW = 0x0011, - IMA_ADPCM = 0x0012, - MS_ADPCM = 0x0013, - GSM610 = 0x0020, - VOX_ADPCM = 0x0021, - G721_32 = 0x0030, - G723_24 = 0x0031, - G723_40 = 0x0032, - DWVW_12 = 0x0040, - DWVW_16 = 0x0041, - DWVW_24 = 0x0042, - DWVW_N = 0x0043, - DPCM_8 = 0x0050, - DPCM_16 = 0x0051, + PCM_S8 = 0x0001 + PCM_16 = 0x0002 + PCM_24 = 0x0003 + PCM_32 = 0x0004 + PCM_U8 = 0x0005 + FLOAT = 0x0006 + DOUBLE = 0x0007 + ULAW = 0x0010 + ALAW = 0x0011 + IMA_ADPCM = 0x0012 + MS_ADPCM = 0x0013 + GSM610 = 0x0020 + VOX_ADPCM = 0x0021 + G721_32 = 0x0030 + G723_24 = 0x0031 + G723_40 = 0x0032 + DWVW_12 = 0x0040 + DWVW_16 = 0x0041 + DWVW_24 = 0x0042 + DWVW_N = 0x0043 + DPCM_8 = 0x0050 + DPCM_16 = 0x0051 VORBIS = 0x0060 end enum Endians - FILE = 0x00000000, - LITTLE = 0x10000000, - BIG = 0x20000000, + FILE = 0x00000000 + LITTLE = 0x10000000 + BIG = 0x20000000 CPU = 0x30000000 end enum Masks : CUInt - SF_FORMAT_SUBMASK = 0x0000FFFF, - SF_FORMAT_TYPEMASK = 0x0FFF0000, + SF_FORMAT_SUBMASK = 0x0000FFFF + SF_FORMAT_TYPEMASK = 0x0FFF0000 SF_FORMAT_ENDMASK = 0x30000000 end enum DefaultSubtypes - WAV = Subtypes::PCM_16, - AIFF = Subtypes::PCM_16, - AU = Subtypes::PCM_16, - PAF = Subtypes::PCM_16, - SVX = Subtypes::PCM_16, - NIST = Subtypes::PCM_16, - VOC = Subtypes::PCM_16, - IRCAM = Subtypes::PCM_16, - W64 = Subtypes::PCM_16, - MAT4 = Subtypes::DOUBLE, - MAT5 = Subtypes::DOUBLE, - PVF = Subtypes::PCM_16, - XI = Subtypes::DPCM_16, - HTK = Subtypes::PCM_16, - SDS = Subtypes::PCM_16, - AVR = Subtypes::PCM_16, - WAVEX = Subtypes::PCM_16, - SD2 = Subtypes::PCM_16, - FLAC = Subtypes::PCM_16, - CAF = Subtypes::PCM_16, - WVE = Subtypes::ALAW, - OGG = Subtypes::VORBIS, - MPC2K = Subtypes::PCM_16, + WAV = Subtypes::PCM_16 + AIFF = Subtypes::PCM_16 + AU = Subtypes::PCM_16 + PAF = Subtypes::PCM_16 + SVX = Subtypes::PCM_16 + NIST = Subtypes::PCM_16 + VOC = Subtypes::PCM_16 + IRCAM = Subtypes::PCM_16 + W64 = Subtypes::PCM_16 + MAT4 = Subtypes::DOUBLE + MAT5 = Subtypes::DOUBLE + PVF = Subtypes::PCM_16 + XI = Subtypes::DPCM_16 + HTK = Subtypes::PCM_16 + SDS = Subtypes::PCM_16 + AVR = Subtypes::PCM_16 + WAVEX = Subtypes::PCM_16 + SD2 = Subtypes::PCM_16 + FLAC = Subtypes::PCM_16 + CAF = Subtypes::PCM_16 + WVE = Subtypes::ALAW + OGG = Subtypes::VORBIS + MPC2K = Subtypes::PCM_16 RF64 = Subtypes::PCM_16 end @@ -195,7 +195,7 @@ lib LibSndFile end enum Ambisonic - SF_AMBISONIC_NONE = 0x40, + SF_AMBISONIC_NONE = 0x40 SF_AMBISONIC_B_FORMAT = 0x41 end @@ -455,103 +455,103 @@ module SoundFile def set_string(str, type) type = LibSndFile::StringType.parse(type).value - LibSndFile.set_string(@handle, type, str) + @handle.try { |handle| LibSndFile.set_string(handle, type, str) } end def get_string(type) type = LibSndFile::StringType.parse(type).value - str = LibSndFile.get_string(@handle, type) + str = @handle.try { |handle| LibSndFile.get_string(handle, type) } return "" unless str String.new(str) end def read_raw(ptr, size) - LibSndFile.read_raw(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_raw(handle, ptr, size) } end def read_short(ptr, size) - LibSndFile.read_short(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_short(handle, ptr, size) } end def read_int(ptr, size) - LibSndFile.read_int(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_int(handle, ptr, size) } end def read_float(ptr, size) - LibSndFile.read_float(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_float(handle, ptr, size) } end def read_double(ptr, size) - LibSndFile.read_double(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_double(handle, ptr, size) } end def readf_short(ptr, size) - LibSndFile.read_short(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_short(handle, ptr, size) } end def readf_int(ptr, size) - LibSndFile.read_int(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_int(handle, ptr, size) } end def readf_float(ptr, size) - LibSndFile.read_float(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_float(handle, ptr, size) } end def readf_double(ptr, size) - LibSndFile.read_double(@handle, ptr, size) + @handle.try { |handle| LibSndFile.read_double(handle, ptr, size) } end def write_short(ptr, size) - LibSndFile.write_short(@handle, ptr, size) + @handle.try { |handle| LibSndFile.write_short(handle, ptr, size) } end def write_raw(ptr, size) - LibSndFile.write_raw(@handle, ptr, size) + @handle.try { |handle| LibSndFile.write_raw(handle, ptr, size) } end def write_int(ptr, size) - LibSndFile.write_int(@handle, ptr, size) + @handle.try { |handle| LibSndFile.write_int(handle, ptr, size) } end def write_float(ptr, size) - LibSndFile.write_float(@handle, ptr, size) + @handle.try { |handle| LibSndFile.write_float(handle, ptr, size) } end def write_double(ptr, size) - LibSndFile.write_double(@handle, ptr, size) + @handle.try { |handle| LibSndFile.write_double(handle, ptr, size) } end def writef_short(ptr, size) - LibSndFile.writef_short(@handle, ptr, size) + @handle.try { |handle| LibSndFile.writef_short(handle, ptr, size) } end def writef_int(ptr, size) - LibSndFile.writef_int(@handle, ptr, size) + @handle.try { |handle| LibSndFile.writef_int(handle, ptr, size) } end def writef_float(ptr, size) - LibSndFile.writef_float(@handle, ptr, size) + @handle.try { |handle| LibSndFile.writef_float(handle, ptr, size) } end def writef_double(ptr, size) - LibSndFile.writef_double(@handle, ptr, size) + @handle.try { |handle| LibSndFile.writef_double(handle, ptr, size) } end def seek(frames, whence) whence = LibSndFile::Whence.parse(whence) - LibSndFile.seek(@handle, frames, whence) + @handle.try { |handle| LibSndFile.seek(handle, frames, whence) } end def close - LibSndFile.close(@handle) + @handle.try { |handle| LibSndFile.close(handle) } end def error_number - LibSndFile.error(@handle) + @handle.try { |handle| LibSndFile.error(handle) } end def error_to_s - String.new(LibSndFile.str_error(error_number)) + error_number.try { |number| String.new(LibSndFile.str_error(number)) } end def no_error? @@ -632,14 +632,14 @@ module SoundFile def sf_version ver = Bytes.new(25) cmd = LibSndFile::Command::SFC_GET_LIB_VERSION - LibSndFile.command(@handle, cmd, ver, ver.size) + @handle.try { |handle| LibSndFile.command(handle, cmd, ver, ver.size) } String.new(ver).strip end def get_log_info log = Slice.new(2048, UInt8.new(0x20)) cmd = LibSndFile::Command::SFC_GET_LOG_INFO - size = LibSndFile.command(@handle, cmd, log, log.size) + size = @handle.try { |handle| LibSndFile.command(handle, cmd, log, log.size) } return "" if size == 0 (String.new(log).strip)[0..-2] end @@ -648,7 +648,7 @@ module SoundFile max = Float64.new(0.0) max_ptr = pointerof(max) cmd = LibSndFile::Command::SFC_CALC_SIGNAL_MAX - return nil unless LibSndFile.command(@handle, cmd, max_ptr, sizeof(Float64)) == 0 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, max_ptr, sizeof(Float64)) } == 0 max end @@ -656,21 +656,21 @@ module SoundFile max = Float64.new(0.0) max_ptr = pointerof(max) cmd = LibSndFile::Command::SFC_CALC_NORM_SIGNAL_MAX - return nil unless LibSndFile.command(@handle, cmd, max_ptr, sizeof(Float64)) == 0 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, max_ptr, sizeof(Float64)) } == 0 max end def calc_max_all_channels max = Slice.new(channels, Float64.new(0.0)) cmd = LibSndFile::Command::SFC_CALC_MAX_ALL_CHANNELS - return nil unless LibSndFile.command(@handle, cmd, max, sizeof(Float64) * channels) == 0 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, max, sizeof(Float64) * channels) } == 0 max.to_a end def calc_norm_max_all_channels max = Slice.new(channels, Float64.new(0.0)) cmd = LibSndFile::Command::SFC_CALC_NORM_MAX_ALL_CHANNELS - return nil unless LibSndFile.command(@handle, cmd, max, sizeof(Float64) * channels) == 0 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, max, sizeof(Float64) * channels) } == 0 max.to_a end @@ -678,56 +678,56 @@ module SoundFile max = Float64.new(0.0) max_ptr = pointerof(max) cmd = LibSndFile::Command::SFC_GET_SIGNAL_MAX - return nil unless LibSndFile.command(@handle, cmd, max_ptr, sizeof(Float64)) == 1 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, max_ptr, sizeof(Float64)) } == 1 max end def get_max_all_channels max = Slice.new(channels, Float64.new(0.0)) cmd = LibSndFile::Command::SFC_GET_MAX_ALL_CHANNELS - return nil unless LibSndFile.command(@handle, cmd, max, sizeof(Float64) * channels) == 1 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, max, sizeof(Float64) * channels) } == 1 max.to_a end def set_norm_float(val) true_false = val ? sf_true : sf_false cmd = LibSndFile::Command::SFC_SET_NORM_FLOAT - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end def set_norm_double(val) true_false = val ? sf_true : sf_false cmd = LibSndFile::Command::SFC_SET_NORM_DOUBLE - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end def get_norm_float cmd = LibSndFile::Command::SFC_GET_NORM_FLOAT - LibSndFile.command(@handle, cmd, nil, 0) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, 0) } end def get_norm_double cmd = LibSndFile::Command::SFC_GET_NORM_DOUBLE - LibSndFile.command(@handle, cmd, nil, 0) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, 0) } end def set_scale_float_int_read(val) true_false = val ? sf_true : sf_false cmd = LibSndFile::Command::SFC_SET_SCALE_FLOAT_INT_READ - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end def set_scale_int_float_write(val) true_false = val ? sf_true : sf_false cmd = LibSndFile::Command::SFC_SET_SCALE_INT_FLOAT_WRITE - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end def get_simple_format_count count = Int32.new(0) count_ptr = pointerof(count) cmd = LibSndFile::Command::SFC_GET_SIMPLE_FORMAT_COUNT - return nil unless LibSndFile.command(@handle, cmd, count_ptr, sizeof(Int32)) == 0 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, count_ptr, sizeof(Int32)) } == 0 count end @@ -737,14 +737,14 @@ module SoundFile ptr = pointerof(format_info) cmd = LibSndFile::Command::SFC_GET_SIMPLE_FORMAT size = sizeof(LibSndFile::SFFormatInfo) - return nil unless LibSndFile.command(@handle, cmd, ptr, size) == 0 + return nil unless @handle.try { |handle| LibSndFile.command(handle, cmd, ptr, size) } == 0 format_info end def get_format_info(format_info : LibSndFile::SFFormatInfo) ptr = pointerof(format_info) cmd = LibSndFile::Command::SFC_GET_FORMAT_INFO - LibSndFile.command(@handle, cmd, ptr, sizeof(LibSndFile::SFFormatInfo)) + @handle.try { |handle| LibSndFile.command(handle, cmd, ptr, sizeof(LibSndFile::SFFormatInfo)) } format_info end @@ -752,14 +752,14 @@ module SoundFile count = Int32.new(0) count_ptr = pointerof(count) cmd = LibSndFile::Command::SFC_GET_FORMAT_MAJOR_COUNT - raise "format_major_count error" unless LibSndFile.command(@handle, cmd, count_ptr, sizeof(Int32)) == 0 + raise "format_major_count error" unless @handle.try { |handle| LibSndFile.command(handle, cmd, count_ptr, sizeof(Int32)) } == 0 count end def get_format_major(finfo : LibSndFile::SFFormatInfo) ptr = pointerof(finfo) cmd = LibSndFile::Command::SFC_GET_FORMAT_MAJOR - LibSndFile.command(@handle, cmd, ptr, sizeof(LibSndFile::SFFormatInfo)) + @handle.try { |handle| LibSndFile.command(handle, cmd, ptr, sizeof(LibSndFile::SFFormatInfo)) } finfo end @@ -767,62 +767,62 @@ module SoundFile count = Int32.new(0) count_ptr = pointerof(count) cmd = LibSndFile::Command::SFC_GET_FORMAT_SUBTYPE_COUNT - raise "format_subtype_count error" unless LibSndFile.command(@handle, cmd, count_ptr, sizeof(Int32)) == 0 + raise "format_subtype_count error" unless @handle.try { |handle| LibSndFile.command(handle, cmd, count_ptr, sizeof(Int32)) } == 0 count end def set_add_peak_chunk(val) true_false = val ? sf_true : sf_false cmd = LibSndFile::Command::SFC_SET_ADD_PEAK_CHUNK - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end def update_header_now cmd = LibSndFile::Command::SFC_UPDATE_HEADER_NOW - LibSndFile.command(@handle, cmd, nil, 0) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, 0) } end def set_update_header_auto(val) true_false = val ? sf_true : sf_false cmd = LibSndFile::Command::SFC_SET_UPDATE_HEADER_AUTO - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end def file_truncate(frames : Int32) sfcount = LibSndFile::SFCount.new(frames) ptr = pointerof(sfcount) cmd = LibSndFile::Command::SFC_FILE_TRUNCATE - LibSndFile.command(@handle, cmd, ptr, sizeof(LibSndFile::SFCount)) + @handle.try { |handle| LibSndFile.command(handle, cmd, ptr, sizeof(LibSndFile::SFCount)) } end def set_raw_start_offset(frames : Int32) frames = LibSndFile::SFCount.new(frames) ptr = pointerof(frames) cmd = LibSndFile::Command::SFC_SET_RAW_START_OFFSET - LibSndFile.command(@handle, cmd, ptr, sizeof(LibSndFile::SFCount)) + @handle.try { |handle| LibSndFile.command(handle, cmd, ptr, sizeof(LibSndFile::SFCount)) } end def set_clipping(val) true_false = val == true ? sf_true : sf_false cmd = LibSndFile::Command::SFC_SET_CLIPPING - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end def get_clipping cmd = LibSndFile::Command::SFC_GET_CLIPPING - LibSndFile.command(@handle, cmd, nil, 0) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, 0) } end def get_embed_file_info data = LibSndFile::SFEmbedFileInfo.new ptr = pointerof(data) cmd = LibSndFile::Command::SFC_GET_EMBED_FILE_INFO - LibSndFile.command(@handle, cmd, ptr, sizeof(LibSndFile::SFEmbedFileInfo)) + @handle.try { |handle| LibSndFile.command(handle, cmd, ptr, sizeof(LibSndFile::SFEmbedFileInfo)) } end def wavex_get_ambisonic cmd = LibSndFile::Command::SFC_WAVEX_GET_AMBISONIC - res = LibSndFile.command(@handle, cmd, nil, 0) + res = @handle.try { |handle| LibSndFile.command(handle, cmd, nil, 0) } return "SF_AMBISONIC_NONE" if res == LibSndFile::Ambisonic::SF_AMBISONIC_NONE.value return "SF_AMBISONIC_B_FORMAT" if res == LibSndFile::Ambisonic::SF_AMBISONIC_B_FORMAT.value res.to_s(16) @@ -832,7 +832,7 @@ module SoundFile val = LibSndFile::Ambisonic::SF_AMBISONIC_NONE val = LibSndFile::Ambisonic::SF_AMBISONIC_B_FORMAT if ambi == :b_format cmd = LibSndFile::Command::SFC_WAVEX_SET_AMBISONIC - res = LibSndFile.command(@handle, cmd, nil, val.value) + res = @handle.try { |handle| LibSndFile.command(handle, cmd, nil, val.value) } return "SF_AMBISONIC_NONE" if res == LibSndFile::Ambisonic::SF_AMBISONIC_NONE.value return "SF_AMBISONIC_B_FORMAT" if res == LibSndFile::Ambisonic::SF_AMBISONIC_B_FORMAT.value res @@ -844,7 +844,7 @@ module SoundFile end quality_ptr = pointerof(quality) cmd = LibSndFile::Command::SFC_SET_VBR_ENCODING_QUALITY - LibSndFile.command(@handle, cmd, quality_ptr, sizeof(Float64)) + @handle.try { |handle| LibSndFile.command(handle, cmd, quality_ptr, sizeof(Float64)) } end def set_compression_level(level : Float64) @@ -853,12 +853,12 @@ module SoundFile end level_ptr = pointerof(level) cmd = LibSndFile::Command::SFC_SET_COMPRESSION_LEVEL - LibSndFile.command(@handle, cmd, level_ptr, sizeof(Float64)) + @handle.try { |handle| LibSndFile.command(handle, cmd, level_ptr, sizeof(Float64)) } end def raw_data_needs_endswap cmd = LibSndFile::Command::SFC_RAW_DATA_NEEDS_ENDSWAP - LibSndFile.command(@handle, cmd, nil, 0) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, 0) } end def get_broadcast_info @@ -904,7 +904,7 @@ module SoundFile def get_cue_count count = UInt32.new(0) cmd = LibSndFile::Command::SFC_GET_CUE_COUNT - res = LibSndFile.command(@handle, cmd, pointerof(count), sizeof(UInt32)) + res = @handle.try { |handle| LibSndFile.command(handle, cmd, pointerof(count), sizeof(UInt32)) } raise "Error getting cue count" unless res == 0 count end @@ -923,7 +923,7 @@ module SoundFile def rf64_auto_downgrade(val) true_false = val ? sf_true : sf_false cmd = LibSndFile::Command::SFC_RF64_AUTO_DOWNGRADE - LibSndFile.command(@handle, cmd, nil, true_false) + @handle.try { |handle| LibSndFile.command(handle, cmd, nil, true_false) } end end