require 'ffi' module WaveOutWrapper WAVE_FORMAT_PCM = 1 WAVE_MAPPER = -1 class WAVEFORMATEX < FFI::Struct def initialize(nSamplesPerSec, wBitsPerSample, nChannels, cbSize) self[:nSamplesPerSec] = nSamplesPerSec self[:wBitsPerSample] = wBitsPerSample self[:nChannels] = nChannels self[:cbSize] = cbSize self[:wFormatTag] = WAVE_FORMAT_PCM self[:nBlockAlign] = (self[:wBitsPerSample] >> 3) * self[:nChannels] self[:nAvgBytesPerSec] = self[:nBlockAlign] * self[:nSamplesPerSec] end layout :wFormatTag, :ushort, :nChannels, :ushort, :nSamplesPerSec, :ulong, :nAvgBytesPerSec, :ulong, :nBlockAlign, :ushort, :wBitsPerSample, :ushort, :cbSize, :ushort end class Sound extend FFI::Library private typedef :uint, :mmresult typedef :ulong, :dword ffi_lib :winmm attach_function :waveOutOpen, [:pointer, :uint, :pointer, :dword, :dword, :dword], :mmresult attach_function :waveOutClose, [:pointer], :mmresult public def self.play hWaveOut = FFI::MemoryPointer.new(:int) wfx = WAVEFORMATEX.new(44100, 16, 2, 0) waveOutOpenResult = waveOutOpen(hWaveOut, WAVE_MAPPER, wfx.pointer, 0, 0, 0) raise SystemCallError, FFI.errno, "waveOutOpen didn't work: #{waveOutOpenResult}" if waveOutOpenResult != 0 waveOutClose(hWaveOut) self end end end WaveOutWrapper::Sound.play # this won't actually play anything, but it doesn't return an error!
Saturday, March 8, 2014
waveOutOpen
I have successfully made a library call to waveOutOpen!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment