// Port of pflib/external/openal/oalSource.h (Apple OpenAL, Playground SDK 4.0): OALSource (an AL source playing // through one bus of its context's 3DMixer), its buffer queues (BufferQueue / BufferInfo), the per-format // AudioConverters (ACMap / ACInfo), the render-thread message queue (PlaybackMessage) and OALSourceMap. // Layouts from the DWARF of parity-out/sdk/dbgobj/oalSource.o; the game uses the same offsets (OALSource // constructor game @0x00124984). // // OALSource (size 364): +0x000 mSelfToken, +0x004 mSafeForDeletion, +0x008 mOwningContext, +0x00c mIsSuspended, // +0x00d mCalculateDistance, +0x00e mResetBusFormat, +0x00f mResetPitch, +0x010 mBufferQueueActive, // +0x014 mBufferQueueInactive, +0x018 mCurrentBufferIndex, +0x01c mQueueIsProcessed, +0x020 mRenderThreadID, // +0x024 mPlayGuard (CAGuard), +0x078 mPlayCallback, +0x080 mCurrentPlayBus, +0x084 mACMap, +0x088 mOutputSilence, // +0x08c mPosition[3], +0x098 mVelocity[3], +0x0a4 mConeDirection[3], +0x0b0 mLooping, +0x0b4 mSourceRelative, // +0x0b8 mSourceType, +0x0bc mConeInnerAngle, +0x0c0 mConeOuterAngle, +0x0c4 mConeOuterGain, +0x0c8 mConeGainScaler, // +0x0cc mAttenuationGainScaler, +0x0d0 mReadIndex, +0x0d4 mCachedInputL1, +0x0d8 mCachedInputL2, // +0x0dc mCachedInputR1, +0x0e0 mCachedInputR2, +0x0e4 mTempSourceStorageBufferSize, +0x0e8 mTempSourceStorage, // +0x0ec mState, +0x0f0 mGain, +0x0f4 mPitch, +0x0f8 mDopplerScaler, +0x0fc mRollOffFactor, // +0x100 mReferenceDistance, +0x104 mMaxDistance, +0x108 mMinGain, +0x10c mMaxGain, +0x110 mRampState, // +0x114 mBufferCountToUnqueueInPostRender, +0x118 mTransitioningToFlushQ, +0x11c mPlaybackHeadPosition, // +0x120 mASAReverbSendLevel, +0x124 mASAOcclusion, +0x128 mASAObstruction, +0x12c mRogerBeepNode, // +0x130 mRogerBeepAU, +0x134 mASARogerBeepEnable, +0x135 mASARogerBeepOn, +0x138 mASARogerBeepGain, // +0x13c mASARogerBeepSensitivity, +0x140 mASARogerBeepType, +0x144 mASARogerBeepPreset, +0x148 mDistortionNode, // +0x14c mDistortionAU, +0x150 mASADistortionEnable, +0x151 mASADistortionOn, +0x154 mASADistortionMix, // +0x158 mASADistortionType, +0x15c mASADistortionPreset, +0x160 mRenderUnit, +0x164 mRenderElement, // +0x168 mMessageQueue. // BufferInfo (size 20): +0x00 mBufferToken, +0x04 mBuffer, +0x08 mOffset, +0x0c mProcessedState, +0x10 mACToken. // BufferQueue (size 8): a std::list. ACInfo (size 44): +0x00 mConverter, +0x04 mInputFormat. // ACMap and OALSourceMap (size 24): std::multimap / std::multimap. // PlaybackMessage (size 16): +0x00 mNext, +0x04 mMessageID, +0x08 mAppendBuffer, +0x0c mBuffersToUnqueueInPostRender. // The containers are gcc 4.0 libstdc++ ones in the binary; the port uses the host std::list / std::multimap // (same ordering: multimap insertions of an equal key go after the existing ones, as insert_equal does). // // Header code (inline members below) is emitted out of line in the SDK's oalSource.o / oalContext.o // (__textcoal_nt, addresses at each member) and inlined everywhere in the game, except OALSourceMap's and // BufferQueue's constructors (game @0x001923e8 / @0x001926b4, __textcoal_nt). BufferQueue::Get takes a short // index (std::advance with a negative count walks backwards), as the game's inlined copies do with the low 16 // bits of mCurrentBufferIndex; Size counts the nodes (gcc 4.0 list::size). // // Constants without names in the binary (Apple's #defines; names from Apple's OpenAL sources where known): the // render states AL_INITIAL / AL_PLAYING / AL_PAUSED / AL_STOPPED (al.h) plus kTransitionToStop 'tste' (the state // while the render thread finishes a stop), the PlaybackMessage IDs kMQ_*, the BufferInfo processed states and // the ramp states. #ifndef OALSOURCE_H_INCLUDED #define OALSOURCE_H_INCLUDED #include "oal_sysdecls.h" #include "CAGuard.h" #include "CAStreamBasicDescription.h" #include "oalAtomicStack.h" #include "oalBuffer.h" #include #include class OALContext; class OALSource; // mState while the render thread finishes a stop (GetState reports AL_STOPPED) #define kTransitionToStop 'tste' // returned by ACComplexInputDataProc when the converter has to be called again #define OALSourceError_CallConverterAgain 'agan' // BufferInfo::mProcessedState enum { kPendingProcessing = 0, kInProgress = 1, kProcessed = 2 }; // OALSource::mRampState enum { kRampingComplete = -2, kRampDown = -1, kNoRamping = 0, kRampUp = 1 }; // PlaybackMessage::mMessageID enum { kMQ_NoMessage = 0, kMQ_Stop = 1, kMQ_Rewind = 2, kMQ_SetBuffer = 3, kMQ_Play = 4, kMQ_Pause = 5, kMQ_Resume = 6, kMQ_SetFramePosition = 7, kMQ_ClearBuffersFromQueue = 8, kMQ_DeconstructionStop = 9 }; struct BufferInfo { ALuint mBufferToken; OALBuffer* mBuffer; UInt32 mOffset; UInt32 mProcessedState; ALuint mACToken; }; class BufferQueue : std::list { public: // game @0x001926b4 sdk @0x360f6 (oalSource.o) BufferQueue() {} void AppendBuffer(OALSource* thisSource, ALuint inBufferToken, OALBuffer* inBuffer, ALuint inACToken); ALuint RemoveQueueEntryByIndex(OALSource* thisSource, UInt32 inIndex, bool inReleaseIt); UInt32 GetQueueSizeInFrames(); UInt32 GetBufferFrameCount(UInt32 inBufferIndex); void SetFirstBufferOffset(UInt32 inFrameOffset); ALuint GetBufferTokenByIndex(UInt32 inBufferIndex); UInt32 GetCurrentFrame(UInt32 inBufferIndex); // sdk @0x35378 (oalSource.o) BufferInfo* Get(short inBufferIndex) { iterator it = begin(); std::advance(it, inBufferIndex); if (it != end()) { return (&(*it)); } return (NULL); } // sdk @0x3528e (oalSource.o) void SetBufferAsProcessed(UInt32 inBufferIndex) { iterator it = begin(); std::advance(it, inBufferIndex); if (it != end()) { (*it).mProcessedState = kProcessed; } } // sdk @0x350ee (oalSource.o) void ResetBuffers() { iterator it = begin(); while (it != end()) { (*it).mProcessedState = kPendingProcessing; (*it).mOffset = 0; ++it; } } // sdk @0x358de (oalSource.o) UInt32 Size() const { return (UInt32)size(); } // sdk @0x34d00 (oalSource.o) bool Empty() const { return empty(); } private: UInt32 GetPacketSize(); UInt32 FrameOffsetToPacketOffset(UInt32 inFrameOffset); }; struct ACInfo { AudioConverterRef mConverter; CAStreamBasicDescription mInputFormat; }; class ACMap : std::multimap > { public: // sdk @0x367be (oalSource.o) void Add(const ALuint inACToken, ACInfo* inACInfo) { iterator it = upper_bound(inACToken); insert(it, value_type(inACToken, *inACInfo)); } // sdk @0x35afe (oalSource.o) AudioConverterRef Get(ALuint inACToken) { iterator it = find(inACToken); iterator theEnd = end(); if (it != theEnd) { return ((*it).second.mConverter); } return (NULL); } // sdk @0x354fc (oalSource.o) void GetACForFormat(CAStreamBasicDescription* inFormat, ALuint& outToken) { iterator it = begin(); outToken = 0; while (it != end()) { if ((*it).second.mInputFormat == *inFormat) { outToken = (*it).first; it = end(); } else { ++it; } } return; } void Remove(const ALuint inACToken); // sdk @0x35594 (oalSource.o) void RemoveAllConverters() { iterator it = begin(); iterator theEnd = end(); while (it != theEnd) { AudioConverterDispose((*it).second.mConverter); ++it; } } UInt32 Size() const { return (UInt32)size(); } bool Empty() const { return empty(); } }; class PlaybackMessage { public: // sdk @0x34c4c (oalSource.o) PlaybackMessage(UInt32 inMessage, OALBuffer* inBuffer, UInt32 inNumBuffers) : mMessageID(inMessage), mAppendBuffer(inBuffer), mBuffersToUnqueueInPostRender(inNumBuffers) { } // sdk @0x34c70 (oalSource.o) ~PlaybackMessage() {} // sdk @0x34c78 (oalSource.o) PlaybackMessage* get_next() { return mNext; } // sdk @0x34c86 (oalSource.o) void set_next(PlaybackMessage* next) { mNext = next; } PlaybackMessage* mNext; UInt32 mMessageID; OALBuffer* mAppendBuffer; UInt32 mBuffersToUnqueueInPostRender; }; class OALSource { public: OALSource(const ALuint inSelfToken, OALContext* inOwningContext); ~OALSource(); // set methods - public void SetPitch(Float32 inPitch); void SetGain(Float32 inGain); void SetMinGain(Float32 inMinGain); void SetMaxGain(Float32 inMaxGain); void SetReferenceDistance(Float32 inReferenceDistance); void SetMaxDistance(Float32 inMaxDistance); void SetRollOffFactor(Float32 inRollOffFactor); void SetLooping(UInt32 inLooping); void SetPosition(Float32 inX, Float32 inY, Float32 inZ); void SetVelocity(Float32 inX, Float32 inY, Float32 inZ); void SetDirection(Float32 inX, Float32 inY, Float32 inZ); void SetSourceRelative(UInt32 inSourceRelative); void SetChannelParameters(); void SetConeInnerAngle(Float32 inConeInnerAngle); void SetConeOuterAngle(Float32 inConeOuterAngle); void SetConeOuterGain(Float32 inConeOuterGain); void SetQueueOffset(UInt32 inOffsetType, Float32 inOffset); void SetUpDeconstruction(); // get methods - public Float32 GetPitch(); Float32 GetDopplerScaler(); Float32 GetGain(); Float32 GetMinGain(); Float32 GetMaxGain(); Float32 GetReferenceDistance(); Float32 GetMaxDistance(); Float32 GetRollOffFactor(); UInt32 GetLooping(); void GetPosition(Float32& inX, Float32& inY, Float32& inZ); void GetVelocity(Float32& inX, Float32& inY, Float32& inZ); void GetDirection(Float32& inX, Float32& inY, Float32& inZ); UInt32 GetSourceRelative(); UInt32 GetSourceType(); Float32 GetConeInnerAngle(); Float32 GetConeOuterAngle(); Float32 GetConeOuterGain(); UInt32 GetState(); ALuint GetToken(); UInt32 GetQueueOffset(UInt32 inOffsetType); // buffer queue methods - public UInt32 GetQLength(); UInt32 GetBuffersProcessed(); void SetBuffer(ALuint inBufferToken, OALBuffer* inBuffer); ALuint GetBuffer(); void AddToQueue(ALuint inBufferToken, OALBuffer* inBuffer); void RemoveBuffersFromQueue(UInt32 inCount, ALuint* outBufferTokens); bool IsSourceTransitioningToFlushQ(); bool IsBufferInActiveQueue(ALuint inBufferToken); // sdk @0x1f728 (oalContext.o) bool IsSafeForDeletion() { return (mSafeForDeletion); } // playback methods - public void Play(); void Pause(); void Resume(); void Rewind(); void Stop(); void Suspend(); void Unsuspend(); // ASA methods - public (sdk only, not ported) void SetReverbSendLevel(Float32 inReverbLevel); void SetOcclusion(Float32 inOcclusion); void SetObstruction(Float32 inObstruction); void SetRogerBeepEnable(Boolean inEnable); void SetRogerBeepOn(Boolean inOn); void SetRogerBeepGain(Float32 inGain); void SetRogerBeepSensitivity(UInt32 inSensitivity); void SetRogerBeepType(UInt32 inType); void SetRogerBeepPreset(FSRef* inRef); void SetDistortionEnable(Boolean inEnable); void SetDistortionOn(Boolean inOn); void SetDistortionMix(Float32 inMix); void SetDistortionType(SInt32 inType); void SetDistortionPreset(FSRef* inRef); Float32 GetReverbSendLevel(); Float32 GetOcclusion(); Float32 GetObstruction(); Boolean GetRogerBeepEnable(); Boolean GetRogerBeepOn(); Float32 GetRogerBeepGain(); UInt32 GetRogerBeepSensitivity(); UInt32 GetRogerBeepType(); Boolean GetDistortionEnable(); Boolean GetDistortionOn(); Float32 GetDistortionMix(); SInt32 GetDistortionType(); private: void JoinBufferLists(); void LoopToBeginning(); void ChangeChannelSettings(); void InitSource(); void SetState(UInt32 inState); OSStatus DoPreRender(); OSStatus DoRender(AudioBufferList* ioData); OSStatus DoSRCRender(AudioBufferList* ioData); OSStatus DoPostRender(); bool ConeAttenuation(); void CalculateDistanceAndAzimuth(Float32* outDistance, Float32* outAzimuth, Float32* outElevation, Float32* outDopplerShift); // sdk @0x34dd6 (oalSource.o) bool CallingInRenderThread() const { return (pthread_self() == mRenderThreadID); } void UpdateBusGain(); void UpdateBusFormat(); void UpdateBusReverb(); void UpdateBusOcclusion(); void UpdateBusObstruction(); void UpdateQueue(); void RampDown(AudioBufferList* ioData); void RampUp(AudioBufferList* ioData); void ClearActiveQueue(); void SkipALNONEBuffers(); void AddNotifyAndRenderProcs(); void ReleaseNotifyAndRenderProcs(); void DisconnectFromBus(); void SetupMixerBus(); void SetupDistortionAU(); void SetupRogerBeepAU(); bool PrepBufferQueueForPlayback(); UInt32 SecondsToFrames(Float32 inSeconds); UInt32 BytesToFrames(Float32 inBytes); void AppendBufferToQueue(ALuint inBufferToken, OALBuffer* inBuffer); UInt32 FramesToSeconds(UInt32 inFrames); UInt32 FramesToBytes(UInt32 inFrames); void PostRenderSetBuffer(ALuint inBufferToken, OALBuffer* inBuffer); void PostRenderRemoveBuffersFromQueue(UInt32 inBuffersToUnqueue); void FlushBufferQueue(); void StopFromDestructor(); void AdvanceQueueToFrameIndex(UInt32 inFrameOffset); OSStatus SetDistanceParams(bool inChangeReferenceDistance, bool inChangeMaxDistance); Float32 GetMaxAttenuation(Float32 inRefDistance, Float32 inMaxDistance, Float32 inRolloff); BufferInfo* NextPlayableBufferInActiveQ(); static OSStatus SourceNotificationProc(void* inRefCon, AudioUnitRenderActionFlags* inActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList* ioData); static OSStatus SourceInputProc(void* inRefCon, AudioUnitRenderActionFlags* inActionFlags, const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList* ioData); static OSStatus ACComplexInputDataProc(AudioConverterRef inAudioConverter, UInt32* ioNumberDataPackets, AudioBufferList* ioData, AudioStreamPacketDescription** outDataPacketDescription, void* inUserData); ALuint mSelfToken; bool mSafeForDeletion; OALContext* mOwningContext; bool mIsSuspended; bool mCalculateDistance; bool mResetBusFormat; bool mResetPitch; BufferQueue* mBufferQueueActive; BufferQueue* mBufferQueueInactive; ALuint mCurrentBufferIndex; bool mQueueIsProcessed; pthread_t mRenderThreadID; CAGuard mPlayGuard; AURenderCallbackStruct mPlayCallback; int mCurrentPlayBus; ACMap* mACMap; bool mOutputSilence; Float32 mPosition[3]; Float32 mVelocity[3]; Float32 mConeDirection[3]; UInt32 mLooping; UInt32 mSourceRelative; UInt32 mSourceType; Float32 mConeInnerAngle; Float32 mConeOuterAngle; Float32 mConeOuterGain; Float32 mConeGainScaler; Float32 mAttenuationGainScaler; float mReadIndex; float mCachedInputL1; float mCachedInputL2; float mCachedInputR1; float mCachedInputR2; UInt32 mTempSourceStorageBufferSize; AudioBufferList* mTempSourceStorage; UInt32 mState; float mGain; Float32 mPitch; Float32 mDopplerScaler; Float32 mRollOffFactor; Float32 mReferenceDistance; Float32 mMaxDistance; Float32 mMinGain; Float32 mMaxGain; SInt32 mRampState; UInt32 mBufferCountToUnqueueInPostRender; bool mTransitioningToFlushQ; UInt32 mPlaybackHeadPosition; Float32 mASAReverbSendLevel; Float32 mASAOcclusion; Float32 mASAObstruction; AUNode mRogerBeepNode; AudioUnit mRogerBeepAU; Boolean mASARogerBeepEnable; Boolean mASARogerBeepOn; Float32 mASARogerBeepGain; UInt32 mASARogerBeepSensitivity; UInt32 mASARogerBeepType; char* mASARogerBeepPreset; AUNode mDistortionNode; AudioUnit mDistortionAU; Boolean mASADistortionEnable; Boolean mASADistortionOn; Float32 mASADistortionMix; SInt32 mASADistortionType; char* mASADistortionPreset; AudioUnit mRenderUnit; UInt32 mRenderElement; TAtomicStack mMessageQueue; }; class OALSourceMap : std::multimap > { public: // game @0x001923e8 sdk @0x20062 (oalContext.o) OALSourceMap() {} // sdk @0x204e6 (oalContext.o) void Add(const ALuint inSourceToken, OALSource** inSource) { iterator it = upper_bound(inSourceToken); insert(it, value_type(inSourceToken, *inSource)); } // sdk @0x1f900 (oalContext.o) OALSource* GetSourceByIndex(UInt32 inIndex) { iterator it = begin(); for (UInt32 i = 0; i < inIndex; i++) { if (it != end()) { it++; } else { i = inIndex; } } if (it != end()) { return ((*it).second); } return (NULL); } // sdk @0x1fc62 (oalContext.o) OALSource* Get(ALuint inSourceToken) { iterator it = find(inSourceToken); if (it != end()) { return ((*it).second); } return (NULL); } // sdk @0x1f8a2 (oalContext.o) void MarkAllSourcesForRecalculation() { iterator it = begin(); while (it != end()) { (*it).second->SetChannelParameters(); it++; } return; } void SuspendAllSources(); void UnsuspendAllSources(); // sdk @0x1fe4e (oalContext.o) void Remove(const ALuint inSourceToken) { iterator it = find(inSourceToken); if (it != end()) { erase(it); } } // sdk @0x1f7c4 (oalContext.o) UInt32 Size() const { return (UInt32)size(); } bool Empty() const { return empty(); } }; #endif