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.

696 lines
16KB

  1. #include "AudioStream.h"
  2. #include "arm_math.h"
  3. // When changing multiple audio object settings that must update at
  4. // the same time, these functions allow the audio library interrupt
  5. // to be disabled. For example, you may wish to begin playing a note
  6. // in response to reading an analog sensor. If you have "velocity"
  7. // information, you might start the sample playing and also adjust
  8. // the gain of a mixer channel. Use AudioNoInterrupts() first, then
  9. // make both changes to the 2 separate objects. Then allow the audio
  10. // library to update with AudioInterrupts(). Both changes will happen
  11. // at the same time, because AudioNoInterrupts() prevents any updates
  12. // while you make changes.
  13. #define AudioNoInterrupts() (NVIC_DISABLE_IRQ(IRQ_SOFTWARE))
  14. #define AudioInterrupts() (NVIC_ENABLE_IRQ(IRQ_SOFTWARE))
  15. // waveforms.c
  16. extern "C" {
  17. extern const int16_t AudioWaveformSine[257];
  18. extern const int16_t AudioWaveformTriangle[257];
  19. extern const int16_t AudioWaveformSquare[257];
  20. extern const int16_t AudioWaveformSawtooth[257];
  21. }
  22. // windows.c
  23. extern "C" {
  24. extern const int16_t AudioWindowHanning256[];
  25. extern const int16_t AudioWindowBartlett256[];
  26. extern const int16_t AudioWindowBlackman256[];
  27. extern const int16_t AudioWindowFlattop256[];
  28. extern const int16_t AudioWindowBlackmanHarris256[];
  29. extern const int16_t AudioWindowNuttall256[];
  30. extern const int16_t AudioWindowBlackmanNuttall256[];
  31. extern const int16_t AudioWindowWelch256[];
  32. extern const int16_t AudioWindowHamming256[];
  33. extern const int16_t AudioWindowCosine256[];
  34. extern const int16_t AudioWindowTukey256[];
  35. }
  36. class AudioAnalyzeFFT256 : public AudioStream
  37. {
  38. public:
  39. AudioAnalyzeFFT256(uint8_t navg = 8, const int16_t *win = AudioWindowHanning256)
  40. : AudioStream(1, inputQueueArray), outputflag(false),
  41. prevblock(NULL), count(0), naverage(navg), window(win) { init(); }
  42. bool available() {
  43. if (outputflag == true) {
  44. outputflag = false;
  45. return true;
  46. }
  47. return false;
  48. }
  49. virtual void update(void);
  50. //uint32_t cycles;
  51. int32_t output[128] __attribute__ ((aligned (4)));
  52. private:
  53. void init(void);
  54. const int16_t *window;
  55. audio_block_t *prevblock;
  56. int16_t buffer[512] __attribute__ ((aligned (4)));
  57. uint8_t count;
  58. uint8_t naverage;
  59. bool outputflag;
  60. audio_block_t *inputQueueArray[1];
  61. };
  62. class AudioSynthWaveform : public AudioStream
  63. {
  64. public:
  65. AudioSynthWaveform(const int16_t *waveform)
  66. : AudioStream(0, NULL), wavetable(waveform), magnitude(0), phase(0)
  67. , ramp_up(0), ramp_down(0), ramp_mag(0), ramp_length(0)
  68. { }
  69. void frequency(float freq) {
  70. if (freq > AUDIO_SAMPLE_RATE_EXACT / 2 || freq < 0.0) return;
  71. phase_increment = (freq / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  72. }
  73. void amplitude(float n) { // 0 to 1.0
  74. if (n < 0) n = 0;
  75. else if (n > 1.0) n = 1.0;
  76. // Ramp code
  77. if(magnitude && (n == 0)) {
  78. ramp_down = ramp_length;
  79. ramp_up = 0;
  80. last_magnitude = magnitude;
  81. }
  82. else if((magnitude == 0) && n) {
  83. ramp_up = ramp_length;
  84. ramp_down = 0;
  85. }
  86. // set new magnitude
  87. magnitude = n * 32767.0;
  88. }
  89. virtual void update(void);
  90. void set_ramp_length(uint16_t r_length);
  91. private:
  92. const int16_t *wavetable;
  93. uint16_t magnitude;
  94. uint16_t last_magnitude;
  95. uint32_t phase;
  96. uint32_t phase_increment;
  97. uint32_t ramp_down;
  98. uint32_t ramp_up;
  99. uint32_t ramp_mag;
  100. uint16_t ramp_length;
  101. };
  102. #if 0
  103. class AudioSineWaveMod : public AudioStream
  104. {
  105. public:
  106. AudioSineWaveMod() : AudioStream(1, inputQueueArray) {}
  107. void frequency(float freq);
  108. //void amplitude(q15 n);
  109. virtual void update(void);
  110. private:
  111. uint32_t phase;
  112. uint32_t phase_increment;
  113. uint32_t modulation_factor;
  114. audio_block_t *inputQueueArray[1];
  115. };
  116. #endif
  117. class AudioOutputPWM : public AudioStream
  118. {
  119. public:
  120. AudioOutputPWM(void) : AudioStream(1, inputQueueArray) { begin(); }
  121. virtual void update(void);
  122. void begin(void);
  123. friend void dma_ch3_isr(void);
  124. private:
  125. static audio_block_t *block_1st;
  126. static audio_block_t *block_2nd;
  127. static uint32_t block_offset;
  128. static bool update_responsibility;
  129. static uint8_t interrupt_count;
  130. audio_block_t *inputQueueArray[1];
  131. };
  132. class AudioOutputAnalog : public AudioStream
  133. {
  134. public:
  135. AudioOutputAnalog(void) : AudioStream(1, inputQueueArray) { begin(); }
  136. virtual void update(void);
  137. void begin(void);
  138. void analogReference(int ref);
  139. friend void dma_ch4_isr(void);
  140. private:
  141. static audio_block_t *block_left_1st;
  142. static audio_block_t *block_left_2nd;
  143. static bool update_responsibility;
  144. audio_block_t *inputQueueArray[1];
  145. };
  146. class AudioPrint : public AudioStream
  147. {
  148. public:
  149. AudioPrint(const char *str) : AudioStream(1, inputQueueArray), name(str) {}
  150. virtual void update(void);
  151. private:
  152. const char *name;
  153. audio_block_t *inputQueueArray[1];
  154. };
  155. class AudioInputI2S : public AudioStream
  156. {
  157. public:
  158. AudioInputI2S(void) : AudioStream(0, NULL) { begin(); }
  159. virtual void update(void);
  160. void begin(void);
  161. friend void dma_ch1_isr(void);
  162. protected:
  163. AudioInputI2S(int dummy): AudioStream(0, NULL) {} // to be used only inside AudioInputI2Sslave !!
  164. static bool update_responsibility;
  165. private:
  166. static audio_block_t *block_left;
  167. static audio_block_t *block_right;
  168. static uint16_t block_offset;
  169. };
  170. class AudioOutputI2S : public AudioStream
  171. {
  172. public:
  173. AudioOutputI2S(void) : AudioStream(2, inputQueueArray) { begin(); }
  174. virtual void update(void);
  175. void begin(void);
  176. friend void dma_ch0_isr(void);
  177. friend class AudioInputI2S;
  178. protected:
  179. AudioOutputI2S(int dummy): AudioStream(2, inputQueueArray) {} // to be used only inside AudioOutputI2Sslave !!
  180. static void config_i2s(void);
  181. static audio_block_t *block_left_1st;
  182. static audio_block_t *block_right_1st;
  183. static bool update_responsibility;
  184. private:
  185. static audio_block_t *block_left_2nd;
  186. static audio_block_t *block_right_2nd;
  187. static uint16_t block_left_offset;
  188. static uint16_t block_right_offset;
  189. audio_block_t *inputQueueArray[2];
  190. };
  191. class AudioInputI2Sslave : public AudioInputI2S
  192. {
  193. public:
  194. AudioInputI2Sslave(void) : AudioInputI2S(0) { begin(); }
  195. void begin(void);
  196. friend void dma_ch1_isr(void);
  197. };
  198. class AudioOutputI2Sslave : public AudioOutputI2S
  199. {
  200. public:
  201. AudioOutputI2Sslave(void) : AudioOutputI2S(0) { begin(); } ;
  202. void begin(void);
  203. friend class AudioInputI2Sslave;
  204. friend void dma_ch0_isr(void);
  205. protected:
  206. static void config_i2s(void);
  207. };
  208. class AudioInputAnalog : public AudioStream
  209. {
  210. public:
  211. AudioInputAnalog(unsigned int pin) : AudioStream(0, NULL) { begin(pin); }
  212. virtual void update(void);
  213. void begin(unsigned int pin);
  214. friend void dma_ch2_isr(void);
  215. private:
  216. static audio_block_t *block_left;
  217. static uint16_t block_offset;
  218. uint16_t dc_average;
  219. static bool update_responsibility;
  220. };
  221. #include "SD.h"
  222. class AudioPlaySDcardWAV : public AudioStream
  223. {
  224. public:
  225. AudioPlaySDcardWAV(void) : AudioStream(0, NULL) { begin(); }
  226. void begin(void);
  227. bool play(const char *filename);
  228. void stop(void);
  229. bool start(void);
  230. virtual void update(void);
  231. private:
  232. File wavfile;
  233. bool consume(void);
  234. bool parse_format(void);
  235. uint32_t header[5];
  236. uint32_t data_length; // number of bytes remaining in data section
  237. audio_block_t *block_left;
  238. audio_block_t *block_right;
  239. uint16_t block_offset;
  240. uint8_t buffer[512];
  241. uint16_t buffer_remaining;
  242. uint8_t state;
  243. uint8_t state_play;
  244. uint8_t leftover_bytes;
  245. };
  246. class AudioPlaySDcardRAW : public AudioStream
  247. {
  248. public:
  249. AudioPlaySDcardRAW(void) : AudioStream(0, NULL) { begin(); }
  250. void begin(void);
  251. bool play(const char *filename);
  252. void stop(void);
  253. virtual void update(void);
  254. private:
  255. File rawfile;
  256. audio_block_t *block;
  257. bool playing;
  258. bool paused;
  259. };
  260. class AudioPlayMemory : public AudioStream
  261. {
  262. public:
  263. AudioPlayMemory(void) : AudioStream(0, NULL), playing(0) { }
  264. void play(const unsigned int *data);
  265. void stop(void);
  266. virtual void update(void);
  267. private:
  268. const unsigned int *next;
  269. uint32_t length;
  270. int16_t prior;
  271. volatile uint8_t playing;
  272. };
  273. class AudioMixer4 : public AudioStream
  274. {
  275. public:
  276. AudioMixer4(void) : AudioStream(4, inputQueueArray) {
  277. for (int i=0; i<4; i++) multiplier[i] = 65536;
  278. }
  279. virtual void update(void);
  280. void gain(unsigned int channel, float gain) {
  281. if (channel >= 4) return;
  282. if (gain > 32767.0f) gain = 32767.0f;
  283. else if (gain < 0.0f) gain = 0.0f;
  284. multiplier[channel] = gain * 65536.0f; // TODO: proper roundoff?
  285. }
  286. private:
  287. int32_t multiplier[4];
  288. audio_block_t *inputQueueArray[4];
  289. };
  290. class AudioFilterBiquad : public AudioStream
  291. {
  292. public:
  293. AudioFilterBiquad(int *parameters)
  294. : AudioStream(1, inputQueueArray), definition(parameters) { }
  295. virtual void update(void);
  296. private:
  297. int *definition;
  298. audio_block_t *inputQueueArray[1];
  299. };
  300. class AudioEffectFade : public AudioStream
  301. {
  302. public:
  303. AudioEffectFade(void)
  304. : AudioStream(1, inputQueueArray), position(0xFFFFFFFF) {}
  305. void fadeIn(uint32_t milliseconds) {
  306. uint32_t samples = (uint32_t)(milliseconds * 441u + 5u) / 10u;
  307. //Serial.printf("fadeIn, %u samples\n", samples);
  308. fadeBegin(0xFFFFFFFFu / samples, 1);
  309. }
  310. void fadeOut(uint32_t milliseconds) {
  311. uint32_t samples = (uint32_t)(milliseconds * 441u + 5u) / 10u;
  312. //Serial.printf("fadeOut, %u samples\n", samples);
  313. fadeBegin(0xFFFFFFFFu / samples, 0);
  314. }
  315. virtual void update(void);
  316. private:
  317. void fadeBegin(uint32_t newrate, uint8_t dir);
  318. uint32_t position; // 0 = off, 0xFFFFFFFF = on
  319. uint32_t rate;
  320. uint8_t direction; // 0 = fading out, 1 = fading in
  321. audio_block_t *inputQueueArray[1];
  322. };
  323. class AudioAnalyzeToneDetect : public AudioStream
  324. {
  325. public:
  326. AudioAnalyzeToneDetect(void)
  327. : AudioStream(1, inputQueueArray), thresh(6554), enabled(false) { }
  328. void frequency(float freq, uint16_t cycles=10) {
  329. set_params((int32_t)(cos((double)freq
  330. * (2.0 * 3.14159265358979323846 / AUDIO_SAMPLE_RATE_EXACT))
  331. * (double)2147483647.999), cycles,
  332. (float)AUDIO_SAMPLE_RATE_EXACT / freq * (float)cycles + 0.5f);
  333. }
  334. void set_params(int32_t coef, uint16_t cycles, uint16_t len);
  335. bool available(void) {
  336. __disable_irq();
  337. bool flag = new_output;
  338. if (flag) new_output = false;
  339. __enable_irq();
  340. return flag;
  341. }
  342. float read(void);
  343. void threshold(float level) {
  344. if (level < 0.01f) thresh = 655;
  345. else if (level > 0.99f) thresh = 64881;
  346. else thresh = level * 65536.0f + 0.5f;
  347. }
  348. operator bool(); // true if at or above threshold, false if below
  349. virtual void update(void);
  350. private:
  351. int32_t coefficient; // Goertzel algorithm coefficient
  352. int32_t s1, s2; // Goertzel algorithm state
  353. int32_t out1, out2; // Goertzel algorithm state output
  354. uint16_t length; // number of samples to analyze
  355. uint16_t count; // how many left to analyze
  356. uint16_t ncycles; // number of waveform cycles to seek
  357. uint16_t thresh; // threshold, 655 to 64881 (1% to 99%)
  358. bool enabled;
  359. volatile bool new_output;
  360. audio_block_t *inputQueueArray[1];
  361. };
  362. // TODO: more audio processing objects....
  363. // sine wave with frequency modulation (phase)
  364. // waveforms with bandwidth limited tables for synth
  365. // envelope: attack-decay-sustain-release, maybe other more complex?
  366. // MP3 decoding - it is possible with optimized code?
  367. // other decompression, ADPCM, Vorbis, Speex, etc?
  368. // A base class for all Codecs, DACs and ADCs, so at least the
  369. // most basic functionality is consistent.
  370. #define AUDIO_INPUT_LINEIN 0
  371. #define AUDIO_INPUT_MIC 1
  372. class AudioControl
  373. {
  374. public:
  375. virtual bool enable(void) = 0;
  376. virtual bool disable(void) = 0;
  377. virtual bool volume(float volume) = 0; // volume 0.0 to 100.0
  378. virtual bool inputLevel(float volume) = 0; // volume 0.0 to 100.0
  379. virtual bool inputSelect(int n) = 0;
  380. };
  381. class AudioControlWM8731 : public AudioControl
  382. {
  383. public:
  384. bool enable(void);
  385. bool disable(void) { return false; }
  386. bool volume(float n) { return volumeInteger(n * 0.8 + 47.499); }
  387. bool inputLevel(float n) { return false; }
  388. bool inputSelect(int n) { return false; }
  389. protected:
  390. bool write(unsigned int reg, unsigned int val);
  391. bool volumeInteger(unsigned int n); // range: 0x2F to 0x7F
  392. };
  393. class AudioControlWM8731master : public AudioControlWM8731
  394. {
  395. public:
  396. bool enable(void);
  397. };
  398. class AudioControlSGTL5000 : public AudioControl
  399. {
  400. public:
  401. bool enable(void);
  402. bool disable(void) { return false; }
  403. bool volume(float n) { return volumeInteger(n * 1.29 + 0.499); }
  404. bool inputLevel(float n) {return false;}
  405. bool muteHeadphone(void) { return write(0x0024, ana_ctrl | (1<<4)); }
  406. bool unmuteHeadphone(void) { return write(0x0024, ana_ctrl & ~(1<<4)); }
  407. bool muteLineout(void) { return write(0x0024, ana_ctrl | (1<<8)); }
  408. bool unmuteLineout(void) { return write(0x0024, ana_ctrl & ~(1<<8)); }
  409. bool inputSelect(int n) {
  410. if (n == AUDIO_INPUT_LINEIN) {
  411. return write(0x0024, ana_ctrl | (1<<2));
  412. } else if (n == AUDIO_INPUT_MIC) {
  413. //return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2));
  414. return write(0x002A, 0x0173) && write(0x0024, ana_ctrl & ~(1<<2)); // +40dB
  415. } else {
  416. return false;
  417. }
  418. }
  419. //bool inputLinein(void) { return write(0x0024, ana_ctrl | (1<<2)); }
  420. //bool inputMic(void) { return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2)); }
  421. protected:
  422. bool muted;
  423. bool volumeInteger(unsigned int n); // range: 0x00 to 0x80
  424. uint16_t ana_ctrl;
  425. unsigned int read(unsigned int reg);
  426. bool write(unsigned int reg, unsigned int val);
  427. };
  428. /******************************************************************/
  429. // Maximum number of coefficients in a FIR filter
  430. // The audio breaks up with 128 coefficients so a
  431. // maximum of 150 is more than sufficient
  432. #define MAX_COEFFS 150
  433. // Indicates that the code should just pass through the audio
  434. // without any filtering (as opposed to doing nothing at all)
  435. #define FIR_PASSTHRU ((short *) 1)
  436. class AudioFilterFIR :
  437. public AudioStream
  438. {
  439. public:
  440. AudioFilterFIR(void):
  441. AudioStream(2,inputQueueArray), coeff_p(NULL)
  442. {
  443. }
  444. void begin(short *coeff_p,int f_pin);
  445. virtual void update(void);
  446. void stop(void);
  447. private:
  448. audio_block_t *inputQueueArray[2];
  449. // arm state arrays and FIR instances for left and right channels
  450. // the state arrays are defined to handle a maximum of MAX_COEFFS
  451. // coefficients in a filter
  452. q15_t l_StateQ15[AUDIO_BLOCK_SAMPLES + MAX_COEFFS];
  453. q15_t r_StateQ15[AUDIO_BLOCK_SAMPLES + MAX_COEFFS];
  454. arm_fir_instance_q15 l_fir_inst;
  455. arm_fir_instance_q15 r_fir_inst;
  456. // pointer to current coefficients or NULL or FIR_PASSTHRU
  457. short *coeff_p;
  458. };
  459. /******************************************************************/
  460. // A u d i o E f f e c t F l a n g e
  461. // Written by Pete (El Supremo) Jan 2014
  462. #define DELAY_PASSTHRU 0
  463. class AudioEffectFlange :
  464. public AudioStream
  465. {
  466. public:
  467. AudioEffectFlange(void):
  468. AudioStream(2,inputQueueArray) {
  469. }
  470. boolean begin(short *delayline,int d_length,int delay_offset,int d_depth,float delay_rate);
  471. boolean modify(int delay_offset,int d_depth,float delay_rate);
  472. virtual void update(void);
  473. void stop(void);
  474. private:
  475. audio_block_t *inputQueueArray[2];
  476. static short *l_delayline;
  477. static short *r_delayline;
  478. static int delay_length;
  479. static short l_circ_idx;
  480. static short r_circ_idx;
  481. static int delay_depth;
  482. static int delay_offset_idx;
  483. static int delay_rate_incr;
  484. static unsigned int l_delay_rate_index;
  485. static unsigned int r_delay_rate_index;
  486. };
  487. /******************************************************************/
  488. // A u d i o E f f e c t C h o r u s
  489. // Written by Pete (El Supremo) Jan 2014
  490. #define DELAY_PASSTHRU -1
  491. class AudioEffectChorus :
  492. public AudioStream
  493. {
  494. public:
  495. AudioEffectChorus(void):
  496. AudioStream(2,inputQueueArray) {
  497. }
  498. boolean begin(short *delayline,int delay_length,int n_chorus);
  499. virtual void update(void);
  500. void stop(void);
  501. void modify(int n_chorus);
  502. private:
  503. audio_block_t *inputQueueArray[2];
  504. static short *l_delayline;
  505. static short *r_delayline;
  506. static short l_circ_idx;
  507. static short r_circ_idx;
  508. static int num_chorus;
  509. static int delay_length;
  510. };
  511. /******************************************************************/
  512. // A u d i o T o n e S w e e p
  513. // Written by Pete (El Supremo) Feb 2014
  514. class AudioToneSweep : public AudioStream
  515. {
  516. public:
  517. AudioToneSweep(void) :
  518. AudioStream(0,NULL), sweep_busy(0)
  519. { }
  520. boolean begin(short t_amp,int t_lo,int t_hi,float t_time);
  521. virtual void update(void);
  522. unsigned char busy(void);
  523. private:
  524. short tone_amp;
  525. int tone_lo;
  526. int tone_hi;
  527. uint64_t tone_freq;
  528. uint64_t tone_phase;
  529. uint64_t tone_incr;
  530. int tone_sign;
  531. unsigned char sweep_busy;
  532. };