|
| 1 | +/* |
| 2 | +Copyright (c) 2022 Karl Stavestrand <karl@stavestrand.no> |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | +#ifdef __cplusplus |
| 23 | +extern "C" { |
| 24 | +#endif |
| 25 | +#pragma once |
| 26 | +#include <stdint.h> |
| 27 | + |
| 28 | +#include <fftw3.h> |
| 29 | + |
| 30 | +// cava_plan, parameters used internally by cavacore, do not modify these directly |
| 31 | +// only the cut off frequencies is of any potential interest to read out, |
| 32 | +// the rest should most likley be hidden somehow |
| 33 | +struct cava_plan { |
| 34 | + int FFTbassbufferSize; |
| 35 | + int FFTbufferSize; |
| 36 | + int number_of_bars; |
| 37 | + int audio_channels; |
| 38 | + int input_buffer_size; |
| 39 | + int rate; |
| 40 | + int bass_cut_off_bar; |
| 41 | + int sens_init; |
| 42 | + int autosens; |
| 43 | + int frame_skip; |
| 44 | + int status; |
| 45 | + char error_message[1024]; |
| 46 | + |
| 47 | + double sens; |
| 48 | + double framerate; |
| 49 | + double noise_reduction; |
| 50 | + |
| 51 | + fftw_plan p_bass_l, p_bass_r; |
| 52 | + fftw_plan p_l, p_r; |
| 53 | + |
| 54 | + fftw_complex *out_bass_l, *out_bass_r; |
| 55 | + fftw_complex *out_l, *out_r; |
| 56 | + |
| 57 | + double *bass_multiplier; |
| 58 | + double *multiplier; |
| 59 | + |
| 60 | + double *in_bass_r_raw, *in_bass_l_raw; |
| 61 | + double *in_r_raw, *in_l_raw; |
| 62 | + double *in_bass_r, *in_bass_l; |
| 63 | + double *in_r, *in_l; |
| 64 | + double *prev_cava_out, *cava_mem; |
| 65 | + double *input_buffer, *cava_peak; |
| 66 | + |
| 67 | + double *eq; |
| 68 | + |
| 69 | + float *cut_off_frequency; |
| 70 | + int *FFTbuffer_lower_cut_off; |
| 71 | + int *FFTbuffer_upper_cut_off; |
| 72 | + double *cava_fall; |
| 73 | +}; |
| 74 | + |
| 75 | +// cava_init, initialize visualization, takes the following parameters: |
| 76 | + |
| 77 | +// number_of_bars, number of wanted bars per channel |
| 78 | + |
| 79 | +// rate, sample rate of input signal |
| 80 | + |
| 81 | +// channels, number of interleaved channels in input |
| 82 | + |
| 83 | +// autosens, toggle automatic sensitivity adjustment 1 = on, 0 = off |
| 84 | +// on, gives a dynamically adjusted output signal from 0 to 1 |
| 85 | +// the output is continously adjusted to use the entire range |
| 86 | +// off, will pass the raw values from cava directly to the output |
| 87 | +// the max values will then be dependent on the input |
| 88 | + |
| 89 | +// noise_reduction, adjust noise reduciton filters. 0 - 1, recomended 0.77 |
| 90 | +// the raw visualization is very noisy, this factor adjusts the integral |
| 91 | +// and gravity filters inside cavacore to keep the signal smooth |
| 92 | +// 1 will be very slow and smooth, 0 will be fast but noisy. |
| 93 | + |
| 94 | +// low_cut_off, high_cut_off cut off frequencies for visualization in Hz |
| 95 | +// recomended: 50, 10000 |
| 96 | + |
| 97 | +// returns a cava_plan to be used by cava_execute. If cava_plan.status is 0 all is OK. |
| 98 | +// If cava_plan.status is -1, cava_init was called with an illegal paramater, see error string in |
| 99 | +// cava_plan.error_message |
| 100 | +extern struct cava_plan *cava_init(int number_of_bars, unsigned int rate, int channels, |
| 101 | + int autosens, double noise_reduction, int low_cut_off, |
| 102 | + int high_cut_off); |
| 103 | + |
| 104 | +// cava_execute, executes visualization |
| 105 | + |
| 106 | +// cava_in, input buffer can be any size. internal buffers in cavacore is |
| 107 | +// 4096 * number of channels at 44100 samples rate, if new_samples is greater |
| 108 | +// then samples will be discarded. However it is recomended to use less |
| 109 | +// new samples per execution as this determines your framerate. |
| 110 | +// 512 samples at 44100 sample rate mono, gives about 86 frames per second. |
| 111 | + |
| 112 | +// new_samples, the number of samples in cava_in to be processed per execution |
| 113 | +// in case of async reading of data this number is allowed to vary from execution to execution |
| 114 | + |
| 115 | +// cava_out, output buffer. Size must be number of bars * number of channels. Bars will |
| 116 | +// be sorted from lowest to highest frequency. If stereo input channels are configured |
| 117 | +// then all left channel bars will be first then the right. |
| 118 | + |
| 119 | +// plan, the cava_plan struct returned from cava_init |
| 120 | + |
| 121 | +// cava_execute assumes cava_in samples to be interleaved if more than one channel |
| 122 | +// only up to two channels are supported. |
| 123 | +extern void cava_execute(double *cava_in, int new_samples, double *cava_out, |
| 124 | + struct cava_plan *plan); |
| 125 | + |
| 126 | +// cava_destroy, destroys the plan, frees up memory |
| 127 | +extern void cava_destroy(struct cava_plan *plan); |
| 128 | + |
| 129 | +#ifdef __cplusplus |
| 130 | +} |
| 131 | +#endif |
0 commit comments