| 1 | /* Copyright 2012 Kjetil S. Matheussen |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU General Public License |
| 5 | as published by the Free Software Foundation; either version 2 |
| 6 | of the License, or (at your option) any later version. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 16 | |
| 17 | #ifndef AUDIO_SMOOTH_PROC_H |
| 18 | #define AUDIO_SMOOTH_PROC_H |
| 19 | |
| 20 | typedef struct{ |
| 21 | float* values; |
| 22 | |
| 23 | volatile float target_value; |
| 24 | |
| 25 | float start_value; |
| 26 | float end_value; |
| 27 | |
| 28 | int num_values; |
| 29 | |
| 30 | bool smoothing_is_necessary; |
| 31 | } Smooth; |
| 32 | |
| 33 | typedef struct{ |
| 34 | float vals [2][2]; |
| 35 | } Panvals; |
| 36 | |
| 37 | |
| 38 | |
| 39 | // Copied from https://github.com/kmatheussen/soundengine/blob/master/SoundObject.java (written by me) |
| 40 | // |
| 41 | static inline Panvals get_pan_vals_vector(float pan, int num_source_channels){ |
| 42 | Panvals p; |
| 43 | |
| 44 | const float sqrt2 = 1.414213562373095; |
| 45 | const float pan_scaleval = 2.0f-(2*sqrt2); |
| 46 | |
| 47 | if(num_source_channels==1){ |
| 48 | |
| 49 | float x=scale(pan,-1,1,0,1); |
| 50 | p.vals[0][0] = ((1.0f-x)*((pan_scaleval*(1.0f-x))+(1.0f-pan_scaleval))); |
| 51 | p.vals[0][1] = x * ( (pan_scaleval*x) + (1.0f-pan_scaleval)); |
| 52 | |
| 53 | }else{ |
| 54 | |
| 55 | if(pan<=0){ |
| 56 | //fprintf(stderr,"p1\n"); |
| 57 | p.vals[0][0] = 1.0f; |
| 58 | p.vals[0][1] = 0.0f; |
| 59 | |
| 60 | pan=scale(pan,-1,0,-1,1); |
| 61 | float x=scale(pan,-1,1,0,1); |
| 62 | //fprintf(stderr,"p2\n"); |
| 63 | p.vals[1][0] = ((1.0f-x)*((pan_scaleval*(1.0f-x))+(1.0f-pan_scaleval))); |
| 64 | //fprintf(stderr,"p3\n"); |
| 65 | p.vals[1][1] = x * ( (pan_scaleval*x) + (1.0f-pan_scaleval)); |
| 66 | //fprintf(stderr,"p4\n"); |
| 67 | //System.out.println("l/r/pan "+p.vals[1][0]+" "+p.vals[1][1]+" "+pan); |
| 68 | |
| 69 | }else{ |
| 70 | //fprintf(stderr,"p5\n"); |
| 71 | pan=scale(pan,0,1,-1,1); |
| 72 | float x=scale(pan,-1,1,0,1); |
| 73 | //fprintf(stderr,"p6\n"); |
| 74 | p.vals[0][0] = ((1.0f-x)*((pan_scaleval*(1.0f-x))+(1.0f-pan_scaleval))); |
| 75 | //fprintf(stderr,"p7\n"); |
| 76 | p.vals[0][1] = x * ( (pan_scaleval*x) + (1.0f-pan_scaleval)); |
| 77 | //fprintf(stderr,"p8\n"); |
| 78 | |
| 79 | p.vals[1][0] = 0.0f; |
| 80 | p.vals[1][1] = 1.0f; |
| 81 | //System.out.println("l/r/pan "+p.vals[0][0]+" "+p.vals[0][1]+" "+pan); |
| 82 | |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return p; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | extern LANGSPEC void SMOOTH_init(Smooth *smooth, float value, int blocksize); |
| 91 | extern LANGSPEC void SMOOTH_new_blocksize(Smooth *smooth, int blockframes); |
| 92 | extern LANGSPEC void SMOOTH_release(Smooth *smooth); |
| 93 | extern LANGSPEC void SMOOTH_set_target_value(Smooth *smooth, float value); |
| 94 | extern LANGSPEC float SMOOTH_get_target_value(Smooth *smooth); |
| 95 | extern LANGSPEC void SMOOTH_called_per_block(Smooth *smooth); |
| 96 | extern LANGSPEC void SMOOTH_apply_volume(Smooth *smooth, float *sound, int num_frames); |
| 97 | extern LANGSPEC void SMOOTH_apply_inverted_volume(Smooth *smooth, float *sound, int num_frames); |
| 98 | extern LANGSPEC void SMOOTH_copy_sound(Smooth *smooth, float *dst, float *src, int num_frames); |
| 99 | extern LANGSPEC void SMOOTH_mix_sounds_raw(float *target, float *source, int num_frames, float start_volume, float end_volume); |
| 100 | extern LANGSPEC void SMOOTH_mix_sounds(Smooth *smooth, float *target, float *source, int num_frames); |
| 101 | extern LANGSPEC void SMOOTH_mix_sounds_using_inverted_values(Smooth *smooth, float *target, float *source, int num_frames); |
| 102 | extern LANGSPEC void SMOOTH_apply_pan(Smooth *smooth, float **sound, int num_channels, int num_frames); |
| 103 | |
| 104 | #endif // AUDIO_SMOOTH_PROC_H |