Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

410 lines
8.6KB

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