Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PWMAudio support for the RP2040 (#597)
Uses the RP2040 HW PWM generator to provide a stereo audio signal.
- Loading branch information
1 parent
46b3657
commit 0b5da0e
Showing
8 changed files
with
11,480 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
examples/PlayMODFromPROGMEMToPWM/PlayMODFromPROGMEMToPWM.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <Arduino.h> | ||
#include "AudioFileSourcePROGMEM.h" | ||
#include "AudioGeneratorMOD.h" | ||
#include "AudioOutputPWM.h" | ||
|
||
#if !defined(ARDUINO_ARCH_RP2040) | ||
void setup() { | ||
Serial.begin(115200); | ||
Serial.printf("Only for the RP2040/Raspberry Pi Pico\n"); | ||
} | ||
|
||
void loop() { | ||
} | ||
#else | ||
|
||
// 5_steps.mod sample from the mod archive: https://modarchive.org/ | ||
#include "5steps.h" | ||
|
||
AudioGeneratorMOD *mod; | ||
AudioFileSourcePROGMEM *file; | ||
AudioOutputPWM *out; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
delay(1000); | ||
|
||
audioLogger = &Serial; | ||
file = new AudioFileSourcePROGMEM(steps_mod, sizeof(steps_mod)); | ||
out = new AudioOutputPWM(); | ||
mod = new AudioGeneratorMOD(); | ||
mod->SetBufferSize(3*1024); | ||
mod->SetSampleRate(44100); | ||
mod->SetStereoSeparation(32); | ||
mod->begin(file, out); | ||
} | ||
|
||
void loop() | ||
{ | ||
if (mod->isRunning()) { | ||
if (!mod->loop()) mod->stop(); | ||
} else { | ||
Serial.printf("MOD done\n"); | ||
delay(1000); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
AudioOutputPWM | ||
Base class for the RP2040 PWM audio | ||
Copyright (C) 2023 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/>. | ||
*/ | ||
|
||
#if defined(ARDUINO_ARCH_RP2040) | ||
#include <Arduino.h> | ||
#include "AudioOutputPWM.h" | ||
|
||
AudioOutputPWM::AudioOutputPWM(long sampleRate, pin_size_t data) { | ||
pwmOn = false; | ||
mono = false; | ||
bps = 16; | ||
channels = 2; | ||
hertz = sampleRate; | ||
doutPin = data; | ||
SetGain(1.0); | ||
pwm.setStereo(true); | ||
} | ||
|
||
AudioOutputPWM::~AudioOutputPWM() { | ||
stop(); | ||
} | ||
|
||
bool AudioOutputPWM::SetRate(int hz) { | ||
this->hertz = hz; | ||
if (pwmOn) { | ||
pwm.setFrequency(hz); | ||
} | ||
return true; | ||
} | ||
|
||
bool AudioOutputPWM::SetBitsPerSample(int bits) { | ||
if ( (bits != 16) && (bits != 8) ) return false; | ||
this->bps = bits; | ||
return true; | ||
} | ||
|
||
bool AudioOutputPWM::SetChannels(int channels) { | ||
if ( (channels < 1) || (channels > 2) ) return false; | ||
this->channels = channels; | ||
return true; | ||
} | ||
|
||
bool AudioOutputPWM::SetOutputModeMono(bool mono) { | ||
this->mono = mono; | ||
return true; | ||
} | ||
|
||
bool AudioOutputPWM::begin() { | ||
if (!pwmOn) { | ||
pwm.setPin(doutPin); | ||
pwm.begin(hertz); | ||
} | ||
pwmOn = true; | ||
SetRate(hertz); // Default | ||
return true; | ||
} | ||
|
||
bool AudioOutputPWM::ConsumeSample(int16_t sample[2]) { | ||
|
||
if (!pwmOn) | ||
return false; | ||
|
||
int16_t ms[2]; | ||
|
||
ms[0] = sample[0]; | ||
ms[1] = sample[1]; | ||
MakeSampleStereo16( ms ); | ||
|
||
if (this->mono) { | ||
// Average the two samples and overwrite | ||
int32_t ttl = ms[LEFTCHANNEL] + ms[RIGHTCHANNEL]; | ||
ms[LEFTCHANNEL] = ms[RIGHTCHANNEL] = (ttl>>1) & 0xffff; | ||
} | ||
|
||
if (pwm.available()) { | ||
pwm.write((int16_t) ms[0]); | ||
pwm.write((int16_t) ms[1]); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
void AudioOutputPWM::flush() { | ||
pwm.flush(); | ||
} | ||
|
||
bool AudioOutputPWM::stop() { | ||
if (!pwmOn) | ||
return false; | ||
|
||
pwm.end(); | ||
pwmOn = false; | ||
return true; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
AudioOutputPWM | ||
Base class for the RP2040 PWM audio output | ||
Copyright (C) 2023 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/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "AudioOutput.h" | ||
|
||
#if defined(ARDUINO_ARCH_RP2040) | ||
#include <Arduino.h> | ||
#include <PWMAudio.h> | ||
|
||
class AudioOutputPWM : public AudioOutput | ||
{ | ||
public: | ||
AudioOutputPWM(long sampleRate = 44100, pin_size_t data = 0); | ||
virtual ~AudioOutputPWM() 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 void flush() override; | ||
virtual bool stop() override; | ||
|
||
bool SetOutputModeMono(bool mono); // Force mono output no matter the input | ||
|
||
protected: | ||
bool SetPinout(); | ||
virtual int AdjustI2SRate(int hz) { return hz; } | ||
uint8_t portNo; | ||
int output_mode; | ||
bool mono; | ||
bool pwmOn; | ||
|
||
uint8_t doutPin; | ||
|
||
PWMAudio pwm; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters