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.

207 line
6.5KB

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