1 /* 2 * play.h 3 * DIN Is Noise is copyright (c) 2006-2021 Jagannathan Sampath 4 * DIN Is Noise is released under GNU Public License 2.0 5 * For more information, please visit https://dinisnoise.org/ 6 */ 7 8 9 #ifndef PLAY 10 #define PLAY 11 12 #include "note.h" 13 #include "curve_mixer.h" 14 15 // 16 // renders a tone 17 // 18 19 struct solver; 20 21 struct play : set_mixer { 22 23 // pitch 24 // 25 solver* sol; // solver linked to waveform 26 float x; // current x on the waveform; x usually goes from 0 to 1. 0 = waveform start, 1 = waveform end ie 1 cycle. 27 float dx; // change of x every sample; determines how fast the waveform is sampled and hence frequency of the tone (equivalent to variable step in note.h) 28 float* pdx; // array of dx [interpolated from last to present for smooth frequency change] 29 30 // volume 31 // 32 float vol; // current volume 33 float* pvol; // array of volumes [interpolated from last to present for smooth volume change] 34 35 // for calculation of pdx and pvol 36 // 37 float da; // delta alpha 38 int n; // buffer size 39 int n_1; // n-1 40 41 // for mixing waveforms 42 curve_mixer mixer; 43 void set_mix (multi_curve& crv, const std::string& nam); 44 45 play (solver* s); 46 play (); 47 play (const play&); 48 ~play (); 49 void alloc (); 50 void realloc (); 51 52 void init (); 53 set_waveplay54 inline void set_wave (solver* s) { sol = s;} set_stepplay55 inline void set_step (float xd) {dx = xd;} set_volumeplay56 inline void set_volume (float v) {vol = v;} setplay57 inline void set (float xd, float l, float r) {dx = xd; vol = l;} 58 59 void interpolate_buffer (float* buf, float s, float e); 60 void fill_pitch (float xd); 61 void fill_volume (float v); 62 void set_interpolated_pitch (float xd); 63 void set_interpolated_pitch (float xd, int check); 64 void set_interpolated_volume (float v); 65 void set_interpolated_volume (float v, int check); 66 set_interpolated_pitch_volumeplay67 void set_interpolated_pitch_volume (float xd, float v) { 68 set_interpolated_pitch (xd); 69 set_interpolated_volume (v); 70 } 71 set_interpolated_pitch_volumeplay72 void set_interpolated_pitch_volume (float xd, float v, int check) { 73 set_interpolated_pitch (xd, check); 74 set_interpolated_volume (v, check); 75 } 76 operatorplay77 inline void operator() (float* out, int n, float* wav, float vol) { 78 // no modulation audio output 79 for (int i = 0; i < n; ++i) out[i] += (vol * wav[i]); 80 } 81 operatorplay82 inline void operator() (float* out, int n, float* wav, float* vola) { 83 // no modulation audio output with volume array 84 for (int i = 0; i < n; ++i) out[i] += (vola[i] * wav[i]); 85 } 86 operatorplay87 void operator() (float* out, int n, float* wav, float* am, float vol) { 88 // AM modulation in audio output 89 for (int i = 0; i < n; ++i) out[i] += ((vol + am[i]) * wav[i]); 90 } 91 92 void gen_wav_and_mix (float* wav, int n); 93 void gen_wav_fm (solver& s, float& xx, float* wav, float* fm, int n); 94 void gen_wav_am (float* out, float* wav, float* am, int n); 95 void gen_wav_fm_am_mix (float* out, int n); 96 void gen_wav_mix (float* out, float vol, int n); 97 void gen_wav_mix (float* out, float* vol, int n); 98 void master (float* left, float* right, float* wav, int n, float* vol); 99 void master (float* left, float* right, float* wav, int n, float vol); 100 101 }; 102 103 #endif 104