Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

490 linhas
11KB

  1. #include "AudioStream.h"
  2. // When changing multiple audio object settings that must update at
  3. // the same time, these functions allow the audio library interrupt
  4. // to be disabled. For example, you may wish to begin playing a note
  5. // in response to reading an analog sensor. If you have "velocity"
  6. // information, you might start the sample playing and also adjust
  7. // the gain of a mixer channel. Use AudioNoInterrupts() first, then
  8. // make both changes to the 2 separate objects. Then allow the audio
  9. // library to update with AudioInterrupts(). Both changes will happen
  10. // at the same time, because AudioNoInterrupts() prevents any updates
  11. // while you make changes.
  12. #define AudioNoInterrupts() (NVIC_DISABLE_IRQ(IRQ_SOFTWARE))
  13. #define AudioInterrupts() (NVIC_ENABLE_IRQ(IRQ_SOFTWARE))
  14. // waveforms.c
  15. extern "C" {
  16. extern const int16_t AudioWaveformSine[257];
  17. extern const int16_t AudioWaveformTriangle[257];
  18. extern const int16_t AudioWaveformSquare[257];
  19. extern const int16_t AudioWaveformSawtooth[257];
  20. }
  21. // windows.c
  22. extern "C" {
  23. extern const int16_t AudioWindowHanning256[];
  24. extern const int16_t AudioWindowBartlett256[];
  25. extern const int16_t AudioWindowBlackman256[];
  26. extern const int16_t AudioWindowFlattop256[];
  27. extern const int16_t AudioWindowBlackmanHarris256[];
  28. extern const int16_t AudioWindowNuttall256[];
  29. extern const int16_t AudioWindowBlackmanNuttall256[];
  30. extern const int16_t AudioWindowWelch256[];
  31. extern const int16_t AudioWindowHamming256[];
  32. extern const int16_t AudioWindowCosine256[];
  33. extern const int16_t AudioWindowTukey256[];
  34. }
  35. class AudioAnalyzeFFT256 : public AudioStream
  36. {
  37. public:
  38. AudioAnalyzeFFT256(uint8_t navg = 8, const int16_t *win = AudioWindowHanning256)
  39. : AudioStream(1, inputQueueArray), outputflag(false),
  40. prevblock(NULL), count(0), naverage(navg), window(win) { init(); }
  41. bool available() {
  42. if (outputflag == true) {
  43. outputflag = false;
  44. return true;
  45. }
  46. return false;
  47. }
  48. virtual void update(void);
  49. //uint32_t cycles;
  50. int32_t output[128] __attribute__ ((aligned (4)));
  51. private:
  52. void init(void);
  53. const int16_t *window;
  54. audio_block_t *prevblock;
  55. int16_t buffer[512] __attribute__ ((aligned (4)));
  56. uint8_t count;
  57. uint8_t naverage;
  58. bool outputflag;
  59. audio_block_t *inputQueueArray[1];
  60. };
  61. class AudioSynthWaveform : public AudioStream
  62. {
  63. public:
  64. AudioSynthWaveform(const int16_t *waveform)
  65. : AudioStream(0, NULL), wavetable(waveform), magnitude(0), phase(0) { }
  66. void frequency(float freq) {
  67. if (freq > AUDIO_SAMPLE_RATE_EXACT / 2 || freq < 0.0) return;
  68. phase_increment = (freq / AUDIO_SAMPLE_RATE_EXACT) * 4294967296.0f;
  69. }
  70. void amplitude(float n) { // 0 to 1.0
  71. if (n < 0) n = 0;
  72. else if (n > 1.0) n = 1.0;
  73. magnitude = n * 32767.0;
  74. }
  75. virtual void update(void);
  76. private:
  77. const int16_t *wavetable;
  78. uint16_t magnitude;
  79. uint32_t phase;
  80. uint32_t phase_increment;
  81. };
  82. #if 0
  83. class AudioSineWaveMod : public AudioStream
  84. {
  85. public:
  86. AudioSineWaveMod() : AudioStream(1, inputQueueArray) {}
  87. void frequency(float freq);
  88. //void amplitude(q15 n);
  89. virtual void update(void);
  90. private:
  91. uint32_t phase;
  92. uint32_t phase_increment;
  93. uint32_t modulation_factor;
  94. audio_block_t *inputQueueArray[1];
  95. };
  96. #endif
  97. class AudioOutputPWM : public AudioStream
  98. {
  99. public:
  100. AudioOutputPWM(void) : AudioStream(1, inputQueueArray) { begin(); }
  101. virtual void update(void);
  102. void begin(void);
  103. friend void dma_ch3_isr(void);
  104. private:
  105. static audio_block_t *block_1st;
  106. static audio_block_t *block_2nd;
  107. static uint32_t block_offset;
  108. static bool update_responsibility;
  109. static uint8_t interrupt_count;
  110. audio_block_t *inputQueueArray[1];
  111. };
  112. class AudioOutputAnalog : public AudioStream
  113. {
  114. public:
  115. AudioOutputAnalog(void) : AudioStream(1, inputQueueArray) { begin(); }
  116. virtual void update(void);
  117. void begin(void);
  118. void analogReference(int ref);
  119. friend void dma_ch4_isr(void);
  120. private:
  121. static audio_block_t *block_left_1st;
  122. static audio_block_t *block_left_2nd;
  123. static bool update_responsibility;
  124. audio_block_t *inputQueueArray[1];
  125. };
  126. class AudioPrint : public AudioStream
  127. {
  128. public:
  129. AudioPrint(const char *str) : AudioStream(1, inputQueueArray), name(str) {}
  130. virtual void update(void);
  131. private:
  132. const char *name;
  133. audio_block_t *inputQueueArray[1];
  134. };
  135. class AudioInputI2S : public AudioStream
  136. {
  137. public:
  138. AudioInputI2S(void) : AudioStream(0, NULL) { begin(); }
  139. virtual void update(void);
  140. void begin(void);
  141. friend void dma_ch1_isr(void);
  142. protected:
  143. AudioInputI2S(int dummy): AudioStream(0, NULL) {} // to be used only inside AudioInputI2Sslave !!
  144. static bool update_responsibility; // TODO: implement and test this.
  145. private:
  146. static audio_block_t *block_left;
  147. static audio_block_t *block_right;
  148. static uint16_t block_offset;
  149. };
  150. class AudioOutputI2S : public AudioStream
  151. {
  152. public:
  153. AudioOutputI2S(void) : AudioStream(2, inputQueueArray) { begin(); }
  154. virtual void update(void);
  155. void begin(void);
  156. friend void dma_ch0_isr(void);
  157. friend class AudioInputI2S;
  158. protected:
  159. AudioOutputI2S(int dummy): AudioStream(2, inputQueueArray) {} // to be used only inside AudioOutputI2Sslave !!
  160. static void config_i2s(void);
  161. static audio_block_t *block_left_1st;
  162. static audio_block_t *block_right_1st;
  163. static bool update_responsibility;
  164. private:
  165. static audio_block_t *block_left_2nd;
  166. static audio_block_t *block_right_2nd;
  167. static uint16_t block_left_offset;
  168. static uint16_t block_right_offset;
  169. audio_block_t *inputQueueArray[2];
  170. };
  171. class AudioInputI2Sslave : public AudioInputI2S
  172. {
  173. public:
  174. AudioInputI2Sslave(void) : AudioInputI2S(0) { begin(); }
  175. void begin(void);
  176. friend void dma_ch1_isr(void);
  177. };
  178. class AudioOutputI2Sslave : public AudioOutputI2S
  179. {
  180. public:
  181. AudioOutputI2Sslave(void) : AudioOutputI2S(0) { begin(); } ;
  182. void begin(void);
  183. friend class AudioInputI2Sslave;
  184. friend void dma_ch0_isr(void);
  185. protected:
  186. static void config_i2s(void);
  187. };
  188. class AudioInputAnalog : public AudioStream
  189. {
  190. public:
  191. AudioInputAnalog(unsigned int pin) : AudioStream(0, NULL) { begin(pin); }
  192. virtual void update(void);
  193. void begin(unsigned int pin);
  194. friend void dma_ch2_isr(void);
  195. private:
  196. static audio_block_t *block_left;
  197. static uint16_t block_offset;
  198. uint16_t dc_average;
  199. //static bool update_responsibility; // TODO: implement and test this.
  200. };
  201. #include "SD.h"
  202. class AudioPlaySDcardWAV : public AudioStream
  203. {
  204. public:
  205. AudioPlaySDcardWAV(void) : AudioStream(0, NULL) { begin(); }
  206. void begin(void);
  207. bool play(const char *filename);
  208. void stop(void);
  209. bool start(void);
  210. virtual void update(void);
  211. private:
  212. File wavfile;
  213. bool consume(void);
  214. bool parse_format(void);
  215. uint32_t header[5];
  216. uint32_t data_length; // number of bytes remaining in data section
  217. audio_block_t *block_left;
  218. audio_block_t *block_right;
  219. uint16_t block_offset;
  220. uint8_t buffer[512];
  221. uint16_t buffer_remaining;
  222. uint8_t state;
  223. uint8_t state_play;
  224. uint8_t leftover_bytes;
  225. };
  226. class AudioPlaySDcardRAW : public AudioStream
  227. {
  228. public:
  229. AudioPlaySDcardRAW(void) : AudioStream(0, NULL) { begin(); }
  230. void begin(void);
  231. bool play(const char *filename);
  232. void stop(void);
  233. virtual void update(void);
  234. private:
  235. File rawfile;
  236. audio_block_t *block;
  237. bool playing;
  238. bool paused;
  239. };
  240. class AudioPlayMemory : public AudioStream
  241. {
  242. public:
  243. AudioPlayMemory(void) : AudioStream(0, NULL), playing(0) { }
  244. void play(const unsigned int *data);
  245. void stop(void);
  246. virtual void update(void);
  247. private:
  248. const unsigned int *next;
  249. uint32_t length;
  250. int16_t prior;
  251. volatile uint8_t playing;
  252. };
  253. class AudioMixer4 : public AudioStream
  254. {
  255. public:
  256. AudioMixer4(void) : AudioStream(4, inputQueueArray) {
  257. for (int i=0; i<4; i++) multiplier[i] = 65536;
  258. }
  259. virtual void update(void);
  260. void gain(unsigned int channel, float gain) {
  261. if (channel >= 4) return;
  262. if (gain > 32767.0f) gain = 32767.0f;
  263. else if (gain < 0.0f) gain = 0.0f;
  264. multiplier[channel] = gain * 65536.0f; // TODO: proper roundoff?
  265. }
  266. private:
  267. int32_t multiplier[4];
  268. audio_block_t *inputQueueArray[4];
  269. };
  270. class AudioFilterBiquad : public AudioStream
  271. {
  272. public:
  273. AudioFilterBiquad(int *parameters)
  274. : AudioStream(1, inputQueueArray), definition(parameters) { }
  275. virtual void update(void);
  276. private:
  277. int *definition;
  278. audio_block_t *inputQueueArray[1];
  279. };
  280. // TODO: more audio processing objects....
  281. // N-channel mixer, adjustable gain on each channel
  282. // sine wave with frequency modulation (phase)
  283. // non-sine oscillators, ramp, triangle, square/pulse, etc
  284. // envelope: attack-decay-sustain-release, maybe other more complex?
  285. // filters, low pass, high pass, bandpass, notch
  286. // frequency analysis - FFT, single frequency (eg, filter for DTMF)
  287. // MP3 decoding - it is possible with optimized code?
  288. // other decompression, ADPCM, Vorbis, Speex, etc?
  289. // A base class for all Codecs, DACs and ADCs, so at least the
  290. // most basic functionality is consistent.
  291. #define AUDIO_INPUT_LINEIN 0
  292. #define AUDIO_INPUT_MIC 1
  293. class AudioControl
  294. {
  295. public:
  296. virtual bool enable(void) = 0;
  297. virtual bool disable(void) = 0;
  298. virtual bool volume(float volume) = 0; // volume 0.0 to 100.0
  299. virtual bool inputLevel(float volume) = 0; // volume 0.0 to 100.0
  300. virtual bool inputSelect(int n) = 0;
  301. };
  302. class AudioControlWM8731 : public AudioControl
  303. {
  304. public:
  305. bool enable(void);
  306. bool disable(void) { return false; }
  307. bool volume(float n) { return volumeInteger(n * 0.8 + 47.499); }
  308. bool inputLevel(float n) { return false; }
  309. bool inputSelect(int n) { return false; }
  310. protected:
  311. bool write(unsigned int reg, unsigned int val);
  312. bool volumeInteger(unsigned int n); // range: 0x2F to 0x7F
  313. };
  314. class AudioControlWM8731master : public AudioControlWM8731
  315. {
  316. public:
  317. bool enable(void);
  318. };
  319. class AudioControlSGTL5000 : public AudioControl
  320. {
  321. public:
  322. bool enable(void);
  323. bool disable(void) { return false; }
  324. bool volume(float n) { return volumeInteger(n * 1.29 + 0.499); }
  325. bool inputLevel(float n) {return false;}
  326. bool muteHeadphone(void) { return write(0x0024, ana_ctrl | (1<<4)); }
  327. bool unmuteHeadphone(void) { return write(0x0024, ana_ctrl & ~(1<<4)); }
  328. bool muteLineout(void) { return write(0x0024, ana_ctrl | (1<<8)); }
  329. bool unmuteLineout(void) { return write(0x0024, ana_ctrl & ~(1<<8)); }
  330. bool inputSelect(int n) {
  331. if (n == AUDIO_INPUT_LINEIN) {
  332. return write(0x0024, ana_ctrl | (1<<2));
  333. } else if (n == AUDIO_INPUT_MIC) {
  334. //return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2));
  335. return write(0x002A, 0x0173) && write(0x0024, ana_ctrl & ~(1<<2)); // +40dB
  336. } else {
  337. return false;
  338. }
  339. }
  340. //bool inputLinein(void) { return write(0x0024, ana_ctrl | (1<<2)); }
  341. //bool inputMic(void) { return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2)); }
  342. protected:
  343. bool muted;
  344. bool volumeInteger(unsigned int n); // range: 0x00 to 0x80
  345. uint16_t ana_ctrl;
  346. unsigned int read(unsigned int reg);
  347. bool write(unsigned int reg, unsigned int val);
  348. };