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.

206 lines
6.4KB

  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. #define UNITY_GAIN INT32_MAX // Max amplitude / no attenuation
  32. #define DEFAULT_AMPLITUDE 90
  33. #define SAMPLES_PER_MSEC (AUDIO_SAMPLE_RATE_EXACT/1000.0)
  34. #define TRIANGLE_INITIAL_PHASE (-0x40000000)
  35. #define LFO_SMOOTHNESS 3
  36. #define LFO_PERIOD (AUDIO_BLOCK_SAMPLES/(1 << (LFO_SMOOTHNESS-1)))
  37. #define ENVELOPE_PERIOD 8
  38. enum envelopeStateEnum { STATE_IDLE, STATE_DELAY, STATE_ATTACK, STATE_HOLD, STATE_DECAY, STATE_SUSTAIN, STATE_RELEASE };
  39. #define CENTS_SHIFT(C) (pow(2.0, C/1200.0))
  40. #define NOTE(N) (440.0 * pow(2.0, (N - 69) / 12.0))
  41. #define DECIBEL_SHIFT(dB) (pow(10.0, dB/20.0))
  42. // TODO: move all this stuff into the AudioSynthWavetable class, not global scope
  43. struct sample_data {
  44. // SAMPLE VALUES
  45. const int16_t* sample;
  46. const bool LOOP;
  47. const int INDEX_BITS;
  48. const float PER_HERTZ_PHASE_INCREMENT;
  49. const uint32_t MAX_PHASE;
  50. const uint32_t LOOP_PHASE_END;
  51. const uint32_t LOOP_PHASE_LENGTH;
  52. const uint16_t INITIAL_ATTENUATION_SCALAR;
  53. // VOLUME ENVELOPE VALUES
  54. const uint32_t DELAY_COUNT;
  55. const uint32_t ATTACK_COUNT;
  56. const uint32_t HOLD_COUNT;
  57. const uint32_t DECAY_COUNT;
  58. const uint32_t RELEASE_COUNT;
  59. const int32_t SUSTAIN_MULT;
  60. // VIRBRATO VALUES
  61. const uint32_t VIBRATO_DELAY;
  62. const uint32_t VIBRATO_INCREMENT;
  63. const float VIBRATO_PITCH_COEFFICIENT_INITIAL;
  64. const float VIBRATO_PITCH_COEFFICIENT_SECOND;
  65. // MODULATION VALUES
  66. const uint32_t MODULATION_DELAY;
  67. const uint32_t MODULATION_INCREMENT;
  68. const float MODULATION_PITCH_COEFFICIENT_INITIAL;
  69. const float MODULATION_PITCH_COEFFICIENT_SECOND;
  70. const int32_t MODULATION_AMPLITUDE_INITIAL_GAIN;
  71. const int32_t MODULATION_AMPLITUDE_SECOND_GAIN;
  72. };
  73. struct instrument_data {
  74. const uint8_t sample_count;
  75. const uint8_t* sample_note_ranges;
  76. const sample_data* samples;
  77. };
  78. class AudioSynthWavetable : public AudioStream
  79. {
  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. void playFrequency(float freq, int amp = DEFAULT_AMPLITUDE);
  150. void playNote(int note, int amp = DEFAULT_AMPLITUDE);
  151. bool isPlaying(void) { return env_state != STATE_IDLE; }
  152. void setFrequency(float freq);
  153. virtual void update(void);
  154. envelopeStateEnum getEnvState(void) { return env_state; }
  155. private:
  156. void setState(int note, int amp, float freq);
  157. volatile bool state_change = false;
  158. volatile const instrument_data* instrument = NULL;
  159. volatile const sample_data* current_sample = NULL;
  160. //sample output state
  161. volatile uint32_t tone_phase = 0;
  162. volatile uint32_t tone_incr = 0;
  163. volatile uint16_t tone_amp = 0;
  164. //volume environment state
  165. volatile envelopeStateEnum env_state = STATE_IDLE;
  166. volatile int32_t env_count = 0;
  167. volatile int32_t env_mult = 0;
  168. volatile int32_t env_incr = 0;
  169. //vibrato LFO state
  170. volatile uint32_t vib_count = 0;
  171. volatile uint32_t vib_phase = 0;
  172. volatile int32_t vib_pitch_offset_init = 0;
  173. volatile int32_t vib_pitch_offset_scnd = 0;
  174. //modulation LFO state
  175. volatile uint32_t mod_count = 0;
  176. volatile uint32_t mod_phase = TRIANGLE_INITIAL_PHASE;
  177. volatile int32_t mod_pitch_offset_init = 0;
  178. volatile int32_t mod_pitch_offset_scnd = 0;
  179. };