版本

menu_open

Quick Start Sample Integration - MIDI

MIDI Integration Example

MIDI events are posted by calling the AK::SoundEngine::PostMIDIOnEvent() function. The following code shows examples of:

  • setting initial MIDI parameters,
  • posting MIDI events using the event's name (name given to the event by the Wwise user) or the event's ID as defined in "Wwise_IDs.h" (header file generated by Wwise).
#include "Wwise_IDs.h" // IDs generated by Wwise

// After Sound Engine initialization, register a global callback function.
// Be sure to register for callback just before the message queue is processed;
// this will ensure that any newly posted MIDI events are processed during
// the current frame (i.e. immediately after we exit our callback function).
AK::SoundEngine::RegisterGlobalCallback( &MIDICallback, AkGlobalCallbackLocation_PreProcessMessageQueueForRender )

// Obtain Sound Engine audio settings. These are required to determine the time
// equivalent of one audio frame.
AkAudioSettings audioSettings;
AK::SoundEngine::GetAudioSettings( audioSettings );

(...)

// Callback function used to post MIDI events.
void MIDICallback( bool in_bLastCall )
{
    // Post MIDI event(s) if required.
    // Take extra care to ensure this code is thread-safe.

    // For this example, assume we need to post the note-on and note-off
    // events for the same note.
    AkMIDIPost aPosts[2];

    const AkUInt8 byNote = 60;
    const AkUInt8 byChan = 0; // Range 0-15 mapped to MIDI channel range 1-16.

    const AkUInt32 uOnSamples = 0; // Note will start at beginning of current frame.
    const AkUInt32 uOffSamples = uOnOffset + audioSettings.uNumSamplesPerFrame / 2;
    // Note will end mid-frame.
    
    // Note-on
    AkMIDIPost& noteOn = aPosts[0];
    noteOn.byType = AK_MIDI_EVENT_TYPE_NOTE_ON;
    noteOn.byChan = byChan;
    noteOn.NoteOnOff.byNote = byNote;
    noteOn.NoteOnOff.byVelocity = 72; // random velocity
    noteOn.uOffset = uOnSamples;

    // Note-off
    AkMIDIPost& noteOff = aPosts[1];
    noteOff.byType = AK_MIDI_EVENT_TYPE_NOTE_OFF;
    noteOff.byChan = byChan;
    noteOff.NoteOnOff.byNote = byNote;
    noteOff.NoteOnOff.byVelocity = 0; // not used for note-off
    noteOff.uOffset = uOffSamples;

    AkUniqueID eventID = AK::SoundEngine::GetIDFromString( "MIDIEventName" );
    AK::SoundEngine::PostMIDIOnEvent( eventID, REGISTERED_MIDI_GAME_OBJECT, aPosts, 2 );
}

Refer to Integration Details - Events for more information regarding events. Refer to Integration Details - MIDI for more information regarding MIDI.


此页面对您是否有帮助?

需要技术支持?

仍有疑问?或者问题?需要更多信息?欢迎联系我们,我们可以提供帮助!

查看我们的“技术支持”页面

介绍一下自己的项目。我们会竭力为您提供帮助。

来注册自己的项目,我们帮您快速入门,不带任何附加条件!

开始 Wwise 之旅