1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Refactored prepare() method in Audio library

This commit is contained in:
Scott Fitzgerald 2012-10-20 00:46:02 +02:00 committed by Cristian Maglie
parent 43cf5b0ed6
commit c97e3623a4
3 changed files with 14 additions and 15 deletions

View File

@ -33,6 +33,17 @@ void AudioClass::end() {
free( buffer);
}
void AudioClass::prepare(int16_t *buffer, int S, int volume){
uint16_t *ubuffer = (uint16_t*) buffer;
for (int i=0; i<S; i++) {
// set volume amplitude (signed multiply)
buffer[i] = buffer[i] * volume / 1024;
// convert from signed 16 bit to unsigned 12 bit for DAC.
ubuffer[i] += 0x8000;
ubuffer[i] >>= 4;
}
}
size_t AudioClass::write(const uint32_t *data, size_t size) {
const uint32_t TAG = 0x10000000;
int i;

View File

@ -18,6 +18,7 @@
class AudioClass : public Print {
public:
AudioClass(DACClass &_dac) : dac(&_dac) { };
void prepare(int16_t *buffer, int S, int volume);
void begin(uint32_t sampleRate, uint32_t msPreBuffer);
void end();

View File

@ -44,8 +44,8 @@ void loop()
myFile.read(buffer, sizeof(buffer));
// Prepare samples
int volume = analogRead(2);
prepare(buffer, S, volume);
int volume = 1024;
Audio.prepare(buffer, S, volume);
// Feed samples to audio
Audio.write(buffer, S);
@ -58,20 +58,7 @@ void loop()
}
myFile.close();
Serial.println("End of file. Thank you for listening!");
while (true) ;
}
void prepare(int16_t *buffer, int S, int volume) {
uint16_t *ubuffer = (uint16_t*) buffer;
for (int i=0; i<S; i++) {
// set volume amplitude (signed multiply)
buffer[i] = buffer[i] * volume / 1024;
// convert from signed 16 bit to unsigned 12 bit for DAC.
ubuffer[i] += 0x8000;
ubuffer[i] >>= 4;
}
}