[136] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 by Cristian Maglie <c.maglie@arduino.cc>
|
---|
| 3 | * Audio library for Arduino Due.
|
---|
| 4 | *
|
---|
| 5 | * This file is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of either the GNU General Public License version 2
|
---|
| 7 | * or the GNU Lesser General Public License version 2.1, both as
|
---|
| 8 | * published by the Free Software Foundation.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | #ifndef AUDIO_H
|
---|
| 12 | #define AUDIO_H
|
---|
| 13 |
|
---|
| 14 | #include "Arduino.h"
|
---|
| 15 | #include "Print.h"
|
---|
| 16 | #include "DAC.h"
|
---|
| 17 |
|
---|
| 18 | class AudioClass : public Print {
|
---|
| 19 | public:
|
---|
| 20 | AudioClass(DACClass &_dac) : dac(&_dac) { };
|
---|
| 21 | void prepare(int16_t *buffer, int S, int volume);
|
---|
| 22 | void begin(uint32_t sampleRate, uint32_t msPreBuffer);
|
---|
| 23 | void end();
|
---|
| 24 |
|
---|
| 25 | virtual size_t write(uint8_t c) { /* not implemented */ };
|
---|
| 26 | virtual size_t write(const uint8_t *data, size_t size) { return write(reinterpret_cast<const uint32_t*>(data), size/4) * 4; };
|
---|
| 27 | virtual size_t write(const uint16_t *data, size_t size) { return write(reinterpret_cast<const uint32_t*>(data), size/2) * 2; };
|
---|
| 28 | virtual size_t write(const int16_t *data, size_t size) { return write(reinterpret_cast<const uint32_t*>(data), size/2) * 2; };
|
---|
| 29 | virtual size_t write(const uint32_t *data, size_t size);
|
---|
| 30 |
|
---|
| 31 | void debug() {
|
---|
| 32 | // Serial1.print(running-buffer, DEC);
|
---|
| 33 | // Serial1.print(" ");
|
---|
| 34 | // Serial1.print(current-buffer, DEC);
|
---|
| 35 | // Serial1.print(" ");
|
---|
| 36 | // Serial1.print(next-buffer, DEC);
|
---|
| 37 | // Serial1.print(" ");
|
---|
| 38 | // Serial1.println(last-buffer, DEC);
|
---|
| 39 | }
|
---|
| 40 | private:
|
---|
| 41 | void enqueue();
|
---|
| 42 | static void onTransmitEnd(void *me);
|
---|
| 43 | uint32_t bufferSize;
|
---|
| 44 | uint32_t *buffer;
|
---|
| 45 | uint32_t *half;
|
---|
| 46 | uint32_t *last;
|
---|
| 47 | uint32_t *volatile running;
|
---|
| 48 | uint32_t *volatile next;
|
---|
| 49 |
|
---|
| 50 | uint32_t *cook(const uint32_t *buffer, size_t size);
|
---|
| 51 |
|
---|
| 52 | DACClass *dac;
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | extern AudioClass Audio;
|
---|
| 56 |
|
---|
| 57 | #endif
|
---|