No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

770 líneas
18KB

  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. #ifdef ORIGINAL_AUDIOSYNTHWAVEFORM
  63. class AudioSynthWaveform : public AudioStream
  64. {
  65. public:
  66. AudioSynthWaveform(const int16_t *waveform)
  67. : AudioStream(0, NULL), wavetable(waveform), magnitude(0), phase(0)
  68. , ramp_up(0), ramp_down(0), ramp_mag(0), ramp_length(0)
  69. { }
  70. void frequency(float freq) {
  71. if (freq > AUDIO_SAMPLE_RATE_EXACT / 2 || freq < 0.0) return;
  72. phase_increment = (freq / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  73. }
  74. void amplitude(float n) { // 0 to 1.0
  75. if (n < 0) n = 0;
  76. else if (n > 1.0) n = 1.0;
  77. // Ramp code
  78. if(magnitude && (n == 0)) {
  79. ramp_down = ramp_length;
  80. ramp_up = 0;
  81. last_magnitude = magnitude;
  82. }
  83. else if((magnitude == 0) && n) {
  84. ramp_up = ramp_length;
  85. ramp_down = 0;
  86. }
  87. // set new magnitude
  88. magnitude = n * 32767.0;
  89. }
  90. virtual void update(void);
  91. void set_ramp_length(uint16_t r_length);
  92. private:
  93. const int16_t *wavetable;
  94. uint16_t magnitude;
  95. uint16_t last_magnitude;
  96. uint32_t phase;
  97. uint32_t phase_increment;
  98. uint32_t ramp_down;
  99. uint32_t ramp_up;
  100. uint32_t ramp_mag;
  101. uint16_t ramp_length;
  102. };
  103. #else
  104. #define AUDIO_SAMPLE_RATE_ROUNDED (44118)
  105. #define DELAY_PASSTHRU -1
  106. #define TONE_TYPE_SINE 0
  107. #define TONE_TYPE_SAWTOOTH 1
  108. #define TONE_TYPE_SQUARE 2
  109. #define TONE_TYPE_TRIANGLE 3
  110. class AudioSynthWaveform :
  111. public AudioStream
  112. {
  113. public:
  114. AudioSynthWaveform(void) :
  115. AudioStream(0,NULL), tone_amp(0), ramp_up(0), ramp_down(0),
  116. ramp_length(0), tone_phase(0), tone_incr(0),
  117. tone_type(0), tone_freq(0)
  118. {
  119. }
  120. // Change the frequency on-the-fly to permit a phase-continuous
  121. // change between two frequencies.
  122. void frequency(int t_hi)
  123. {
  124. tone_incr = (0x100000000LL*t_hi)/AUDIO_SAMPLE_RATE_EXACT;
  125. }
  126. // If ramp_length is non-zero this will set up
  127. // either a rmap up or a ramp down when a wave
  128. // first starts or when the amplitude is set
  129. // back to zero.
  130. // Note that if the ramp_length is N, the generated
  131. // wave will be N samples longer than when it is not
  132. // ramp
  133. void amplitude(float n) { // 0 to 1.0
  134. if (n < 0) n = 0;
  135. else if (n > 1.0) n = 1.0;
  136. // Ramp code
  137. if(tone_amp && (n == 0)) {
  138. ramp_down = ramp_length;
  139. ramp_up = 0;
  140. last_tone_amp = tone_amp;
  141. }
  142. else if((tone_amp == 0) && n) {
  143. ramp_up = ramp_length;
  144. ramp_down = 0;
  145. // reset the phase when the amplitude was zero
  146. // and has now been increased. Note that this
  147. // happens even if the wave is not ramped
  148. // so that the signal starts at zero
  149. tone_phase = 0;
  150. }
  151. // set new magnitude
  152. tone_amp = n * 32767.0;
  153. }
  154. boolean begin(float t_amp,int t_hi,short t_type);
  155. virtual void update(void);
  156. void set_ramp_length(uint16_t r_length);
  157. private:
  158. short tone_amp;
  159. short last_tone_amp;
  160. short tone_freq;
  161. uint32_t tone_phase;
  162. uint32_t tone_incr;
  163. short tone_type;
  164. uint32_t ramp_down;
  165. uint32_t ramp_up;
  166. uint16_t ramp_length;
  167. };
  168. #endif
  169. #if 0
  170. class AudioSineWaveMod : public AudioStream
  171. {
  172. public:
  173. AudioSineWaveMod() : AudioStream(1, inputQueueArray) {}
  174. void frequency(float freq);
  175. //void amplitude(q15 n);
  176. virtual void update(void);
  177. private:
  178. uint32_t phase;
  179. uint32_t phase_increment;
  180. uint32_t modulation_factor;
  181. audio_block_t *inputQueueArray[1];
  182. };
  183. #endif
  184. class AudioOutputPWM : public AudioStream
  185. {
  186. public:
  187. AudioOutputPWM(void) : AudioStream(1, inputQueueArray) { begin(); }
  188. virtual void update(void);
  189. void begin(void);
  190. friend void dma_ch3_isr(void);
  191. private:
  192. static audio_block_t *block_1st;
  193. static audio_block_t *block_2nd;
  194. static uint32_t block_offset;
  195. static bool update_responsibility;
  196. static uint8_t interrupt_count;
  197. audio_block_t *inputQueueArray[1];
  198. };
  199. class AudioOutputAnalog : public AudioStream
  200. {
  201. public:
  202. AudioOutputAnalog(void) : AudioStream(1, inputQueueArray) { begin(); }
  203. virtual void update(void);
  204. void begin(void);
  205. void analogReference(int ref);
  206. friend void dma_ch4_isr(void);
  207. private:
  208. static audio_block_t *block_left_1st;
  209. static audio_block_t *block_left_2nd;
  210. static bool update_responsibility;
  211. audio_block_t *inputQueueArray[1];
  212. };
  213. class AudioPrint : public AudioStream
  214. {
  215. public:
  216. AudioPrint(const char *str) : AudioStream(1, inputQueueArray), name(str) {}
  217. virtual void update(void);
  218. private:
  219. const char *name;
  220. audio_block_t *inputQueueArray[1];
  221. };
  222. class AudioInputI2S : public AudioStream
  223. {
  224. public:
  225. AudioInputI2S(void) : AudioStream(0, NULL) { begin(); }
  226. virtual void update(void);
  227. void begin(void);
  228. friend void dma_ch1_isr(void);
  229. protected:
  230. AudioInputI2S(int dummy): AudioStream(0, NULL) {} // to be used only inside AudioInputI2Sslave !!
  231. static bool update_responsibility;
  232. private:
  233. static audio_block_t *block_left;
  234. static audio_block_t *block_right;
  235. static uint16_t block_offset;
  236. };
  237. class AudioOutputI2S : public AudioStream
  238. {
  239. public:
  240. AudioOutputI2S(void) : AudioStream(2, inputQueueArray) { begin(); }
  241. virtual void update(void);
  242. void begin(void);
  243. friend void dma_ch0_isr(void);
  244. friend class AudioInputI2S;
  245. protected:
  246. AudioOutputI2S(int dummy): AudioStream(2, inputQueueArray) {} // to be used only inside AudioOutputI2Sslave !!
  247. static void config_i2s(void);
  248. static audio_block_t *block_left_1st;
  249. static audio_block_t *block_right_1st;
  250. static bool update_responsibility;
  251. private:
  252. static audio_block_t *block_left_2nd;
  253. static audio_block_t *block_right_2nd;
  254. static uint16_t block_left_offset;
  255. static uint16_t block_right_offset;
  256. audio_block_t *inputQueueArray[2];
  257. };
  258. class AudioInputI2Sslave : public AudioInputI2S
  259. {
  260. public:
  261. AudioInputI2Sslave(void) : AudioInputI2S(0) { begin(); }
  262. void begin(void);
  263. friend void dma_ch1_isr(void);
  264. };
  265. class AudioOutputI2Sslave : public AudioOutputI2S
  266. {
  267. public:
  268. AudioOutputI2Sslave(void) : AudioOutputI2S(0) { begin(); } ;
  269. void begin(void);
  270. friend class AudioInputI2Sslave;
  271. friend void dma_ch0_isr(void);
  272. protected:
  273. static void config_i2s(void);
  274. };
  275. class AudioInputAnalog : public AudioStream
  276. {
  277. public:
  278. AudioInputAnalog(unsigned int pin) : AudioStream(0, NULL) { begin(pin); }
  279. virtual void update(void);
  280. void begin(unsigned int pin);
  281. friend void dma_ch2_isr(void);
  282. private:
  283. static audio_block_t *block_left;
  284. static uint16_t block_offset;
  285. uint16_t dc_average;
  286. static bool update_responsibility;
  287. };
  288. #include "SD.h"
  289. class AudioPlaySDcardWAV : public AudioStream
  290. {
  291. public:
  292. AudioPlaySDcardWAV(void) : AudioStream(0, NULL) { begin(); }
  293. void begin(void);
  294. bool play(const char *filename);
  295. void stop(void);
  296. bool start(void);
  297. virtual void update(void);
  298. private:
  299. File wavfile;
  300. bool consume(void);
  301. bool parse_format(void);
  302. uint32_t header[5];
  303. uint32_t data_length; // number of bytes remaining in data section
  304. audio_block_t *block_left;
  305. audio_block_t *block_right;
  306. uint16_t block_offset;
  307. uint8_t buffer[512];
  308. uint16_t buffer_remaining;
  309. uint8_t state;
  310. uint8_t state_play;
  311. uint8_t leftover_bytes;
  312. };
  313. class AudioPlaySDcardRAW : public AudioStream
  314. {
  315. public:
  316. AudioPlaySDcardRAW(void) : AudioStream(0, NULL) { begin(); }
  317. void begin(void);
  318. bool play(const char *filename);
  319. void stop(void);
  320. virtual void update(void);
  321. private:
  322. File rawfile;
  323. audio_block_t *block;
  324. bool playing;
  325. bool paused;
  326. };
  327. class AudioPlayMemory : public AudioStream
  328. {
  329. public:
  330. AudioPlayMemory(void) : AudioStream(0, NULL), playing(0) { }
  331. void play(const unsigned int *data);
  332. void stop(void);
  333. virtual void update(void);
  334. private:
  335. const unsigned int *next;
  336. uint32_t length;
  337. int16_t prior;
  338. volatile uint8_t playing;
  339. };
  340. class AudioMixer4 : public AudioStream
  341. {
  342. public:
  343. AudioMixer4(void) : AudioStream(4, inputQueueArray) {
  344. for (int i=0; i<4; i++) multiplier[i] = 65536;
  345. }
  346. virtual void update(void);
  347. void gain(unsigned int channel, float gain) {
  348. if (channel >= 4) return;
  349. if (gain > 32767.0f) gain = 32767.0f;
  350. else if (gain < 0.0f) gain = 0.0f;
  351. multiplier[channel] = gain * 65536.0f; // TODO: proper roundoff?
  352. }
  353. private:
  354. int32_t multiplier[4];
  355. audio_block_t *inputQueueArray[4];
  356. };
  357. class AudioFilterBiquad : public AudioStream
  358. {
  359. public:
  360. AudioFilterBiquad(int *parameters)
  361. : AudioStream(1, inputQueueArray), definition(parameters) { }
  362. virtual void update(void);
  363. private:
  364. int *definition;
  365. audio_block_t *inputQueueArray[1];
  366. };
  367. class AudioEffectFade : public AudioStream
  368. {
  369. public:
  370. AudioEffectFade(void)
  371. : AudioStream(1, inputQueueArray), position(0xFFFFFFFF) {}
  372. void fadeIn(uint32_t milliseconds) {
  373. uint32_t samples = (uint32_t)(milliseconds * 441u + 5u) / 10u;
  374. //Serial.printf("fadeIn, %u samples\n", samples);
  375. fadeBegin(0xFFFFFFFFu / samples, 1);
  376. }
  377. void fadeOut(uint32_t milliseconds) {
  378. uint32_t samples = (uint32_t)(milliseconds * 441u + 5u) / 10u;
  379. //Serial.printf("fadeOut, %u samples\n", samples);
  380. fadeBegin(0xFFFFFFFFu / samples, 0);
  381. }
  382. virtual void update(void);
  383. private:
  384. void fadeBegin(uint32_t newrate, uint8_t dir);
  385. uint32_t position; // 0 = off, 0xFFFFFFFF = on
  386. uint32_t rate;
  387. uint8_t direction; // 0 = fading out, 1 = fading in
  388. audio_block_t *inputQueueArray[1];
  389. };
  390. class AudioAnalyzeToneDetect : public AudioStream
  391. {
  392. public:
  393. AudioAnalyzeToneDetect(void)
  394. : AudioStream(1, inputQueueArray), thresh(6554), enabled(false) { }
  395. void frequency(float freq, uint16_t cycles=10) {
  396. set_params((int32_t)(cos((double)freq
  397. * (2.0 * 3.14159265358979323846 / AUDIO_SAMPLE_RATE_EXACT))
  398. * (double)2147483647.999), cycles,
  399. (float)AUDIO_SAMPLE_RATE_EXACT / freq * (float)cycles + 0.5f);
  400. }
  401. void set_params(int32_t coef, uint16_t cycles, uint16_t len);
  402. bool available(void) {
  403. __disable_irq();
  404. bool flag = new_output;
  405. if (flag) new_output = false;
  406. __enable_irq();
  407. return flag;
  408. }
  409. float read(void);
  410. void threshold(float level) {
  411. if (level < 0.01f) thresh = 655;
  412. else if (level > 0.99f) thresh = 64881;
  413. else thresh = level * 65536.0f + 0.5f;
  414. }
  415. operator bool(); // true if at or above threshold, false if below
  416. virtual void update(void);
  417. private:
  418. int32_t coefficient; // Goertzel algorithm coefficient
  419. int32_t s1, s2; // Goertzel algorithm state
  420. int32_t out1, out2; // Goertzel algorithm state output
  421. uint16_t length; // number of samples to analyze
  422. uint16_t count; // how many left to analyze
  423. uint16_t ncycles; // number of waveform cycles to seek
  424. uint16_t thresh; // threshold, 655 to 64881 (1% to 99%)
  425. bool enabled;
  426. volatile bool new_output;
  427. audio_block_t *inputQueueArray[1];
  428. };
  429. // TODO: more audio processing objects....
  430. // sine wave with frequency modulation (phase)
  431. // waveforms with bandwidth limited tables for synth
  432. // envelope: attack-decay-sustain-release, maybe other more complex?
  433. // MP3 decoding - it is possible with optimized code?
  434. // other decompression, ADPCM, Vorbis, Speex, etc?
  435. // A base class for all Codecs, DACs and ADCs, so at least the
  436. // most basic functionality is consistent.
  437. #define AUDIO_INPUT_LINEIN 0
  438. #define AUDIO_INPUT_MIC 1
  439. class AudioControl
  440. {
  441. public:
  442. virtual bool enable(void) = 0;
  443. virtual bool disable(void) = 0;
  444. virtual bool volume(float volume) = 0; // volume 0.0 to 100.0
  445. virtual bool inputLevel(float volume) = 0; // volume 0.0 to 100.0
  446. virtual bool inputSelect(int n) = 0;
  447. };
  448. class AudioControlWM8731 : public AudioControl
  449. {
  450. public:
  451. bool enable(void);
  452. bool disable(void) { return false; }
  453. bool volume(float n) { return volumeInteger(n * 0.8 + 47.499); }
  454. bool inputLevel(float n) { return false; }
  455. bool inputSelect(int n) { return false; }
  456. protected:
  457. bool write(unsigned int reg, unsigned int val);
  458. bool volumeInteger(unsigned int n); // range: 0x2F to 0x7F
  459. };
  460. class AudioControlWM8731master : public AudioControlWM8731
  461. {
  462. public:
  463. bool enable(void);
  464. };
  465. class AudioControlSGTL5000 : public AudioControl
  466. {
  467. public:
  468. bool enable(void);
  469. bool disable(void) { return false; }
  470. bool volume(float n) { return volumeInteger(n * 1.29 + 0.499); }
  471. bool inputLevel(float n) {return false;}
  472. bool muteHeadphone(void) { return write(0x0024, ana_ctrl | (1<<4)); }
  473. bool unmuteHeadphone(void) { return write(0x0024, ana_ctrl & ~(1<<4)); }
  474. bool muteLineout(void) { return write(0x0024, ana_ctrl | (1<<8)); }
  475. bool unmuteLineout(void) { return write(0x0024, ana_ctrl & ~(1<<8)); }
  476. bool inputSelect(int n) {
  477. if (n == AUDIO_INPUT_LINEIN) {
  478. return write(0x0024, ana_ctrl | (1<<2));
  479. } else if (n == AUDIO_INPUT_MIC) {
  480. //return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2));
  481. return write(0x002A, 0x0173) && write(0x0024, ana_ctrl & ~(1<<2)); // +40dB
  482. } else {
  483. return false;
  484. }
  485. }
  486. //bool inputLinein(void) { return write(0x0024, ana_ctrl | (1<<2)); }
  487. //bool inputMic(void) { return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2)); }
  488. protected:
  489. bool muted;
  490. bool volumeInteger(unsigned int n); // range: 0x00 to 0x80
  491. uint16_t ana_ctrl;
  492. unsigned int read(unsigned int reg);
  493. bool write(unsigned int reg, unsigned int val);
  494. };
  495. /******************************************************************/
  496. // Maximum number of coefficients in a FIR filter
  497. // The audio breaks up with 128 coefficients so a
  498. // maximum of 150 is more than sufficient
  499. #define MAX_COEFFS 150
  500. // Indicates that the code should just pass through the audio
  501. // without any filtering (as opposed to doing nothing at all)
  502. #define FIR_PASSTHRU ((short *) 1)
  503. class AudioFilterFIR :
  504. public AudioStream
  505. {
  506. public:
  507. AudioFilterFIR(void):
  508. AudioStream(2,inputQueueArray), coeff_p(NULL)
  509. {
  510. }
  511. void begin(short *coeff_p,int f_pin);
  512. virtual void update(void);
  513. void stop(void);
  514. private:
  515. audio_block_t *inputQueueArray[2];
  516. // arm state arrays and FIR instances for left and right channels
  517. // the state arrays are defined to handle a maximum of MAX_COEFFS
  518. // coefficients in a filter
  519. q15_t l_StateQ15[AUDIO_BLOCK_SAMPLES + MAX_COEFFS];
  520. q15_t r_StateQ15[AUDIO_BLOCK_SAMPLES + MAX_COEFFS];
  521. arm_fir_instance_q15 l_fir_inst;
  522. arm_fir_instance_q15 r_fir_inst;
  523. // pointer to current coefficients or NULL or FIR_PASSTHRU
  524. short *coeff_p;
  525. };
  526. /******************************************************************/
  527. // A u d i o E f f e c t F l a n g e
  528. // Written by Pete (El Supremo) Jan 2014
  529. #define DELAY_PASSTHRU 0
  530. class AudioEffectFlange :
  531. public AudioStream
  532. {
  533. public:
  534. AudioEffectFlange(void):
  535. AudioStream(2,inputQueueArray) {
  536. }
  537. boolean begin(short *delayline,int d_length,int delay_offset,int d_depth,float delay_rate);
  538. boolean modify(int delay_offset,int d_depth,float delay_rate);
  539. virtual void update(void);
  540. void stop(void);
  541. private:
  542. audio_block_t *inputQueueArray[2];
  543. static short *l_delayline;
  544. static short *r_delayline;
  545. static int delay_length;
  546. static short l_circ_idx;
  547. static short r_circ_idx;
  548. static int delay_depth;
  549. static int delay_offset_idx;
  550. static int delay_rate_incr;
  551. static unsigned int l_delay_rate_index;
  552. static unsigned int r_delay_rate_index;
  553. };
  554. /******************************************************************/
  555. // A u d i o E f f e c t C h o r u s
  556. // Written by Pete (El Supremo) Jan 2014
  557. #define DELAY_PASSTHRU -1
  558. class AudioEffectChorus :
  559. public AudioStream
  560. {
  561. public:
  562. AudioEffectChorus(void):
  563. AudioStream(2,inputQueueArray) {
  564. }
  565. boolean begin(short *delayline,int delay_length,int n_chorus);
  566. virtual void update(void);
  567. void stop(void);
  568. void modify(int n_chorus);
  569. private:
  570. audio_block_t *inputQueueArray[2];
  571. static short *l_delayline;
  572. static short *r_delayline;
  573. static short l_circ_idx;
  574. static short r_circ_idx;
  575. static int num_chorus;
  576. static int delay_length;
  577. };
  578. /******************************************************************/
  579. // A u d i o T o n e S w e e p
  580. // Written by Pete (El Supremo) Feb 2014
  581. class AudioToneSweep : public AudioStream
  582. {
  583. public:
  584. AudioToneSweep(void) :
  585. AudioStream(0,NULL), sweep_busy(0)
  586. { }
  587. boolean begin(short t_amp,int t_lo,int t_hi,float t_time);
  588. virtual void update(void);
  589. unsigned char busy(void);
  590. private:
  591. short tone_amp;
  592. int tone_lo;
  593. int tone_hi;
  594. uint64_t tone_freq;
  595. uint64_t tone_phase;
  596. uint64_t tone_incr;
  597. int tone_sign;
  598. unsigned char sweep_busy;
  599. };