You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 4 години
преди 4 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2017, TeensyAudio PSU Team
  3. *
  4. * Development of this audio library was sponsored by PJRC.COM, LLC.
  5. * Please support PJRC's efforts to develop open source
  6. * software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "synth_wavetable.h"
  28. #include <dspinst.h>
  29. #include <SerialFlash.h>
  30. //#define TIME_TEST_ON
  31. //#define ENVELOPE_DEBUG
  32. // Performance testing macro generally unrelated to the wavetable object, but was used to
  33. // fine tune the performance of specific blocks of code in update(); usage is to specify a
  34. // display interval in ms, then place the code block to be tracked *IN* the macro parens as
  35. // the second argument
  36. #ifdef TIME_TEST_ON
  37. #define TIME_TEST(INTERVAL, CODE_BLOCK_TO_TEST) \
  38. static float MICROS_AVG = 0.0; \
  39. static int TEST_CUR_CNT = 0; \
  40. static int TEST_LST_CNT = 0; \
  41. static int NEXT_DISPLAY = 0; \
  42. static int TEST_TIME_ACC = 0; \
  43. int micros_start = micros(); \
  44. CODE_BLOCK_TO_TEST \
  45. int micros_end = micros(); \
  46. TEST_TIME_ACC += micros_end - micros_start; \
  47. ++TEST_CUR_CNT; \
  48. if (NEXT_DISPLAY < micros_end) { \
  49. MICROS_AVG += (TEST_TIME_ACC - TEST_CUR_CNT * MICROS_AVG) / (TEST_LST_CNT + TEST_CUR_CNT); \
  50. NEXT_DISPLAY = micros_end + INTERVAL*1000; \
  51. TEST_LST_CNT += TEST_CUR_CNT; \
  52. TEST_TIME_ACC = TEST_CUR_CNT = 0; \
  53. Serial.printf("avg: %f, n: %i\n", MICROS_AVG, TEST_LST_CNT); \
  54. }
  55. #else
  56. #define TIME_TEST(INTERVAL, CODE_BLOCK_TO_TEST) do { } while(0); \
  57. CODE_BLOCK_TO_TEST
  58. #endif
  59. // Debug code to track state variables for the volume envelope
  60. #ifdef ENVELOPE_DEBUG
  61. #define PRINT_ENV(NAME) Serial.printf("%14s-- env_mult:%06.4f%% of UNITY_GAIN env_incr:%06.4f%% of UNITY_GAIN env_count:%i\n", #NAME, float(env_mult)/float(UNITY_GAIN), float(env_incr)/float(UNITY_GAIN), env_count);
  62. #else
  63. #define PRINT_ENV(NAME) do { } while(0);
  64. #endif
  65. /**
  66. * @brief Stop playing waveform.
  67. *
  68. * Waveform does not immediately stop,
  69. * but fades out based on release time.
  70. *
  71. */
  72. void AudioSynthWavetable::stop(void) {
  73. cli();
  74. if (env_state != STATE_IDLE) {
  75. env_state = STATE_RELEASE;
  76. env_count = current_sample->RELEASE_COUNT;
  77. if (env_count == 0) env_count = 1;
  78. env_incr = -(env_mult) / (env_count * ENVELOPE_PERIOD);
  79. }
  80. PRINT_ENV(STATE_RELEASE);
  81. sei();
  82. }
  83. /**
  84. * @brief Play waveform at defined frequency, amplitude.
  85. *
  86. * @param freq Frequency of note to playback, value between 1.0 and half of AUDIO_SAMPLE_RATE_EXACT
  87. * @param amp Amplitude scaling of note, value between 0-127, with 127 being base volume
  88. */
  89. void AudioSynthWavetable::playFrequency(float freq, int amp) {
  90. setState(freqToNote(freq), amp, freq);
  91. }
  92. /**
  93. * @brief Play sample at specified note, amplitude.
  94. *
  95. * @param note Midi note number to playback, value between 0-127
  96. * @param amp Amplitude scaling of playback, value between 0-127, with 127 being base volume
  97. */
  98. void AudioSynthWavetable::playNote(int note, int amp) {
  99. setState(note, amp, noteToFreq(note));
  100. }
  101. /**
  102. * @brief Initializes object state variables, sets freq/amp, and chooses appropriate sample
  103. *
  104. * @param note Midi note number to play, value between 0-127
  105. * @param amp the amplitude level at which playback should occur
  106. * @param freq exact frequency of the note to be played played
  107. */
  108. void AudioSynthWavetable::setState(int note, int amp, float freq) {
  109. cli();
  110. int i;
  111. env_state = STATE_IDLE;
  112. // note ranges calculated by sound font decoder
  113. for (i = 0; note > instrument->sample_note_ranges[i]; i++);
  114. current_sample = &instrument->samples[i];
  115. if (current_sample == NULL) {
  116. sei();
  117. return;
  118. }
  119. setFrequency(freq);
  120. vib_count = mod_count = tone_phase = env_incr = env_mult = 0;
  121. vib_phase = mod_phase = TRIANGLE_INITIAL_PHASE;
  122. env_count = current_sample->DELAY_COUNT;
  123. // linear scalar for amp with UINT16_MAX being no attenuation
  124. tone_amp = amp * (UINT16_MAX / 127);
  125. // scale relative to initial attenuation defined by soundfont file
  126. tone_amp = current_sample->INITIAL_ATTENUATION_SCALAR * tone_amp >> 16;
  127. env_state = STATE_DELAY;
  128. PRINT_ENV(STATE_DELAY);
  129. state_change = true;
  130. sei();
  131. }
  132. /**
  133. * @brief Set various integer offsets to values that will produce intended frequencies
  134. * @details the main integer offset, tone_incr, is used to step through the current sample's 16-bit PCM audio sample.
  135. * Specifically, the tone_incr is the rate at which the interpolation code in update() steps through uint32_t space.
  136. * The remaining offset variables represent a minimum and maximum offset allowed for tone_incr, which allows for low-frequency
  137. * variation in playback frequency (aka vibrato). Further details on implementation in update() and in sample_data.h.
  138. *
  139. * @param freq frequency of the generated output (between 0 and the board-specific sample rate)
  140. */
  141. void AudioSynthWavetable::setFrequency(float freq) {
  142. float tone_incr_temp = freq * current_sample->PER_HERTZ_PHASE_INCREMENT;
  143. tone_incr = tone_incr_temp;
  144. vib_pitch_offset_init = tone_incr_temp * current_sample->VIBRATO_PITCH_COEFFICIENT_INITIAL;
  145. vib_pitch_offset_scnd = tone_incr_temp * current_sample->VIBRATO_PITCH_COEFFICIENT_SECOND;
  146. mod_pitch_offset_init = tone_incr_temp * current_sample->MODULATION_PITCH_COEFFICIENT_INITIAL;
  147. mod_pitch_offset_scnd = tone_incr_temp * current_sample->MODULATION_PITCH_COEFFICIENT_SECOND;
  148. }
  149. /**
  150. * @brief Called by the AudioStream library to fill the audio output buffer.
  151. * The major parts are the interpoalation stage, and the volume envelope stage.
  152. * Further details on implementation included inline.
  153. *
  154. */
  155. void AudioSynthWavetable::update(void) {
  156. #if defined(KINETISK) || defined(__IMXRT1062__)
  157. // exit if nothing to do
  158. if (env_state == STATE_IDLE || (current_sample->LOOP == false && tone_phase >= current_sample->MAX_PHASE)) {
  159. env_state = STATE_IDLE;
  160. return;
  161. }
  162. // else locally copy object state and continue
  163. this->state_change = false;
  164. const sample_data* s = (const sample_data*)current_sample;
  165. uint32_t tone_phase = this->tone_phase;
  166. uint32_t tone_incr = this->tone_incr;
  167. uint16_t tone_amp = this->tone_amp;
  168. envelopeStateEnum env_state = this->env_state;
  169. int32_t env_count = this->env_count;
  170. int32_t env_mult = this->env_mult;
  171. int32_t env_incr = this->env_incr;
  172. uint32_t vib_count = this->vib_count;
  173. uint32_t vib_phase = this->vib_phase;
  174. int32_t vib_pitch_offset_init = this->vib_pitch_offset_init;
  175. int32_t vib_pitch_offset_scnd = this->vib_pitch_offset_scnd;
  176. uint32_t mod_count = this->mod_count;
  177. int32_t mod_phase = this->mod_phase;
  178. int32_t mod_pitch_offset_init = this->mod_pitch_offset_init;
  179. int32_t mod_pitch_offset_scnd = this->mod_pitch_offset_scnd;
  180. audio_block_t* block;
  181. block = allocate();
  182. if (block == NULL) return;
  183. uint32_t* p, *end;
  184. uint32_t index, phase_scale;
  185. int32_t s1, s2;
  186. uint32_t tmp1, tmp2;
  187. // filling audio_block two samples at a time
  188. p = (uint32_t*)block->data;
  189. end = p + AUDIO_BLOCK_SAMPLES / 2;
  190. // Main loop to handle interpolation, vibrato (vibrato LFO and modulation LFO), and tremolo (modulation LFO only)
  191. // Virbrato and modulation offsets/multipliers are updated depending on the LFO_SMOOTHNESS, with max smoothness (7) being one
  192. // update per loop interation, and minimum smoothness (1) being once per loop. Hence there is a configurable trade-off
  193. // between performance and the smoothness of LFO changes to pitch/amplitude as well as the vibrato/modulation delay granularity
  194. // also note that the vibrato/tremolo for the two LFO are defined in the SoundFont spec to be a cents (vibrato) or centibel (tremolo)
  195. // diviation oscillating with a triangle wave at a given frequency; the following implementation gets the critical points of those
  196. // oscillations correct, but linearly interpolates the *frequency* and *amplitude* range between those points, which technically results
  197. // in a "bowing" of the triangle wave curve relative to what it should be (although this typically isn't audible)
  198. while (p < end) {
  199. // TODO: more elegant support of non-looping samples
  200. if (s->LOOP == false && tone_phase >= s->MAX_PHASE) break;
  201. // variable to accumulate LFO pitch offsets; stays 0 if still in vibrato/modulation delay
  202. int32_t tone_incr_offset = 0;
  203. if (vib_count++ > s->VIBRATO_DELAY) {
  204. vib_phase += s->VIBRATO_INCREMENT;
  205. // convert uint32_t phase value to int32_t triangle wave value
  206. // TRIANGLE_INITIAL_PHASE (0xC0000000) and 0x40000000 -> 0, 0 -> INT32_MAX/2, 0x80000000 -> INT32_MIN/2
  207. int32_t vib_scale = vib_phase & 0x80000000 ? 0x40000000 + vib_phase : 0x3FFFFFFF - vib_phase;
  208. // select a vibrato pitch offset based on sign of scale; note that the values "init" and "scnd" values
  209. // produced by the decoder script will either both be negative, or both be positive; this allows the
  210. // scalar to either start with either a downward (negative offset) or upward (positive) pitch oscillation
  211. int32_t vib_pitch_offset = vib_scale >= 0 ? vib_pitch_offset_init : vib_pitch_offset_scnd;
  212. // scale the offset and accumulate into offset
  213. // note the offset value is already preshifted by << 2 to account for this func shifting >> 32
  214. tone_incr_offset = multiply_accumulate_32x32_rshift32_rounded(tone_incr_offset, vib_scale, vib_pitch_offset);
  215. }
  216. // variable to hold an adjusted amplitude attenuation value; stays at tone_amp if modulation in delay
  217. int32_t mod_amp = tone_amp;
  218. if (mod_count++ > s->MODULATION_DELAY) {
  219. // pitch LFO component is same as above, but we'll also use the scale value for tremolo below
  220. mod_phase += s->MODULATION_INCREMENT;
  221. int32_t mod_scale = mod_phase & 0x80000000 ? 0x40000000 + mod_phase : 0x3FFFFFFF - mod_phase;
  222. int32_t mod_pitch_offset = mod_scale >= 0 ? mod_pitch_offset_init : mod_pitch_offset_scnd;
  223. tone_incr_offset = multiply_accumulate_32x32_rshift32_rounded(tone_incr_offset, mod_scale, mod_pitch_offset);
  224. // similar to pitch, sign of init and scnd are either both + or - to allow correct triangle direction
  225. int32_t mod_amp_offset = (mod_scale >= 0 ? s->MODULATION_AMPLITUDE_INITIAL_GAIN : s->MODULATION_AMPLITUDE_SECOND_GAIN);
  226. // here we scale the amp offset which, similar to the pitch offset, is already pre-shifted by << 2
  227. mod_scale = multiply_32x32_rshift32(mod_scale, mod_amp_offset);
  228. // the resulting scalar is then used to scale mod_map (possibly resulting in a negative) and add that back into mod_amp
  229. mod_amp = signed_multiply_accumulate_32x16b(mod_amp, mod_scale, mod_amp);
  230. }
  231. // producing 2 output values per iteration; repeat more depending on the LFO_SMOOTHNESS
  232. // this segment linearly interpolates, calculates how far we step through the sample data, and scales amplitude
  233. for (int i = LFO_PERIOD / 2; i; --i, ++p) {
  234. // INDEX_BITS representing the higher order bits we use to index into the sample data
  235. index = tone_phase >> (32 - s->INDEX_BITS);
  236. // recast as uint32_t to load in packed variable; initially int16_t* since we may need to read accross a word boundry
  237. // note we are assuming a little-endian cpu (i.e. the first sample is loaded into the lower half-word)
  238. tmp1 = *((uint32_t*)(s->sample + index));
  239. // phase_scale here being the next 16-bits after the first INDEX_BITS, representing the distince between the samples to interpolate at
  240. // 0x0000 gives us all of the first sample point, 0xFFFF all of the second, anything inbetween a sliding mix
  241. phase_scale = (tone_phase << s->INDEX_BITS) >> 16;
  242. // scaling of second sample point
  243. s1 = signed_multiply_32x16t(phase_scale, tmp1);
  244. // then add in scaling of first point
  245. s1 = signed_multiply_accumulate_32x16b(s1, 0xFFFF - phase_scale, tmp1);
  246. // apply amplitude scaling
  247. s1 = signed_multiply_32x16b(mod_amp, s1);
  248. // iterate tone_phase, giving us our desired frequency playback, and apply the offset, giving us our pitch LFOs
  249. tone_phase += tone_incr + tone_incr_offset;
  250. // break if no loop and we've gone past the end of the sample
  251. if (s->LOOP == false && tone_phase >= s->MAX_PHASE) break;
  252. // move phase back if a looped sample has overstepped its loop
  253. tone_phase = s->LOOP && tone_phase >= s->LOOP_PHASE_END ? tone_phase - s->LOOP_PHASE_LENGTH : tone_phase;
  254. //repeat as above
  255. index = tone_phase >> (32 - s->INDEX_BITS);
  256. tmp1 = *((uint32_t*)(s->sample + index));
  257. phase_scale = (tone_phase << s->INDEX_BITS) >> 16;
  258. s2 = signed_multiply_32x16t(phase_scale, tmp1);
  259. s2 = signed_multiply_accumulate_32x16b(s2, 0xFFFF - phase_scale, tmp1);
  260. s2 = signed_multiply_32x16b(mod_amp, s2);
  261. tone_phase += tone_incr + tone_incr_offset;
  262. if (s->LOOP == false && tone_phase >= s->MAX_PHASE) break;
  263. tone_phase = s->LOOP && tone_phase >= s->LOOP_PHASE_END ? tone_phase - s->LOOP_PHASE_LENGTH : tone_phase;
  264. // pack the two output samples into the audio_block
  265. *p = pack_16b_16b(s2, s1);
  266. }
  267. }
  268. // fill with 0s if non-looping sample that ended prematurely
  269. if (p < end) {
  270. env_state = STATE_IDLE;
  271. env_count = 0;
  272. while (p < end) *p++ = 0;
  273. }
  274. // filling audio_block two samples at a time
  275. p = (uint32_t *)block->data;
  276. end = p + AUDIO_BLOCK_SAMPLES / 2;
  277. // the following code handles the volume envelope with the following state transitions controlled here:
  278. // STATE_DELAY -> STATE_ATTACK -> STATE_HOLD -> STATE_DECAY -> STATE_SUSTAIN or STATE_IDLE
  279. // STATE_RELEASE -> STATE_IDLE
  280. // When STATE_SUSTAIN is reached, it is held indefinitely.
  281. // Outside of this code, playNote() and playFrequency() will initially set STATE_DELAY, and stop()
  282. // is responsible for setting STATE_RELEASE which can occur during any state, except STATE_IDLE
  283. // State defintions:
  284. // idle - not playing (generally should never arrive here)
  285. // delay - full attenuation
  286. // attack - linear ramp from full attenuation to no attenuation
  287. // hold - no attenuation
  288. // decay - linear ramp down to a given level of attenuation (SUSTAIN_MULT)
  289. // sustain - constant attenuation at a given level (SUSTAIN_MULT)
  290. // release - linear ramp down from current attenuation level to full attenuation
  291. // Definitions of the states generally follow the SoundFont spec, with a major exception being that all
  292. // volume scaling is linear realtive to amplitude; this is correct with respect to the attack, but not
  293. // the correct implementation relative to the decay and release which should be scaling linearly relative
  294. // to centibels. Practically this means the decay and release happen too slowing intially, and too quick
  295. // near the end
  296. // other points of note are that one env_count corresponds to 1 second * ENVELOPE_PERIOD / AUDIO_SAMPLE_RATE_EXACT;
  297. // the ENVELOPE_PERIOD is the number of samples processed per iteration of the following loop
  298. while (p < end) {
  299. // note env_count == 0 is used as a trigger for state transition
  300. if (env_count <= 0) switch (env_state) {
  301. case STATE_DELAY:
  302. env_state = STATE_ATTACK;
  303. env_count = s->ATTACK_COUNT;
  304. env_incr = UNITY_GAIN / (env_count * ENVELOPE_PERIOD);
  305. PRINT_ENV(STATE_ATTACK);
  306. continue;
  307. case STATE_ATTACK:
  308. env_mult = UNITY_GAIN;
  309. env_state = STATE_HOLD;
  310. env_count = s->HOLD_COUNT;
  311. env_incr = 0;
  312. PRINT_ENV(STATE_HOLD);
  313. continue;
  314. case STATE_HOLD:
  315. env_state = STATE_DECAY;
  316. env_count = s->DECAY_COUNT;
  317. env_incr = (-s->SUSTAIN_MULT) / (env_count * ENVELOPE_PERIOD);
  318. PRINT_ENV(STATE_DECAY);
  319. continue;
  320. case STATE_DECAY:
  321. env_mult = UNITY_GAIN - s->SUSTAIN_MULT;
  322. // UINT16_MAX is a value approximately corresponding to the -100 dBFS defined in the SoundFont spec as full attenuation
  323. // hence this comparison either sends the state to indefinite STATE_SUSTAIN, or immediately into STATE_RELEASE -> STATE_IDLE
  324. env_state = env_mult < UNITY_GAIN / UINT16_MAX ? STATE_RELEASE : STATE_SUSTAIN;
  325. env_incr = 0;
  326. continue;
  327. case STATE_SUSTAIN:
  328. env_count = INT32_MAX;
  329. PRINT_ENV(STATE_SUSTAIN);
  330. continue;
  331. case STATE_RELEASE:
  332. env_state = STATE_IDLE;
  333. for (; p < end; ++p) *p = 0;
  334. PRINT_ENV(STATE_IDLE);
  335. continue;
  336. default:
  337. p = end;
  338. PRINT_ENV(DEFAULT);
  339. continue;
  340. }
  341. env_mult += env_incr;
  342. // env_mult is INT32_MAX at max (i.e. 31-bits), so shift << 1 so result is aligned with high halfword of tmp1/tmp2
  343. tmp1 = signed_multiply_32x16b(env_mult, p[0]) << 1;
  344. env_mult += env_incr;
  345. tmp2 = signed_multiply_32x16t(env_mult, p[0]) << 1;
  346. // pack from high halfword of tmp1, tmp2
  347. p[0] = pack_16t_16t(tmp2, tmp1);
  348. env_mult += env_incr;
  349. tmp1 = signed_multiply_32x16b(env_mult, p[1]) << 1;
  350. env_mult += env_incr;
  351. tmp2 = signed_multiply_32x16t(env_mult, p[1]) << 1;
  352. p[1] = pack_16t_16t(tmp2, tmp1);
  353. env_mult += env_incr;
  354. tmp1 = signed_multiply_32x16b(env_mult, p[2]) << 1;
  355. env_mult += env_incr;
  356. tmp2 = signed_multiply_32x16t(env_mult, p[2]) << 1;
  357. p[2] = pack_16t_16t(tmp2, tmp1);
  358. env_mult += env_incr;
  359. tmp1 = signed_multiply_32x16b(env_mult, p[3]) << 1;
  360. env_mult += env_incr;
  361. tmp2 = signed_multiply_32x16t(env_mult, p[3]) << 1;
  362. p[3] = pack_16t_16t(tmp2, tmp1);
  363. p += ENVELOPE_PERIOD / 2;
  364. env_count--;
  365. }
  366. // copy state back, unless there was a state change
  367. if (this->state_change == false) {
  368. this->tone_phase = tone_phase;
  369. this->env_state = env_state;
  370. this->env_count = env_count;
  371. this->env_mult = env_mult;
  372. this->env_incr = env_incr;
  373. if (this->env_state != STATE_IDLE) {
  374. this->vib_count = vib_count;
  375. this->vib_phase = vib_phase;
  376. this->mod_count = mod_count;
  377. this->mod_phase = mod_phase;
  378. }
  379. else {
  380. this->vib_count = this->mod_count = 0;
  381. this->vib_phase = this->mod_phase = TRIANGLE_INITIAL_PHASE;
  382. }
  383. }
  384. transmit(block);
  385. release(block);
  386. #endif
  387. }