2012-05-02 23 views
0

私はPractical Ruby Gemsという本で作業しています。 、私はそれを実行しようとすると、私は理解していない、次のエラーを取得未定義のメソッド、MIDI

require 'dl/import' 
class LiveMIDI 
ON = 0x90 
OFF = 0x80 
PC = 0xC0 
    def initialize 
    open 
    end 
    def noteon(channel, note, velocity=64) 
    message(ON | channel, note, velocity) 
    end 
    def noteoff(channel, note, velocity=64) 
    message(OFF | channel, note, velocity) 
    end 
    def programchange(channel, preset) 
    message(PC | channel, preset) 
    end 
    module C 
    extend DL::Importer 
    dlload '/System/Library/Frameworks/CoreMIDI.framework/Versions/Current/CoreMIDI' 
    extern "int MIDIClientCreate(void *, void *, void *, void *)" 
    extern "int MIDIClientDispose(void *)" 
    extern "int MIDIGetNumberOfDestinations()" 
    extern "void * MIDIGetDestination(int)" 
    extern "int MIDIOutputPortCreate(void *, void *, void *)" 
    extern "void * MIDIPacketListInit(void *)" 
    extern "void * MIDIPacketListAdd(void *, int, void *, int, int, int, void *)" 
    extern "int MIDISend(void *, void *, void *)" 
    end 
    module CF 
    extend DL::Importer 
    dlload '/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation' 
    extern "void * CFStringCreateWithCString (void *, char *, int)" 
    end 
    def open 
    client_name = CF.CFStringCreateWithCString(nil, "RubyMIDI", 0) 
    @client = DL::PtrData.new(nil) 
    C.mIDIClientCreate(client_name, nil, nil, @client.ref); 

    port_name = CF.cFStringCreateWithCString(nil, "Output", 0) 
    @outport = DL::PtrData.new(nil) 
    C.mIDIOutputPortCreate(@client, port_name, @outport.ref); 

    num = C.mIDIGetNumberOfDestinations() 
    raise NoMIDIDestinations if num < 1 
    @destination = C.mIDIGetDestination(0) 
    end 

    def close 
    C.mIDIClientDispose(@client) 
    end 

    def message(*args) 
    format = "C" * args.size 
    bytes = args.pack(format).to_ptr 
    packet_list = DL.malloc(256) 
    packet_ptr = C.mIDIPacketListInit(packet_list) 
    # Pass in two 32 bit 0s for the 64 bit time 
    packet_ptr = C.mIDIPacketListAdd(packet_list, 256, packet_ptr, 0, 0, args.size, bytes) 
    C.mIDISend(@outport, @destination, packet_list) 
    end 
end 

私は前に:

DLで働いたことがありません:それは私が書いた、私に以下のコードを与えました
livemidi.rb:36:in `open': undefined method `cFStringCreateWithCString' for LiveMIDI::CF:Module (NoMethodError) 
    from livemidi.rb:7:in `initialize' 
    from livemidi.rb:63:in `new' 
    from livemidi.rb:63:in `<main>' 

なぜですか? Mac OS XでRuby 1.9.3を使用しています このバグを修正するのに手伝ってもらえますか?

+0

明らかに、 'CFStringCreateWithCString'というメソッドはありません。 –

答えて

0

Apple Developer Documentationを検索すると、CFStringCreateWithCStringというメソッドが見つかります。 CFStringCreateWithCStringのメソッドシグネチャは、定義するものとは異なります。正しいメソッド定義は次のとおりです。

CFStringRef CFStringCreateWithCString (
    CFAllocatorRef alloc, 
    const char *cStr, 
    CFStringEncoding encoding 
); 

つまり、変更する必要があります。

extern "void * CFStringCreateWithCString (void *, char *, int)" 

へ。

extern "CFStringRef CFStringCreateWithCString(CFAllocatorRef, const char*, CFStringEncoding)" 
0

あなたの悩みは、あなたがcFStringCreateWithCStringを呼んでいるということに見えますが、機能はCFStringCreateWithCStringと呼ばれる - 総額が重要です。

+0

36行目は 'CFStringCreateWithCString'を呼び出します。 –

+0

@dunsmoreb:エラーは明らかに小文字のバージョンを示しているので、エラーの原因となったコードとは異なるバージョンのコードを貼り付ける必要があります。実際には大文字のバージョンが存在します。コードを実行してこれを確認しました。 – Chuck

関連する問題