Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ed2604015 | ||
|
|
5a5c2a88a3 | ||
|
|
c3c9a147c5 | ||
|
|
4111404bac | ||
|
|
d4c09498c5 | ||
|
|
8bf52d4c11 | ||
|
|
f974af9bf3 | ||
|
|
9b4f36009b | ||
|
|
0d6e1340f0 | ||
|
|
8d6631e4ab | ||
|
|
a72bee6d73 |
No files matched your search
@@ -1,4 +1,4 @@
|
||||
# ESP8266Audio - supports ESP8266 & ESP32 [](https://gitter.im/ESP8266Audio/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
# ESP8266Audio - supports ESP8266 & ESP32 & Raspberry Pi RP2040[](https://gitter.im/ESP8266Audio/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||
Arduino library for parsing and decoding MOD, WAV, MP3, FLAC, MIDI, AAC, and RTTL files and playing them on an I2S DAC or even using a software-simulated delta-sigma DAC with dynamic 32x-128x oversampling.
|
||||
|
||||
ESP8266 is fully supported and most mature, but ESP32 is also mostly there with built-in DAC as well as external ones.
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <Arduino.h>
|
||||
#include "AudioFileSourceFunction.h"
|
||||
#include "AudioGeneratorWAV.h"
|
||||
#include "AudioOutputI2SNoDAC.h"
|
||||
|
||||
float hz = 440.f;
|
||||
|
||||
// pre-defined function can also be used to generate the wave
|
||||
float sine_wave(const float time) {
|
||||
float v = sin(TWO_PI * hz * time); // C
|
||||
v *= fmod(time, 1.f); // change linear
|
||||
v *= 0.5; // scale
|
||||
return v;
|
||||
};
|
||||
|
||||
AudioGeneratorWAV* wav;
|
||||
AudioFileSourceFunction* file;
|
||||
AudioOutputI2SNoDAC* out;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
// ===== create instance with length of song in [sec] =====
|
||||
file = new AudioFileSourceFunction(8.);
|
||||
//
|
||||
// you can set (sec, channels, hz, bit/sample) but you should care about
|
||||
// the trade-off between performance and the audio quality
|
||||
//
|
||||
// file = new AudioFileSourceFunction(sec, channels, hz, bit/sample);
|
||||
// channels : default = 1
|
||||
// hz : default = 8000 (8000, 11025, 22050, 44100, 48000, etc.)
|
||||
// bit/sample : default = 16 (8, 16, 32)
|
||||
|
||||
// ===== set your sound function =====
|
||||
file->addAudioGenerators([&](const float time) {
|
||||
float v = sin(TWO_PI * hz * time); // generate sine wave
|
||||
v *= fmod(time, 1.f); // change linear
|
||||
v *= 0.5; // scale
|
||||
return v;
|
||||
});
|
||||
//
|
||||
// sound function should have one argument(float) and one return(float)
|
||||
// param : float (current time [sec] of the song)
|
||||
// return : float (the amplitude of sound which varies from -1.f to +1.f)
|
||||
//
|
||||
// sound function can be registerd only one or the same number with channels
|
||||
// if the channels > 1 && the number of function == 1,
|
||||
// same function are used to generate the sound in every channel
|
||||
//
|
||||
// file = new AudioFileSourceFunction(8., 2);
|
||||
// file->addAudioGenerators(
|
||||
// // L (channel 0)
|
||||
// [](const float time) {
|
||||
// return 0.25 * sin(TWO_PI * 440.f * time) * fmod(time, 1.f); // C
|
||||
// },
|
||||
// // R (channel 1)
|
||||
// [](const float time) {
|
||||
// return 0.25 * sin(TWO_PI * 550.f * time) * fmod(time, 1.f); // E
|
||||
// }
|
||||
// );
|
||||
//
|
||||
// you can also use the pre-defined function
|
||||
// file->addAudioGenerators(sine_wave);
|
||||
|
||||
out = new AudioOutputI2SNoDAC();
|
||||
wav = new AudioGeneratorWAV();
|
||||
wav->begin(file, out);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (wav->isRunning()) {
|
||||
if (!wav->loop()) wav->stop();
|
||||
} else {
|
||||
Serial.println("function done!");
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,4 @@
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include <WiFi.h>
|
||||
#else
|
||||
#include <ESP8266WiFi.h>
|
||||
#endif
|
||||
|
||||
#include "AudioFileSourcePROGMEM.h"
|
||||
#include "AudioGeneratorWAV.h"
|
||||
@@ -18,7 +13,6 @@ AudioOutputI2SNoDAC *out;
|
||||
|
||||
void setup()
|
||||
{
|
||||
WiFi.mode(WIFI_OFF);
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.printf("WAV start\n");
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/earlephilhower/ESP8266Audio"
|
||||
},
|
||||
"version": "1.9.0",
|
||||
"version": "1.9.2",
|
||||
"homepage": "https://github.com/earlephilhower/ESP8266Audio",
|
||||
"frameworks": "Arduino",
|
||||
"examples": [
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
name=ESP8266Audio
|
||||
version=1.9.0
|
||||
version=1.9.2
|
||||
author=Earle F. Philhower, III
|
||||
maintainer=Earle F. Philhower, III
|
||||
sentence=Audio file and I2S sound playing routines for ESP8266, ESP32, and Raspberry Pi Pico RP2040
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
AudioFileSourceFunction
|
||||
Audio ouptut generator which can generate WAV file data from function
|
||||
|
||||
Copyright (C) 2021 Hideaki Tai
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "AudioFileSourceFunction.h"
|
||||
|
||||
AudioFileSourceFunction::AudioFileSourceFunction(float sec, uint16_t channels, uint32_t sample_per_sec, uint16_t bits_per_sample) {
|
||||
uint32_t bytes_per_sec = sample_per_sec * channels * bits_per_sample / 8;
|
||||
uint32_t len = uint32_t(sec * (float)bytes_per_sec);
|
||||
|
||||
// RIFF chunk
|
||||
strncpy(wav_header.riff.chunk_id, "RIFF", 4);
|
||||
wav_header.riff.chunk_size = 4 // size of riff chunk w/o chunk_id and chunk_size
|
||||
+ 8 + 16 // size of format chunk
|
||||
+ 8 + len; // size of data chunk
|
||||
strncpy(wav_header.riff.format, "WAVE", 4);
|
||||
|
||||
// format chunk
|
||||
strncpy(wav_header.format.chunk_id, "fmt ", 4);
|
||||
wav_header.format.chunk_size = 16;
|
||||
wav_header.format.format_tag = 0x0001; // PCM
|
||||
wav_header.format.channels = channels;
|
||||
wav_header.format.sample_per_sec = sample_per_sec;
|
||||
wav_header.format.avg_bytes_per_sec = bytes_per_sec;
|
||||
wav_header.format.block_align = channels * bits_per_sample / 8;
|
||||
wav_header.format.bits_per_sample = bits_per_sample;
|
||||
|
||||
// data chunk
|
||||
strncpy(wav_header.data.chunk_id, "data", 4);
|
||||
wav_header.data.chunk_size = len;
|
||||
|
||||
funcs.reserve(channels);
|
||||
pos = 0;
|
||||
size = sizeof(WavHeader) + len;
|
||||
is_ready = false;
|
||||
is_unique = false;
|
||||
}
|
||||
|
||||
AudioFileSourceFunction::~AudioFileSourceFunction() {
|
||||
close();
|
||||
}
|
||||
|
||||
uint32_t AudioFileSourceFunction::read(void* data, uint32_t len) {
|
||||
// callback size must be 1 or equal to channels
|
||||
if (!is_ready)
|
||||
return 0;
|
||||
|
||||
uint8_t* d = reinterpret_cast<uint8_t*>(data);
|
||||
uint32_t i = 0;
|
||||
while (i < len) {
|
||||
uint32_t p = pos + i;
|
||||
if (p < sizeof(WavHeader)) {
|
||||
// header bytes
|
||||
d[i] = wav_header.bytes[p];
|
||||
i += 1;
|
||||
} else {
|
||||
// data bytes
|
||||
float time = (float)(p - sizeof(WavHeader)) / (float)wav_header.format.avg_bytes_per_sec;
|
||||
float v = funcs[0](time);
|
||||
for (size_t ch = 0; ch < wav_header.format.channels; ++ch) {
|
||||
if (!is_unique && ch > 0)
|
||||
v = funcs[ch](time);
|
||||
|
||||
switch (wav_header.format.bits_per_sample) {
|
||||
case 8: {
|
||||
Uint8AndInt8 vs {int8_t(v * (float)0x7F)};
|
||||
d[i] = vs.u;
|
||||
break;
|
||||
}
|
||||
case 32: {
|
||||
Uint8AndInt32 vs {int32_t(v * (float)0x7FFFFFFF)};
|
||||
d[i + 0] = vs.u[0];
|
||||
d[i + 1] = vs.u[1];
|
||||
d[i + 2] = vs.u[2];
|
||||
d[i + 3] = vs.u[3];
|
||||
break;
|
||||
}
|
||||
case 16:
|
||||
default: {
|
||||
Uint8AndInt16 vs {int16_t(v * (float)0x7FFF)};
|
||||
d[i + 0] = vs.u[0];
|
||||
d[i + 1] = vs.u[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
i += wav_header.format.block_align;
|
||||
}
|
||||
}
|
||||
pos += i;
|
||||
return (pos >= size) ? 0 : i;
|
||||
}
|
||||
|
||||
bool AudioFileSourceFunction::seek(int32_t pos, int dir) {
|
||||
if (dir == SEEK_SET) {
|
||||
if (pos < 0 || (uint32_t)pos >= size)
|
||||
return false;
|
||||
this->pos = pos;
|
||||
} else if (dir == SEEK_CUR) {
|
||||
int32_t p = (int32_t)this->pos + pos;
|
||||
if (p < 0 || (uint32_t)p >= size)
|
||||
return false;
|
||||
this->pos = p;
|
||||
} else {
|
||||
int32_t p = (int32_t)this->size + pos;
|
||||
if (p < 0 || (uint32_t)p >= size)
|
||||
return false;
|
||||
this->pos = p;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioFileSourceFunction::close() {
|
||||
funcs.clear();
|
||||
pos = 0;
|
||||
size = 0;
|
||||
is_ready = false;
|
||||
is_unique = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AudioFileSourceFunction::isOpen() {
|
||||
return is_ready;
|
||||
}
|
||||
|
||||
uint32_t AudioFileSourceFunction::getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
uint32_t AudioFileSourceFunction::getPos() {
|
||||
return pos;
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
AudioFileSourceFunction
|
||||
Audio ouptut generator which can generate WAV file data from function
|
||||
|
||||
Copyright (C) 2021 Hideaki Tai
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _AUDIOFILESOURCEFUNCTION_H
|
||||
#define _AUDIOFILESOURCEFUNCTION_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "AudioFileSource.h"
|
||||
|
||||
class AudioFileSourceFunction : public AudioFileSource {
|
||||
union WavHeader {
|
||||
struct {
|
||||
// RIFF chunk
|
||||
struct {
|
||||
char chunk_id[4]; // "RIFF"
|
||||
uint32_t chunk_size; // 4 + (8 + sizeof(format_chunk)(16)) + (8 + sizeof(data_chunk))
|
||||
char format[4]; // "WAVE"
|
||||
} riff;
|
||||
// format chunk
|
||||
struct {
|
||||
char chunk_id[4]; // "fmt "
|
||||
uint32_t chunk_size; // 16
|
||||
uint16_t format_tag; // 1: PCM
|
||||
uint16_t channels; // 1: MONO, 2: STEREO
|
||||
uint32_t sample_per_sec; // 8000, 11025, 22050, 44100, 48000
|
||||
uint32_t avg_bytes_per_sec; // sample_per_sec * channels * bits_per_sample / 8
|
||||
uint16_t block_align; // channels * bits_per_sample / 8
|
||||
uint16_t bits_per_sample; // 8, 16, 32
|
||||
} format;
|
||||
// data chunk
|
||||
struct {
|
||||
char chunk_id[4]; // "data"
|
||||
uint32_t chunk_size; // num_samples * channels * bytes_per_sample
|
||||
// audio data follows here...
|
||||
} data;
|
||||
};
|
||||
uint8_t bytes[44];
|
||||
} wav_header;
|
||||
|
||||
union Uint8AndInt8 {
|
||||
int8_t i;
|
||||
uint8_t u;
|
||||
};
|
||||
|
||||
union Uint8AndInt16 {
|
||||
int16_t i;
|
||||
uint8_t u[2];
|
||||
};
|
||||
|
||||
union Uint8AndInt32 {
|
||||
int32_t i;
|
||||
uint8_t u[4];
|
||||
};
|
||||
|
||||
using callback_t = std::function<float(float)>;
|
||||
std::vector<callback_t> funcs;
|
||||
uint32_t pos;
|
||||
uint32_t size;
|
||||
bool is_ready;
|
||||
bool is_unique;
|
||||
|
||||
public:
|
||||
AudioFileSourceFunction(float sec, uint16_t channels = 1, uint32_t sample_per_sec = 8000, uint16_t bits_per_sample = 16);
|
||||
virtual ~AudioFileSourceFunction() override;
|
||||
|
||||
template <typename F, typename... Fs>
|
||||
bool addAudioGenerators(const F& f, Fs&&... fs) {
|
||||
funcs.emplace_back(f);
|
||||
return addAudioGenerators(std::forward<Fs>(fs)...);
|
||||
}
|
||||
bool addAudioGenerators() {
|
||||
funcs.shrink_to_fit();
|
||||
if (funcs.size() == 1) {
|
||||
is_ready = true;
|
||||
is_unique = true;
|
||||
return true;
|
||||
} else if (funcs.size() == wav_header.format.channels) {
|
||||
is_ready = true;
|
||||
is_unique = false;
|
||||
return true;
|
||||
} else {
|
||||
is_ready = false;
|
||||
is_unique = false;
|
||||
funcs.clear();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual uint32_t read(void* data, uint32_t len) override;
|
||||
virtual bool seek(int32_t pos, int dir) override;
|
||||
|
||||
virtual bool close() override;
|
||||
virtual bool isOpen() override;
|
||||
|
||||
virtual uint32_t getSize() override;
|
||||
virtual uint32_t getPos() override;
|
||||
};
|
||||
|
||||
#endif // _AUDIOFILESOURCEFUNCTION_H
|
||||
@@ -21,9 +21,7 @@
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include "driver/i2s.h"
|
||||
#elif defined(ESP8266)
|
||||
#include <i2s.h>
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
#elif defined(ARDUINO_ARCH_RP2040) || defined(ESP8266)
|
||||
#include <I2S.h>
|
||||
#endif
|
||||
#include "AudioOutputI2S.h"
|
||||
@@ -231,7 +229,9 @@ bool AudioOutputI2S::begin(bool txDAC)
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
(void)txDAC;
|
||||
if (!i2sOn) {
|
||||
I2S.begin(hertz, bclkPin, doutPin);
|
||||
I2S.setBCLK(bclkPin);
|
||||
I2S.setDOUT(doutPin);
|
||||
I2S.begin(hertz);
|
||||
}
|
||||
#endif
|
||||
i2sOn = true;
|
||||
|
||||
@@ -21,9 +21,7 @@
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include "driver/i2s.h"
|
||||
#elif defined(ESP8266)
|
||||
#include <i2s.h>
|
||||
#elif defined(ARDUINO_ARCH_RP2040)
|
||||
#elif defined(ARDUINO_ARCH_RP2040) || defined(ESP8266)
|
||||
#include <I2S.h>
|
||||
#endif
|
||||
#include "AudioOutputI2SNoDAC.h"
|
||||
|
||||
Reference in new issue
Block a user