Overview of ACP
This section describes the programming interface to the ACP. Theacphal library (acphal.lib) defines the API set for Scalable Hardware Audio Processing Engine (SHAPE). Also included in the Microsoft Game Development Kit (GDK) is a collection of audio utilities and source code to help you prepare audio data for use with SHAPE. The utility headers define the following:
- The context structures for each supported data format
- A full set of functions that can be used to read and write to these contexts
- Flowgraphs
- DMA utilities
- EQ compressor utilities
- Filter volume utilities
- PCM utilities
- SRC utilities
- XMA utilities
- Target values
- Thread safety
- Guidelines for using SRC for PCM and XMA data
- Pausing and resuming a title
Flowgraphs
To use SHAPE, a title would usually create a SHAPE flowgraph. For a description of an alternative, see the XMA utilities section. A SHAPE flowgraph is an array of commands (one per SHAPE block) and accompanying context data that describes the order of operations of the individual blocks and the data on which they operate. The ACP uses the data in the flowgraph to correctly schedule the operations within the SHAPE blocks. The title is responsible for building the flowgraph and submitting it to the ACP. To build a flowgraph, use ShapeFlowGraph (flowgraph utility methods). In the following figures, the green blocks represent SHAPE components, the cyan blocks are the source material, and the labeled yellow circles are the hardware mix buffers.- 3D sounds
- Front end to a software audio engine
- Rendering audio
- Flowgraph parsing
- Updating flowgraphs
- Multiple flowgraphs
3D sounds
Figure 1. Two voices, each panned between two outputs, with a send to a common output. In code, this flowgraph can be represented as follows.Front end to a software audio engine
Figure 2. A basic front end to a software engine. Potentially, all voices that use this model would use the same structure. In code, this flowgraph can be represented as follows.Rendering audio
To render audio- Create a flowgraph like the ones shown in the previous examples and that defines the audio graph to be processed.
- Create context structures that define how and what the flowgraph will process.
-
Use the SubmitCommand method to submit the flowgraph to the ACP. Based on the parameters to
SubmitCommand, the flowgraph is processed once and then discarded or processed every audio frame. -
The title is responsible for updating the context data for each audio frame by using the
ACP_COMMAND_UPDATE_*_CONTEXTcommands or by correctly synchronizing the updates with the flowgraph processing and manually updating the contexts.- ACP_COMMAND_UPDATE_DMA_CONTEXT
- ACP_COMMAND_UPDATE_EQCOMP_CONTEXT
- ACP_COMMAND_UPDATE_FILTVOL_CONTEXT
- ACP_COMMAND_UPDATE_PCM_CONTEXT
- ACP_COMMAND_UPDATE_SRC_CONTEXT
- ACP_COMMAND_UPDATE_XMA_CONTEXT
ACP_COMMAND_TYPE_UPDATE_*_CONTEXT commands to update a small number of contexts per audio frame. Using these commands to update a large number of contexts isn’t efficient because a large amount of context data must be copied and transmitted to the ACP in addition to processing of the commands. If you want to update a large number of contexts, a title should modify the context data and then submit non-persistent flowgraphs to the ACP or make good use of the ACP_COMMAND_TYPE_START_FLOWGRAPH command and the waitForStart parameter to the ACP_COMMAND_LOAD_SHAPE_FLOWGRAPH command to hold on processing until the context is updated. When the flowgraph completes processing, the contexts are free to be updated again.
A third alternative to updating contexts is to use a double-buffering process. The title can update the second copy of the contexts while the flowgraph is being processed and swap the contexts at the start of an audio frame.
Flowgraph parsing
The ACP terminates persistent flowgraph parsing at the end of the audio frame, which is an ACP audio frame with a 2.667-ms limit. If a title has a flowgraph that takes 75 percent of an audio frame to process but the title doesn’t let the processing start until 30 percent of the way through the audio frame, the portions of the flowgraph that weren’t parsed are purged. The flowgraph itself isn’t altered. When this occurs, the ACP sends theACP_MESSAGE_TYPE_FLOWGRAPH_TERMINATED message if the title has registered to receive messages. To determine which commands didn’t get inserted into the internal SHAPE queues, the title would examine the queued flag of the flowgraph command.
Non-persistent flowgraphs are usually processed to completion regardless of the time of their submission. One of the exceptions is the blocking of one or more commands due to the unavailability of the source data or poor use of the disabled flag. A title can submit a non-persistent flowgraph at any point during an ACP audio frame and be certain (except for the few boundary cases) that it will be completed. The benefit is that a title doesn’t have to be perfectly synchronous with the audio clock as long as the title is still servicing the flowgraphs within the ACP audio frame interval of 2.667 ms. The risk is that a title can cause audio dropouts if it doesn’t submit their flowgraphs consistently or if the flowgraphs require more than 2.667 ms.
Following is a summary of the lifetime of flowgraphs.
- A persistent flowgraph stays active on the ACP until it’s replaced by submitting a new flowgraph or a null flowgraph, which effectively removes it.
- The ACP starts processing a persistent flowgraph at the beginning of an audio frame unless the client instructs the ACP to wait for a start command.
- The ACP stops processing a persistent flowgraph shortly before the end of the audio frame, even if it hasn’t been completely processed.
- A non-persistent flowgraph is active only until it’s completed, and then it’s removed.
- A non-persistent flowgraph remains active between audio frames. It’s removed only after it has been completed.
Command processing is not tied to flowgraph parsing. The ACP is constantly scanning for new commands and then processes them as fast as possible while parsing a flowgraph or doing other work.
Updating flowgraphs
The options for updating flowgraphs are similar to those for updating contexts as previously described. The basic rule is the same: don’t update flowgraphs while they’re being processed. You can use three strategies to update flowgraphs.- Use non-persistent flowgraphs, rebuild them as needed, and submit them at the beginning of the audio frame. A title can reuse the same flowgraph as many times as necessary. If the flowgraph doesn’t change, the title doesn’t need to rebuild it. You can also double-buffer this approach by constructing a new flowgraph while the old one is being processed. When being rebuilt, not only can flowgraphs be constructed from scratch, but their sections can also be stored and linked as needed by appropriately setting the mix buffer IDs.
-
Use persistent flowgraphs and rebuild and replace them only when changes are needed. Double-buffering works here, too. The ACP can be allowed to run free by not using the
waitForStartparameter to ACP_COMMAND_LOAD_SHAPE_FLOWGRAPH. The ACP starts processing the flowgraph as soon as the audio frame starts and right after the commands tagged for that audio frame are processed. Alternatively, thewaitForStartparameter can be set to prevent the flowgraph from being processed until theACP_COMMAND_TYPE_START_FLOWGRAPHcommand is sent. This doesn’t affect flowgraph updates directly but allows for a slight improvement in synchronization. -
Use the
disabledflag on the flowgraph commands to disable portions of a flowgraph. For example, a master flowgraph can be built, but only the required sections are enabled for each run. This must be tied with option 1 or 2 to get the new flowgraph to the ACP. Note that the ACP currently doesn’t traverse the flowgraph to look for orphaned blocks. A title must disable complete paths through the flowgraph (not just the first nodes). If this isn’t done, the SHAPE commands temporarily stall the hardware. These stalls could be simple, such as a single block that can’t be processed and is removed at a small cost. They could also be severe enough to stall an entire voice.
ACP_MESSAGE_TYPE_FLOWGRAPH_COMPLETED to manage updates, the ACP can be left idle for an extended period of time between the ACP adding the message to the message queue and the title then calling PopMessage. Don’t use this approach to manage updates - keep the ACP active.
Multiple flowgraphs
A title can process multiple flowgraphs per audio frame if their combined requirements don’t exceed the hardware capabilities. The benefit is that a title can run multiple audio engines, both middleware and custom, or break up their parsing into more manageable chunks. The downside is that the SHAPE hardware won’t run as efficiently in this mode. To reach the maximum throughput of SHAPE, each SHAPE block must be kept 100 percent busy, which is impossible when multiple flowgraphs are being processed. Only one flowgraph can be loaded per ACP client. If a new flowgraph is submitted by a client, it replaces the existing one. A title that requires support for multiple flowgraphs either needs to have separate clients for each flowgraph type (each client with its own command and message queues) or needs to wait for a flowgraph to complete before submitting a new one. Support for multiple flowgraphs is designed to be used by multiple clients - not a single client. This allows a middleware engine to submit its flowgraph and the title to submit a separate flowgraph for additional custom processing. For each ACP client required by the title, create an instance of theIACPHAL interface.
Because all ACP and SHAPE resources are shared among all clients (context arrays in particular), the clients must coordinate the allocation and sharing of those resources.
DMA utilities
Include the Direct Memory Access (DMA) utilities in the ShapeDMAContext.h file. The following utilities operate on a SHAPE_DMA_CONTEXT structure. For more information, see ShapeDmaContext (DMA utility methods).EQ compressor utilities
Include the EQCOMP utilities in the ShapeEqCompContext.h file. These utilities operate on a SHAPE_EQCOMP_CONTEXT structure. For more information, see ShapeEqCompContext (EQCOMP utility methods).Filter volume utilities
Include the filter volume utilities in the ShapeFiltVolContext.h file. These utilities operate on a SHAPE_FILTVOL_CONTEXT structure. For more information, see ShapeFiltVolContext (FLTVOL utility methods).PCM utilities
Include the Pulse Code Modulation (PCM) utilities in the ShapePCMContext.h file. These utilities operate on a SHAPE_PCM_CONTEXT structure. For more information, see ShapePcmContext (PCM utility methods).SRC utilities
Include the Sample Rate Convertor (SRC) utilities in the ShapeSRCContext.h file. These utilities operate on a SHAPE_SRC_CONTEXT structure. For more information, see ShapeSrcContext (SRC utility methods).XMA utilities
Include the XMA utilities in the ShapeXMAContext.h file. These utilities operate on a SHAPE_XMA_CONTEXT structure. For more information, see ShapeXmaContext (XMA utility methods). The XMA decode capabilities of hardware emulation are limited. For details, see the SHAPE_XMA_CONTEXT structure topic. A title can use XMA data without using flowgraphs, but the data still has to go through the ACP by using the ACP_COMMAND_TYPE commands as shown in the following table.
Using these commands, a title can use the XBOX One ACP HAL almost identically to how it uses the XBOX 360 XMA HAL with the following order of operation.
- Populate the context or contexts with relevant data: buffers, offsets, and more.
- Enable the contexts.
-
Update the contexts. If the context is disabled, the title is free to directly modify the contents. If the context is enabled, the title can disable it first or use the
ACP_COMMAND_TYPE_UPDATE_XMA_CONTEXTcommand, which can be more efficient because the ACP deals with disabling, waiting, and updating.
Target values
The target values that can be set in the utility functions represent the final values of the parameter at the end of the audio frame. For example, the SHAPE_FILTVOL_CONTEXT structure contains values forgain and gainTarget. While the frame is being processed, the gain is calculated by using linear interpolation and the following equation.
gain will equal gainTarget.
Target parameters can be set for the ACP as shown in the following table.
Thread safety
The IACPHAL interface methods and ACPHAL methods are thread-safe. If the call ApuCreateHeap is made, the heap is used by all threads. The following utility functions aren’t thread-safe. However, the source code is provided for them. If necessary, you can make them thread-safe. The typical way to do this is to use Critical Section Objects.- ShapeFlowGraph (flowgraph)
- ShapeDmaContext (DMA)
- ShapeEqCompContext (EQCOMP)
- ShapeFiltVolContext (FLTVOL)
- ShapePcmContext (PCM)
- ShapeSrcContext (SRC)
- ShapeXmaContext (XMA)
Guidelines for using SRC for PCM and XMA data
The following table shows the guidelines for using the SRC block with PCM and XMA data.When any of these scenarios are completed, the SRC command is
SHAPE_SRC_COMMAND_TYPE_STOP_IMMEDIATE.Pausing and resuming a title
A title must be able to pause and resume, for example, when the user puts it into Constrained mode. To pause and resume when coding directly to the SHAPE hardware by usingIAcpHal, have the title simply stop submitting commands. The commands that were already submitted will complete normally and might fill the message queue.
If a title is using persistent flowgraphs, they should load a null flowgraph to stop processing. This differs from the pause and resume process when you code by using XAudio2. For more information, see XAudio2 overview.
Reference API documentation
- Acphal (API contents)
- Shapedmacontext (API contents)
- Structures
- Shapeeqcompcontext (API contents)
- Structures
- Shapefiltvolcontext (API contents)
- Structures
- Shapeflowgraph (API contents)
- Shapepcmcontext (API contents)
- Structures
- Shapesrccontext (API contents)
- Structures
- Shapexmacontext (API contents)
- Structures
- apu (API contents)
- Functions
