Skip to content

Commit 06b9074

Browse files
committed
Improve audio handling on PWM.
1 parent d7394a5 commit 06b9074

File tree

14 files changed

+34
-30
lines changed

14 files changed

+34
-30
lines changed

player/.vscode/settings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"files.associations": {
3-
"functional": "cpp"
3+
"functional": "cpp",
4+
"*.tcc": "cpp",
5+
"exception": "cpp",
6+
"fstream": "cpp",
7+
"iosfwd": "cpp",
8+
"new": "cpp",
9+
"ostream": "cpp",
10+
"sstream": "cpp",
11+
"streambuf": "cpp"
412
}
513
}

player/src/AudioOutput/AudioOutput.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AudioOutput
1818
// something the output device expects - for the default case
1919
// this is simply a pass through
2020
virtual int16_t process_sample(int16_t sample) { return sample; }
21-
virtual void write(int8_t *samples, int count) = 0;
21+
virtual void write(uint8_t *samples, int count) = 0;
2222

2323
void setVolume(int volume){
2424
if (volume > 10 || volume < 0) mVolume = 10;

player/src/AudioOutput/I2SBase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void I2SBase::stop()
2121
i2s_driver_uninstall(m_i2s_port);
2222
}
2323

24-
void I2SBase::write(int8_t *samples, int count)
24+
void I2SBase::write(uint8_t *samples, int count)
2525
{
2626
int sample_index = 0;
2727
while (sample_index < count)
@@ -30,7 +30,7 @@ void I2SBase::write(int8_t *samples, int count)
3030
for (int i = 0; i < NUM_FRAMES_TO_SEND && sample_index < count; i++)
3131
{
3232
// shift up to 16 bit samples
33-
int sample = process_sample((samples[sample_index] * mVolume / 10) << 8);
33+
int sample = process_sample(((samples[sample_index] - 128) * mVolume / 10) << 8);
3434
m_tmp_frames[i * 2] = sample;
3535
m_tmp_frames[i * 2 + 1] = sample;
3636
samples_to_send++;

player/src/AudioOutput/I2SBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class I2SBase : public AudioOutput
1616
public:
1717
I2SBase(i2s_port_t i2s_port);
1818
void stop();
19-
void write(int8_t *samples, int count);
19+
void write(uint8_t *samples, int count);
2020
// override this in derived classes to turn the sample into
2121
// something the output device expects - for the default case
2222
// this is simply a pass through

player/src/AudioOutput/PDMTimerOuput.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void PDMTimerOutput::start(uint32_t sample_rate)
8383
Serial.println("PDM Started");
8484
}
8585

86-
void PDMTimerOutput::write(int8_t *samples, int count)
86+
void PDMTimerOutput::write(uint8_t *samples, int count)
8787
{
8888
// Serial.printf("Count %d\n", mCount);
8989
while (true)
@@ -97,7 +97,7 @@ void PDMTimerOutput::write(int8_t *samples, int count)
9797
mSecondBuffer = (int8_t *)realloc(mSecondBuffer, count);
9898
// copy them into the second buffer
9999
for(int i = 0; i < count; i++) {
100-
mSecondBuffer[i] = samples[i] * mVolume / 10;
100+
mSecondBuffer[i] = (samples[i] - 128) * mVolume / 10;
101101
}
102102
// second buffer is now full of samples
103103
mSecondBufferLength = count;

player/src/AudioOutput/PDMTimerOutput.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class PDMTimerOutput : public AudioOutput
3131
mBufferSemaphore = xSemaphoreCreateBinary();
3232
xSemaphoreGive(mBufferSemaphore);
3333
}
34-
void write(int8_t *samples, int count);
34+
void write(uint8_t *samples, int count);
3535
void start(uint32_t sample_rate);
3636
void stop() {}
3737

player/src/AudioOutput/PWMTimerOuput.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ void PWMTimerOutput::start(uint32_t sample_rate)
1919
{
2020
mSampleRate = sample_rate;
2121

22-
ledcSetup(0, 50000, 8);
23-
ledcAttachPin(mPDMPin, 0);
22+
ledcSetup(2, 32000, 11);
23+
ledcAttachPin(mPDMPin, 2);
2424

2525
// create a timer that will fire at the sample rate
2626
timer_config_t timer_config = {
@@ -67,7 +67,7 @@ void PWMTimerOutput::start(uint32_t sample_rate)
6767
Serial.println("PDM Started");
6868
}
6969

70-
void PWMTimerOutput::write(int8_t *samples, int count)
70+
void PWMTimerOutput::write(uint8_t *samples, int count)
7171
{
7272
// Serial.printf("Count %d\n", mCount);
7373
while (true)
@@ -78,10 +78,10 @@ void PWMTimerOutput::write(int8_t *samples, int count)
7878
{
7979
//Serial.println("Filling second buffer");
8080
// make sure there's enough room for the samples
81-
mSecondBuffer = (int8_t *)realloc(mSecondBuffer, count);
81+
mSecondBuffer = (uint8_t *)realloc(mSecondBuffer, count);
8282
// copy them into the second buffer
8383
for(int i = 0; i < count; i++) {
84-
mSecondBuffer[i] = samples[i] * mVolume / 10;
84+
mSecondBuffer[i] = samples[i];
8585
}
8686
// second buffer is now full of samples
8787
mSecondBufferLength = count;
@@ -103,9 +103,9 @@ void PWMTimerOutput::onTimer()
103103
{
104104
mCount++;
105105
// get the first sample from the buffer
106-
int16_t sample = mBuffer[mCurrentIndex];
106+
uint32_t sample = (uint32_t(mBuffer[mCurrentIndex]) << 3);
107107
mCurrentIndex++;
108-
ledcWrite(0, sample+128);
108+
ledcWrite(2, sample * mVolume / 10);
109109
}
110110
if(mCurrentIndex >= mBufferLength)
111111
{
@@ -115,7 +115,7 @@ void PWMTimerOutput::onTimer()
115115
{
116116
if (mSecondBufferLength > 0) {
117117
// swap the buffers
118-
int8_t *tmp = mBuffer;
118+
uint8_t *tmp = mBuffer;
119119
mBuffer = mSecondBuffer;
120120
mBufferLength = mSecondBufferLength;
121121
mSecondBuffer = tmp;

player/src/AudioOutput/PWMTimerOutput.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class PWMTimerOutput : public AudioOutput
1717
gpio_num_t mPDMPin;
1818
uint32_t mSampleRate;
1919
SemaphoreHandle_t mBufferSemaphore;
20-
int8_t *mBuffer=NULL;;
20+
uint8_t *mBuffer=NULL;;
2121
int mCurrentIndex=0;
2222
int mBufferLength=0;
23-
int8_t *mSecondBuffer=NULL;;
23+
uint8_t *mSecondBuffer=NULL;;
2424
int mSecondBufferLength=0;
2525
int mCount = 0;
2626
void onTimer();
@@ -31,7 +31,7 @@ class PWMTimerOutput : public AudioOutput
3131
mBufferSemaphore = xSemaphoreCreateBinary();
3232
xSemaphoreGive(mBufferSemaphore);
3333
}
34-
void write(int8_t *samples, int count);
34+
void write(uint8_t *samples, int count);
3535
void start(uint32_t sample_rate);
3636
void stop() {}
3737

player/src/AudioSource/AudioSource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ class AudioSource {
1010
public:
1111
virtual void start() {};
1212
// get up to maxSamples of audio data and return the number of samples retrieved
13-
virtual int getAudioSamples(int8_t **buffer, size_t &bufferSize, int currentAudioSample) = 0;
13+
virtual int getAudioSamples(uint8_t **buffer, size_t &bufferSize, int currentAudioSample) = 0;
1414
};

player/src/AudioSource/NetworkAudioSource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ NetworkAudioSource::NetworkAudioSource(NetworkChannelData *channelData): mChanne
1111
{
1212
}
1313

14-
int NetworkAudioSource::getAudioSamples(int8_t **buffer, size_t &bufferSize, int currentAudioSample)
14+
int NetworkAudioSource::getAudioSamples(uint8_t **buffer, size_t &bufferSize, int currentAudioSample)
1515
{
1616
if (WiFi.status() == WL_CONNECTED)
1717
{
1818
// resize the buffer if needed
1919
if (bufferSize < SAMPLES_PER_CHUNK)
2020
{
21-
*buffer = (int8_t *)realloc(*buffer, SAMPLES_PER_CHUNK);
21+
*buffer = (uint8_t *)realloc(*buffer, SAMPLES_PER_CHUNK);
2222
bufferSize = SAMPLES_PER_CHUNK;
2323
}
2424
std::string url = mChannelData->getAudioURL() + "/" + std::to_string(currentAudioSample) + "/" + std::to_string(bufferSize);

0 commit comments

Comments
 (0)