【问题标题】:No sound. Uses Ruby and winmm through RubyDL没有声音。通过 RubyDL 使用 Ruby 和 winmm
【发布时间】:2015-09-17 04:08:36
【问题描述】:

预期行为:中间 C 在一个 midi 乐器上演奏,然后在另一个乐器上演奏。实际行为:DL 弃用警告并且没有声音。运行 Windows 7。

代码:

require "dl/import"

class LiveMIDI
    ON = 0x90
    OFF =0x80
    PC = 0xc0

    def initialize
        open
    end

    def note_on(channel, note, velocity=64)
        message(ON | channel, note, velocity)
    end

    def note_off(channel, note, velocity=64)
        message(OFF | channel, note, velocity)
    end

    def program_change(channel, preset) 
        message(PC | channel, preset)
    end

    module C
        extend DL::Importer
        dlload "winmm"

        extern "int midiOutOpen(HMIDIOUT*, int, int, int, int)"
        extern "int midiOutClose(int)"
        extern "int midiOutShortMsg(int, int)"
    end

    def open
        @device = DL.malloc(DL::Importer.sizeof("int"))
        C.midiOutOpen(@device, -1, 0, 0, 0)
    end

    def close
        C.midiOutClose(@device.ptr.to_i)
    end

    def message(one, two=0, three=0)
        message = one + (two << 8) + (three << 16)
        C.midiOutShortMsg(DL::CPtr.to_ptr(@device).to_i, message)
    end
end

midi = LiveMIDI.new
midi.note_on(0, 60, 100)
sleep(1)
midi.note_off(0, 60)
midi.program_change(1, 40)
midi.note_on(1, 60, 100)
sleep(1)
midi.note_off(1, 60)

摘自《实用 Ruby 项目》一书。根据第 2 章 11-15 页上的数字。代码稍作修改以处理 Ruby 1.9 中对 Ruby DL 的更改。

【问题讨论】:

  • 我认为应该是DL::CPtr.malloc(DL::Importer.sizeof("int")@device.ptr.to_i 而不是DL::CPtr.to_ptr(@device).to_i。还有一些类型是错误的,并且代码可能无法在 64 位 Windows 上运行。除非使用 Fiddle(DL 的继任者)的可能答案对您来说没问题,否则我无法对其进行测试。
  • 甜蜜!我进行了更改,并且成功了!如果您想进行这些更改并将其作为答案发布,我会接受。如果你给我看小提琴代码,我也会很感激。非常感谢!

标签: c ruby windows midi rubydl


【解决方案1】:

你必须写

DL::CPtr.malloc(DL::Importer.sizeof("int")

而不是

DL.malloc(DL::Importer.sizeof("int"))

创建一个指针对象 (DL::CPtr) 而不仅仅是获取整数地址。

还有

DL::CPtr.to_ptr(@device).to_i

必须是

@device.ptr.to_i

甚至可能

@device.ptr

这是使用 DL 替换 Fiddle 的代码的固定版本:

require 'fiddle/import'
require 'fiddle/types'

class LiveMIDI
  ON = 0x90
  OFF = 0x80
  PC = 0xc0

  def initialize
    open
  end

  def note_on(channel, note, velocity = 64)
    message(ON | channel, note, velocity)
  end

  def note_off(channel, note, velocity = 64)
    message(OFF | channel, note, velocity)
  end

  def program_change(channel, preset)
    message(PC | channel, preset)
  end

  module C
    extend Fiddle::Importer
    dlload 'winmm'
    # defines a few Windows-specific types such as DWORD or UINT
    include Fiddle::Win32Types
    # some other types not defined by the previous line
    typealias 'HMIDIOUT', 'void*'
    typealias 'LPHMIDIOUT', 'HMIDIOUT*'
    typealias 'DWORD_PTR', 'uintptr_t'
    typealias 'MMRESULT', 'UINT'

    extern 'MMRESULT midiOutOpen(LPHMIDIOUT, UINT, DWORD_PTR, DWORD_PTR, DWORD)'
    extern 'MMRESULT midiOutClose(HMIDIOUT)'
    extern 'MMRESULT midiOutShortMsg(HMIDIOUT, DWORD)'
  end

  def open
    @device = Fiddle::Pointer.malloc(Fiddle::SIZEOF_VOIDP)
    C.midiOutOpen(@device, -1, 0, 0, 0)
  end

  def close
    C.midiOutClose(@device.ptr)
  end

  def message(one, two = 0, three = 0)
    message = one + (two << 8) + (three << 16)
    C.midiOutShortMsg(@device.ptr, message)
  end
end

除了我根据the documentation on MSDN 添加或更正的类型之外,它或多或少是相同的。错误的类型可能会导致不明显的问题。

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 2011-12-02
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多