Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

11 роки тому
10 роки тому
10 роки тому
11 роки тому
11 роки тому
11 роки тому
10 роки тому
11 роки тому
11 роки тому
11 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. void frequency(float freq) {
  68. if (freq > AUDIO_SAMPLE_RATE_EXACT / 2 || freq < 0.0) return;
  69. phase_increment = (freq / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  70. }
  71. void amplitude(float n) { // 0 to 1.0
  72. if (n < 0) n = 0;
  73. else if (n > 1.0) n = 1.0;
  74. magnitude = n * 32767.0;
  75. }
  76. virtual void update(void);
  77. private:
  78. const int16_t *wavetable;
  79. uint16_t magnitude;
  80. uint32_t phase;
  81. uint32_t phase_increment;
  82. };
  83. #if 0
  84. class AudioSineWaveMod : public AudioStream
  85. {
  86. public:
  87. AudioSineWaveMod() : AudioStream(1, inputQueueArray) {}
  88. void frequency(float freq);
  89. //void amplitude(q15 n);
  90. virtual void update(void);
  91. private:
  92. uint32_t phase;
  93. uint32_t phase_increment;
  94. uint32_t modulation_factor;
  95. audio_block_t *inputQueueArray[1];
  96. };
  97. #endif
  98. class AudioOutputPWM : public AudioStream
  99. {
  100. public:
  101. AudioOutputPWM(void) : AudioStream(1, inputQueueArray) { begin(); }
  102. virtual void update(void);
  103. void begin(void);
  104. friend void dma_ch3_isr(void);
  105. private:
  106. static audio_block_t *block_1st;
  107. static audio_block_t *block_2nd;
  108. static uint32_t block_offset;
  109. static bool update_responsibility;
  110. static uint8_t interrupt_count;
  111. audio_block_t *inputQueueArray[1];
  112. };
  113. class AudioOutputAnalog : public AudioStream
  114. {
  115. public:
  116. AudioOutputAnalog(void) : AudioStream(1, inputQueueArray) { begin(); }
  117. virtual void update(void);
  118. void begin(void);
  119. void analogReference(int ref);
  120. friend void dma_ch4_isr(void);
  121. private:
  122. static audio_block_t *block_left_1st;
  123. static audio_block_t *block_left_2nd;
  124. static bool update_responsibility;
  125. audio_block_t *inputQueueArray[1];
  126. };
  127. class AudioPrint : public AudioStream
  128. {
  129. public:
  130. AudioPrint(const char *str) : AudioStream(1, inputQueueArray), name(str) {}
  131. virtual void update(void);
  132. private:
  133. const char *name;
  134. audio_block_t *inputQueueArray[1];
  135. };
  136. class AudioInputI2S : public AudioStream
  137. {
  138. public:
  139. AudioInputI2S(void) : AudioStream(0, NULL) { begin(); }
  140. virtual void update(void);
  141. void begin(void);
  142. friend void dma_ch1_isr(void);
  143. protected:
  144. AudioInputI2S(int dummy): AudioStream(0, NULL) {} // to be used only inside AudioInputI2Sslave !!
  145. static bool update_responsibility;
  146. private:
  147. static audio_block_t *block_left;
  148. static audio_block_t *block_right;
  149. static uint16_t block_offset;
  150. };
  151. class AudioOutputI2S : public AudioStream
  152. {
  153. public:
  154. AudioOutputI2S(void) : AudioStream(2, inputQueueArray) { begin(); }
  155. virtual void update(void);
  156. void begin(void);
  157. friend void dma_ch0_isr(void);
  158. friend class AudioInputI2S;
  159. protected:
  160. AudioOutputI2S(int dummy): AudioStream(2, inputQueueArray) {} // to be used only inside AudioOutputI2Sslave !!
  161. static void config_i2s(void);
  162. static audio_block_t *block_left_1st;
  163. static audio_block_t *block_right_1st;
  164. static bool update_responsibility;
  165. private:
  166. static audio_block_t *block_left_2nd;
  167. static audio_block_t *block_right_2nd;
  168. static uint16_t block_left_offset;
  169. static uint16_t block_right_offset;
  170. audio_block_t *inputQueueArray[2];
  171. };
  172. class AudioInputI2Sslave : public AudioInputI2S
  173. {
  174. public:
  175. AudioInputI2Sslave(void) : AudioInputI2S(0) { begin(); }
  176. void begin(void);
  177. friend void dma_ch1_isr(void);
  178. };
  179. class AudioOutputI2Sslave : public AudioOutputI2S
  180. {
  181. public:
  182. AudioOutputI2Sslave(void) : AudioOutputI2S(0) { begin(); } ;
  183. void begin(void);
  184. friend class AudioInputI2Sslave;
  185. friend void dma_ch0_isr(void);
  186. protected:
  187. static void config_i2s(void);
  188. };
  189. class AudioInputAnalog : public AudioStream
  190. {
  191. public:
  192. AudioInputAnalog(unsigned int pin) : AudioStream(0, NULL) { begin(pin); }
  193. virtual void update(void);
  194. void begin(unsigned int pin);
  195. friend void dma_ch2_isr(void);
  196. private:
  197. static audio_block_t *block_left;
  198. static uint16_t block_offset;
  199. uint16_t dc_average;
  200. static bool update_responsibility;
  201. };
  202. #include "SD.h"
  203. class AudioPlaySDcardWAV : public AudioStream
  204. {
  205. public:
  206. AudioPlaySDcardWAV(void) : AudioStream(0, NULL) { begin(); }
  207. void begin(void);
  208. bool play(const char *filename);
  209. void stop(void);
  210. bool start(void);
  211. virtual void update(void);
  212. private:
  213. File wavfile;
  214. bool consume(void);
  215. bool parse_format(void);
  216. uint32_t header[5];
  217. uint32_t data_length; // number of bytes remaining in data section
  218. audio_block_t *block_left;
  219. audio_block_t *block_right;
  220. uint16_t block_offset;
  221. uint8_t buffer[512];
  222. uint16_t buffer_remaining;
  223. uint8_t state;
  224. uint8_t state_play;
  225. uint8_t leftover_bytes;
  226. };
  227. class AudioPlaySDcardRAW : public AudioStream
  228. {
  229. public:
  230. AudioPlaySDcardRAW(void) : AudioStream(0, NULL) { begin(); }
  231. void begin(void);
  232. bool play(const char *filename);
  233. void stop(void);
  234. virtual void update(void);
  235. private:
  236. File rawfile;
  237. audio_block_t *block;
  238. bool playing;
  239. bool paused;
  240. };
  241. class AudioPlayMemory : public AudioStream
  242. {
  243. public:
  244. AudioPlayMemory(void) : AudioStream(0, NULL), playing(0) { }
  245. void play(const unsigned int *data);
  246. void stop(void);
  247. virtual void update(void);
  248. private:
  249. const unsigned int *next;
  250. uint32_t length;
  251. int16_t prior;
  252. volatile uint8_t playing;
  253. };
  254. class AudioMixer4 : public AudioStream
  255. {
  256. public:
  257. AudioMixer4(void) : AudioStream(4, inputQueueArray) {
  258. for (int i=0; i<4; i++) multiplier[i] = 65536;
  259. }
  260. virtual void update(void);
  261. void gain(unsigned int channel, float gain) {
  262. if (channel >= 4) return;
  263. if (gain > 32767.0f) gain = 32767.0f;
  264. else if (gain < 0.0f) gain = 0.0f;
  265. multiplier[channel] = gain * 65536.0f; // TODO: proper roundoff?
  266. }
  267. private:
  268. int32_t multiplier[4];
  269. audio_block_t *inputQueueArray[4];
  270. };
  271. class AudioFilterBiquad : public AudioStream
  272. {
  273. public:
  274. AudioFilterBiquad(int *parameters)
  275. : AudioStream(1, inputQueueArray), definition(parameters) { }
  276. virtual void update(void);
  277. private:
  278. int *definition;
  279. audio_block_t *inputQueueArray[1];
  280. };
  281. class AudioEffectFade : public AudioStream
  282. {
  283. public:
  284. AudioEffectFade(void)
  285. : AudioStream(1, inputQueueArray), position(0xFFFFFFFF) {}
  286. void fadeIn(uint32_t milliseconds) {
  287. uint32_t samples = (uint32_t)(milliseconds * 441u + 5u) / 10u;
  288. //Serial.printf("fadeIn, %u samples\n", samples);
  289. fadeBegin(0xFFFFFFFFu / samples, 1);
  290. }
  291. void fadeOut(uint32_t milliseconds) {
  292. uint32_t samples = (uint32_t)(milliseconds * 441u + 5u) / 10u;
  293. //Serial.printf("fadeOut, %u samples\n", samples);
  294. fadeBegin(0xFFFFFFFFu / samples, 0);
  295. }
  296. virtual void update(void);
  297. private:
  298. void fadeBegin(uint32_t newrate, uint8_t dir);
  299. uint32_t position; // 0 = off, 0xFFFFFFFF = on
  300. uint32_t rate;
  301. uint8_t direction; // 0 = fading out, 1 = fading in
  302. audio_block_t *inputQueueArray[1];
  303. };
  304. class AudioAnalyzeToneDetect : public AudioStream
  305. {
  306. public:
  307. AudioAnalyzeToneDetect(void)
  308. : AudioStream(1, inputQueueArray), thresh(6554), enabled(false) { }
  309. void frequency(float freq, uint16_t cycles=10) {
  310. set_params((int32_t)(cos((double)freq
  311. * (2.0 * 3.14159265358979323846 / AUDIO_SAMPLE_RATE_EXACT))
  312. * (double)2147483647.999), cycles,
  313. (float)AUDIO_SAMPLE_RATE_EXACT / freq * (float)cycles + 0.5f);
  314. }
  315. void set_params(int32_t coef, uint16_t cycles, uint16_t len);
  316. bool available(void) {
  317. __disable_irq();
  318. bool flag = new_output;
  319. if (flag) new_output = false;
  320. __enable_irq();
  321. return flag;
  322. }
  323. float read(void);
  324. void threshold(float level) {
  325. if (level < 0.01f) thresh = 655;
  326. else if (level > 0.99f) thresh = 64881;
  327. else thresh = level * 65536.0f + 0.5f;
  328. }
  329. operator bool(); // true if at or above threshold, false if below
  330. virtual void update(void);
  331. private:
  332. int32_t coefficient; // Goertzel algorithm coefficient
  333. int32_t s1, s2; // Goertzel algorithm state
  334. int32_t out1, out2; // Goertzel algorithm state output
  335. uint16_t length; // number of samples to analyze
  336. uint16_t count; // how many left to analyze
  337. uint16_t ncycles; // number of waveform cycles to seek
  338. uint16_t thresh; // threshold, 655 to 64881 (1% to 99%)
  339. bool enabled;
  340. volatile bool new_output;
  341. audio_block_t *inputQueueArray[1];
  342. };
  343. // TODO: more audio processing objects....
  344. // sine wave with frequency modulation (phase)
  345. // waveforms with bandwidth limited tables for synth
  346. // envelope: attack-decay-sustain-release, maybe other more complex?
  347. // MP3 decoding - it is possible with optimized code?
  348. // other decompression, ADPCM, Vorbis, Speex, etc?
  349. // A base class for all Codecs, DACs and ADCs, so at least the
  350. // most basic functionality is consistent.
  351. #define AUDIO_INPUT_LINEIN 0
  352. #define AUDIO_INPUT_MIC 1
  353. class AudioControl
  354. {
  355. public:
  356. virtual bool enable(void) = 0;
  357. virtual bool disable(void) = 0;
  358. virtual bool volume(float volume) = 0; // volume 0.0 to 100.0
  359. virtual bool inputLevel(float volume) = 0; // volume 0.0 to 100.0
  360. virtual bool inputSelect(int n) = 0;
  361. };
  362. class AudioControlWM8731 : public AudioControl
  363. {
  364. public:
  365. bool enable(void);
  366. bool disable(void) { return false; }
  367. bool volume(float n) { return volumeInteger(n * 0.8 + 47.499); }
  368. bool inputLevel(float n) { return false; }
  369. bool inputSelect(int n) { return false; }
  370. protected:
  371. bool write(unsigned int reg, unsigned int val);
  372. bool volumeInteger(unsigned int n); // range: 0x2F to 0x7F
  373. };
  374. class AudioControlWM8731master : public AudioControlWM8731
  375. {
  376. public:
  377. bool enable(void);
  378. };
  379. class AudioControlSGTL5000 : public AudioControl
  380. {
  381. public:
  382. bool enable(void);
  383. bool disable(void) { return false; }
  384. bool volume(float n) { return volumeInteger(n * 1.29 + 0.499); }
  385. bool inputLevel(float n) {return false;}
  386. bool muteHeadphone(void) { return write(0x0024, ana_ctrl | (1<<4)); }
  387. bool unmuteHeadphone(void) { return write(0x0024, ana_ctrl & ~(1<<4)); }
  388. bool muteLineout(void) { return write(0x0024, ana_ctrl | (1<<8)); }
  389. bool unmuteLineout(void) { return write(0x0024, ana_ctrl & ~(1<<8)); }
  390. bool inputSelect(int n) {
  391. if (n == AUDIO_INPUT_LINEIN) {
  392. return write(0x0024, ana_ctrl | (1<<2));
  393. } else if (n == AUDIO_INPUT_MIC) {
  394. //return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2));
  395. return write(0x002A, 0x0173) && write(0x0024, ana_ctrl & ~(1<<2)); // +40dB
  396. } else {
  397. return false;
  398. }
  399. }
  400. //bool inputLinein(void) { return write(0x0024, ana_ctrl | (1<<2)); }
  401. //bool inputMic(void) { return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2)); }
  402. protected:
  403. bool muted;
  404. bool volumeInteger(unsigned int n); // range: 0x00 to 0x80
  405. uint16_t ana_ctrl;
  406. unsigned int read(unsigned int reg);
  407. bool write(unsigned int reg, unsigned int val);
  408. };
  409. /******************************************************************/
  410. // Maximum number of coefficients in a FIR filter
  411. // The audio breaks up with 128 coefficients so a
  412. // maximum of 150 is more than sufficient
  413. #define MAX_COEFFS 150
  414. // Indicates that the code should just pass through the audio
  415. // without any filtering (as opposed to doing nothing at all)
  416. #define FIR_PASSTHRU ((short *) 1)
  417. class AudioFilterFIR :
  418. public AudioStream
  419. {
  420. public:
  421. AudioFilterFIR(void):
  422. AudioStream(2,inputQueueArray) {
  423. }
  424. void begin(short *coeff_p,int f_pin);
  425. virtual void update(void);
  426. void stop(void);
  427. private:
  428. audio_block_t *inputQueueArray[2];
  429. static q15_t l_StateQ15[];
  430. static q15_t r_StateQ15[];
  431. static arm_fir_instance_q15 l_fir_inst;
  432. static arm_fir_instance_q15 r_fir_inst;
  433. static short *coeff_p;
  434. };