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.

427 lines
8.9KB

  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 AudioOutputAnalog : public AudioStream
  101. {
  102. public:
  103. AudioOutputAnalog(void) : AudioStream(1, inputQueueArray) { begin(); }
  104. virtual void update(void);
  105. void begin(void);
  106. friend void dma_ch4_isr(void);
  107. private:
  108. static audio_block_t *block_left_1st;
  109. static audio_block_t *block_left_2nd;
  110. static bool update_responsibility;
  111. audio_block_t *inputQueueArray[1];
  112. };
  113. class AudioPrint : public AudioStream
  114. {
  115. public:
  116. AudioPrint(const char *str) : AudioStream(1, inputQueueArray), name(str) {}
  117. virtual void update(void);
  118. private:
  119. const char *name;
  120. audio_block_t *inputQueueArray[1];
  121. };
  122. class AudioInputI2S : public AudioStream
  123. {
  124. public:
  125. AudioInputI2S(void) : AudioStream(0, NULL) { begin(); }
  126. virtual void update(void);
  127. void begin(void);
  128. friend void dma_ch1_isr(void);
  129. private:
  130. static audio_block_t *block_left;
  131. static audio_block_t *block_right;
  132. static uint16_t block_offset;
  133. static bool update_responsibility; // TODO: implement and test this.
  134. };
  135. class AudioOutputI2S : public AudioStream
  136. {
  137. public:
  138. AudioOutputI2S(void) : AudioStream(2, inputQueueArray) { begin(); }
  139. virtual void update(void);
  140. void begin(void);
  141. friend void dma_ch0_isr(void);
  142. friend class AudioInputI2S;
  143. private:
  144. static void config_i2s(void);
  145. static audio_block_t *block_left_1st;
  146. static audio_block_t *block_right_1st;
  147. static audio_block_t *block_left_2nd;
  148. static audio_block_t *block_right_2nd;
  149. static uint16_t block_left_offset;
  150. static uint16_t block_right_offset;
  151. static bool update_responsibility;
  152. audio_block_t *inputQueueArray[2];
  153. };
  154. class AudioInputAnalog : public AudioStream
  155. {
  156. public:
  157. AudioInputAnalog(unsigned int pin) : AudioStream(0, NULL) { begin(pin); }
  158. virtual void update(void);
  159. void begin(unsigned int pin);
  160. friend void dma_ch2_isr(void);
  161. private:
  162. static audio_block_t *block_left;
  163. static uint16_t block_offset;
  164. uint16_t dc_average;
  165. //static bool update_responsibility; // TODO: implement and test this.
  166. };
  167. #include "SD.h"
  168. class AudioPlaySDcardWAV : public AudioStream
  169. {
  170. public:
  171. AudioPlaySDcardWAV(void) : AudioStream(0, NULL) { begin(); }
  172. void begin(void);
  173. bool play(const char *filename);
  174. void stop(void);
  175. bool start(void);
  176. virtual void update(void);
  177. private:
  178. File wavfile;
  179. bool consume(void);
  180. bool parse_format(void);
  181. uint32_t header[5];
  182. uint32_t data_length; // number of bytes remaining in data section
  183. audio_block_t *block_left;
  184. audio_block_t *block_right;
  185. uint16_t block_offset;
  186. uint8_t buffer[512];
  187. uint16_t buffer_remaining;
  188. uint8_t state;
  189. uint8_t state_play;
  190. uint8_t leftover_bytes;
  191. };
  192. class AudioPlaySDcardRAW : public AudioStream
  193. {
  194. public:
  195. AudioPlaySDcardRAW(void) : AudioStream(0, NULL) { begin(); }
  196. void begin(void);
  197. bool play(const char *filename);
  198. void stop(void);
  199. virtual void update(void);
  200. private:
  201. File rawfile;
  202. audio_block_t *block;
  203. bool playing;
  204. bool paused;
  205. };
  206. class AudioPlayMemory : public AudioStream
  207. {
  208. public:
  209. AudioPlayMemory(void) : AudioStream(0, NULL), playing(0) { }
  210. void play(const unsigned int *data);
  211. void stop(void);
  212. virtual void update(void);
  213. private:
  214. const unsigned int *next;
  215. uint32_t length;
  216. int16_t prior;
  217. volatile uint8_t playing;
  218. };
  219. class AudioMixer4 : public AudioStream
  220. {
  221. public:
  222. AudioMixer4(void) : AudioStream(4, inputQueueArray) {
  223. for (int i=0; i<4; i++) multiplier[i] = 65536;
  224. }
  225. virtual void update(void);
  226. void gain(unsigned int channel, float gain) {
  227. if (channel >= 4) return;
  228. if (gain > 32767.0f) gain = 32767.0f;
  229. else if (gain < 0.0f) gain = 0.0f;
  230. multiplier[channel] = gain * 65536.0f; // TODO: proper roundoff?
  231. }
  232. private:
  233. int32_t multiplier[4];
  234. audio_block_t *inputQueueArray[4];
  235. };
  236. // TODO: more audio processing objects....
  237. // N-channel mixer, adjustable gain on each channel
  238. // sine wave with frequency modulation (phase)
  239. // non-sine oscillators, ramp, triangle, square/pulse, etc
  240. // envelope: attack-decay-sustain-release, maybe other more complex?
  241. // filters, low pass, high pass, bandpass, notch
  242. // frequency analysis - FFT, single frequency (eg, filter for DTMF)
  243. // MP3 decoding - it is possible with optimized code?
  244. // other decompression, ADPCM, Vorbis, Speex, etc?
  245. // A base class for all Codecs, DACs and ADCs, so at least the
  246. // most basic functionality is consistent.
  247. #define AUDIO_INPUT_LINEIN 0
  248. #define AUDIO_INPUT_MIC 1
  249. class AudioControl
  250. {
  251. public:
  252. virtual bool enable(void) = 0;
  253. virtual bool disable(void) = 0;
  254. virtual bool volume(float volume) = 0; // volume 0.0 to 100.0
  255. virtual bool inputLevel(float volume) = 0; // volume 0.0 to 100.0
  256. virtual bool inputSelect(int n) = 0;
  257. };
  258. class AudioControlWM8731 : public AudioControl
  259. {
  260. public:
  261. bool enable(void);
  262. bool volume(float n) { return volumeInteger(n * 0.8 + 47.499); }
  263. bool inputLevel(float n) { return false; }
  264. bool inputSelect(int n) { return false; }
  265. protected:
  266. bool write(unsigned int reg, unsigned int val);
  267. bool volumeInteger(unsigned int n); // range: 0x2F to 0x7F
  268. };
  269. class AudioControlSGTL5000 : public AudioControl
  270. {
  271. public:
  272. bool enable(void);
  273. bool disable(void) { return false; }
  274. bool volume(float n) { return volumeInteger(n * 1.29 + 0.499); }
  275. bool inputLevel(float n) {return false;}
  276. bool muteHeadphone(void) { return write(0x0024, ana_ctrl | (1<<4)); }
  277. bool unmuteHeadphone(void) { return write(0x0024, ana_ctrl & ~(1<<4)); }
  278. bool muteLineout(void) { return write(0x0024, ana_ctrl | (1<<8)); }
  279. bool unmuteLineout(void) { return write(0x0024, ana_ctrl & ~(1<<8)); }
  280. bool inputSelect(int n) {
  281. if (n == AUDIO_INPUT_LINEIN) {
  282. return write(0x0024, ana_ctrl | (1<<2));
  283. } else if (n == AUDIO_INPUT_MIC) {
  284. //return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2));
  285. return write(0x002A, 0x0173) && write(0x0024, ana_ctrl & ~(1<<2)); // +40dB
  286. } else {
  287. return false;
  288. }
  289. }
  290. //bool inputLinein(void) { return write(0x0024, ana_ctrl | (1<<2)); }
  291. //bool inputMic(void) { return write(0x002A, 0x0172) && write(0x0024, ana_ctrl & ~(1<<2)); }
  292. protected:
  293. bool muted;
  294. bool volumeInteger(unsigned int n); // range: 0x00 to 0x80
  295. uint16_t ana_ctrl;
  296. unsigned int read(unsigned int reg);
  297. bool write(unsigned int reg, unsigned int val);
  298. };