/* Copyright (c) 2024 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #ifndef __LoopRampAudioStream_H__ #define __LoopRampAudioStream_H__ typedef struct LoopRampAudioStream LoopRampAudioStream; #define LoopRampAudioStream_superclass PCMAudioStream #include "pcmaudio/PCMAudioStream.h" #define LoopRampAudioStream_ivars \ PCMAudioStream_ivars \ \ AudioFrameIndex frameIndex; \ bool looped; \ AudioFrameIndex loopFrameCount; \ AudioFrameIndex framePlayedCountTotal; \ AudioFrameIndex rampStartFrameIndex; \ AudioFrameIndex rampEndFrameIndex; \ float rampStartVolume; \ float rampEndVolume; \ float lastRampStartVolume; \ float lastRampEndVolume; \ double queuedRampDuration; \ unsigned int rampQueued; \ unsigned int rampQueueProcessed; #define LoopRampAudioStream_vtable(self_type) \ PCMAudioStream_vtable(self_type) \ \ void (* writeAudioFrames)(self_type * self, void * outBuffer, AudioFrameIndex frameCount, AudioFrameIndex * ioOutFrameIndex, float rampStartVolume, float rampEndVolume, AudioFrameIndex rampFrameCountTotal); \ void (* updateRamp)(self_type * self); \ void (* rampVolume)(self_type * self, bool upward, double duration); stemobject_declare(LoopRampAudioStream) bool LoopRampAudioStream_init(LoopRampAudioStream * self, AudioFrameIndex startFrameIndex, AudioFrameIndex loopFrameCount, unsigned int bytesPerSample, unsigned int channelCount, unsigned int sampleRate); AudioFrameIndex LoopRampAudioStream_read(LoopRampAudioStream * self, AudioFrameIndex frameCount, void * outBuffer, bool loop); // May cause synchronization issues if used while playing; for now, use only on stopped or paused streams void LoopRampAudioStream_seek(LoopRampAudioStream * self, AudioFrameIndex offset, int whence); // May be called multiple times from default read() implementation void LoopRampAudioStream_writeAudioFrames(LoopRampAudioStream * self, void * outBuffer, AudioFrameIndex frameCount, AudioFrameIndex * ioOutFrameIndex, float rampStartVolume, float rampEndVolume, AudioFrameIndex rampFrameCountTotal); // Volume can either ramp from full volume to silent or vice versa, depending on the value of upward. // If ramping downward, the stream will end after the ramp has completed. void LoopRampAudioStream_updateRamp(LoopRampAudioStream * self); void LoopRampAudioStream_rampVolume(LoopRampAudioStream * self, bool upward, double duration); #endif