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.

synth_wavetable.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #pragma once
  27. #include <core/Arduino.h>
  28. #include <core/AudioStream.h>
  29. #include <math.h>
  30. #include <stdint.h>
  31. #define WAVETABLE_CENTS_SHIFT(C) (pow(2.0, (C)/1200.0))
  32. #define WAVETABLE_NOTE_TO_FREQUENCY(N) (440.0 * pow(2.0, ((N) - 69) / 12.0))
  33. #define WAVETABLE_DECIBEL_SHIFT(dB) (pow(10.0, (dB)/20.0))
  34. class AudioSynthWavetable : public AudioStream
  35. {
  36. public:
  37. struct sample_data {
  38. // SAMPLE VALUES
  39. const int16_t* sample;
  40. const bool LOOP;
  41. const int INDEX_BITS;
  42. const float PER_HERTZ_PHASE_INCREMENT;
  43. const uint32_t MAX_PHASE;
  44. const uint32_t LOOP_PHASE_END;
  45. const uint32_t LOOP_PHASE_LENGTH;
  46. const uint16_t INITIAL_ATTENUATION_SCALAR;
  47. // VOLUME ENVELOPE VALUES
  48. const uint32_t DELAY_COUNT;
  49. const uint32_t ATTACK_COUNT;
  50. const uint32_t HOLD_COUNT;
  51. const uint32_t DECAY_COUNT;
  52. const uint32_t RELEASE_COUNT;
  53. const int32_t SUSTAIN_MULT;
  54. // VIRBRATO VALUES
  55. const uint32_t VIBRATO_DELAY;
  56. const uint32_t VIBRATO_INCREMENT;
  57. const float VIBRATO_PITCH_COEFFICIENT_INITIAL;
  58. const float VIBRATO_PITCH_COEFFICIENT_SECOND;
  59. // MODULATION VALUES
  60. const uint32_t MODULATION_DELAY;
  61. const uint32_t MODULATION_INCREMENT;
  62. const float MODULATION_PITCH_COEFFICIENT_INITIAL;
  63. const float MODULATION_PITCH_COEFFICIENT_SECOND;
  64. const int32_t MODULATION_AMPLITUDE_INITIAL_GAIN;
  65. const int32_t MODULATION_AMPLITUDE_SECOND_GAIN;
  66. };
  67. static const int32_t UNITY_GAIN = INT32_MAX;
  68. static constexpr float SAMPLES_PER_MSEC = (AUDIO_SAMPLE_RATE_EXACT/1000.0);
  69. static const int32_t LFO_SMOOTHNESS = 3;
  70. static constexpr float LFO_PERIOD = (AUDIO_BLOCK_SAMPLES/(1 << (LFO_SMOOTHNESS-1)));
  71. static const int32_t ENVELOPE_PERIOD = 8;
  72. struct instrument_data {
  73. const uint8_t sample_count;
  74. const uint8_t* sample_note_ranges;
  75. const sample_data* samples;
  76. };
  77. enum { DEFAULT_AMPLITUDE = 90 };
  78. enum { TRIANGLE_INITIAL_PHASE = -0x40000000 };
  79. enum envelopeStateEnum { STATE_IDLE, STATE_DELAY, STATE_ATTACK, STATE_HOLD, STATE_DECAY, STATE_SUSTAIN, STATE_RELEASE };
  80. public:
  81. /**
  82. * Class constructor.
  83. */
  84. AudioSynthWavetable(void) : AudioStream(0, NULL) {}
  85. /**
  86. * @brief Set the instrument_data struct to be used as the playback instrument.
  87. *
  88. * A wavetable uses a set of samples to generate sound.
  89. * This function is used to set the instrument samples.
  90. * @param instrument a struct of type instrument_data, commonly prodced from a
  91. * decoded SoundFont file using the SoundFont Decoder Script which accompanies this library.
  92. */
  93. void setInstrument(const instrument_data& instrument) {
  94. cli();
  95. this->instrument = &instrument;
  96. current_sample = NULL;
  97. env_state = STATE_IDLE;
  98. state_change = true;
  99. sei();
  100. }
  101. /**
  102. * @brief Changes the amplitude to 'v'
  103. *
  104. * A value of 0 will set the synth output to minimum amplitude
  105. * (i.e., no output). A value of 1 will set the output to the
  106. * maximum amplitude. Amplitude is set linearly with intermediate
  107. * values.
  108. * @param v a value between 0.0 and 1.0
  109. */
  110. void amplitude(float v) {
  111. v = (v < 0.0) ? 0.0 : (v > 1.0) ? 1.0 : v;
  112. tone_amp = (uint16_t)(UINT16_MAX*v);
  113. }
  114. /**
  115. * @brief Scale midi_amp to a value between 0.0 and 1.0
  116. * using a logarithmic tranformation.
  117. *
  118. * @param midi_amp a value between 0 and 127
  119. * @return a value between 0.0 to 1.0
  120. */
  121. static float midi_volume_transform(int midi_amp) {
  122. // scale midi_amp which is 0 t0 127 to be between
  123. // 0 and 1 using a logarithmic transformation
  124. return powf(midi_amp / 127.0, 4);
  125. }
  126. /**
  127. * @brief Convert a MIDI note value to
  128. * its corresponding frequency.
  129. *
  130. * @param note a value between 0 and 127
  131. * @return a frequency
  132. */
  133. static float noteToFreq(int note) {
  134. float exp = note * (1.0 / 12.0) + 3.0313597;
  135. return powf(2.0, exp);
  136. }
  137. /**
  138. * @brief Convert a frequency to the corressponding
  139. * MIDI note value.
  140. *
  141. * @param freq the frequency value as a float to convert
  142. * @return a MIDI note (between 0 - 127)
  143. */
  144. static int freqToNote(float freq) {
  145. return 12*log2f(freq) - 35.8763164;
  146. }
  147. // Defined in AudioSynthWavetable.cpp
  148. void stop(void);
  149. // TODO: amplitude should be 0 to 1.0 scale
  150. void playFrequency(float freq, int amp = DEFAULT_AMPLITUDE);
  151. void playNote(int note, int amp = DEFAULT_AMPLITUDE);
  152. bool isPlaying(void) { return env_state != STATE_IDLE; }
  153. void setFrequency(float freq);
  154. virtual void update(void);
  155. envelopeStateEnum getEnvState(void) { return env_state; }
  156. private:
  157. void setState(int note, int amp, float freq);
  158. volatile bool state_change = false;
  159. volatile const instrument_data* instrument = NULL;
  160. volatile const sample_data* current_sample = NULL;
  161. //sample output state
  162. volatile uint32_t tone_phase = 0;
  163. volatile uint32_t tone_incr = 0;
  164. volatile uint16_t tone_amp = 0;
  165. //volume environment state
  166. volatile envelopeStateEnum env_state = STATE_IDLE;
  167. volatile int32_t env_count = 0;
  168. volatile int32_t env_mult = 0;
  169. volatile int32_t env_incr = 0;
  170. //vibrato LFO state
  171. volatile uint32_t vib_count = 0;
  172. volatile uint32_t vib_phase = 0;
  173. volatile int32_t vib_pitch_offset_init = 0;
  174. volatile int32_t vib_pitch_offset_scnd = 0;
  175. //modulation LFO state
  176. volatile uint32_t mod_count = 0;
  177. volatile uint32_t mod_phase = TRIANGLE_INITIAL_PHASE;
  178. volatile int32_t mod_pitch_offset_init = 0;
  179. volatile int32_t mod_pitch_offset_scnd = 0;
  180. };