2017-02-20 18:55:16 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include "teensy-speaker.h"
|
2020-07-10 00:31:51 +00:00
|
|
|
#include "teensy-println.h"
|
2020-07-13 02:13:33 +00:00
|
|
|
#include <Audio.h>
|
|
|
|
|
|
|
|
TeensyAudio audioDriver;
|
2020-07-15 14:19:57 +00:00
|
|
|
AudioMixer4 mixer2; //xy=280,253
|
2020-07-13 02:13:33 +00:00
|
|
|
AudioMixer4 mixer1; //xy=280,175
|
2020-07-15 14:19:57 +00:00
|
|
|
//AudioOutputI2S i2s; //xy=452,189
|
|
|
|
AudioOutputMQS i2s; //xy=452,189
|
2020-07-13 02:13:33 +00:00
|
|
|
|
|
|
|
AudioConnection patchCord1(audioDriver, 0, mixer1, 0);
|
2020-07-15 14:19:57 +00:00
|
|
|
AudioConnection patchCord2(audioDriver, 0, mixer2, 0);
|
|
|
|
AudioConnection patchCord3(mixer2, 0, i2s, 1);
|
2020-07-13 02:13:33 +00:00
|
|
|
AudioConnection patchCord4(mixer1, 0, i2s, 0);
|
2017-02-20 18:55:16 +00:00
|
|
|
|
2017-02-27 01:34:38 +00:00
|
|
|
#include "globals.h"
|
2017-02-20 18:55:16 +00:00
|
|
|
|
2020-07-14 01:10:26 +00:00
|
|
|
#define HIGHVAL (0x4FFF)
|
|
|
|
#define LOWVAL (-0x4FFF)
|
2020-07-13 02:13:33 +00:00
|
|
|
|
|
|
|
// Ring buffer that we fill with 44.1kHz data
|
2020-07-15 00:43:41 +00:00
|
|
|
#define BUFSIZE (4096)
|
2022-01-21 20:36:44 +00:00
|
|
|
#define CACHEMULTIPLIER 10
|
2020-07-14 01:10:26 +00:00
|
|
|
static volatile uint32_t bufIdx; // 0 .. BUFSIZE-1
|
2020-08-02 14:02:35 +00:00
|
|
|
static volatile uint64_t skippedSamples; // Who knows where this will
|
|
|
|
// wind up
|
2020-07-14 01:10:26 +00:00
|
|
|
static volatile uint8_t audioRunning = 0; // FIXME: needs constants abstracted
|
2020-08-02 13:06:15 +00:00
|
|
|
static volatile int64_t lastFilledTime = 0;
|
2020-07-14 01:10:26 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
// how full do we want the audio buffer before we start it playing?
|
|
|
|
#define AUDIO_WATERLEVEL 4096
|
|
|
|
|
2020-07-14 01:10:26 +00:00
|
|
|
#define SAMPLEBYTES sizeof(short)
|
2020-07-15 00:43:41 +00:00
|
|
|
EXTMEM short soundBuf[BUFSIZE*CACHEMULTIPLIER];
|
2020-07-14 01:10:26 +00:00
|
|
|
|
|
|
|
static bool toggleState = false;
|
2020-07-10 00:31:51 +00:00
|
|
|
|
|
|
|
TeensySpeaker::TeensySpeaker(uint8_t sda, uint8_t scl) : PhysicalSpeaker()
|
2017-02-20 18:55:16 +00:00
|
|
|
{
|
2017-02-26 16:00:41 +00:00
|
|
|
toggleState = false;
|
2020-08-02 14:02:35 +00:00
|
|
|
mixerValue = 0x80;
|
|
|
|
|
2020-07-13 02:13:33 +00:00
|
|
|
AudioMemory(8);
|
2017-02-20 18:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TeensySpeaker::~TeensySpeaker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:31:51 +00:00
|
|
|
void TeensySpeaker::begin()
|
2017-02-20 18:55:16 +00:00
|
|
|
{
|
2020-12-29 03:49:57 +00:00
|
|
|
float curVolume = (float)g_volume / 15.0;
|
|
|
|
mixer1.gain(0, curVolume); // left channel
|
|
|
|
mixer1.gain(1, curVolume); // right channel
|
|
|
|
mixer2.gain(0, curVolume); // left channel
|
|
|
|
mixer2.gain(1, curVolume); // right channel
|
2020-07-14 01:10:26 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
memset(soundBuf, 0, sizeof(soundBuf));
|
|
|
|
|
2020-07-10 00:31:51 +00:00
|
|
|
toggleState = false;
|
2020-07-14 01:10:26 +00:00
|
|
|
bufIdx = 0;
|
|
|
|
skippedSamples = 0;
|
|
|
|
audioRunning = 0;
|
2020-07-10 00:31:51 +00:00
|
|
|
}
|
2018-01-03 01:28:47 +00:00
|
|
|
|
2020-08-02 13:06:15 +00:00
|
|
|
void TeensySpeaker::toggle(int64_t c)
|
2020-07-10 00:31:51 +00:00
|
|
|
{
|
2020-07-13 02:13:33 +00:00
|
|
|
// Figure out when the last time was that we put data in the audio buffer;
|
|
|
|
// then figure out how many audio buffer cycles we have to fill from that
|
|
|
|
// CPU time to this one.
|
|
|
|
__disable_irq();
|
|
|
|
|
|
|
|
// We expect to have filled to this cycle number...
|
2020-08-02 13:06:15 +00:00
|
|
|
int64_t expectedCycleNumber = (float)c * (float)AUDIO_SAMPLE_RATE_EXACT / (float)g_speed;
|
2020-07-14 01:10:26 +00:00
|
|
|
// Dynamically initialize the lastFilledTime based on the start time of the
|
|
|
|
// audio channel.
|
|
|
|
if (lastFilledTime == 0)
|
|
|
|
lastFilledTime = expectedCycleNumber;
|
|
|
|
|
|
|
|
// and we have filled to cycle number lastFilledTime. So how many do
|
|
|
|
// we need? This subtracts skippedSamples because those were filled
|
|
|
|
// automatically by the audioCallback when we had no data.
|
2020-08-02 14:02:35 +00:00
|
|
|
int64_t audioBufferSamples = expectedCycleNumber - lastFilledTime - skippedSamples;
|
2020-07-14 01:10:26 +00:00
|
|
|
// If audioBufferSamples < 0, then we need to keep some
|
|
|
|
// skippedSamples for later; otherwise we can keep moving forward.
|
|
|
|
if (audioBufferSamples < 0) {
|
|
|
|
skippedSamples = -audioBufferSamples;
|
|
|
|
audioBufferSamples = 0;
|
|
|
|
} else {
|
|
|
|
// Otherwise we consumed them and can forget about it.
|
|
|
|
skippedSamples = 0;
|
|
|
|
}
|
2020-07-13 02:13:33 +00:00
|
|
|
|
2020-07-14 01:10:26 +00:00
|
|
|
int32_t newIdx = bufIdx + audioBufferSamples;
|
|
|
|
|
|
|
|
if (audioBufferSamples == 0) {
|
|
|
|
// If the toggle wouldn't result in at least 1 buffer sample
|
|
|
|
// change, then we'll blatantly skip it here. If this turns out to
|
|
|
|
// be a problem, we could try setting audioBufferSamples++ and
|
|
|
|
// then twiddle the lastFilledTime so it looks like it's more in
|
|
|
|
// the future, but I suspect that would mean missing more future
|
|
|
|
// events, just like we would have missed this one.
|
|
|
|
//
|
|
|
|
// But I think this is probably okay - because something that's
|
|
|
|
// toggling the speaker fast enough that our 44k audio can't keep
|
|
|
|
// up with the individual changes is likely to toggle again in a
|
|
|
|
// moment without significant distortion?
|
2020-07-15 00:43:41 +00:00
|
|
|
__enable_irq();
|
2020-07-14 01:10:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-13 02:13:33 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
if (newIdx >= sizeof(soundBuf)/SAMPLEBYTES) {
|
2020-07-14 01:10:26 +00:00
|
|
|
// Buffer overrun error. Shouldn't happen?
|
2020-07-15 00:43:41 +00:00
|
|
|
println("OVERRUN");
|
|
|
|
newIdx = (sizeof(soundBuf)/SAMPLEBYTES) - 1;
|
2020-07-13 02:13:33 +00:00
|
|
|
}
|
|
|
|
lastFilledTime = expectedCycleNumber;
|
2020-07-14 01:10:26 +00:00
|
|
|
|
|
|
|
// Flip the toggle state
|
|
|
|
toggleState = !toggleState;
|
|
|
|
|
|
|
|
// Fill from bufIdx .. newIdx and set bufIdx to newIdx when done.
|
|
|
|
if (newIdx > bufIdx) {
|
|
|
|
long count = (long)newIdx - bufIdx;
|
|
|
|
for (long i=0; i<count; i++) {
|
2021-01-20 12:42:01 +00:00
|
|
|
if (bufIdx+i+1 < sizeof(soundBuf)/sizeof(short)) {
|
|
|
|
soundBuf[bufIdx+i] = toggleState ? HIGHVAL : LOWVAL;
|
|
|
|
}
|
2020-07-14 01:10:26 +00:00
|
|
|
}
|
|
|
|
bufIdx = newIdx;
|
|
|
|
}
|
2020-07-13 02:13:33 +00:00
|
|
|
__enable_irq();
|
2020-07-10 00:31:51 +00:00
|
|
|
}
|
2020-07-08 13:43:44 +00:00
|
|
|
|
2020-08-02 13:06:15 +00:00
|
|
|
void TeensySpeaker::maintainSpeaker(int64_t c, uint64_t microseconds)
|
2020-07-10 00:31:51 +00:00
|
|
|
{
|
|
|
|
begin(); // flush! Hack. FIXME.
|
2017-02-20 18:55:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 00:31:51 +00:00
|
|
|
void TeensySpeaker::maintainSpeaker()
|
2017-02-20 18:55:16 +00:00
|
|
|
{
|
|
|
|
}
|
2017-02-24 15:15:17 +00:00
|
|
|
|
2017-02-26 16:00:41 +00:00
|
|
|
void TeensySpeaker::beginMixing()
|
2017-02-24 15:15:17 +00:00
|
|
|
{
|
2017-12-31 22:21:34 +00:00
|
|
|
// unused
|
2017-02-24 15:15:17 +00:00
|
|
|
}
|
2017-02-26 16:00:41 +00:00
|
|
|
|
|
|
|
void TeensySpeaker::mixOutput(uint8_t v)
|
|
|
|
{
|
2017-12-31 22:21:34 +00:00
|
|
|
// unused
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 02:13:33 +00:00
|
|
|
void TeensyAudio::update(void)
|
|
|
|
{
|
|
|
|
audio_block_t *block;
|
2020-07-15 00:43:41 +00:00
|
|
|
short *stream;
|
2020-07-13 02:13:33 +00:00
|
|
|
|
2020-07-14 01:10:26 +00:00
|
|
|
if (audioRunning == 0)
|
|
|
|
audioRunning = 1;
|
2020-07-15 00:43:41 +00:00
|
|
|
|
|
|
|
block = allocate();
|
|
|
|
if (!block) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = block->data;
|
|
|
|
|
2020-07-14 01:10:26 +00:00
|
|
|
if (g_biosInterrupt) {
|
|
|
|
// While the BIOS is running, we don't put samples in the audio queue.
|
|
|
|
audioRunning = 0;
|
2020-07-15 00:43:41 +00:00
|
|
|
memset(stream, 0, AUDIO_BLOCK_SAMPLES * SAMPLEBYTES);
|
|
|
|
goto done;
|
2020-07-14 01:10:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
if (audioRunning == 1 && bufIdx >= AUDIO_WATERLEVEL) {
|
2020-07-14 01:10:26 +00:00
|
|
|
// We have enough samples in the buffer to fill it, so we're fully
|
|
|
|
// up and running.
|
|
|
|
audioRunning = 2;
|
|
|
|
} else if (audioRunning == 1) {
|
|
|
|
// Still waiting for the first fill; return an empty buffer.
|
2020-07-15 00:43:41 +00:00
|
|
|
memset(stream, 0, AUDIO_BLOCK_SAMPLES * SAMPLEBYTES);
|
|
|
|
goto done;
|
2020-07-14 01:10:26 +00:00
|
|
|
}
|
2020-07-15 00:43:41 +00:00
|
|
|
|
2020-08-02 14:02:35 +00:00
|
|
|
static short lastKnownSample = 0; // saved for when the apple is quiescent
|
2020-07-15 00:43:41 +00:00
|
|
|
|
|
|
|
if (bufIdx >= AUDIO_BLOCK_SAMPLES) {
|
|
|
|
memcpy(stream, (void *)soundBuf, AUDIO_BLOCK_SAMPLES * SAMPLEBYTES);
|
|
|
|
lastKnownSample = stream[AUDIO_BLOCK_SAMPLES-1];
|
2020-07-14 01:10:26 +00:00
|
|
|
|
2020-07-15 00:43:41 +00:00
|
|
|
if (bufIdx > AUDIO_BLOCK_SAMPLES) {
|
2020-07-14 01:10:26 +00:00
|
|
|
// move the remaining data down
|
2020-07-15 00:43:41 +00:00
|
|
|
memcpy((void *)soundBuf, (void *)&soundBuf[AUDIO_BLOCK_SAMPLES], (bufIdx - AUDIO_BLOCK_SAMPLES + 1)*SAMPLEBYTES);
|
|
|
|
bufIdx -= AUDIO_BLOCK_SAMPLES;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (bufIdx) {
|
|
|
|
// partial buffer exists
|
|
|
|
memcpy(stream, (void *)soundBuf, bufIdx * SAMPLEBYTES);
|
|
|
|
// and it's a partial underrun. Track the number of samples we skipped
|
|
|
|
// so we can keep the audio buffer in sync.
|
|
|
|
skippedSamples += AUDIO_BLOCK_SAMPLES - bufIdx;
|
|
|
|
for (int32_t i=0; i<AUDIO_BLOCK_SAMPLES-bufIdx; i++) {
|
|
|
|
stream[i+bufIdx] = lastKnownSample;
|
2020-07-14 01:10:26 +00:00
|
|
|
}
|
2020-08-02 14:02:35 +00:00
|
|
|
bufIdx = 0;
|
2020-07-14 01:10:26 +00:00
|
|
|
} else {
|
2020-07-15 00:43:41 +00:00
|
|
|
// No big deal - buffer underrun might just mean nothing is
|
|
|
|
// trying to play audio right now.
|
|
|
|
skippedSamples += AUDIO_BLOCK_SAMPLES;
|
|
|
|
for (int32_t i=0; i<AUDIO_BLOCK_SAMPLES; i++) {
|
|
|
|
stream[i] = 0;
|
2020-07-13 02:13:33 +00:00
|
|
|
}
|
2020-07-15 00:43:41 +00:00
|
|
|
// memset(stream, 0, AUDIO_BLOCK_SAMPLES * SAMPLEBYTES);
|
2020-07-13 02:13:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-15 00:43:41 +00:00
|
|
|
done:
|
|
|
|
transmit(block, 0);
|
|
|
|
release(block);
|
2020-07-13 02:13:33 +00:00
|
|
|
}
|