快速使用MIDIPacketList [英] Using MIDIPacketList in swift

查看:189
本文介绍了快速使用MIDIPacketList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看一些使用核心midi的midi输出的例子.

I am looking at some of the examples of midi output using core midi.

具体地这个问题

this

我在objC中有基于这些代码的代码,现在我想尝试将其转换为swift.

I have code that works based on these in objC and I now want to try to translate that to swift.

我最不明白的那一行是:MIDIPacketList* pktList = (MIDIPacketList*) pktBuffer;

the line I least understand is this one: MIDIPacketList* pktList = (MIDIPacketList*) pktBuffer;

我将其读取为声明了MIDIPacketList类型的指针pktlist并为其分配了pktBuffer的值,并将其转换为MIDIPacketList类型

I read this as declaring a pointer pktlist of type MIDIPacketList and assigning it the value of pktBuffer, cast to the type MIDIPacketList

我是C和objC的新手,这对我来说毫无意义.

I'm new to C and objC and this makes no sense to me.

MIDIPacketList是一个定义的结构,此处:

MIDIPacketList is a struct defined here:

以什么方式将字节数组强制转换为结构类型MIDIPacketList,这是什么目的?我猜想它正在尝试定义数据包列表的大小,但是为什么我们需要在这里进行操作呢?无论如何,这是怎么做的呢?为什么用MIDIPacketListAdd做到这一点还不够,而稍后会发生几行呢?

in what way does it make sense to cast a bytearray to the struct type MIDIPacketList and what is this trying to achieve? I guess it is trying to define the size of the packet list, but why do we need to do that here, and how does this do it anyway? why is it not sufficient to do it with MIDIPacketListAdd as happens a few lines later?

这是我快速尝试的Midi输出类的尝试-谁能看到出什么问题了吗?该代码在运行之前不会在Xcode中给出任何错误.我在objC中有此版本的工作版本,但是我无法快速获取定义的数据包列表的大小. (至少我认为是问题所在)

here is my attempt at a midi output class in swift - can anyone see what is going wrong? this code gives no errors in Xcode until it is run. I have a working version of this in objC, but i can't get the size of the packet list defined in swift. (at least I think that's the problem)

import Foundation
import CoreMIDI

class MidiOutClass {

var midiClient = MIDIClientRef()
var midiSource = MIDIEndpointRef()


func openOutput() {
    MIDIClientCreate("MIDI client", nil, nil, &midiClient)
    MIDISourceCreate(midiClient, "MIDI Source",&midiSource)
    println("midi out opened")//should only do this if successful
}

func noteOn(channel: Int, note: Int, velocity:Int) {
    midisend((0x90+channel), note: note, value: velocity)
}

func polyAfter(channel: Int, note: Int, value:Int) {
    midisend((0xA0+channel), note: note, value: value)
}

func noteOff(channel: Int, note: Int) {
    midisend((0x90+channel), note: note, value: 0 )
}

func midisend(status:Int, note: Int, value:Int) {

    var packet: UnsafeMutablePointer<MIDIPacket> = nil
    //var buffer = [Byte](count:1024, repeatedValue: 0)
//this is the array I'm trying to use in a similar way to the obj C.

    var packetList: UnsafeMutablePointer<MIDIPacketList> = nil
    let midiDataToSend:[Byte] = [Byte(status), Byte(note), Byte(value)];
    packet = MIDIPacketListInit(packetList);
    packet = MIDIPacketListAdd(packetList, 1024, packet, 0, 3, midiDataToSend);

    if (packet == nil ) {
        println("failed to send the midi.")
    } else {
        MIDIReceived(midiSource, packetList)
        println("sent some stuff")
    }
}

}//end MidiOutClass

推荐答案

我找到了答案 objC版本中的字节数组是一个讨厌的hack,用于向packetList分配一些内存

the byte array in the objC version is a nasty hack to allocate some memory to the packetList

这是我修改后的代码,现在可以使用.

here's my revised code, which now works.

    func midisend(status:Int, note: Int, value:Int) {

    var packet = UnsafeMutablePointer<MIDIPacket>.alloc(1)
    var packetList = UnsafeMutablePointer<MIDIPacketList>.alloc(1)
    let midiDataToSend:[Byte] = [Byte(status), Byte(note), Byte(value)];
    packet = MIDIPacketListInit(packetList);
    packet = MIDIPacketListAdd(packetList, 1024, packet, 0, 3, midiDataToSend);

    if (packet == nil ) {
        println("failed to send the midi.")
    } else {
        MIDIReceived(midiSource, packetList)
        println("sent some stuff")
    }
    packet.destroy()
    //packet.dealloc(1)
    packetList.destroy()
    packetList.dealloc(1)
}

但是,注释掉的dealloc似乎是不必要的,我不确定为什么. MIDIReceived会处理吗?

However, the commented out dealloc seems to be unnecessary, I'm not exactly sure why yet. Does MIDIReceived take care of it?

如果有人有更好的解决方案-也许根本不用指针,请发布它!

If anyone has a better solution - maybe without using pointers at all, please post it!

这篇关于快速使用MIDIPacketList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆