/* Copyright (c) 2020 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 __AudioMath_H__ #define __AudioMath_H__ #ifdef __cplusplus extern "C" { #endif #include "pcmaudio/PCMAudio.h" #include #include typedef float (* AudioMath_envelopeFunction)(float value); typedef float (* AudioMath_waveFunction)(float value); #define resampleFrameCount(frame_count, old_sample_rate, new_sample_rate) (((frame_count) * (AudioFrameIndex) (new_sample_rate) + (old_sample_rate - 1)) / (old_sample_rate)) #define AUDIO_CHANNEL_COUNT_MAX 2 typedef struct AudioResampleState { float lastSample[AUDIO_CHANNEL_COUNT_MAX]; float lastSampleBlend; } AudioResampleState; // Assumes bytesPerSample 1 = int8_t (-128 - 127), 2 = int16_t (-32768 - 32767), 4 = float (-1.0f - 1.0f) AudioResampleState initAudioResampleState(void); void convertAudioSamples(const void * inSamples, AudioFrameIndex inFrameCount, unsigned int inChannelCount, unsigned int inSampleRate, unsigned int inBytesPerSample, void * outSamples, AudioFrameIndex outFrameCountMax, unsigned int outChannelCount, unsigned int outSampleRate, unsigned int outBytesPerSample, AudioFrameIndex * outFramesRead, AudioFrameIndex * outFramesWritten, AudioResampleState * ioResampleState); void mixAudioSamples(const void * inSamples, unsigned int inChannelCount, unsigned int inSampleRate, unsigned int inBytesPerSample, void * ioMixedSamples, unsigned int outChannelCount, unsigned int outSampleRate, unsigned int outBytesPerSample, float * channelMultipliers, // Must include a number of elements equal to outChannelCount AudioFrameIndex inFrameCount, AudioFrameIndex outFrameCountMax, AudioFrameIndex * outFramesRead, AudioFrameIndex * outFramesWritten, AudioResampleState * ioResampleState); float * fourierTransform(const void * inSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount); // transformedSamples will be destructively altered void inverseFourierTransform(float * transformedSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount, void * outSamples); // Clamps all samples in ioSamples to within the range specified by minValue and maxValue, which // must point to numbers of a type specified by bytesPerSample (1 = int8, 2 = int16, 4 = float) void clampAudioSamples(void * ioSamples, void * minValue, void * maxValue, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount); void amplifyAudioSamples(void * ioSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount, float multiplier); void applyAmplitudeEnvelope(AudioMath_envelopeFunction function, void * ioSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount); void normalizeAudioSamples(void * ioSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount, float maxValue); void stretchAudioSamples(int multiplier, int divisor, void * ioSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount); void equalizeAudioSamples(unsigned int bandCount, float * bands, void * ioSamples, unsigned int channelCount, unsigned int bytesPerSample, unsigned int sampleRate, AudioFrameIndex frameCount); void reverseAudioSamples(void * ioSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount); void centerAudioSamples(void * ioSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount, float centerValue); void generateTone(float frequency, AudioMath_waveFunction function, void * outSamples, unsigned int channelCount, unsigned int sampleRate, unsigned int bytesPerSample, AudioFrameIndex frameCount); void generateNoise(uint32_t seed, void * outSamples, unsigned int channelCount, unsigned int bytesPerSample, AudioFrameIndex frameCount); #ifdef __cplusplus } #endif #endif