WIP - AudioOutputMixer to combine multiple streams

This runs but amplification is not working yet.  See MixerSample
for a very crufty example.
This commit is contained in:
Earle F. Philhower, III committed 2018-02-25 13:37:17 -08:00
1 parent 11bd5d2188
commit ca50839712
5 files changed
+50286

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2S.h"
#include "AudioOutputMixer.h"
// VIOLA sample taken from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html
#include "viola.h"
AudioGeneratorWAV *wav[2];
AudioFileSourcePROGMEM *file[2];
AudioOutputI2S *out;
AudioOutputMixer *mixer;
AudioOutputMixerStub *stub[2];
void setup()
{
WiFi.mode(WIFI_OFF);
Serial.begin(115200);
delay(1000);
Serial.printf("WAV start\n");
file[0] = new AudioFileSourcePROGMEM( viola, sizeof(viola) );
out = new AudioOutputI2S();
mixer = new AudioOutputMixer(32, out);
stub[0] = mixer->NewInput();
wav[0] = new AudioGeneratorWAV();
wav[0]->begin(file[0], stub[0]);
// Begin wav[1] later in loop()
Serial.printf("starting 1\n");
}
void loop()
{
static uint32_t start = 0;
static bool go = false;
if (!start) start = millis();
if (wav[0]->isRunning()) {
if (!wav[0]->loop()) { wav[0]->stop(); stub[0]->stop(); Serial.printf("stopping 1\n"); }
}
if (millis()-start > 3000) {
if (!go) {
Serial.printf("starting 2\n");
stub[1] = mixer->NewInput();
wav[1] = new AudioGeneratorWAV();
file[1] = new AudioFileSourcePROGMEM( viola, sizeof(viola) );
wav[1]->begin(file[1], stub[1]);
go = true;
}
if (wav[1]->isRunning()) {
if (!wav[1]->loop()) { wav[1]->stop(); stub[1]->stop(); Serial.printf("stopping 2\n");}
}
}
}
File diff suppressed because it is too large. Load diff
+2
View File
@@ -22,3 +22,5 @@ AudioOutputNull KEYWORD1
AudioOutputBuffer KEYWORD1
AudioOutputSerialWAV KEYWORD1
AudioOutputSPIFFSWAV KEYWORD1
AudioOutputMixer KEYWORD1
AudioOutputMixerStub KEYWORD1
+235
View File
@@ -0,0 +1,235 @@
/*
AudioOutputMixer
Simple mixer which can combine multiple inputs to a single output stream
Copyright (C) 2018 Earle F. Philhower, III
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 <Arduino.h>
#include "AudioOutputMixer.h"
AudioOutputMixerStub::AudioOutputMixerStub(AudioOutputMixer *sink, int id) : AudioOutput()
{
this->id = id;
this->parent = sink;
}
AudioOutputMixerStub::~AudioOutputMixerStub()
{
parent->RemoveInput(id);
}
bool AudioOutputMixerStub::SetRate(int hz)
{
return parent->SetRate(hz, id);
}
bool AudioOutputMixerStub::SetBitsPerSample(int bits)
{
return parent->SetBitsPerSample(bits, id);
}
bool AudioOutputMixerStub::SetChannels(int channels)
{
return parent->SetChannels(channels, id);
}
bool AudioOutputMixerStub::begin()
{
return parent->begin(id);
}
bool AudioOutputMixerStub::ConsumeSample(int16_t sample[2])
{
// int16_t amp[2] = { Amplify(sample[LEFTCHANNEL]), Amplify(sample[RIGHTCHANNEL]) };
return parent->ConsumeSample(sample, id);
}
bool AudioOutputMixerStub::stop()
{
return parent->stop(id);
}
AudioOutputMixer::AudioOutputMixer(int buffSizeSamples, AudioOutput *dest) : AudioOutput()
{
buffSize = buffSizeSamples;
leftAccum = (int32_t*)calloc(sizeof(int32_t), buffSize);
rightAccum = (int32_t*)calloc(sizeof(int32_t), buffSize);
for (int i=0; i<maxStubs; i++) {
stubAllocated[i] = false;
stubRunning[i] = false;
writePtr[i] = 0;
}
readPtr = 0;
sink = dest;
sinkStarted = false;
}
AudioOutputMixer::~AudioOutputMixer()
{
free(leftAccum);
free(rightAccum);
}
// Most "standard" interfaces should fail, only MixerStub should be able to talk to us
bool AudioOutputMixer::SetRate(int hz)
{
(void) hz;
return false;
}
bool AudioOutputMixer::SetBitsPerSample(int bits)
{
(void) bits;
return false;
}
bool AudioOutputMixer::SetChannels(int channels)
{
(void) channels;
return false;
}
bool AudioOutputMixer::ConsumeSample(int16_t sample[2])
{
(void) sample;
return false;
}
bool AudioOutputMixer::begin()
{
return false;
}
bool AudioOutputMixer::stop()
{
return false;
}
// TODO - actually ensure all samples are same speed, size, channels, rate
bool AudioOutputMixer::SetRate(int hz, int id)
{
(void) id;
return sink->SetRate(hz);
}
bool AudioOutputMixer::SetBitsPerSample(int bits, int id)
{
(void) id;
return sink->SetBitsPerSample(bits);
}
bool AudioOutputMixer::SetChannels(int channels, int id)
{
(void) id;
return sink->SetChannels(channels);
}
bool AudioOutputMixer::begin(int id)
{
stubRunning[id] = true;
if (!sinkStarted) {
sinkStarted = true;
return sink->begin();
} else {
return true;
}
}
AudioOutputMixerStub *AudioOutputMixer::NewInput()
{
for (int i=0; i<maxStubs; i++) {
if (!stubAllocated[i]) {
stubAllocated[i] = true;
stubRunning[i] = false;
writePtr[i] = readPtr; // TODO - should it be 1 before readPtr?
AudioOutputMixerStub *stub = new AudioOutputMixerStub(this, i);
return stub;
}
}
return nullptr;
}
void AudioOutputMixer::RemoveInput(int id)
{
stubAllocated[id] = false;
stubRunning[id] = false;
}
bool AudioOutputMixer::ConsumeSample(int16_t sample[2], int id)
{
// First, try and fill I2S...
// This is not optimal, but algorithmically should work fine
bool avail;
do {
avail = true;
for (int i=0; i<maxStubs && avail; i++) {
if (stubRunning[i] && writePtr[i] == readPtr) {
avail = false; // The read pointer is touching an active writer, can't advance
}
}
if (avail) {
int16_t s[2];
if (leftAccum[readPtr] > 32767) {
s[LEFTCHANNEL] = 32767;
} else if (leftAccum[readPtr] < -32767) {
s[LEFTCHANNEL] = -32767;
} else {
s[LEFTCHANNEL] = leftAccum[readPtr];
}
if (rightAccum[readPtr] > 32767) {
s[RIGHTCHANNEL] = 32767;
} else if (rightAccum[readPtr] < -32767) {
s[RIGHTCHANNEL] = -32767;
} else {
s[RIGHTCHANNEL] = rightAccum[readPtr];
}
// s[LEFTCHANNEL] = Amplify(s[LEFTCHANNEL]);
// s[RIGHTCHANNEL] = Amplify(s[RIGHTCHANNEL]);
if (!sink->ConsumeSample(s)) {
break; // Can't stuff any more in I2S...
}
// Clear the accums and advance the pointer to next potential sample
leftAccum[readPtr] = 0;
rightAccum[readPtr] = 0;
readPtr = (readPtr + 1) % buffSize;
}
} while (avail);
// Now, do we have space for a new sample?
int nextWritePtr = (writePtr[id] + 1) % buffSize;
if (nextWritePtr == readPtr) {
return false;
}
leftAccum[writePtr[id]] += sample[LEFTCHANNEL];
rightAccum[writePtr[id]] += sample[RIGHTCHANNEL];
writePtr[id] = nextWritePtr;
return true;
}
bool AudioOutputMixer::stop(int id)
{
stubRunning[id] = false;
return true;
}
+87
View File
@@ -0,0 +1,87 @@
/*
AudioOutputMixer
Simple mixer which can combine multiple inputs to a single output stream
Copyright (C) 2017 Earle F. Philhower, III
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 _AUDIOOUTPUTMIXER_H
#define _AUDIOOUTPUTMIXER_H
#include "AudioOutput.h"
class AudioOutputMixer;
// The output stub exported by the mixer for use by the generator
class AudioOutputMixerStub : public AudioOutput
{
public:
AudioOutputMixerStub(AudioOutputMixer *sink, int id);
virtual ~AudioOutputMixerStub() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
virtual bool stop() override;
protected:
AudioOutputMixer *parent;
int id;
};
// Single mixer object per output
class AudioOutputMixer : public AudioOutput
{
public:
AudioOutputMixer(int samples, AudioOutput *sink);
virtual ~AudioOutputMixer() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
virtual bool SetChannels(int channels) override;
virtual bool begin() override;
virtual bool ConsumeSample(int16_t sample[2]) override;
virtual bool stop() override;
AudioOutputMixerStub *NewInput(); // Get a new stub to pass to a generator
// Stub called functions
friend class AudioOutputMixerStub;
private:
void RemoveInput(int id);
bool SetRate(int hz, int id);
bool SetBitsPerSample(int bits, int id);
bool SetChannels(int channels, int id);
bool begin(int id);
bool ConsumeSample(int16_t sample[2], int id);
bool stop(int id);
protected:
enum { maxStubs = 8 };
AudioOutput *sink;
bool sinkStarted;
int16_t buffSize;
int32_t *leftAccum;
int32_t *rightAccum;
bool stubAllocated[maxStubs];
bool stubRunning[maxStubs];
int16_t writePtr[maxStubs]; // Array of pointers for allocated stubs
int16_t readPtr;
};
#endif